Skip to content

Commit

Permalink
fix: editprofile discard changes dialog shown on back press
Browse files Browse the repository at this point in the history
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
  • Loading branch information
addiegupta committed Mar 22, 2019
1 parent 95b8235 commit 83ecebe
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/org/fossasia/openevent/general/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.navigation
import kotlinx.android.synthetic.main.activity_main.navigationAuth
import kotlinx.android.synthetic.main.activity_main.mainFragmentCoordinatorLayout
import org.fossasia.openevent.general.auth.EditProfileFragment
import org.fossasia.openevent.general.search.RC_CREDENTIALS_READ
import org.fossasia.openevent.general.search.RC_CREDENTIALS_SAVE
import org.fossasia.openevent.general.search.SmartAuthViewModel
Expand Down Expand Up @@ -68,10 +69,23 @@ class MainActivity : AppCompatActivity() {
}
R.id.orderCompletedFragment -> navController.popBackStack(R.id.eventDetailsFragment, false)
R.id.welcomeFragment -> finish()
R.id.editProfileFragment -> {

// Calls the handleBackPress method in EditProfileFragment
val hostFragment = supportFragmentManager.findFragmentById(R.id.frameContainer) as? NavHostFragment
(hostFragment?.childFragmentManager?.fragments?.get(0) as? EditProfileFragment)?.handleBackPress()
}
else -> super.onBackPressed()
}
}

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

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == RC_CREDENTIALS_READ || requestCode == RC_CREDENTIALS_SAVE)
SmartAuthViewModel().onActivityResult(requestCode, resultCode, data, this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import kotlinx.android.synthetic.main.fragment_edit_profile.view.lastName
import kotlinx.android.synthetic.main.fragment_edit_profile.view.profilePhoto
import kotlinx.android.synthetic.main.fragment_edit_profile.view.progressBar
import org.fossasia.openevent.general.CircleTransform
import org.fossasia.openevent.general.MainActivity
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.utils.Utils.hideSoftKeyboard
import org.fossasia.openevent.general.utils.Utils.requireDrawable
Expand Down Expand Up @@ -121,7 +122,8 @@ class EditProfileFragment : Fragment() {
.observe(viewLifecycleOwner, Observer {
Snackbar.make(rootView.editProfileCoordinatorLayout, it, Snackbar.LENGTH_LONG).show()
if (it == USER_UPDATED) {
activity?.onBackPressed()
val thisActivity = activity
if (thisActivity is MainActivity) thisActivity.onSuperBackPressed()
}
})

Expand Down Expand Up @@ -169,22 +171,7 @@ class EditProfileFragment : Fragment() {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
if (!avatarUpdated && rootView.lastName.text.toString() == userLastName &&
rootView.firstName.text.toString() == userFirstName) {
activity?.onBackPressed()
} else {
hideSoftKeyboard(context, rootView)
val dialog = AlertDialog.Builder(context)
dialog.setMessage("Your changes have not been saved")
dialog.setNegativeButton("Discard") { _, _ ->
activity?.onBackPressed()
}
dialog.setPositiveButton("Save") { _, _ ->
editProfileViewModel.updateProfile(encodedImage, rootView.firstName.text.toString(),
rootView.lastName.text.toString())
}
dialog.create().show()
}
handleBackPress()
true
}
else -> super.onOptionsItemSelected(item)
Expand Down Expand Up @@ -219,6 +206,29 @@ class EditProfileFragment : Fragment() {
}
}

/**
* Handles back press when up button or back button is pressed
*/
fun handleBackPress() {
val thisActivity = activity
if (!avatarUpdated && rootView.lastName.text.toString() == userLastName &&
rootView.firstName.text.toString() == userFirstName ) {
if(thisActivity is MainActivity) thisActivity.onSuperBackPressed()
} else {
hideSoftKeyboard(context, rootView)
val dialog = AlertDialog.Builder(context)
dialog.setMessage(getString(R.string.changes_not_saved))
dialog.setNegativeButton(getString(R.string.discard)) { _, _ ->
if(thisActivity is MainActivity) thisActivity.onSuperBackPressed()
}
dialog.setPositiveButton(getString(R.string.save)) { _, _ ->
editProfileViewModel.updateProfile(encodedImage, rootView.firstName.text.toString(),
rootView.lastName.text.toString())
}
dialog.create().show()
}
}

override fun onDestroyView() {
val activity = activity as? AppCompatActivity
activity?.supportActionBar?.setDisplayHomeAsUpEnabled(false)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
<string name="welcome_back">Welcome back!</string>
<string name="update">Update</string>

<!--edit profile dialog-->
<string name="changes_not_saved" translatable="false">Your changes have not been saved</string>
<string name="discard" translatable="false">Discard</string>
<string name="save" translatable="false">Save</string>

<!--eventyay-->
<string name="eventyay_logo" translatable="false">eventyay</string>

Expand Down

0 comments on commit 83ecebe

Please sign in to comment.