From b4604735d1707cbda95c6685a0b2942759ef097b Mon Sep 17 00:00:00 2001 From: Kaushik N Sanji <26028981+kaushiknsanji@users.noreply.github.com> Date: Fri, 8 Nov 2019 16:23:57 +0530 Subject: [PATCH] fix: Discard dialog for unsaved changes to be shown only when there is an update/change Discard dialog was previously shown even when there was no change in the data. This fix compares the existing state with its updated state in order to show the Discard dialog, only when there is mismatch between the states, to allow the user to save the unsaved changes. --- .../storeapp/data/local/models/Product.java | 40 ++++++++++++++ .../data/local/models/ProductAttribute.java | 30 +++++++++++ .../local/models/ProductSupplierInfo.java | 32 ++++++++++++ .../storeapp/data/local/models/Supplier.java | 36 +++++++++++++ .../config/ProductConfigActivityFragment.java | 29 +++++++++++ .../config/ProductConfigContract.java | 25 +++++++++ .../config/ProductConfigPresenter.java | 45 ++++++++++++++-- .../image/ProductImageActivityFragment.java | 13 +++++ .../products/image/ProductImageContract.java | 19 ++++++- .../products/image/ProductImagePresenter.java | 52 +++++++++++++++++-- .../SupplierConfigActivityFragment.java | 28 ++++++++++ .../config/SupplierConfigContract.java | 25 +++++++++ .../config/SupplierConfigPresenter.java | 50 +++++++++++++++--- 13 files changed, 410 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/Product.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/Product.java index 5f6afdf..1e1bfe6 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/Product.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/Product.java @@ -257,6 +257,46 @@ public void setProductAttributes(ArrayList productAttributes) mProductAttributes = productAttributes; } + /** + * Indicates whether some other object is "equal to" this one. + * + * @param o The reference object with which to compare. + * @return TRUE if this object is the same as the {@code o} + * argument; FALSE otherwise. + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Product product = (Product) o; + + if (mId != product.mId) return false; + if (!mName.equals(product.mName)) return false; + if (!mSku.equals(product.mSku)) return false; + if (!mDescription.equals(product.mDescription)) return false; + if (!mCategory.equals(product.mCategory)) return false; + if (!mProductImages.equals(product.mProductImages)) return false; + return mProductAttributes.equals(product.mProductAttributes); + } + + /** + * Returns a hash code value for the object. + * + * @return A hash code value for this object. + */ + @Override + public int hashCode() { + int result = mId; + result = 31 * result + mName.hashCode(); + result = 31 * result + mSku.hashCode(); + result = 31 * result + mDescription.hashCode(); + result = 31 * result + mCategory.hashCode(); + result = 31 * result + mProductImages.hashCode(); + result = 31 * result + mProductAttributes.hashCode(); + return result; + } + /** * Static Builder class that constructs {@link Product} */ diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/ProductAttribute.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/ProductAttribute.java index 91faf58..b5f9743 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/ProductAttribute.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/ProductAttribute.java @@ -142,6 +142,36 @@ public void setAttributeValue(String attributeValue) { mAttributeValue = attributeValue; } + /** + * Indicates whether some other object is "equal to" this one. + * + * @param o The reference object with which to compare. + * @return TRUE if this object is the same as the {@code o} + * argument; FALSE otherwise. + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ProductAttribute that = (ProductAttribute) o; + + if (!mAttributeName.equals(that.mAttributeName)) return false; + return mAttributeValue.equals(that.mAttributeValue); + } + + /** + * Returns a hash code value for the object. + * + * @return A hash code value for this object. + */ + @Override + public int hashCode() { + int result = mAttributeName.hashCode(); + result = 31 * result + mAttributeValue.hashCode(); + return result; + } + /** * Static Builder class that constructs {@link ProductAttribute} */ diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/ProductSupplierInfo.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/ProductSupplierInfo.java index bc14ade..753c408 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/ProductSupplierInfo.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/ProductSupplierInfo.java @@ -151,6 +151,38 @@ public void setUnitPrice(float unitPrice) { mUnitPrice = unitPrice; } + /** + * Indicates whether some other object is "equal to" this one. + * + * @param o The reference object with which to compare. + * @return TRUE if this object is the same as the {@code o} + * argument; FALSE otherwise. + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ProductSupplierInfo that = (ProductSupplierInfo) o; + + if (mItemId != that.mItemId) return false; + if (mSupplierId != that.mSupplierId) return false; + return Float.compare(that.mUnitPrice, mUnitPrice) == 0; + } + + /** + * Returns a hash code value for the object. + * + * @return A hash code value for this object. + */ + @Override + public int hashCode() { + int result = mItemId; + result = 31 * result + mSupplierId; + result = 31 * result + (mUnitPrice != +0.0f ? Float.floatToIntBits(mUnitPrice) : 0); + return result; + } + /** * Creates and returns a copy of this object. * diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/Supplier.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/Supplier.java index db47e17..89d3395 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/Supplier.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/data/local/models/Supplier.java @@ -211,6 +211,42 @@ public void setProductSupplierInfoList(ArrayList productSup mProductSupplierInfoList = productSupplierInfoList; } + /** + * Indicates whether some other object is "equal to" this one. + * + * @param o The reference object with which to compare. + * @return TRUE if this object is the same as the {@code o} + * argument; FALSE otherwise. + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Supplier supplier = (Supplier) o; + + if (mId != supplier.mId) return false; + if (!mName.equals(supplier.mName)) return false; + if (!mCode.equals(supplier.mCode)) return false; + if (!mContacts.equals(supplier.mContacts)) return false; + return mProductSupplierInfoList.equals(supplier.mProductSupplierInfoList); + } + + /** + * Returns a hash code value for the object. + * + * @return A hash code value for this object. + */ + @Override + public int hashCode() { + int result = mId; + result = 31 * result + mName.hashCode(); + result = 31 * result + mCode.hashCode(); + result = 31 * result + mContacts.hashCode(); + result = 31 * result + mProductSupplierInfoList.hashCode(); + return result; + } + /** * Static Builder class that constructs {@link Supplier} */ diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigActivityFragment.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigActivityFragment.java index 08da035..85aa931 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigActivityFragment.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigActivityFragment.java @@ -464,6 +464,35 @@ public void showUpdateImagesSuccess() { } } + /** + * Method invoked when the user clicks on the android home/up button + * or the back key is pressed, to extract the current modifications on the Existing Product, + * in order to display the Discard Dialog if required. + */ + @Override + public void readExistingProductDetails() { + //Delegating to the Presenter to trigger focus loss on listener registered Views, + //in order to persist their data + mPresenter.triggerFocusLost(); + + //Retrieving the data from the views and the adapter + String productName = mEditTextProductName.getText().toString().trim(); + String productSku = mEditTextProductSku.getText().toString().trim(); + String productDescription = mEditTextProductDescription.getText().toString().trim(); + ArrayList productAttributes = mProductAttributesAdapter.getProductAttributes(); + mCategoryOtherText = mEditTextProductCategoryOther.getText().toString().trim(); + + //Delegating to the Presenter, to check for any changes + mPresenter.checkForModifications( + productName, + productSku, + productDescription, + mCategoryLastSelected, + mCategoryOtherText, + productAttributes + ); + } + /** * Method that displays the EditText Field associated with the 'Other' Category * selected by the user. diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigContract.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigContract.java index ebd4c9f..a84af5b 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigContract.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigContract.java @@ -222,6 +222,13 @@ interface View extends BaseView { * the {@link com.example.kaushiknsanji.storeapp.ui.products.image.ProductImageActivity} */ void showUpdateImagesSuccess(); + + /** + * Method invoked when the user clicks on the android home/up button + * or the back key is pressed, to extract the current modifications on the Existing Product, + * in order to display the Discard Dialog if required. + */ + void readExistingProductDetails(); } /** @@ -355,6 +362,24 @@ void onSave(String productName, String productSku, String productDescription, */ void onUpOrBackAction(); + /** + * Method invoked only for an Existing Product, when the user clicks on the + * android home/up button or the back key is pressed. This looks for any changes done on + * the Existing Product in order to display the Discard Dialog, to see + * if the user wants to keep/discard the changes. + * + * @param productName The Name of the Product entered by the User + * @param productSku The SKU of the Product entered by the User + * @param productDescription The Description of the Product entered by the User + * @param categorySelected The Category selected for the Product + * @param categoryOtherText The Custom Category entered by the User when not found in + * predefined Categories + * @param productAttributes The Attributes describing the Product, which are optional + */ + void checkForModifications(String productName, String productSku, String productDescription, + String categorySelected, String categoryOtherText, + ArrayList productAttributes); + /** * Method invoked when the user decides to exit without entering/saving any data */ diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigPresenter.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigPresenter.java index e15e2a9..a0449b5 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigPresenter.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/config/ProductConfigPresenter.java @@ -842,16 +842,55 @@ public void onFailure(int messageId, @Nullable Object... args) { @Override public void onUpOrBackAction() { if (mIsProductNameEntered) { - //When the User has entered the Product Name, then we have some unsaved changes + //When the User has entered the Product Name + + //If this is an Existing Product, then extract the updated details + //to see if we have some unsaved changes + if (mProductId > ProductConfigContract.NEW_PRODUCT_INT) { + mProductConfigView.readExistingProductDetails(); + } - //Show the discard dialog to see if the user wants to keep editing/discard the changes - mProductConfigView.showDiscardDialog(); } else { //When the User has not yet entered the Product Name, then silently close the Activity finishActivity(); } } + /** + * Method invoked only for an Existing Product, when the user clicks on the + * android home/up button or the back key is pressed. This looks for any changes done on + * the Existing Product in order to display the Discard Dialog, to see + * if the user wants to keep/discard the changes. + * + * @param productName The Name of the Product entered by the User + * @param productSku The SKU of the Product entered by the User + * @param productDescription The Description of the Product entered by the User + * @param categorySelected The Category selected for the Product + * @param categoryOtherText The Custom Category entered by the User when not found in + * predefined Categories + * @param productAttributes The Attributes describing the Product, which are optional + */ + @Override + public void checkForModifications(String productName, String productSku, + String productDescription, String categorySelected, + String categoryOtherText, + ArrayList productAttributes) { + + //Create the Updated Product + Product updatedProduct = createProductForUpdate(productName, productSku, productDescription, + categorySelected, categoryOtherText, productAttributes, mProductImages); + + if (!updatedProduct.equals(mExistingProduct)) { + //Display the discard dialog when there is a mismatch between the + //details of the Existing and Updated Product + mProductConfigView.showDiscardDialog(); + } else { + //When there is no change in the Product details, silently close the Activity + finishActivity(); + } + + } + /** * Method invoked when the user decides to exit without entering/saving any data */ diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImageActivityFragment.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImageActivityFragment.java index ea60ce8..d0ce84d 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImageActivityFragment.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImageActivityFragment.java @@ -543,6 +543,19 @@ public void showImageAlreadyPicked() { } } + /** + * Method invoked when the user clicks on the android home/up button + * or the back key is pressed, to extract the original/existing list of Product Images, + * in order to display the Discard Dialog in case of some changes. + */ + @Override + public void readExistingProductImages() { + if (getArguments() != null) { + //Delegating to the Presenter, to check for any changes + mPresenter.checkForModifications(getArguments().getParcelableArrayList(ARGUMENT_LIST_PRODUCT_IMAGES)); + } + } + /** * Callback method of {@link ProductImagePickerDialogFragment} * invoked when the user chooses the "Take Photo" option in the dialog. diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImageContract.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImageContract.java index 75a2802..7d6e994 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImageContract.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImageContract.java @@ -150,6 +150,13 @@ interface View extends BaseView { * was already picked and present in the list. */ void showImageAlreadyPicked(); + + /** + * Method invoked when the user clicks on the android home/up button + * or the back key is pressed, to extract the original/existing list of Product Images, + * in order to display the Discard Dialog in case of some changes. + */ + void readExistingProductImages(); } /** @@ -251,7 +258,6 @@ interface Presenter extends BasePresenter { */ void restoreSelectionTrackers(ArrayList imageSelectionTrackers); - /** * Method invoked by the View when the activity was started/restored, to restore the list of * {@link ProductImage} objects. @@ -287,6 +293,17 @@ interface Presenter extends BasePresenter { */ void onUpOrBackAction(); + /** + * Method invoked when the user clicks on the android home/up button + * or the back key is pressed. This looks for any changes done to the existing list + * of {@link ProductImage} objects, in order to display the Discard Dialog, to see + * if the user wants to keep/discard the changes. + * + * @param existingProductImages Existing list of {@link ProductImage}, each of which holds + * the URI information of the Image File. + */ + void checkForModifications(ArrayList existingProductImages); + /** * Method that sets the result and passes the information back to the calling Parent Activity */ diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImagePresenter.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImagePresenter.java index 61c6d51..eddc093 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImagePresenter.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/products/image/ProductImagePresenter.java @@ -872,8 +872,8 @@ private void restoreMode() { */ @Override public void restoreProductImages(ArrayList productImages) { - //Save the list in the Presenter - mProductImages = productImages; + //Save the copy of the list in the Presenter + mProductImages = deepCopyProductImages(productImages); //Send the list to the RecyclerView's Adapter to display submitListToAdapter(mProductImages); @@ -1134,8 +1134,9 @@ public void onUpOrBackAction() { if (mLastChosenProductImage != null) { //When we have an Image that was last chosen to be shown as selected - //Show the discard dialog to see if the user wants to keep/discard the changes - mProductImageView.showDiscardDialog(); + //Extract the Existing list of Product Images to see if we have some unsaved changes + mProductImageView.readExistingProductImages(); + } else { //When we do not have an Image that was last chosen to be shown as selected @@ -1144,6 +1145,49 @@ public void onUpOrBackAction() { } } + /** + * Method invoked when the user clicks on the android home/up button + * or the back key is pressed. This looks for any changes done to the existing list + * of {@link ProductImage} objects, in order to display the Discard Dialog, to see + * if the user wants to keep/discard the changes. + * + * @param existingProductImages Existing list of {@link ProductImage}, each of which holds + * the URI information of the Image File. + */ + @Override + public void checkForModifications(ArrayList existingProductImages) { + //Creating a new list to load and apply the unsaved changes to the updated copy + //list of Product Images + ArrayList updatedProductImages = new ArrayList<>(); + + //Iterating over the updated list of Product Images to copy, apply any unsaved changes + //and load them to the new list + for (ProductImage productImage : mProductImages) { + //Create a copy of the current Product Image + ProductImage newProductImage = (ProductImage) productImage.clone(); + if (newProductImage.getImageUri().equals(mLastChosenProductImage.getImageUri())) { + //When the current Product Image is the last chosen Product Image, + //update it as the default and load it to the new list + newProductImage.setDefault(true); + updatedProductImages.add(newProductImage); + } else { + //When the current Product Image is NOT the last chosen Product Image, + //load it to the new list AS-IS + updatedProductImages.add(newProductImage); + } + } + + if (!existingProductImages.equals(updatedProductImages)) { + //Display the discard dialog when there is a mismatch between the + //Existing list and Updated list of Product Images + mProductImageView.showDiscardDialog(); + } else { + //When there is no change in the list of Product Images, call to the + //Navigator's doSetResult() to pass the information back to the parent activity + doSetResult(); + } + } + /** * Method that sets the result and passes the information back to the calling Parent Activity */ diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigActivityFragment.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigActivityFragment.java index df84d2f..19c6120 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigActivityFragment.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigActivityFragment.java @@ -932,6 +932,34 @@ public void showSupplierContactsInvalidError(@StringRes int invalidMessageResId) showError(invalidMessageResId); } + /** + * Method invoked when the user clicks on the android home/up button + * or the back key is pressed, to extract the current modifications on the Existing Supplier, + * in order to display the Discard Dialog if required. + */ + @Override + public void readExistingSupplierDetails() { + //Delegating to the Presenter to trigger focus loss on listener registered View, + //in order to persist their data + mPresenter.triggerFocusLost(); + + //Retrieving the data from the views and the adapter + String supplierName = mEditTextSupplierName.getText().toString().trim(); + String supplierCode = mEditTextSupplierCode.getText().toString().trim(); + ArrayList phoneContacts = mPhoneContactsAdapter.getSupplierContacts(); + ArrayList emailContacts = mEmailContactsAdapter.getSupplierContacts(); + ArrayList productSupplierInfoList = mSupplierProductsAdapter.getProductSupplierInfoList(); + + //Delegating to the Presenter, to check for any changes + mPresenter.checkForModifications( + supplierName, + supplierCode, + phoneContacts, + emailContacts, + productSupplierInfoList + ); + } + /** * Method that initializes the RecyclerView 'R.id.recyclerview_supplier_config_phone' and its Adapter. */ diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigContract.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigContract.java index 328041b..e940cd9 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigContract.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigContract.java @@ -227,6 +227,13 @@ void showSupplierContactConflictError(@StringRes int conflictMessageResId, * @param invalidMessageResId The String resource of the error message to be shown. */ void showSupplierContactsInvalidError(@StringRes int invalidMessageResId); + + /** + * Method invoked when the user clicks on the android home/up button + * or the back key is pressed, to extract the current modifications on the Existing Supplier, + * in order to display the Discard Dialog if required. + */ + void readExistingSupplierDetails(); } /** @@ -357,6 +364,24 @@ void onSave(String supplierName, */ void onUpOrBackAction(); + /** + * Method invoked only for an Existing Supplier, when the user clicks on the + * android home/up button or the back key is pressed. This looks for any changes done on + * the Existing Supplier in order to display the Discard Dialog, to see + * if the user wants to keep/discard the changes. + * + * @param supplierName The Name given to the Supplier + * @param supplierCode The Code of the Supplier + * @param phoneContacts List of Phone Contacts {@link SupplierContact} of the Supplier + * @param emailContacts List of Email Contacts {@link SupplierContact} of the Supplier + * @param productSupplierInfoList List of Products sold by the Supplier with their Selling Price {@link ProductSupplierInfo} + */ + void checkForModifications(String supplierName, + String supplierCode, + ArrayList phoneContacts, + ArrayList emailContacts, + ArrayList productSupplierInfoList); + /** * Method invoked when the user decides to exit without entering/saving any data */ diff --git a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigPresenter.java b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigPresenter.java index f934d6c..aa3e916 100644 --- a/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigPresenter.java +++ b/app/src/main/java/com/example/kaushiknsanji/storeapp/ui/suppliers/config/SupplierConfigPresenter.java @@ -130,7 +130,7 @@ public void start() { * to update the view components with Supplier data. */ private void loadExistingSupplier() { - if (mSupplierId != SupplierConfigContract.NEW_SUPPLIER_INT) { + if (mSupplierId > SupplierConfigContract.NEW_SUPPLIER_INT) { //When it is an Edit request //Display progress indicator @@ -809,14 +809,15 @@ private Supplier createSupplierForUpdate(String supplierName, ArrayList productSupplierInfoList) { //Adding all emailContacts to phoneContacts since they belong to the same table - phoneContacts.addAll(emailContacts); + ArrayList allContacts = new ArrayList<>(phoneContacts); + allContacts.addAll(emailContacts); //Building and returning the Supplier built with the details return new Supplier.Builder() .setId(mSupplierId) .setName(supplierName) .setCode(supplierCode) - .setContacts(phoneContacts) + .setContacts(allContacts) .setProductSupplierInfoList(productSupplierInfoList) .createSupplier(); } @@ -957,16 +958,53 @@ public void onFailure(int messageId, @Nullable Object... args) { @Override public void onUpOrBackAction() { if (mIsSupplierNameEntered) { - //When the User has entered the Supplier Name, then we have some unsaved changes + //When the User has entered the Supplier Name + + //If this is an Existing Supplier, then extract the updated details + //to see if we have some unsaved changes + if (mSupplierId > SupplierConfigContract.NEW_SUPPLIER_INT) { + mSupplierConfigView.readExistingSupplierDetails(); + } - //Show the discard dialog to see if the user wants to keep editing/discard the changes - mSupplierConfigView.showDiscardDialog(); } else { //When the User has not yet entered the Supplier Name, then silently close the Activity finishActivity(); } } + /** + * Method invoked only for an Existing Supplier, when the user clicks on the + * android home/up button or the back key is pressed. This looks for any changes done on + * the Existing Supplier in order to display the Discard Dialog, to see + * if the user wants to keep/discard the changes. + * + * @param supplierName The Name given to the Supplier + * @param supplierCode The Code of the Supplier + * @param phoneContacts List of Phone Contacts {@link SupplierContact} of the Supplier + * @param emailContacts List of Email Contacts {@link SupplierContact} of the Supplier + * @param productSupplierInfoList List of Products sold by the Supplier with their Selling Price {@link ProductSupplierInfo} + */ + @Override + public void checkForModifications(String supplierName, String supplierCode, + ArrayList phoneContacts, + ArrayList emailContacts, + ArrayList productSupplierInfoList) { + + //Create the Updated Supplier + Supplier updatedSupplier = createSupplierForUpdate(supplierName, supplierCode, + phoneContacts, emailContacts, productSupplierInfoList); + + if (!updatedSupplier.equals(mExistingSupplier)) { + //Display the discard dialog when there is a mismatch between the + //details of the Existing and Updated Supplier + mSupplierConfigView.showDiscardDialog(); + } else { + //When there is no change in the Supplier details, silently close the Activity + finishActivity(); + } + + } + /** * Method invoked when the user decides to exit without entering/saving any data */