implement contact name updating

This commit is contained in:
tibbi 2017-12-13 20:52:05 +01:00
parent 96fa9ab8dc
commit a04724a2da
3 changed files with 32 additions and 1 deletions

View File

@ -32,5 +32,5 @@ android {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:3.2.18' implementation 'com.simplemobiletools:commons:3.2.19'
} }

View File

@ -160,7 +160,17 @@ class ContactActivity : SimpleActivity() {
} }
private fun saveContact() { private fun saveContact() {
contact!!.apply {
firstName = contact_first_name.value
middleName = contact_middle_name.value
surname = contact_surname.value
number = contact_number.value
email = contact_email.value
if (ContactsHelper(this@ContactActivity).updateContact(this)) {
finish()
}
}
} }
private fun deleteContact() { private fun deleteContact() {

View File

@ -244,6 +244,27 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
return sort return sort
} }
fun updateContact(contact: Contact): Boolean {
return try {
val operations = ArrayList<ContentProviderOperation>()
ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).apply {
val selection = "${ContactsContract.Data.CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ?"
val selectionArgs = arrayOf(contact.id.toString(), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
withSelection(selection, selectionArgs)
withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, contact.firstName)
withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, contact.middleName)
withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, contact.surname)
operations.add(this.build())
}
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
true
} catch (e: Exception) {
activity.showErrorToast(e)
false
}
}
fun deleteContact(contact: Contact) = deleteContacts(arrayListOf(contact)) fun deleteContact(contact: Contact) = deleteContacts(arrayListOf(contact))
fun deleteContacts(contacts: ArrayList<Contact>) { fun deleteContacts(contacts: ArrayList<Contact>) {