Skip to content

Commit

Permalink
fix: Discard dialog for unsaved changes to be shown only when there i…
Browse files Browse the repository at this point in the history
…s 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.
  • Loading branch information
kaushiknsanji committed Nov 8, 2019
1 parent d4b641e commit b460473
Show file tree
Hide file tree
Showing 13 changed files with 410 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,46 @@ public void setProductAttributes(ArrayList<ProductAttribute> productAttributes)
mProductAttributes = productAttributes;
}

/**
* Indicates whether some other object is "equal to" this one.
*
* @param o The reference object with which to compare.
* @return <b>TRUE</b> if this object is the same as the {@code o}
* argument; <b>FALSE</b> 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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>TRUE</b> if this object is the same as the {@code o}
* argument; <b>FALSE</b> 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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>TRUE</b> if this object is the same as the {@code o}
* argument; <b>FALSE</b> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,42 @@ public void setProductSupplierInfoList(ArrayList<ProductSupplierInfo> productSup
mProductSupplierInfoList = productSupplierInfoList;
}

/**
* Indicates whether some other object is "equal to" this one.
*
* @param o The reference object with which to compare.
* @return <b>TRUE</b> if this object is the same as the {@code o}
* argument; <b>FALSE</b> 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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProductAttribute> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ interface View extends BaseView<Presenter> {
* 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();
}

/**
Expand Down Expand Up @@ -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<ProductAttribute> productAttributes);

/**
* Method invoked when the user decides to exit without entering/saving any data
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProductAttribute> 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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ interface View extends BaseView<Presenter> {
* 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();
}

/**
Expand Down Expand Up @@ -251,7 +258,6 @@ interface Presenter extends BasePresenter {
*/
void restoreSelectionTrackers(ArrayList<ImageSelectionTracker> imageSelectionTrackers);


/**
* Method invoked by the View when the activity was started/restored, to restore the list of
* {@link ProductImage} objects.
Expand Down Expand Up @@ -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<ProductImage> existingProductImages);

/**
* Method that sets the result and passes the information back to the calling Parent Activity
*/
Expand Down
Loading

0 comments on commit b460473

Please sign in to comment.