Skip to content

Commit 82f01f8

Browse files
committed
Add the default number indicator at the View Details too
1 parent d83c14d commit 82f01f8

File tree

4 files changed

+158
-34
lines changed

4 files changed

+158
-34
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ android {
6363
}
6464

6565
dependencies {
66-
implementation 'com.github.SimpleMobileTools:Simple-Commons:1bc50d636c'
66+
implementation 'com.github.SimpleMobileTools:Simple-Commons:2752395357'
6767
implementation 'com.googlecode.ez-vcard:ez-vcard:0.11.3'
6868
implementation 'com.github.tibbi:IndicatorFastScroll:4524cd0b61'
6969
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

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

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class EditContactActivity : ContactActivity() {
7070
private var emailViewToColor: EditText? = null
7171
private var originalContactSource = ""
7272

73+
enum class PrimaryNumberStatus {
74+
UNCHANGED, STARRED, UNSTARRED
75+
}
76+
7377
override fun onCreate(savedInstanceState: Bundle?) {
7478
showTransparentTop = true
7579
super.onCreate(savedInstanceState)
@@ -1024,8 +1028,14 @@ class EditContactActivity : ContactActivity() {
10241028
}
10251029
}
10261030

1031+
val contactValues = fillContactValues()
1032+
10271033
val oldPhotoUri = contact!!.photoUri
1028-
contact = fillContactValues()
1034+
val oldPrimary = contact!!.phoneNumbers.find { it.isPrimary }
1035+
val newPrimary = contactValues.phoneNumbers.find { it.isPrimary }
1036+
val primaryState = Pair(oldPrimary, newPrimary)
1037+
1038+
contact = contactValues
10291039

10301040
ensureBackgroundThread {
10311041
config.lastUsedContactSource = contact!!.source
@@ -1034,7 +1044,7 @@ class EditContactActivity : ContactActivity() {
10341044
originalContactSource != contact!!.source -> insertNewContact(true)
10351045
else -> {
10361046
val photoUpdateStatus = getPhotoUpdateStatus(oldPhotoUri, contact!!.photoUri)
1037-
updateContact(photoUpdateStatus)
1047+
updateContact(photoUpdateStatus, primaryState)
10381048
}
10391049
}
10401050
}
@@ -1199,17 +1209,75 @@ class EditContactActivity : ContactActivity() {
11991209
}
12001210
}
12011211

1202-
private fun updateContact(photoUpdateStatus: Int) {
1212+
private fun updateContact(photoUpdateStatus: Int, primaryState: Pair<PhoneNumber?, PhoneNumber?>) {
12031213
isSaving = true
12041214
if (ContactsHelper(this@EditContactActivity).updateContact(contact!!, photoUpdateStatus)) {
1205-
setResult(Activity.RESULT_OK)
1206-
hideKeyboard()
1207-
finish()
1215+
val status = getPrimaryNumberStatus(primaryState.first, primaryState.second)
1216+
if (status != PrimaryNumberStatus.UNCHANGED) {
1217+
updateDefaultNumberForDuplicateContacts(primaryState, status) {
1218+
setResult(Activity.RESULT_OK)
1219+
hideKeyboard()
1220+
finish()
1221+
}
1222+
} else {
1223+
setResult(Activity.RESULT_OK)
1224+
hideKeyboard()
1225+
finish()
1226+
}
12081227
} else {
12091228
toast(R.string.unknown_error_occurred)
12101229
}
12111230
}
12121231

1232+
private fun updateDefaultNumberForDuplicateContacts(
1233+
toggleState: Pair<PhoneNumber?, PhoneNumber?>,
1234+
primaryStatus: PrimaryNumberStatus,
1235+
callback: () -> Unit
1236+
) {
1237+
val contactsHelper = ContactsHelper(this)
1238+
1239+
contactsHelper.getDuplicatesOfContact(contact!!, false) { contacts ->
1240+
ensureBackgroundThread {
1241+
val displayContactSources = getVisibleContactSources()
1242+
contacts.filter { displayContactSources.contains(it.source) }.forEach { contact ->
1243+
val duplicate = contactsHelper.getContactWithId(contact.id, contact.isPrivate())
1244+
if (duplicate != null) {
1245+
if (primaryStatus == PrimaryNumberStatus.UNSTARRED) {
1246+
val number = duplicate.phoneNumbers.find { it.normalizedNumber == toggleState.first!!.normalizedNumber }
1247+
number?.isPrimary = false
1248+
} else if (primaryStatus == PrimaryNumberStatus.STARRED) {
1249+
val number = duplicate.phoneNumbers.find { it.normalizedNumber == toggleState.second!!.normalizedNumber }
1250+
if (number != null) {
1251+
duplicate.phoneNumbers.forEach {
1252+
it.isPrimary = false
1253+
}
1254+
number.isPrimary = true
1255+
}
1256+
}
1257+
1258+
contactsHelper.updateContact(duplicate, PHOTO_UNCHANGED)
1259+
}
1260+
}
1261+
1262+
runOnUiThread {
1263+
callback.invoke()
1264+
}
1265+
}
1266+
}
1267+
}
1268+
1269+
private fun getPrimaryNumberStatus(oldPrimary: PhoneNumber?, newPrimary: PhoneNumber?): PrimaryNumberStatus {
1270+
return if (oldPrimary != null && newPrimary != null && oldPrimary != newPrimary) {
1271+
PrimaryNumberStatus.STARRED
1272+
} else if (oldPrimary == null && newPrimary != null) {
1273+
PrimaryNumberStatus.STARRED
1274+
} else if (oldPrimary != null && newPrimary == null) {
1275+
PrimaryNumberStatus.UNSTARRED
1276+
} else {
1277+
PrimaryNumberStatus.UNCHANGED
1278+
}
1279+
}
1280+
12131281
private fun getPhotoUpdateStatus(oldUri: String, newUri: String): Int {
12141282
return if (oldUri.isEmpty() && newUri.isNotEmpty()) {
12151283
PHOTO_ADDED

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

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import android.view.View
1212
import android.view.WindowInsetsController
1313
import android.view.WindowManager
1414
import android.widget.RelativeLayout
15+
import androidx.core.view.isVisible
1516
import com.bumptech.glide.Glide
1617
import com.bumptech.glide.load.resource.bitmap.FitCenter
1718
import 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) {

app/src/main/res/layout/item_view_phone_number.xml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
34
android:id="@+id/contact_number_holder"
45
android:layout_width="match_parent"
5-
android:layout_height="wrap_content"
6+
android:layout_height="46dp"
67
android:background="?attr/selectableItemBackground"
78
android:paddingStart="@dimen/small_margin"
8-
android:paddingTop="@dimen/normal_margin"
9-
android:paddingEnd="@dimen/normal_margin"
10-
android:paddingBottom="@dimen/normal_margin">
9+
android:paddingEnd="@dimen/normal_margin">
10+
11+
<ImageView
12+
android:id="@+id/default_toggle_icon"
13+
android:layout_width="@dimen/contact_icons_size"
14+
android:layout_height="match_parent"
15+
android:layout_centerVertical="true"
16+
android:layout_toStartOf="@+id/contact_number_type"
17+
android:padding="@dimen/tiny_margin"
18+
android:visibility="gone"
19+
app:srcCompat="@drawable/ic_star_vector" />
1120

1221
<com.simplemobiletools.commons.views.MyTextView
1322
android:id="@+id/contact_number"
1423
android:layout_width="match_parent"
1524
android:layout_height="wrap_content"
1625
android:layout_centerVertical="true"
17-
android:layout_toStartOf="@+id/contact_number_type"
26+
android:layout_toStartOf="@+id/default_toggle_icon"
1827
android:lines="1"
1928
android:maxLines="1"
2029
android:singleLine="true"
@@ -29,6 +38,7 @@
2938
android:layout_alignParentEnd="true"
3039
android:layout_centerVertical="true"
3140
android:gravity="center"
41+
android:minWidth="70dp"
3242
android:paddingStart="@dimen/medium_margin"
3343
android:paddingEnd="@dimen/medium_margin"
3444
android:text="@string/mobile"

0 commit comments

Comments
 (0)