Skip to content

Commit 1ec064e

Browse files
committed
show a Save/Discard prompt at leaving with unsaved changes
1 parent 77e07d4 commit 1ec064e

File tree

1 file changed

+74
-50
lines changed

1 file changed

+74
-50
lines changed

app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.widget.EditText
2020
import android.widget.ImageView
2121
import android.widget.RelativeLayout
2222
import android.widget.TextView
23+
import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
2324
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
2425
import com.simplemobiletools.commons.dialogs.SelectAlarmSoundDialog
2526
import com.simplemobiletools.commons.extensions.*
@@ -53,6 +54,7 @@ class EditContactActivity : ContactActivity() {
5354
private val CHOOSE_PHOTO = 2
5455
private val REMOVE_PHOTO = 3
5556

57+
private var mLastSavePromptTS = 0L
5658
private var wasActivityInitialized = false
5759
private var lastPhotoIntentUri: Uri? = null
5860
private var isSaving = false
@@ -249,6 +251,21 @@ class EditContactActivity : ContactActivity() {
249251
}
250252
}
251253

254+
override fun onBackPressed() {
255+
if (System.currentTimeMillis() - mLastSavePromptTS > SAVE_DISCARD_PROMPT_INTERVAL && hasContactChanged()) {
256+
mLastSavePromptTS = System.currentTimeMillis()
257+
ConfirmationAdvancedDialog(this, "", R.string.save_before_closing, R.string.save, R.string.discard) {
258+
if (it) {
259+
saveContact()
260+
} else {
261+
super.onBackPressed()
262+
}
263+
}
264+
} else {
265+
super.onBackPressed()
266+
}
267+
}
268+
252269
private fun setupMenu() {
253270
(contact_appbar.layoutParams as RelativeLayout.LayoutParams).topMargin = statusBarHeight
254271
contact_toolbar.menu.apply {
@@ -278,6 +295,8 @@ class EditContactActivity : ContactActivity() {
278295
}
279296
}
280297

298+
private fun hasContactChanged() = contact != fillContactValues()
299+
281300
private fun openWith() {
282301
Intent().apply {
283302
action = Intent.ACTION_EDIT
@@ -903,72 +922,77 @@ class EditContactActivity : ContactActivity() {
903922
}
904923

905924
private fun saveContact() {
906-
if (isSaving) {
925+
if (isSaving || contact == null) {
907926
return
908927
}
909928

910-
val filledPhoneNumbers = getFilledPhoneNumbers()
911-
val filledEmails = getFilledEmails()
912-
val filledAddresses = getFilledAddresses()
913-
val filledIMs = getFilledIMs()
914-
val filledEvents = getFilledEvents()
915-
val filledWebsites = getFilledWebsites()
916-
917929
val contactFields = arrayListOf(
918930
contact_prefix, contact_first_name, contact_middle_name, contact_surname, contact_suffix, contact_nickname,
919931
contact_notes, contact_organization_company, contact_organization_job_position
920932
)
921933

922-
if (contactFields.all { it.value.isEmpty() } &&
923-
currentContactPhotoPath.isEmpty() &&
924-
filledPhoneNumbers.isEmpty() &&
925-
filledEmails.isEmpty() &&
926-
filledAddresses.isEmpty() &&
927-
filledIMs.isEmpty() &&
928-
filledEvents.isEmpty() &&
929-
filledWebsites.isEmpty()
930-
) {
931-
toast(R.string.fields_empty)
932-
return
934+
if (contactFields.all { it.value.isEmpty() }) {
935+
if (currentContactPhotoPath.isEmpty() &&
936+
getFilledPhoneNumbers().isEmpty() &&
937+
getFilledEmails().isEmpty() &&
938+
getFilledAddresses().isEmpty() &&
939+
getFilledIMs().isEmpty() &&
940+
getFilledEvents().isEmpty() &&
941+
getFilledWebsites().isEmpty()
942+
) {
943+
toast(R.string.fields_empty)
944+
return
945+
}
933946
}
934947

935-
contact?.apply {
936-
val oldPhotoUri = photoUri
937-
938-
prefix = contact_prefix.value
939-
firstName = contact_first_name.value
940-
middleName = contact_middle_name.value
941-
surname = contact_surname.value
942-
suffix = contact_suffix.value
943-
nickname = contact_nickname.value
944-
photoUri = currentContactPhotoPath
945-
phoneNumbers = filledPhoneNumbers
946-
emails = filledEmails
947-
addresses = filledAddresses
948-
IMs = filledIMs
949-
events = filledEvents
950-
starred = if (isContactStarred()) 1 else 0
951-
notes = contact_notes.value
952-
websites = filledWebsites
953-
954-
val company = contact_organization_company.value
955-
val jobPosition = contact_organization_job_position.value
956-
organization = Organization(company, jobPosition)
948+
val oldPhotoUri = contact!!.photoUri
949+
contact = fillContactValues()
957950

958-
ensureBackgroundThread {
959-
config.lastUsedContactSource = source
960-
when {
961-
id == 0 -> insertNewContact(false)
962-
originalContactSource != source -> insertNewContact(true)
963-
else -> {
964-
val photoUpdateStatus = getPhotoUpdateStatus(oldPhotoUri, photoUri)
965-
updateContact(photoUpdateStatus)
966-
}
951+
ensureBackgroundThread {
952+
config.lastUsedContactSource = contact!!.source
953+
when {
954+
contact!!.id == 0 -> insertNewContact(false)
955+
originalContactSource != contact!!.source -> insertNewContact(true)
956+
else -> {
957+
val photoUpdateStatus = getPhotoUpdateStatus(oldPhotoUri, contact!!.photoUri)
958+
updateContact(photoUpdateStatus)
967959
}
968960
}
969961
}
970962
}
971963

964+
private fun fillContactValues(): Contact {
965+
val filledPhoneNumbers = getFilledPhoneNumbers()
966+
val filledEmails = getFilledEmails()
967+
val filledAddresses = getFilledAddresses()
968+
val filledIMs = getFilledIMs()
969+
val filledEvents = getFilledEvents()
970+
val filledWebsites = getFilledWebsites()
971+
972+
val newContact = contact!!.copy(
973+
prefix = contact_prefix.value,
974+
firstName = contact_first_name.value,
975+
middleName = contact_middle_name.value,
976+
surname = contact_surname.value,
977+
suffix = contact_suffix.value,
978+
nickname = contact_nickname.value,
979+
photoUri = currentContactPhotoPath,
980+
phoneNumbers = filledPhoneNumbers,
981+
emails = filledEmails,
982+
addresses = filledAddresses,
983+
IMs = filledIMs,
984+
events = filledEvents,
985+
starred = if (isContactStarred()) 1 else 0,
986+
notes = contact_notes.value,
987+
websites = filledWebsites,
988+
)
989+
990+
val company = contact_organization_company.value
991+
val jobPosition = contact_organization_job_position.value
992+
newContact.organization = Organization(company, jobPosition)
993+
return newContact
994+
}
995+
972996
private fun getFilledPhoneNumbers(): ArrayList<PhoneNumber> {
973997
val phoneNumbers = ArrayList<PhoneNumber>()
974998
val numbersCount = contact_numbers_holder.childCount

0 commit comments

Comments
 (0)