forked from upstream-mirrors/quik
Build phone number picker
This commit is contained in:
@@ -34,6 +34,7 @@ class CursorToContactImpl @Inject constructor(
|
||||
companion object {
|
||||
val URI = Phone.CONTENT_URI
|
||||
val PROJECTION = arrayOf(
|
||||
Phone._ID,
|
||||
Phone.LOOKUP_KEY,
|
||||
Phone.ACCOUNT_TYPE_AND_DATA_SET,
|
||||
Phone.NUMBER,
|
||||
@@ -44,20 +45,22 @@ class CursorToContactImpl @Inject constructor(
|
||||
Phone.CONTACT_LAST_UPDATED_TIMESTAMP
|
||||
)
|
||||
|
||||
const val COLUMN_LOOKUP_KEY = 0
|
||||
const val COLUMN_ACCOUNT_TYPE = 1
|
||||
const val COLUMN_NUMBER = 2
|
||||
const val COLUMN_TYPE = 3
|
||||
const val COLUMN_LABEL = 4
|
||||
const val COLUMN_DISPLAY_NAME = 5
|
||||
const val COLUMN_STARRED = 6
|
||||
const val CONTACT_LAST_UPDATED = 7
|
||||
const val COLUMN_ID = 0
|
||||
const val COLUMN_LOOKUP_KEY = 1
|
||||
const val COLUMN_ACCOUNT_TYPE = 2
|
||||
const val COLUMN_NUMBER = 3
|
||||
const val COLUMN_TYPE = 4
|
||||
const val COLUMN_LABEL = 5
|
||||
const val COLUMN_DISPLAY_NAME = 6
|
||||
const val COLUMN_STARRED = 7
|
||||
const val CONTACT_LAST_UPDATED = 8
|
||||
}
|
||||
|
||||
override fun map(from: Cursor) = Contact().apply {
|
||||
lookupKey = from.getString(COLUMN_LOOKUP_KEY)
|
||||
name = from.getString(COLUMN_DISPLAY_NAME) ?: ""
|
||||
numbers.add(PhoneNumber(
|
||||
id = from.getLong(COLUMN_ID),
|
||||
accountType = from.getString(COLUMN_ACCOUNT_TYPE),
|
||||
address = from.getString(COLUMN_NUMBER) ?: "",
|
||||
type = Phone.getTypeLabel(context.resources, from.getInt(COLUMN_TYPE),
|
||||
|
||||
@@ -128,7 +128,9 @@ class QkRealmMigration : RealmMigration {
|
||||
?.addField("starred", Boolean::class.java, FieldAttribute.REQUIRED)
|
||||
|
||||
realm.schema.get("PhoneNumber")
|
||||
?.addField("id", Long::class.java, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
|
||||
?.addField("accountType", String::class.java, FieldAttribute.REQUIRED)
|
||||
?.addField("isDefault", Boolean::class.java, FieldAttribute.REQUIRED)
|
||||
|
||||
version++
|
||||
}
|
||||
|
||||
@@ -137,4 +137,20 @@ class ContactRepositoryImpl @Inject constructor(
|
||||
.observeOn(Schedulers.io())
|
||||
}
|
||||
|
||||
override fun setDefaultPhoneNumber(lookupKey: String, phoneNumberId: Long) {
|
||||
Realm.getDefaultInstance().use { realm ->
|
||||
realm.refresh()
|
||||
val contact = realm.where(Contact::class.java)
|
||||
.equalTo("lookupKey", lookupKey)
|
||||
.findFirst()
|
||||
?: return
|
||||
|
||||
realm.executeTransaction {
|
||||
contact.numbers.forEach { number ->
|
||||
number.isDefault = number.id == phoneNumberId
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
realm.delete(Contact::class.java)
|
||||
realm.delete(ContactGroup::class.java)
|
||||
|
||||
contacts = realm.copyToRealm(contacts)
|
||||
contacts = realm.copyToRealmOrUpdate(contacts)
|
||||
realm.insertOrUpdate(getContactGroups(contacts))
|
||||
|
||||
// Update all the recipients with the new contacts
|
||||
@@ -286,6 +286,13 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
}
|
||||
|
||||
private fun getContacts(): List<Contact> {
|
||||
val defaultNumberIds = Realm.getDefaultInstance().use { realm ->
|
||||
realm.where(PhoneNumber::class.java)
|
||||
.equalTo("isDefault", true)
|
||||
.findAll()
|
||||
.map { number -> number.id }
|
||||
}
|
||||
|
||||
return cursorToContact.getContactsCursor()
|
||||
?.map { cursor -> cursorToContact.map(cursor) }
|
||||
?.groupBy { contact -> contact.lookupKey }
|
||||
@@ -297,10 +304,12 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
.flatMap { it.numbers }
|
||||
.sortedBy { it.accountType }
|
||||
.forEach { number ->
|
||||
number.isDefault = defaultNumberIds.any { id -> id == number.id }
|
||||
val duplicate = uniqueNumbers.any { other ->
|
||||
number.accountType != other.accountType
|
||||
&& phoneNumberUtils.compare(number.address, other.address)
|
||||
}
|
||||
|
||||
if (!duplicate) {
|
||||
uniqueNumbers += number
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user