handle retrieving local contacts from the database
This commit is contained in:
parent
644a6a2e00
commit
f7c1f0ab10
|
@ -561,9 +561,7 @@ class EditContactActivity : ContactActivity() {
|
|||
private fun setupNewContact() {
|
||||
supportActionBar?.title = resources.getString(R.string.new_contact)
|
||||
originalContactSource = if (hasContactPermissions()) config.lastUsedContactSource else SMT_PRIVATE
|
||||
val organization = Organization("", "")
|
||||
contact = Contact(0, "", "", "", "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), originalContactSource, 0, 0, "",
|
||||
null, "", ArrayList(), organization, ArrayList(), ArrayList(), ArrayList())
|
||||
contact = getEmptyContact()
|
||||
contact_source.text = getPublicContactSource(contact!!.source)
|
||||
}
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ import com.simplemobiletools.contacts.pro.databases.ContactsDatabase
|
|||
import com.simplemobiletools.contacts.pro.helpers.*
|
||||
import com.simplemobiletools.contacts.pro.interfaces.ContactsDao
|
||||
import com.simplemobiletools.contacts.pro.models.Contact
|
||||
import com.simplemobiletools.contacts.pro.models.Organization
|
||||
import java.io.File
|
||||
|
||||
val Context.config: Config get() = Config.newInstance(applicationContext)
|
||||
|
@ -27,6 +28,13 @@ val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext)
|
|||
|
||||
val Context.contactsDB: ContactsDao get() = ContactsDatabase.getInstance(applicationContext).ContactsDao()
|
||||
|
||||
fun Context.getEmptyContact(): Contact {
|
||||
val originalContactSource = if (hasContactPermissions()) config.lastUsedContactSource else SMT_PRIVATE
|
||||
val organization = Organization("", "")
|
||||
return Contact(0, "", "", "", "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), originalContactSource, 0, 0, "",
|
||||
null, "", ArrayList(), organization, ArrayList(), ArrayList(), ArrayList())
|
||||
}
|
||||
|
||||
fun Context.viewContact(contact: Contact) {
|
||||
Intent(applicationContext, ViewContactActivity::class.java).apply {
|
||||
putExtra(CONTACT_ID, contact.id)
|
||||
|
|
|
@ -37,7 +37,7 @@ class ContactsHelper(val activity: Activity) {
|
|||
getDeviceContacts(contacts)
|
||||
|
||||
if (displayContactSources.contains(SMT_PRIVATE)) {
|
||||
activity.dbHelper.getContacts(activity).forEach {
|
||||
LocalContactsHelper(activity).getAllContacts().forEach {
|
||||
contacts.put(it.id, it)
|
||||
}
|
||||
}
|
||||
|
@ -754,7 +754,7 @@ class ContactsHelper(val activity: Activity) {
|
|||
if (id == 0) {
|
||||
return null
|
||||
} else if (isLocalPrivate) {
|
||||
return activity.dbHelper.getContactWithId(activity, id)
|
||||
return LocalContactsHelper(activity).getContactWithId(id)
|
||||
}
|
||||
|
||||
val selection = "${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.Data.RAW_CONTACT_ID} = ?"
|
||||
|
|
|
@ -1,17 +1,59 @@
|
|||
package com.simplemobiletools.contacts.pro.helpers
|
||||
|
||||
import android.app.Activity
|
||||
import com.simplemobiletools.contacts.pro.extensions.applyRegexFiltering
|
||||
import com.simplemobiletools.contacts.pro.extensions.config
|
||||
import com.simplemobiletools.contacts.pro.extensions.contactsDB
|
||||
import com.simplemobiletools.contacts.pro.extensions.getEmptyContact
|
||||
import com.simplemobiletools.contacts.pro.models.Contact
|
||||
import com.simplemobiletools.contacts.pro.models.LocalContact
|
||||
import com.simplemobiletools.contacts.pro.models.Organization
|
||||
import com.simplemobiletools.contacts.pro.models.PhoneNumber
|
||||
|
||||
class LocalContactsHelper(val activity: Activity) {
|
||||
fun getAllContacts() = activity.contactsDB.getContacts().map { convertLocalContactToContact(it) }.toMutableList() as ArrayList<Contact>
|
||||
|
||||
fun getContactWithId(id: Int) = convertLocalContactToContact(activity.contactsDB.getContactWithId(id))
|
||||
|
||||
fun insertContact(contact: Contact): Boolean {
|
||||
val localContact = convertContactToLocalContact(contact)
|
||||
activity.contactsDB.insertOrUpdate(localContact)
|
||||
return true
|
||||
}
|
||||
|
||||
private fun convertLocalContactToContact(localContact: LocalContact): Contact {
|
||||
val filterDuplicates = activity.config.filterDuplicates
|
||||
val filteredPhoneNumbers = ArrayList<PhoneNumber>()
|
||||
if (filterDuplicates) {
|
||||
localContact.phoneNumbers.mapTo(filteredPhoneNumbers) { PhoneNumber(it.value.applyRegexFiltering(), 0, "") }
|
||||
}
|
||||
|
||||
return activity.getEmptyContact().apply {
|
||||
id = localContact.id!!
|
||||
prefix = localContact.prefix
|
||||
firstName = localContact.firstName
|
||||
middleName = localContact.middleName
|
||||
surname = localContact.surname
|
||||
suffix = localContact.suffix
|
||||
nickname = localContact.nickname
|
||||
photoUri = ""
|
||||
phoneNumbers = localContact.phoneNumbers
|
||||
emails = localContact.emails
|
||||
addresses = localContact.addresses
|
||||
events = localContact.events
|
||||
source = SMT_PRIVATE
|
||||
starred = localContact.starred
|
||||
contactId = localContact.id!!
|
||||
thumbnailUri = ""
|
||||
notes = localContact.notes
|
||||
groups = localContact.groups
|
||||
organization = Organization(localContact.company, localContact.jobPosition)
|
||||
websites = localContact.websites
|
||||
cleanPhoneNumbers = filteredPhoneNumbers
|
||||
IMs = localContact.IMs
|
||||
}
|
||||
}
|
||||
|
||||
private fun convertContactToLocalContact(contact: Contact): LocalContact {
|
||||
return getEmptyLocalContact().apply {
|
||||
id = if (contact.id == 0) null else contact.id
|
||||
|
@ -31,7 +73,7 @@ class LocalContactsHelper(val activity: Activity) {
|
|||
company = contact.organization.company
|
||||
jobPosition = contact.organization.jobPosition
|
||||
websites = contact.websites
|
||||
ims = contact.IMs
|
||||
IMs = contact.IMs
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ interface ContactsDao {
|
|||
fun getContacts(): List<LocalContact>
|
||||
|
||||
@Query("SELECT * FROM contacts WHERE id = :id")
|
||||
fun getContactWithId(id: Long): LocalContact
|
||||
fun getContactWithId(id: Int): LocalContact
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insertOrUpdate(contact: LocalContact)
|
||||
|
|
|
@ -7,9 +7,9 @@ import com.simplemobiletools.commons.helpers.SORT_BY_MIDDLE_NAME
|
|||
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
|
||||
import com.simplemobiletools.contacts.pro.extensions.applyRegexFiltering
|
||||
|
||||
data class Contact(val id: Int, var prefix: String, var firstName: String, var middleName: String, var surname: String, var suffix: String, var nickname: String,
|
||||
data class Contact(var id: Int, var prefix: String, var firstName: String, var middleName: String, var surname: String, var suffix: String, var nickname: String,
|
||||
var photoUri: String, var phoneNumbers: ArrayList<PhoneNumber>, var emails: ArrayList<Email>, var addresses: ArrayList<Address>,
|
||||
var events: ArrayList<Event>, var source: String, var starred: Int, val contactId: Int, val thumbnailUri: String, var photo: Bitmap?, var notes: String,
|
||||
var events: ArrayList<Event>, var source: String, var starred: Int, var contactId: Int, var thumbnailUri: String, var photo: Bitmap?, var notes: String,
|
||||
var groups: ArrayList<Group>, var organization: Organization, var websites: ArrayList<String>, var cleanPhoneNumbers: ArrayList<PhoneNumber>,
|
||||
var IMs: ArrayList<IM>) :
|
||||
Comparable<Contact> {
|
||||
|
|
|
@ -25,9 +25,4 @@ data class LocalContact(
|
|||
@ColumnInfo(name = "company") var company: String,
|
||||
@ColumnInfo(name = "job_position") var jobPosition: String,
|
||||
@ColumnInfo(name = "websites") var websites: ArrayList<String>,
|
||||
@ColumnInfo(name = "ims") var ims: ArrayList<IM>) {
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -655314977575622L
|
||||
}
|
||||
}
|
||||
@ColumnInfo(name = "ims") var IMs: ArrayList<IM>)
|
||||
|
|
Loading…
Reference in New Issue