add a toggle for starting names with surname

This commit is contained in:
tibbi 2017-12-13 17:35:03 +01:00
parent 74c5b19025
commit 42cc8aaa2f
7 changed files with 63 additions and 13 deletions

View File

@ -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
}
}

View File

@ -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
}
}
}

View File

@ -24,10 +24,13 @@ import kotlinx.android.synthetic.main.item_contact.view.*
class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Contact>, 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<Co
private fun setupView(view: View, contact: Contact) {
view.apply {
contact_first_name.text = contact.getFullName()
contact_first_name.text = contact.getFullName(startNameWithSurname)
contact_first_name.setTextColor(textColor)
contact_number.text = contact.number
contact_number.setTextColor(textColor)

View File

@ -22,4 +22,8 @@ class Config(context: Context) : BaseConfig(context) {
set(displayContactSources) = prefs.edit().remove(DISPLAY_CONTACT_SOURCES).putStringSet(DISPLAY_CONTACT_SOURCES, displayContactSources).apply()
fun showAllContacts() = displayContactSources.size == 1 && displayContactSources.first() == "-1"
var startNameWithSurname: Boolean
get() = prefs.getBoolean(START_NAME_WITH_SURNAME, false)
set(startNameWithSurname) = prefs.edit().putBoolean(START_NAME_WITH_SURNAME, startNameWithSurname).apply()
}

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.contacts.helpers
// shared prefs
val CALL_CONTACT_ON_CLICK = "call_contact_on_click"
val DISPLAY_CONTACT_SOURCES = "display_contact_sources"
val START_NAME_WITH_SURNAME = "start_name_with_surname"
val SORTING = "sorting"
val CONTACT_ID = "contact_id"

View File

@ -1,6 +1,9 @@
package com.simplemobiletools.contacts.models
import com.simplemobiletools.commons.helpers.*
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
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<Contact> {
@ -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 {

View File

@ -89,5 +89,25 @@
android:text="@string/call_contact_on_click"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_start_with_surname_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_margin"
android:background="?attr/selectableItemBackground"
android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MySwitchCompat
android:id="@+id/settings_start_with_surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:clickable="false"
android:paddingLeft="@dimen/medium_margin"
android:paddingStart="@dimen/medium_margin"
android:text="@string/start_name_with_surname"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>