Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EditProfile discard changes dialog shown on back press #1307

Merged
merged 3 commits into from
Mar 23, 2019

Conversation

addiegupta
Copy link
Contributor

Fixes Issue #1292. The behaviour for up button has been replicated for the back button. So now if changes are made in the EditProfileFragment and the back button or the up button is pressed, an AlertDialog is displayed which warns the user of the unsaved changes

Changes:
The MainActivity class has been modified to handle the backPress for the EditProfileFragment. It in turn calls a method in EditProfileFragment which handles the backPress. When the back button is finally meant to be pressed from EditProfileFragment to go back, then super.onBackPressed is called using a new method.

Please suggest a better method to implement this if any.

Screenshots for the change:
pr for 1292

@addiegupta addiegupta force-pushed the #1292 branch 2 times, most recently from 7c1e320 to 8a80e79 Compare March 15, 2019 04:37
@fossasia fossasia deleted a comment Mar 15, 2019
@fossasia fossasia deleted a comment Mar 15, 2019
@fossasia fossasia deleted a comment Mar 15, 2019
@addiegupta addiegupta force-pushed the #1292 branch 3 times, most recently from bb97b30 to fe24593 Compare March 15, 2019 11:42
@iamareebjamal
Copy link
Member

The solution presented is very brittle and can fail in many ways

@addiegupta
Copy link
Contributor Author

The solution presented is very brittle and can fail in many ways

Can you guide me towards an efficient solution?

@iamareebjamal
Copy link
Member

Why you haven't just overriden onBackPressed in the fragment?

@addiegupta
Copy link
Contributor Author

Why you haven't just overriden onBackPressed in the fragment?

As far as I checked onBackPressed cannot be overriden in fragments. That is why I took this route.

* Called by EditProfileFragment to go to previous fragment
*/
fun onSuperBackPressed() {
super.onBackPressed()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless as it's just calling onBackPressed. OnBackPressed can be called directly

Copy link
Contributor Author

@addiegupta addiegupta Mar 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use this method to call the direct onBackPressed from the EditProfileFragment. If i would have called the onBackPressed then it would have called the overriden method which would again call the handleBackPress() method causing a cycle

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any changes I should make?

@addiegupta
Copy link
Contributor Author

@iamareebjamal I have updated the branch. Please review

Copy link
Member

@liveHarshit liveHarshit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check if edit text and image field are clicked, then only show an alert dialog.


// Calls the handleBackPress method in EditProfileFragment
val hostFragment = supportFragmentManager.findFragmentById(R.id.frameContainer) as? NavHostFragment
(hostFragment?.childFragmentManager?.fragments?.get(0) as? EditProfileFragment)?.handleBackPress()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too complex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I split these up into more lines of code or do you mean some other way should be used? Because I could not find any other way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just need to call the method in edit profile fragment. Can't you do it by taking the fragment as object?

Copy link
Contributor Author

@addiegupta addiegupta Mar 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I get a reference to the Fragment instance? It is done using FragmentManager only right? If I used any other method to obtain the fragment the app was crashing

@@ -110,7 +111,7 @@ class EditProfileFragment : Fragment() {
.observe(this, Observer {
Snackbar.make(rootView.editProfileCoordinatorLayout, it, Snackbar.LENGTH_LONG).show()
if (it == USER_UPDATED) {
activity?.onBackPressed()
(activity as? MainActivity)?.onSuperBackPressed()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not cast.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I use this method from MainActivity then without casting?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use smart cast instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes made

@addiegupta
Copy link
Contributor Author

You need to check if edit text and image field are clicked, then only show an alert dialog.

This is done in the following code

if (!avatarUpdated && rootView.lastName.text.toString() == userLastName && rootView.firstName.text.toString() == userFirstName)

@@ -110,7 +111,7 @@ class EditProfileFragment : Fragment() {
.observe(this, Observer {
Snackbar.make(rootView.editProfileCoordinatorLayout, it, Snackbar.LENGTH_LONG).show()
if (it == USER_UPDATED) {
activity?.onBackPressed()
activity?.let { act -> if (act is MainActivity) act.onSuperBackPressed() }
}
Copy link
Contributor Author

@addiegupta addiegupta Mar 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liveHarshit I have done it this way instead of using if(activity is MainActivity) activity.onSuperBackPressed() because the following error occurred for this method

Smart cast to 'MainActivity' is impossible, because 'activity' is a property that has open or custom getter

Using let worked

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then use a value - val thisActivity = activity then cast for thisActivity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay! I tried that also but thought this was cleaner. will implement this

* Handles back press when up button or back button is pressed
*/
fun handleBackPress() {
val activity = activity as? MainActivity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't cast like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

@addiegupta addiegupta force-pushed the #1292 branch 2 times, most recently from 83ecebe to d67fbae Compare March 22, 2019 12:13
@addiegupta
Copy link
Contributor Author

@iamareebjamal @liveHarshit any other changes that I should make in this?

Fixes Issue fossasia#1292. The behaviour for up button has been replicated for the back button. So now if changes are made in the EditProfileFragment and the back button or the up button is pressed, an AlertDialog is displayed which warns the user of the unsaved changes
@liveHarshit
Copy link
Member

@iamareebjamal Please review.

Copy link
Member

@iamareebjamal iamareebjamal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very hacky. Features which require such hacky code are generally not worth it

@iamareebjamal iamareebjamal merged commit 8fc9af5 into fossasia:development Mar 23, 2019
@addiegupta addiegupta deleted the #1292 branch March 24, 2019 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants