Fixed contact discard changes dialog showing up if no changes were made

This commit is contained in:
Sylvain Berfini 2024-05-06 11:02:28 +02:00
parent 68f2535072
commit faf20eb369
3 changed files with 45 additions and 22 deletions

View file

@ -121,26 +121,7 @@ class EditContactFragment : SlidingPaneChildFragment() {
viewModel.findFriendByRefKey(refKey)
binding.setBackClickListener {
val model = ConfirmationDialogModel()
val dialog = DialogUtils.getCancelContactChangesConfirmationDialog(
requireActivity(),
model
)
model.dismissEvent.observe(viewLifecycleOwner) {
it.consume {
dialog.dismiss()
}
}
model.confirmEvent.observe(viewLifecycleOwner) {
it.consume {
findNavController().popBackStack()
dialog.dismiss()
}
}
dialog.show()
showAbortConfirmationDialog()
}
binding.setPickImageClickListener {
@ -238,6 +219,13 @@ class EditContactFragment : SlidingPaneChildFragment() {
}
private fun showAbortConfirmationDialog() {
if (!viewModel.isPendingChanges()) {
Log.i("$TAG No changes detected, do not show confirmation dialog")
backPressedCallback.isEnabled = false
findNavController().popBackStack()
return
}
val model = ConfirmationDialogModel()
val dialog = DialogUtils.getCancelContactChangesConfirmationDialog(
requireActivity(),

View file

@ -218,6 +218,7 @@ class NewContactFragment : GenericFragment() {
private fun showAbortConfirmationDialogIfNeededOrGoBack() {
if (!viewModel.isPendingChanges()) {
Log.i("$TAG No changes detected, do not show confirmation dialog")
backPressedCallback.isEnabled = false
findNavController().popBackStack()
return

View file

@ -313,8 +313,42 @@ class ContactNewOrEditViewModel @UiThread constructor() : ViewModel() {
@UiThread
fun isPendingChanges(): Boolean {
if (isEdit.value == true) {
// TODO FIXME: check if values of each field match friend values
return true
if (firstName.value.orEmpty() != friend.vcard?.givenName.orEmpty()) return true
if (lastName.value.orEmpty() != friend.vcard?.familyName.orEmpty()) return true
if (picturePath.value.orEmpty() != friend.photo.orEmpty()) return true
if (company.value.orEmpty() != friend.organization.orEmpty()) return true
if (jobTitle.value.orEmpty() != friend.jobTitle.orEmpty()) return true
for (address in friend.addresses) {
val found = sipAddresses.find {
it.isSip && it.value.value.orEmpty() == address.asStringUriOnly()
}
if (found == null) return true
}
for (address in sipAddresses) {
if (address.value.value.orEmpty().isEmpty()) continue
val found = friend.addresses.find {
it.asStringUriOnly() == address.value.value.orEmpty()
}
if (found == null) return true
}
for (number in friend.phoneNumbers) {
val found = phoneNumbers.find {
!it.isSip && it.value.value.orEmpty() == number
}
if (found == null) return true
}
for (number in phoneNumbers) {
if (number.value.value.orEmpty().isEmpty()) continue
val found = friend.phoneNumbers.find {
it == number.value.value.orEmpty()
}
if (found == null) return true
}
return false
}
return !picturePath.value.isNullOrEmpty() ||