From 644a6a2e0014c1d0f0a6c1fa0f462f79ee117e9d Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 5 Nov 2018 20:24:49 +0100 Subject: [PATCH] handle converting local contacts to the generic contacts --- .../pro/databases/ContactsDatabase.kt | 5 +- .../contacts/pro/helpers/Constants.kt | 2 +- .../contacts/pro/helpers/ContactsHelper.kt | 4 +- .../contacts/pro/helpers/Converters.kt | 59 +++++++++++++++++++ .../pro/helpers/LocalContactsHelper.kt | 37 ++++++++++++ .../contacts/pro/models/LocalContact.kt | 18 +++--- 6 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Converters.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/LocalContactsHelper.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/databases/ContactsDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/databases/ContactsDatabase.kt index 94b02b27..b14e92f7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/databases/ContactsDatabase.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/databases/ContactsDatabase.kt @@ -4,7 +4,9 @@ import android.content.Context import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase +import androidx.room.TypeConverters import androidx.sqlite.db.SupportSQLiteDatabase +import com.simplemobiletools.contacts.pro.helpers.Converters import com.simplemobiletools.contacts.pro.helpers.getEmptyLocalContact import com.simplemobiletools.contacts.pro.interfaces.ContactsDao import com.simplemobiletools.contacts.pro.models.LocalContact @@ -12,12 +14,13 @@ import com.simplemobiletools.contacts.pro.objects.MyExecutor import java.util.concurrent.Executors @Database(entities = [(LocalContact::class)], version = 1) +@TypeConverters(Converters::class) abstract class ContactsDatabase : RoomDatabase() { abstract fun ContactsDao(): ContactsDao companion object { - private val FIRST_CONTACT_ID = 1000000L + private const val FIRST_CONTACT_ID = 1000000 private var db: ContactsDatabase? = null diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt index ea38a550..b1e4bc2a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Constants.kt @@ -121,4 +121,4 @@ const val TELEGRAM_PACKAGE = "org.telegram.messenger" const val SIGNAL_PACKAGE = "org.thoughtcrime.securesms" const val WHATSAPP_PACKAGE = "com.whatsapp" -fun getEmptyLocalContact() = LocalContact(0, "", "", "", "", "", "", null, "", "", "", false, "", "", "", "", "", "", "") +fun getEmptyLocalContact() = LocalContact(0, "", "", "", "", "", "", null, ArrayList(), ArrayList(), ArrayList(), 0, ArrayList(), "", ArrayList(), "", "", ArrayList(), ArrayList()) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt index 57447ffc..d7aa0615 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt @@ -1213,7 +1213,7 @@ class ContactsHelper(val activity: Activity) { fun insertContact(contact: Contact): Boolean { if (contact.source == SMT_PRIVATE) { - return insertLocalContact(contact) + return LocalContactsHelper(activity).insertContact(contact) } try { @@ -1398,8 +1398,6 @@ class ContactsHelper(val activity: Activity) { } } - private fun insertLocalContact(contact: Contact) = activity.dbHelper.insertContact(contact) - private fun addFullSizePhoto(contactId: Long, fullSizePhotoData: ByteArray) { val baseUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, contactId) val displayPhotoUri = Uri.withAppendedPath(baseUri, ContactsContract.RawContacts.DisplayPhoto.CONTENT_DIRECTORY) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Converters.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Converters.kt new file mode 100644 index 00000000..599c17f4 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/Converters.kt @@ -0,0 +1,59 @@ +package com.simplemobiletools.contacts.pro.helpers + +import androidx.room.TypeConverter +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken +import com.simplemobiletools.contacts.pro.models.* + +class Converters { + private val gson = Gson() + private val stringType = object : TypeToken>() {}.type + private val numberType = object : TypeToken>() {}.type + private val emailType = object : TypeToken>() {}.type + private val addressType = object : TypeToken>() {}.type + private val eventType = object : TypeToken>() {}.type + private val groupType = object : TypeToken>() {}.type + private val imType = object : TypeToken>() {}.type + + @TypeConverter + fun jsonToStringList(value: String) = gson.fromJson>(value, stringType) + + @TypeConverter + fun stringListToJson(list: ArrayList) = gson.toJson(list) + + @TypeConverter + fun jsonToPhoneNumberList(value: String) = gson.fromJson>(value, numberType) + + @TypeConverter + fun phoneNumberListToJson(list: ArrayList) = gson.toJson(list) + + @TypeConverter + fun jsonToEmailList(value: String) = gson.fromJson>(value, emailType) + + @TypeConverter + fun emailListToJson(list: ArrayList) = gson.toJson(list) + + @TypeConverter + fun jsonToAddressList(value: String) = gson.fromJson>(value, addressType) + + @TypeConverter + fun addressListToJson(list: ArrayList
) = gson.toJson(list) + + @TypeConverter + fun jsonToEventList(value: String) = gson.fromJson>(value, eventType) + + @TypeConverter + fun eventListToJson(list: ArrayList) = gson.toJson(list) + + @TypeConverter + fun jsonToGroupList(value: String) = gson.fromJson>(value, groupType) + + @TypeConverter + fun groupListToJson(list: ArrayList) = gson.toJson(list) + + @TypeConverter + fun jsonToIMsList(value: String) = gson.fromJson>(value, imType) + + @TypeConverter + fun IMsListToJson(list: ArrayList) = gson.toJson(list) +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/LocalContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/LocalContactsHelper.kt new file mode 100644 index 00000000..93c0e1f5 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/LocalContactsHelper.kt @@ -0,0 +1,37 @@ +package com.simplemobiletools.contacts.pro.helpers + +import android.app.Activity +import com.simplemobiletools.contacts.pro.extensions.contactsDB +import com.simplemobiletools.contacts.pro.models.Contact +import com.simplemobiletools.contacts.pro.models.LocalContact + +class LocalContactsHelper(val activity: Activity) { + fun insertContact(contact: Contact): Boolean { + val localContact = convertContactToLocalContact(contact) + activity.contactsDB.insertOrUpdate(localContact) + return true + } + + private fun convertContactToLocalContact(contact: Contact): LocalContact { + return getEmptyLocalContact().apply { + id = if (contact.id == 0) null else contact.id + prefix = contact.prefix + firstName = contact.firstName + middleName = contact.middleName + surname = contact.surname + suffix = contact.suffix + nickname = contact.nickname + phoneNumbers = contact.phoneNumbers + emails = contact.emails + events = contact.events + starred = contact.starred + addresses = contact.addresses + notes = contact.notes + groups = contact.groups + company = contact.organization.company + jobPosition = contact.organization.jobPosition + websites = contact.websites + ims = contact.IMs + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/LocalContact.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/LocalContact.kt index 4cd51fa5..52b2a47e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/LocalContact.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/models/LocalContact.kt @@ -7,7 +7,7 @@ import androidx.room.PrimaryKey @Entity(tableName = "contacts", indices = [(Index(value = ["id"], unique = true))]) data class LocalContact( - @PrimaryKey(autoGenerate = true) var id: Long?, + @PrimaryKey(autoGenerate = true) var id: Int?, @ColumnInfo(name = "prefix") var prefix: String, @ColumnInfo(name = "first_name") var firstName: String, @ColumnInfo(name = "middle_name") var middleName: String, @@ -15,17 +15,17 @@ data class LocalContact( @ColumnInfo(name = "suffix") var suffix: String, @ColumnInfo(name = "nickname") var nickname: String, @ColumnInfo(name = "photo", typeAffinity = ColumnInfo.BLOB) var photo: ByteArray?, - @ColumnInfo(name = "phone_numbers") var phoneNumbers: String, - @ColumnInfo(name = "emails") var emails: String, - @ColumnInfo(name = "events") var events: String, - @ColumnInfo(name = "starred") var starred: Boolean, - @ColumnInfo(name = "addresses") var addresses: String, + @ColumnInfo(name = "phone_numbers") var phoneNumbers: ArrayList, + @ColumnInfo(name = "emails") var emails: ArrayList, + @ColumnInfo(name = "events") var events: ArrayList, + @ColumnInfo(name = "starred") var starred: Int, + @ColumnInfo(name = "addresses") var addresses: ArrayList
, @ColumnInfo(name = "notes") var notes: String, - @ColumnInfo(name = "groups") var groups: String, + @ColumnInfo(name = "groups") var groups: ArrayList, @ColumnInfo(name = "company") var company: String, @ColumnInfo(name = "job_position") var jobPosition: String, - @ColumnInfo(name = "websites") var websites: String, - @ColumnInfo(name = "ims") var ims: String) { + @ColumnInfo(name = "websites") var websites: ArrayList, + @ColumnInfo(name = "ims") var ims: ArrayList) { companion object { private const val serialVersionUID = -655314977575622L