split the name between first/middle/sur name

This commit is contained in:
tibbi 2017-12-13 13:24:14 +01:00
parent 6ee9a23897
commit 791dbb2960
4 changed files with 102 additions and 30 deletions

View File

@ -135,7 +135,7 @@ class ContactActivity : SimpleActivity() {
private fun setupEditContact() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
supportActionBar?.title = resources.getString(R.string.edit_contact)
contact_name.setText(contact!!.name)
contact_name.setText(contact!!.firstName)
contact_number.setText(contact!!.number)
contact_email.setText(contact!!.email)
}
@ -143,7 +143,7 @@ class ContactActivity : SimpleActivity() {
private fun setupNewContact() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
supportActionBar?.title = resources.getString(R.string.new_contact)
contact = Contact(0, "", "", "", "", "")
contact = Contact(0, "", "", "", "", "", "", "")
}
private fun applyPhotoPlaceholder() {

View File

@ -100,7 +100,7 @@ class ContactsAdapter(activity: SimpleActivity, var contactItems: MutableList<Co
private fun setupView(view: View, contact: Contact) {
view.apply {
contact_name.text = contact.name
contact_name.text = contact.getFullName()
contact_name.setTextColor(textColor)
contact_number.text = contact.number
contact_number.setTextColor(textColor)

View File

@ -41,23 +41,32 @@ class ContactsHelper(val activity: SimpleActivity) {
Thread {
val sources = activity.config.displayContactSources
val questionMarks = ("?," * sources.size).trimEnd(',')
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
val uri = ContactsContract.Data.CONTENT_URI
val projection = getContactProjection()
val selection = if (activity.config.showAllContacts()) null else "${ContactsContract.RawContacts.ACCOUNT_NAME} IN ($questionMarks)"
val selectionArgs = if (activity.config.showAllContacts()) null else sources.toTypedArray()
var selection = "${ContactsContract.Data.MIMETYPE} = ?"
var selectionArgs = arrayOf(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
if (!activity.config.showAllContacts()) {
selection += " AND ${ContactsContract.RawContacts.ACCOUNT_NAME} IN ($questionMarks)"
selectionArgs += sources.toTypedArray()
}
val sortOrder = getSortString()
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, sortOrder)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)
val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) ?: continue
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: ""
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: ""
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID)
val firstName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME) ?: ""
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
val familyName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
if (firstName.isEmpty() && middleName.isEmpty() && familyName.isEmpty())
continue
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI) ?: ""
val number = "" // proper value is obtained below
val email = "" // proper value is obtained below
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)
val contact = Contact(id, name, number, photoUri, email, accountName)
val contact = Contact(id, firstName, middleName, familyName, photoUri, number, email, accountName)
contacts.put(id, contact)
} while (cursor.moveToNext())
}
@ -73,6 +82,12 @@ class ContactsHelper(val activity: SimpleActivity) {
}
}
getNumbers().forEach {
if (contacts.containsKey(it.first)) {
contacts[it.first]!!.number = it.second
}
}
callback(ArrayList(contacts.values))
}.start()
}
@ -94,7 +109,10 @@ class ContactsHelper(val activity: SimpleActivity) {
private fun getEmails(): ArrayList<Pair<Int, String>> {
val pairs = ArrayList<Pair<Int, String>>()
val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI
val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Email.DATA)
val projection = arrayOf(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Email.DATA
)
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, null, null, null)
@ -111,6 +129,29 @@ class ContactsHelper(val activity: SimpleActivity) {
return pairs
}
private fun getNumbers(): ArrayList<Pair<Int, String>> {
val pairs = ArrayList<Pair<Int, String>>()
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
val projection = arrayOf(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER
)
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER)
pairs.add(Pair(id, number))
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
return pairs
}
fun getContactEmail(id: Int): String {
val uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI
val projection = arrayOf(ContactsContract.CommonDataKinds.Email.DATA)
@ -129,24 +170,45 @@ class ContactsHelper(val activity: SimpleActivity) {
return ""
}
fun getContactWithId(id: Int): Contact? {
if (id == 0) {
return null
}
fun getContactNumber(id: Int): String {
val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
val projection = getContactProjection()
val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.NUMBER)
val selection = "${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} = ?"
val selectionArgs = arrayOf(id.toString())
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
val name = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) ?: return null
val number = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER) ?: ""
return cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.NUMBER)
}
} finally {
cursor?.close()
}
return ""
}
fun getContactWithId(id: Int): Contact? {
if (id == 0) {
return null
}
val uri = ContactsContract.Data.CONTENT_URI
val projection = getContactProjection()
val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID} = ?"
val selectionArgs = arrayOf(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, id.toString())
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor?.moveToFirst() == true) {
val firstName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME) ?: ""
val middleName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME) ?: ""
val familyName = cursor.getStringValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME) ?: ""
val photoUri = cursor.getStringValue(ContactsContract.CommonDataKinds.Phone.PHOTO_URI) ?: ""
val number = getContactNumber(id)
val email = getContactEmail(id)
return Contact(id, name, number, photoUri, email, "")
val accountName = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME)
return Contact(id, firstName, middleName, familyName, photoUri, number, email, accountName)
}
} finally {
cursor?.close()
@ -156,10 +218,11 @@ class ContactsHelper(val activity: SimpleActivity) {
}
private fun getContactProjection() = arrayOf(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.CommonDataKinds.StructuredName.PHOTO_URI,
ContactsContract.RawContacts.ACCOUNT_NAME
)

View File

@ -3,7 +3,8 @@ package com.simplemobiletools.contacts.models
import com.simplemobiletools.commons.helpers.SORT_BY_NUMBER
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
data class Contact(val id: Int, var name: String, var number: String, var photoUri: String, var email: String, var source: String) : Comparable<Contact> {
data class Contact(val id: Int, var firstName: String, var middleName: String, var familyName: String, var photoUri: String, var number: String,
var email: String, var source: String) : Comparable<Contact> {
companion object {
var sorting: Int = 0
}
@ -11,12 +12,12 @@ data class Contact(val id: Int, var name: String, var number: String, var photoU
override fun compareTo(other: Contact): Int {
var result = when {
(sorting and SORT_BY_NUMBER != 0) -> number.toLowerCase().compareTo(other.number.toLowerCase())
else -> if (name.first().isLetter() && !other.name.first().isLetter()) {
else -> if (firstName.firstOrNull()?.isLetter() == true && other.firstName.firstOrNull()?.isLetter() == false) {
-1
} else if (!name.first().isLetter() && other.name.first().isLetter()) {
} else if (firstName.firstOrNull()?.isLetter() == false && other.firstName.firstOrNull()?.isLetter() == true) {
1
} else {
name.toLowerCase().compareTo(other.name.toLowerCase())
firstName.toLowerCase().compareTo(other.firstName.toLowerCase())
}
}
@ -29,6 +30,14 @@ data class Contact(val id: Int, var name: String, var number: String, var photoU
fun getBubbleText() = when {
sorting and SORT_BY_NUMBER != 0 -> number
else -> name
else -> firstName
}
fun getFullName(): String {
var name = firstName
if (middleName.isNotEmpty()) {
name += " $middleName"
}
return "$name $familyName".trim()
}
}