mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-19 20:38:53 +01:00
Update contacts during migration
This commit is contained in:
@@ -18,15 +18,22 @@
|
|||||||
*/
|
*/
|
||||||
package com.moez.QKSMS.migration
|
package com.moez.QKSMS.migration
|
||||||
|
|
||||||
|
import com.moez.QKSMS.extensions.map
|
||||||
|
import com.moez.QKSMS.mapper.CursorToContactImpl
|
||||||
import io.realm.DynamicRealm
|
import io.realm.DynamicRealm
|
||||||
|
import io.realm.DynamicRealmObject
|
||||||
import io.realm.FieldAttribute
|
import io.realm.FieldAttribute
|
||||||
|
import io.realm.RealmList
|
||||||
import io.realm.RealmMigration
|
import io.realm.RealmMigration
|
||||||
import io.realm.Sort
|
import io.realm.Sort
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
class QkRealmMigration : RealmMigration {
|
class QkRealmMigration @Inject constructor(
|
||||||
|
private val cursorToContact: CursorToContactImpl
|
||||||
|
) : RealmMigration {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val SCHEMA_VERSION: Long = 9
|
const val SchemaVersion: Long = 9
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
|
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
|
||||||
@@ -119,20 +126,49 @@ class QkRealmMigration : RealmMigration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (version == 8L) {
|
if (version == 8L) {
|
||||||
|
// Delete this data since we'll need to repopulate it with its new primaryKey
|
||||||
|
realm.delete("PhoneNumber")
|
||||||
|
|
||||||
realm.schema.create("ContactGroup")
|
realm.schema.create("ContactGroup")
|
||||||
.addField("id", Long::class.java, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
|
.addField("id", Long::class.java, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
|
||||||
.addField("title", String::class.java, FieldAttribute.REQUIRED)
|
.addField("title", String::class.java, FieldAttribute.REQUIRED)
|
||||||
.addRealmListField("contacts", realm.schema.get("Contact"))
|
.addRealmListField("contacts", realm.schema.get("Contact"))
|
||||||
|
|
||||||
realm.schema.get("Contact")
|
|
||||||
?.addField("starred", Boolean::class.java, FieldAttribute.REQUIRED)
|
|
||||||
?.addField("photoUri", String::class.java)
|
|
||||||
|
|
||||||
realm.schema.get("PhoneNumber")
|
realm.schema.get("PhoneNumber")
|
||||||
?.addField("id", Long::class.java, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
|
?.addField("id", Long::class.java, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED)
|
||||||
?.addField("accountType", String::class.java, FieldAttribute.REQUIRED)
|
?.addField("accountType", String::class.java, FieldAttribute.REQUIRED)
|
||||||
?.addField("isDefault", Boolean::class.java, FieldAttribute.REQUIRED)
|
?.addField("isDefault", Boolean::class.java, FieldAttribute.REQUIRED)
|
||||||
|
|
||||||
|
val phoneNumbers = cursorToContact.getContactsCursor()
|
||||||
|
?.map(cursorToContact::map)
|
||||||
|
?.distinctBy { contact -> contact.numbers.firstOrNull()?.id } // Each row has only one number
|
||||||
|
?.groupBy { contact -> contact.lookupKey }
|
||||||
|
?: mapOf()
|
||||||
|
|
||||||
|
realm.schema.get("Contact")
|
||||||
|
?.addField("starred", Boolean::class.java, FieldAttribute.REQUIRED)
|
||||||
|
?.addField("photoUri", String::class.java)
|
||||||
|
?.transform { realmContact ->
|
||||||
|
val numbers = RealmList<DynamicRealmObject>()
|
||||||
|
phoneNumbers[realmContact.get("lookupKey")]
|
||||||
|
?.flatMap { contact -> contact.numbers }
|
||||||
|
?.map { number ->
|
||||||
|
realm.createObject("PhoneNumber", number.id).apply {
|
||||||
|
setString("accountType", number.accountType)
|
||||||
|
setString("address", number.address)
|
||||||
|
setString("type", number.type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?.let(numbers::addAll)
|
||||||
|
|
||||||
|
val photoUri = phoneNumbers[realmContact.get("lookupKey")]
|
||||||
|
?.firstOrNull { number -> number.photoUri != null }
|
||||||
|
?.photoUri
|
||||||
|
|
||||||
|
realmContact.setList("numbers", numbers)
|
||||||
|
realmContact.setString("photoUri", photoUri)
|
||||||
|
}
|
||||||
|
|
||||||
version++
|
version++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -61,20 +61,21 @@ class QKApplication : Application(), HasActivityInjector, HasBroadcastReceiverIn
|
|||||||
@Inject lateinit var dispatchingServiceInjector: DispatchingAndroidInjector<Service>
|
@Inject lateinit var dispatchingServiceInjector: DispatchingAndroidInjector<Service>
|
||||||
@Inject lateinit var fileLoggingTree: FileLoggingTree
|
@Inject lateinit var fileLoggingTree: FileLoggingTree
|
||||||
@Inject lateinit var nightModeManager: NightModeManager
|
@Inject lateinit var nightModeManager: NightModeManager
|
||||||
|
@Inject lateinit var realmMigration: QkRealmMigration
|
||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
|
||||||
|
AppComponentManager.init(this)
|
||||||
|
appComponent.inject(this)
|
||||||
|
|
||||||
Realm.init(this)
|
Realm.init(this)
|
||||||
Realm.setDefaultConfiguration(RealmConfiguration.Builder()
|
Realm.setDefaultConfiguration(RealmConfiguration.Builder()
|
||||||
.compactOnLaunch()
|
.compactOnLaunch()
|
||||||
.migration(QkRealmMigration())
|
.migration(realmMigration)
|
||||||
.schemaVersion(QkRealmMigration.SCHEMA_VERSION)
|
.schemaVersion(QkRealmMigration.SchemaVersion)
|
||||||
.build())
|
.build())
|
||||||
|
|
||||||
AppComponentManager.init(this)
|
|
||||||
appComponent.inject(this)
|
|
||||||
|
|
||||||
packageManager.getInstallerPackageName(packageName)?.let { installer ->
|
packageManager.getInstallerPackageName(packageName)?.let { installer ->
|
||||||
analyticsManager.setUserProperty("Installer", installer)
|
analyticsManager.setUserProperty("Installer", installer)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user