@@ -12,6 +12,7 @@ import android.view.View
1212import android.view.WindowInsetsController
1313import android.view.WindowManager
1414import android.widget.RelativeLayout
15+ import androidx.core.view.isVisible
1516import com.bumptech.glide.Glide
1617import com.bumptech.glide.load.resource.bitmap.FitCenter
1718import com.bumptech.glide.load.resource.bitmap.RoundedCorners
@@ -45,6 +46,8 @@ class ViewContactActivity : ContactActivity() {
4546 private var contactSources = ArrayList <ContactSource >()
4647 private var showFields = 0
4748 private var fullContact: Contact ? = null // contact with all fields filled from duplicates
49+ private var duplicateInitialized = false
50+ private val mergeDuplicate: Boolean get() = config.mergeDuplicateContacts
4851
4952 private val COMPARABLE_PHONE_NUMBER_LENGTH = 9
5053
@@ -250,15 +253,13 @@ class ViewContactActivity : ContactActivity() {
250253 contactSources = it
251254 runOnUiThread {
252255 setupContactDetails()
253- if (config.mergeDuplicateContacts) {
254- getDuplicateContacts {
255- if (duplicateContacts.isNotEmpty()) {
256- setupContactDetails()
257- }
258- }
259- }
260256 }
261257 }
258+
259+ getDuplicateContacts {
260+ duplicateInitialized = true
261+ setupContactDetails()
262+ }
262263 }
263264
264265 private fun setupContactDetails () {
@@ -282,6 +283,7 @@ class ViewContactActivity : ContactActivity() {
282283
283284 private fun launchEditContact (contact : Contact ) {
284285 wasEditLaunched = true
286+ duplicateInitialized = false
285287 editContact(contact)
286288 }
287289
@@ -332,8 +334,32 @@ class ViewContactActivity : ContactActivity() {
332334
333335 private fun setupPhoneNumbers () {
334336 var phoneNumbers = contact!! .phoneNumbers.toMutableSet() as LinkedHashSet <PhoneNumber >
335- duplicateContacts.forEach {
336- phoneNumbers.addAll(it.phoneNumbers)
337+
338+ if (mergeDuplicate) {
339+ duplicateContacts.forEach {
340+ phoneNumbers.addAll(it.phoneNumbers)
341+ }
342+ }
343+
344+ if (duplicateInitialized) {
345+ val contactDefaultsNumbers = contact!! .phoneNumbers.filter { it.isPrimary }
346+ val duplicateContactsDefaultNumbers = duplicateContacts.flatMap { it.phoneNumbers }.filter { it.isPrimary }
347+ val defaultNumbers = (contactDefaultsNumbers + duplicateContactsDefaultNumbers).toSet()
348+
349+ if (defaultNumbers.size > 1 ) {
350+ phoneNumbers.forEach { it.isPrimary = false }
351+ } else if (defaultNumbers.size == 1 ) {
352+ if (mergeDuplicate) {
353+ val defaultNumber = defaultNumbers.first()
354+ val candidate = phoneNumbers.find { it.normalizedNumber == defaultNumber.normalizedNumber && ! it.isPrimary }
355+ candidate?.isPrimary = true
356+ } else {
357+ duplicateContactsDefaultNumbers.forEach { defaultNumber ->
358+ val candidate = phoneNumbers.find { it.normalizedNumber == defaultNumber.normalizedNumber && ! it.isPrimary }
359+ candidate?.isPrimary = true
360+ }
361+ }
362+ }
337363 }
338364
339365 phoneNumbers = phoneNumbers.distinctBy {
@@ -349,9 +375,8 @@ class ViewContactActivity : ContactActivity() {
349375 contact_numbers_holder.removeAllViews()
350376
351377 if (phoneNumbers.isNotEmpty() && showFields and SHOW_PHONE_NUMBERS_FIELD != 0 ) {
352- phoneNumbers.forEach {
378+ phoneNumbers.forEach { phoneNumber ->
353379 layoutInflater.inflate(R .layout.item_view_phone_number, contact_numbers_holder, false ).apply {
354- val phoneNumber = it
355380 contact_numbers_holder.addView(this )
356381 contact_number.text = phoneNumber.value
357382 contact_number_type.text = getPhoneNumberTypeText(phoneNumber.type, phoneNumber.label)
@@ -366,6 +391,8 @@ class ViewContactActivity : ContactActivity() {
366391 startCallIntent(phoneNumber.value)
367392 }
368393 }
394+
395+ contact_number_holder.default_toggle_icon.isVisible = phoneNumber.isPrimary
369396 }
370397 }
371398 contact_numbers_image.beVisible()
@@ -410,8 +437,11 @@ class ViewContactActivity : ContactActivity() {
410437
411438 private fun setupAddresses () {
412439 var addresses = contact!! .addresses.toMutableSet() as LinkedHashSet <Address >
413- duplicateContacts.forEach {
414- addresses.addAll(it.addresses)
440+
441+ if (mergeDuplicate) {
442+ duplicateContacts.forEach {
443+ addresses.addAll(it.addresses)
444+ }
415445 }
416446
417447 addresses = addresses.sortedBy { it.type }.toMutableSet() as LinkedHashSet <Address >
@@ -442,8 +472,11 @@ class ViewContactActivity : ContactActivity() {
442472
443473 private fun setupIMs () {
444474 var IMs = contact!! .IMs .toMutableSet() as LinkedHashSet <IM >
445- duplicateContacts.forEach {
446- IMs .addAll(it.IMs )
475+
476+ if (mergeDuplicate) {
477+ duplicateContacts.forEach {
478+ IMs .addAll(it.IMs )
479+ }
447480 }
448481
449482 IMs = IMs .sortedBy { it.type }.toMutableSet() as LinkedHashSet <IM >
@@ -470,8 +503,11 @@ class ViewContactActivity : ContactActivity() {
470503
471504 private fun setupEvents () {
472505 var events = contact!! .events.toMutableSet() as LinkedHashSet <Event >
473- duplicateContacts.forEach {
474- events.addAll(it.events)
506+
507+ if (mergeDuplicate) {
508+ duplicateContacts.forEach {
509+ events.addAll(it.events)
510+ }
475511 }
476512
477513 events = events.sortedBy { it.type }.toMutableSet() as LinkedHashSet <Event >
@@ -497,8 +533,11 @@ class ViewContactActivity : ContactActivity() {
497533
498534 private fun setupWebsites () {
499535 var websites = contact!! .websites.toMutableSet() as LinkedHashSet <String >
500- duplicateContacts.forEach {
501- websites.addAll(it.websites)
536+
537+ if (mergeDuplicate) {
538+ duplicateContacts.forEach {
539+ websites.addAll(it.websites)
540+ }
502541 }
503542
504543 websites = websites.sorted().toMutableSet() as LinkedHashSet <String >
@@ -528,8 +567,11 @@ class ViewContactActivity : ContactActivity() {
528567
529568 private fun setupGroups () {
530569 var groups = contact!! .groups.toMutableSet() as LinkedHashSet <Group >
531- duplicateContacts.forEach {
532- groups.addAll(it.groups)
570+
571+ if (mergeDuplicate) {
572+ duplicateContacts.forEach {
573+ groups.addAll(it.groups)
574+ }
533575 }
534576
535577 groups = groups.sortedBy { it.title }.toMutableSet() as LinkedHashSet <Group >
@@ -558,8 +600,11 @@ class ViewContactActivity : ContactActivity() {
558600 if (showFields and SHOW_CONTACT_SOURCE_FIELD != 0 ) {
559601 var sources = HashMap <Contact , String >()
560602 sources[contact!! ] = getPublicContactSourceSync(contact!! .source, contactSources)
561- duplicateContacts.forEach {
562- sources[it] = getPublicContactSourceSync(it.source, contactSources)
603+
604+ if (mergeDuplicate) {
605+ duplicateContacts.forEach {
606+ sources[it] = getPublicContactSourceSync(it.source, contactSources)
607+ }
563608 }
564609
565610 if (sources.size > 1 ) {
0 commit comments