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

Ensure editing non-messenger contact while editing merged contact (#201) #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -258,7 +256,7 @@ class ViewContactActivity : ContactActivity() {
}
}

getDuplicateContacts {
initializeDuplicateContacts {
duplicateInitialized = true
setupContactDetails()
}
Expand Down Expand Up @@ -286,7 +284,7 @@ class ViewContactActivity : ContactActivity() {
private fun launchEditContact(contact: Contact) {
wasEditLaunched = true
duplicateInitialized = false
editContact(contact)
editContact(contact, config.mergeDuplicateContacts)
}

private fun openWith() {
Expand Down Expand Up @@ -819,21 +817,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!!) {
duplicateContacts = it
runOnUiThread {
callback()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/kotlin/org/fossify/contacts/extensions/Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,42 @@ fun Activity.viewContact(contact: Contact) {
}
}

fun Activity.editContact(contact: Contact, isMergedDuplicate: Boolean) {
if (!isMergedDuplicate) {
editContact(contact)
}
ContactsHelper(this).getContactSources { contactSources ->
if (!contact.isMessengerContact(contactSources)) {
editContact(contact)
} else {
getDuplicateContacts(contact) { duplicate ->
val nonMessengerDuplicate = duplicate.find {
!it.isMessengerContact(contactSources)
}
runOnUiThread {
editContact(nonMessengerDuplicate ?: contact)
}
}
}
}
}

fun Activity.getDuplicateContacts(contact: Contact, callback: (duplicateContacts: ArrayList<Contact>) -> Unit) {
val duplicateContacts = ArrayList<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 {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/kotlin/org/fossify/contacts/extensions/Contact.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.fossify.contacts.extensions

import org.fossify.commons.helpers.*
import org.fossify.commons.models.contacts.Contact
import org.fossify.commons.models.contacts.ContactSource

val messengerSources = hashSetOf(TELEGRAM_PACKAGE, SIGNAL_PACKAGE, WHATSAPP_PACKAGE, VIBER_PACKAGE, THREEMA_PACKAGE)

fun Contact.isMessengerContact(contactSources: List<ContactSource>): Boolean {
val sourceData = contactSources.find { it.name == this.source }
return sourceData != null && messengerSources.contains(sourceData.type)
}
Loading