From 74c5b190254bd9377d1c847010183a8a894b9e60 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 13 Dec 2017 17:16:16 +0100 Subject: [PATCH] allow sorting contacts by every name separately --- app/build.gradle | 2 +- .../adapters/FilterContactSourcesAdapter.kt | 4 +-- .../contacts/dialogs/ChangeSortingDialog.kt | 16 +++++----- .../contacts/helpers/Config.kt | 4 +-- .../contacts/helpers/ContactsHelper.kt | 8 +++-- .../contacts/models/Contact.kt | 31 +++++++++++++------ .../main/res/layout/dialog_change_sorting.xml | 20 ++++++++++-- ...iew.xml => item_filter_contact_source.xml} | 0 8 files changed, 59 insertions(+), 26 deletions(-) rename app/src/main/res/layout/{filter_contact_source_view.xml => item_filter_contact_source.xml} (100%) diff --git a/app/build.gradle b/app/build.gradle index ef934f7d..bb9db9c9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,5 +32,5 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:3.2.17' + implementation 'com.simplemobiletools:commons:3.2.18' } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt index d8c815e8..e2a22542 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/adapters/FilterContactSourcesAdapter.kt @@ -8,7 +8,7 @@ import com.simplemobiletools.commons.interfaces.MyAdapterListener import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.extensions.config -import kotlinx.android.synthetic.main.filter_contact_source_view.view.* +import kotlinx.android.synthetic.main.item_filter_contact_source.view.* import java.util.* class FilterContactSourcesAdapter(val activity: SimpleActivity, val contactSources: List, val displayContactSources: Set) : @@ -53,7 +53,7 @@ class FilterContactSourcesAdapter(val activity: SimpleActivity, val contactSourc } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { - val view = activity.layoutInflater.inflate(R.layout.filter_contact_source_view, parent, false) + val view = activity.layoutInflater.inflate(R.layout.item_filter_contact_source, parent, false) return ViewHolder(view, adapterListener, activity) } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/ChangeSortingDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/ChangeSortingDialog.kt index 946ff2e7..cb1cd5d1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/ChangeSortingDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/dialogs/ChangeSortingDialog.kt @@ -3,9 +3,7 @@ package com.simplemobiletools.contacts.dialogs import android.support.v7.app.AlertDialog import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.setupDialogStuff -import com.simplemobiletools.commons.helpers.SORT_BY_NAME -import com.simplemobiletools.commons.helpers.SORT_BY_NUMBER -import com.simplemobiletools.commons.helpers.SORT_DESCENDING +import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.extensions.config import kotlinx.android.synthetic.main.dialog_change_sorting.view.* @@ -31,8 +29,10 @@ class ChangeSortingDialog(val activity: BaseSimpleActivity, val callback: () -> private fun setupSortRadio() { val sortingRadio = view.sorting_dialog_radio_sorting val sortBtn = when { - currSorting and SORT_BY_NUMBER != 0 -> sortingRadio.sorting_dialog_radio_number - else -> sortingRadio.sorting_dialog_radio_name + currSorting and SORT_BY_FIRST_NAME != 0 -> sortingRadio.sorting_dialog_radio_first_name + currSorting and SORT_BY_MIDDLE_NAME != 0 -> sortingRadio.sorting_dialog_radio_middle_name + currSorting and SORT_BY_SURNAME != 0 -> sortingRadio.sorting_dialog_radio_surname + else -> sortingRadio.sorting_dialog_radio_number } sortBtn.isChecked = true } @@ -50,8 +50,10 @@ class ChangeSortingDialog(val activity: BaseSimpleActivity, val callback: () -> private fun dialogConfirmed() { val sortingRadio = view.sorting_dialog_radio_sorting var sorting = when (sortingRadio.checkedRadioButtonId) { - R.id.sorting_dialog_radio_number -> SORT_BY_NUMBER - else -> SORT_BY_NAME + R.id.sorting_dialog_radio_first_name -> SORT_BY_FIRST_NAME + R.id.sorting_dialog_radio_middle_name -> SORT_BY_MIDDLE_NAME + R.id.sorting_dialog_radio_surname -> SORT_BY_SURNAME + else -> SORT_BY_NUMBER } if (view.sorting_dialog_radio_order.checkedRadioButtonId == R.id.sorting_dialog_radio_descending) { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt index 048dea2c..22b6d98a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Config.kt @@ -2,7 +2,7 @@ package com.simplemobiletools.contacts.helpers import android.content.Context import com.simplemobiletools.commons.helpers.BaseConfig -import com.simplemobiletools.commons.helpers.SORT_BY_NAME +import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME class Config(context: Context) : BaseConfig(context) { companion object { @@ -10,7 +10,7 @@ class Config(context: Context) : BaseConfig(context) { } var sorting: Int - get() = prefs.getInt(SORTING, SORT_BY_NAME) + get() = prefs.getInt(SORTING, SORT_BY_FIRST_NAME) set(sorting) = prefs.edit().putInt(SORTING, sorting).apply() var callContact: Boolean diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index e3746371..67c9f6a6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -5,7 +5,9 @@ import android.provider.ContactsContract import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getStringValue import com.simplemobiletools.commons.extensions.showErrorToast -import com.simplemobiletools.commons.helpers.SORT_BY_NAME +import com.simplemobiletools.commons.helpers.SORT_BY_FIRST_NAME +import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME +import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME import com.simplemobiletools.commons.helpers.SORT_DESCENDING import com.simplemobiletools.contacts.activities.SimpleActivity import com.simplemobiletools.contacts.extensions.config @@ -229,7 +231,9 @@ class ContactsHelper(val activity: SimpleActivity) { private fun getSortString(): String { val sorting = activity.config.sorting var sort = when { - sorting and SORT_BY_NAME != 0 -> "${ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME} COLLATE NOCASE" + sorting and SORT_BY_FIRST_NAME != 0 -> "${ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME} COLLATE NOCASE" + sorting and SORT_BY_MIDDLE_NAME != 0 -> "${ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME} COLLATE NOCASE" + sorting and SORT_BY_SURNAME != 0 -> "${ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME} COLLATE NOCASE" else -> ContactsContract.CommonDataKinds.Phone.NUMBER } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt index ada45c2c..e23511cc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/models/Contact.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.contacts.models -import com.simplemobiletools.commons.helpers.SORT_BY_NUMBER -import com.simplemobiletools.commons.helpers.SORT_DESCENDING +import com.simplemobiletools.commons.helpers.* data class Contact(val id: Int, var firstName: String, var middleName: String, var surname: String, var photoUri: String, var number: String, var email: String, var source: String) : Comparable { @@ -11,14 +10,10 @@ data class Contact(val id: Int, var firstName: String, var middleName: String, v override fun compareTo(other: Contact): Int { var result = when { - (sorting and SORT_BY_NUMBER != 0) -> number.toLowerCase().compareTo(other.number.toLowerCase()) - else -> if (firstName.firstOrNull()?.isLetter() == true && other.firstName.firstOrNull()?.isLetter() == false) { - -1 - } else if (firstName.firstOrNull()?.isLetter() == false && other.firstName.firstOrNull()?.isLetter() == true) { - 1 - } else { - firstName.toLowerCase().compareTo(other.firstName.toLowerCase()) - } + 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()) } if (sorting and SORT_DESCENDING != 0) { @@ -40,4 +35,20 @@ data class Contact(val id: Int, var firstName: String, var middleName: String, v } return "$name $surname".trim() } + + private fun compareStrings(first: String, second: String): Int { + return if (first.firstOrNull()?.isLetter() == true && second.firstOrNull()?.isLetter() == false) { + -1 + } else if (first.firstOrNull()?.isLetter() == false && second.firstOrNull()?.isLetter() == true) { + 1 + } else { + if (first.isEmpty() && second.isNotEmpty()) { + 1 + } else if (first.isNotEmpty() && second.isEmpty()) { + -1 + } else { + first.toLowerCase().compareTo(second.toLowerCase()) + } + } + } } diff --git a/app/src/main/res/layout/dialog_change_sorting.xml b/app/src/main/res/layout/dialog_change_sorting.xml index 7c35fc3c..e31b07b3 100644 --- a/app/src/main/res/layout/dialog_change_sorting.xml +++ b/app/src/main/res/layout/dialog_change_sorting.xml @@ -21,12 +21,28 @@ android:layout_marginBottom="@dimen/medium_margin"> + android:text="@string/first_name"/> + + + +