From 3cbf38c92ef413ff40736c9fd50f6c7169aaeed6 Mon Sep 17 00:00:00 2001 From: Agnieszka C <85929121+Aga-C@users.noreply.github.com> Date: Sat, 31 May 2025 17:01:43 +0100 Subject: [PATCH] feat: add dialog for choosing contact source when editing merged contact (#201) * Ensure editing non-messenger contact while editing merged contact (#201) * Added dialog for choosing a contact source * Update CHANGELOG.md * Fixed bugs * fix: add title to chooser dialog For visual context. --------- Co-authored-by: Naveen Singh <36371707+naveensingh@users.noreply.github.com> Co-authored-by: Naveen Singh --- CHANGELOG.md | 5 ++ .../activities/ViewContactActivity.kt | 36 +++++------- .../contacts/adapters/ContactsAdapter.kt | 2 +- .../fossify/contacts/extensions/Activity.kt | 55 ++++++++++++++++++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 75 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88218b9e..84800f4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Dialog for choosing contact source while editing a merged contact ([#201]) + ## [1.1.0] - 2024-10-28 ### Added @@ -42,3 +45,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [1.1.0]: https://github.com/FossifyOrg/Contacts/compare/1.0.1...1.1.0 [1.0.1]: https://github.com/FossifyOrg/Contacts/compare/1.0.0...1.0.1 [1.0.0]: https://github.com/FossifyOrg/Contacts/releases/tag/1.0.0 + +[#201]: https://github.com/FossifyOrg/Contacts/issues/201 diff --git a/app/src/main/kotlin/org/fossify/contacts/activities/ViewContactActivity.kt b/app/src/main/kotlin/org/fossify/contacts/activities/ViewContactActivity.kt index 45cd236c..a72bfe14 100644 --- a/app/src/main/kotlin/org/fossify/contacts/activities/ViewContactActivity.kt +++ b/app/src/main/kotlin/org/fossify/contacts/activities/ViewContactActivity.kt @@ -2,14 +2,12 @@ package org.fossify.contacts.activities import android.content.ActivityNotFoundException import android.content.ContentUris -import android.content.Context import android.content.Intent import android.media.AudioManager import android.media.RingtoneManager import android.net.Uri import android.os.Bundle import android.provider.ContactsContract -import android.telephony.PhoneNumberUtils import android.view.View import android.view.WindowManager import android.widget.RelativeLayout @@ -103,7 +101,7 @@ class ViewContactActivity : ContactActivity() { findItem(R.id.edit).setOnMenuItemClickListener { if (contact != null) { - launchEditContact(contact!!) + launchEditContact(contact!!, false) } true } @@ -258,7 +256,7 @@ class ViewContactActivity : ContactActivity() { } } - getDuplicateContacts { + initializeDuplicateContacts { duplicateInitialized = true setupContactDetails() } @@ -283,10 +281,14 @@ class ViewContactActivity : ContactActivity() { updateTextColors(binding.contactScrollview) } - private fun launchEditContact(contact: Contact) { + private fun launchEditContact(contact: Contact, editExactContact: Boolean) { wasEditLaunched = true duplicateInitialized = false - editContact(contact) + if (editExactContact) { + editContact(contact) + } else { + editContact(contact, config.mergeDuplicateContacts) + } } private fun openWith() { @@ -626,7 +628,7 @@ class ViewContactActivity : ContactActivity() { binding.contactSourcesHolder.addView(root) contactSource.setOnClickListener { - launchEditContact(key) + launchEditContact(key, true) } if (value.lowercase(Locale.getDefault()) == WHATSAPP) { @@ -819,21 +821,11 @@ class ViewContactActivity : ContactActivity() { } } - private fun getDuplicateContacts(callback: () -> Unit) { - ContactsHelper(this).getDuplicatesOfContact(contact!!, false) { contacts -> - ensureBackgroundThread { - duplicateContacts.clear() - val displayContactSources = getVisibleContactSources() - contacts.filter { displayContactSources.contains(it.source) }.forEach { - val duplicate = ContactsHelper(this).getContactWithId(it.id, it.isPrivate()) - if (duplicate != null) { - duplicateContacts.add(duplicate) - } - } - - runOnUiThread { - callback() - } + private fun initializeDuplicateContacts(callback: () -> Unit) { + getDuplicateContacts(contact!!, false) { + duplicateContacts = it + runOnUiThread { + callback() } } } diff --git a/app/src/main/kotlin/org/fossify/contacts/adapters/ContactsAdapter.kt b/app/src/main/kotlin/org/fossify/contacts/adapters/ContactsAdapter.kt index cb94c19f..4b511c23 100644 --- a/app/src/main/kotlin/org/fossify/contacts/adapters/ContactsAdapter.kt +++ b/app/src/main/kotlin/org/fossify/contacts/adapters/ContactsAdapter.kt @@ -192,7 +192,7 @@ class ContactsAdapter( private fun editContact() { val contact = getItemWithKey(selectedKeys.first()) ?: return - activity.editContact(contact) + activity.editContact(contact, config.mergeDuplicateContacts) } private fun askConfirmDelete() { diff --git a/app/src/main/kotlin/org/fossify/contacts/extensions/Activity.kt b/app/src/main/kotlin/org/fossify/contacts/extensions/Activity.kt index 96353f8e..e05e4085 100644 --- a/app/src/main/kotlin/org/fossify/contacts/extensions/Activity.kt +++ b/app/src/main/kotlin/org/fossify/contacts/extensions/Activity.kt @@ -82,7 +82,7 @@ fun SimpleActivity.handleGenericContactClick(contact: Contact) { when (config.onContactClick) { ON_CLICK_CALL_CONTACT -> callContact(contact) ON_CLICK_VIEW_CONTACT -> viewContact(contact) - ON_CLICK_EDIT_CONTACT -> editContact(contact) + ON_CLICK_EDIT_CONTACT -> editContact(contact, config.mergeDuplicateContacts) } } @@ -104,6 +104,59 @@ fun Activity.viewContact(contact: Contact) { } } +fun Activity.editContact(contact: Contact, isMergedDuplicate: Boolean) { + if (!isMergedDuplicate) { + editContact(contact) + } else { + ContactsHelper(this).getContactSources { contactSources -> + getDuplicateContacts(contact, true) { contacts -> + if (contacts.size == 1) { + runOnUiThread { + editContact(contacts.first()) + } + } else { + val items = ArrayList(contacts.mapIndexed { index, contact -> + var source = getPublicContactSourceSync(contact.source, contactSources) + if (source == "") { + source = getString(R.string.phone_storage) + } + RadioItem(index, source, contact) + }.sortedBy { it.title }) + + runOnUiThread { + RadioGroupDialog( + activity = this, + items = items, + titleId = R.string.select_account, + ) { + editContact(it as Contact) + } + } + } + } + } + } +} + +fun Activity.getDuplicateContacts(contact: Contact, includeCurrent: Boolean, callback: (duplicateContacts: ArrayList) -> Unit) { + val duplicateContacts = ArrayList() + if (includeCurrent) { + duplicateContacts.add(contact) + } + ContactsHelper(this).getDuplicatesOfContact(contact, false) { contacts -> + ensureBackgroundThread { + val displayContactSources = getVisibleContactSources() + contacts.filter { displayContactSources.contains(it.source) }.forEach { + val duplicate = ContactsHelper(this).getContactWithId(it.id, it.isPrivate()) + if (duplicate != null) { + duplicateContacts.add(duplicate) + } + } + callback(duplicateContacts) + } + } +} + fun Activity.editContact(contact: Contact) { hideKeyboard() Intent(applicationContext, EditContactActivity::class.java).apply { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4fc886e5..21de99cf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -30,6 +30,7 @@ Edit contact Select contact Nickname + Select account No groups