@@ -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,10 @@ 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 by lazy {
51+ config.mergeDuplicateContacts
52+ }
4853
4954 private val COMPARABLE_PHONE_NUMBER_LENGTH = 9
5055
@@ -250,12 +255,9 @@ class ViewContactActivity : ContactActivity() {
250255 contactSources = it
251256 runOnUiThread {
252257 setupContactDetails()
253- if (config.mergeDuplicateContacts) {
254- getDuplicateContacts {
255- if (duplicateContacts.isNotEmpty()) {
256- setupContactDetails()
257- }
258- }
258+ getDuplicateContacts {
259+ duplicateInitialized = true
260+ setupContactDetails()
259261 }
260262 }
261263 }
@@ -282,6 +284,7 @@ class ViewContactActivity : ContactActivity() {
282284
283285 private fun launchEditContact (contact : Contact ) {
284286 wasEditLaunched = true
287+ duplicateInitialized = false
285288 editContact(contact)
286289 }
287290
@@ -332,8 +335,32 @@ class ViewContactActivity : ContactActivity() {
332335
333336 private fun setupPhoneNumbers () {
334337 var phoneNumbers = contact!! .phoneNumbers.toMutableSet() as LinkedHashSet <PhoneNumber >
335- duplicateContacts.forEach {
336- phoneNumbers.addAll(it.phoneNumbers)
338+
339+ if (mergeDuplicate) {
340+ duplicateContacts.forEach {
341+ phoneNumbers.addAll(it.phoneNumbers)
342+ }
343+ }
344+
345+ if (duplicateInitialized) {
346+ val contactDefaultsNumbers = contact!! .phoneNumbers.filter { it.isPrimary }
347+ val duplicateContactsDefaultNumbers = duplicateContacts.flatMap { it.phoneNumbers }.filter { it.isPrimary }
348+ val defaultNumbers = (contactDefaultsNumbers + duplicateContactsDefaultNumbers).toSet()
349+
350+ if (defaultNumbers.size > 1 ) {
351+ phoneNumbers.forEach { it.isPrimary = false }
352+ } else if (defaultNumbers.size == 1 ) {
353+ if (mergeDuplicate) {
354+ val defaultNumber = defaultNumbers.first()
355+ val candidate = phoneNumbers.find { it.normalizedNumber == defaultNumber.normalizedNumber && ! it.isPrimary }
356+ candidate?.isPrimary = true
357+ } else {
358+ duplicateContactsDefaultNumbers.forEach { defaultNumber ->
359+ val candidate = phoneNumbers.find { it.normalizedNumber == defaultNumber.normalizedNumber && ! it.isPrimary }
360+ candidate?.isPrimary = true
361+ }
362+ }
363+ }
337364 }
338365
339366 phoneNumbers = phoneNumbers.distinctBy {
@@ -349,9 +376,8 @@ class ViewContactActivity : ContactActivity() {
349376 contact_numbers_holder.removeAllViews()
350377
351378 if (phoneNumbers.isNotEmpty() && showFields and SHOW_PHONE_NUMBERS_FIELD != 0 ) {
352- phoneNumbers.forEach {
379+ phoneNumbers.forEach { phoneNumber ->
353380 layoutInflater.inflate(R .layout.item_view_phone_number, contact_numbers_holder, false ).apply {
354- val phoneNumber = it
355381 contact_numbers_holder.addView(this )
356382 contact_number.text = phoneNumber.value
357383 contact_number_type.text = getPhoneNumberTypeText(phoneNumber.type, phoneNumber.label)
@@ -366,6 +392,8 @@ class ViewContactActivity : ContactActivity() {
366392 startCallIntent(phoneNumber.value)
367393 }
368394 }
395+
396+ contact_number_holder.default_toggle_icon.isVisible = duplicateInitialized && phoneNumber.isPrimary
369397 }
370398 }
371399 contact_numbers_image.beVisible()
@@ -410,8 +438,11 @@ class ViewContactActivity : ContactActivity() {
410438
411439 private fun setupAddresses () {
412440 var addresses = contact!! .addresses.toMutableSet() as LinkedHashSet <Address >
413- duplicateContacts.forEach {
414- addresses.addAll(it.addresses)
441+
442+ if (mergeDuplicate) {
443+ duplicateContacts.forEach {
444+ addresses.addAll(it.addresses)
445+ }
415446 }
416447
417448 addresses = addresses.sortedBy { it.type }.toMutableSet() as LinkedHashSet <Address >
@@ -442,8 +473,11 @@ class ViewContactActivity : ContactActivity() {
442473
443474 private fun setupIMs () {
444475 var IMs = contact!! .IMs .toMutableSet() as LinkedHashSet <IM >
445- duplicateContacts.forEach {
446- IMs .addAll(it.IMs )
476+
477+ if (mergeDuplicate) {
478+ duplicateContacts.forEach {
479+ IMs .addAll(it.IMs )
480+ }
447481 }
448482
449483 IMs = IMs .sortedBy { it.type }.toMutableSet() as LinkedHashSet <IM >
@@ -470,8 +504,11 @@ class ViewContactActivity : ContactActivity() {
470504
471505 private fun setupEvents () {
472506 var events = contact!! .events.toMutableSet() as LinkedHashSet <Event >
473- duplicateContacts.forEach {
474- events.addAll(it.events)
507+
508+ if (mergeDuplicate) {
509+ duplicateContacts.forEach {
510+ events.addAll(it.events)
511+ }
475512 }
476513
477514 events = events.sortedBy { it.type }.toMutableSet() as LinkedHashSet <Event >
@@ -497,8 +534,11 @@ class ViewContactActivity : ContactActivity() {
497534
498535 private fun setupWebsites () {
499536 var websites = contact!! .websites.toMutableSet() as LinkedHashSet <String >
500- duplicateContacts.forEach {
501- websites.addAll(it.websites)
537+
538+ if (mergeDuplicate) {
539+ duplicateContacts.forEach {
540+ websites.addAll(it.websites)
541+ }
502542 }
503543
504544 websites = websites.sorted().toMutableSet() as LinkedHashSet <String >
@@ -528,8 +568,11 @@ class ViewContactActivity : ContactActivity() {
528568
529569 private fun setupGroups () {
530570 var groups = contact!! .groups.toMutableSet() as LinkedHashSet <Group >
531- duplicateContacts.forEach {
532- groups.addAll(it.groups)
571+
572+ if (mergeDuplicate) {
573+ duplicateContacts.forEach {
574+ groups.addAll(it.groups)
575+ }
533576 }
534577
535578 groups = groups.sortedBy { it.title }.toMutableSet() as LinkedHashSet <Group >
@@ -558,8 +601,11 @@ class ViewContactActivity : ContactActivity() {
558601 if (showFields and SHOW_CONTACT_SOURCE_FIELD != 0 ) {
559602 var sources = HashMap <Contact , String >()
560603 sources[contact!! ] = getPublicContactSourceSync(contact!! .source, contactSources)
561- duplicateContacts.forEach {
562- sources[it] = getPublicContactSourceSync(it.source, contactSources)
604+
605+ if (mergeDuplicate) {
606+ duplicateContacts.forEach {
607+ sources[it] = getPublicContactSourceSync(it.source, contactSources)
608+ }
563609 }
564610
565611 if (sources.size > 1 ) {
0 commit comments