Skip to content

Commit e013fcf

Browse files
authored
Merge pull request #826 from pavelpoley/task/default-number-indicator
Add the default number indicator at the View Details too
2 parents 75379a8 + cb995a6 commit e013fcf

File tree

4 files changed

+158
-35
lines changed

4 files changed

+158
-35
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 & 23 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,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) {

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)