From 42cc8aaa2f2741bb906061d5374789f6839a95b5 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 13 Dec 2017 17:35:03 +0100 Subject: [PATCH] add a toggle for starting names with surname --- .../contacts/activities/MainActivity.kt | 15 +++++++++---- .../contacts/activities/SettingsActivity.kt | 9 ++++++++ .../contacts/adapters/ContactsAdapter.kt | 5 ++++- .../contacts/helpers/Config.kt | 4 ++++ .../contacts/helpers/Constants.kt | 1 + .../contacts/models/Contact.kt | 22 ++++++++++++------- app/src/main/res/layout/activity_settings.xml | 20 +++++++++++++++++ 7 files changed, 63 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt index 19ff6e85..b87fb3cb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/MainActivity.kt @@ -6,10 +6,7 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN -import com.simplemobiletools.commons.helpers.LICENSE_MULTISELECT -import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS -import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS +import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener import com.simplemobiletools.contacts.BuildConfig import com.simplemobiletools.contacts.R @@ -28,6 +25,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { private var storedTextColor = 0 private var storedBackgroundColor = 0 private var storedPrimaryColor = 0 + private var storedStartNameWithSurname = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -72,6 +70,14 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { contacts_fastscroller.updatePrimaryColor() } + if (storedStartNameWithSurname != config.startNameWithSurname) { + (contacts_list.adapter as ContactsAdapter).apply { + startNameWithSurname = config.startNameWithSurname + config.sorting = if (config.startNameWithSurname) SORT_BY_SURNAME else SORT_BY_FIRST_NAME + initContacts() + } + } + contacts_fastscroller.updateBubbleColors() contacts_fastscroller.allowBubbleDisplay = config.showInfoBubble updateTextColors(contacts_holder) @@ -126,6 +132,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { storedTextColor = textColor storedBackgroundColor = backgroundColor storedPrimaryColor = primaryColor + storedStartNameWithSurname = startNameWithSurname } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/SettingsActivity.kt index 67d25adc..5bc39421 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/activities/SettingsActivity.kt @@ -22,6 +22,7 @@ class SettingsActivity : SimpleActivity() { setupUseEnglish() setupShowInfoBubble() setupCallContactOnClick() + setupStartNameWithSurname() updateTextColors(settings_holder) } @@ -56,4 +57,12 @@ class SettingsActivity : SimpleActivity() { config.callContact = settings_call_contact_on_click.isChecked } } + + private fun setupStartNameWithSurname() { + settings_start_with_surname.isChecked = config.startNameWithSurname + settings_start_with_surname_holder.setOnClickListener { + settings_start_with_surname.toggle() + config.startNameWithSurname = settings_start_with_surname.isChecked + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt index f2516e70..63c487c0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/ContactsAdapter.kt @@ -24,10 +24,13 @@ import kotlinx.android.synthetic.main.item_contact.view.* class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList, val listener: RefreshRecyclerViewListener?, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { + var config = activity.config lateinit private var contactDrawable: Drawable + var startNameWithSurname: Boolean init { initDrawables() + startNameWithSurname = config.startNameWithSurname } override fun getActionMenuId() = R.menu.cab @@ -100,7 +103,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList { @@ -13,7 +16,7 @@ data class Contact(val id: Int, var firstName: String, var middleName: String, v sorting and SORT_BY_FIRST_NAME != 0 -> compareStrings(firstName, other.firstName) sorting and SORT_BY_MIDDLE_NAME != 0 -> compareStrings(middleName, other.middleName) sorting and SORT_BY_SURNAME != 0 -> compareStrings(surname, other.surname) - else -> number.toLowerCase().compareTo(other.number.toLowerCase()) + else -> compareStrings(number, other.number) } if (sorting and SORT_DESCENDING != 0) { @@ -24,16 +27,19 @@ data class Contact(val id: Int, var firstName: String, var middleName: String, v } fun getBubbleText() = when { - sorting and SORT_BY_NUMBER != 0 -> number - else -> firstName + sorting and SORT_BY_FIRST_NAME != 0 -> firstName + sorting and SORT_BY_MIDDLE_NAME != 0 -> middleName + sorting and SORT_BY_SURNAME != 0 -> surname + else -> number } - fun getFullName(): String { - var name = firstName + fun getFullName(startWithSurname: Boolean): String { + var firstPart = if (startWithSurname) surname else firstName if (middleName.isNotEmpty()) { - name += " $middleName" + firstPart += " $middleName" } - return "$name $surname".trim() + val lastPart = if (startWithSurname) firstName else surname + return "$firstPart $lastPart".trim() } private fun compareStrings(first: String, second: String): Int { diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 883190c5..3805e028 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -89,5 +89,25 @@ android:text="@string/call_contact_on_click"/> + + + + + +