From 30d21625bf4b1523b6e56aaf2d1240a0cc1f38e2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 21 Mar 2018 17:27:40 +0100 Subject: [PATCH] create some db helper functions with working with groups --- .../contacts/helpers/ContactsHelper.kt | 6 ++-- .../contacts/helpers/DBHelper.kt | 33 +++++++++++++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt index 61c1e2d0..7bab4b00 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/ContactsHelper.kt @@ -395,7 +395,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) { fun createNewGroup(title: String, accountName: String, accountType: String): Group? { if (accountType == SMT_PRIVATE) { - return activity.dbHelper.createGroup(title) + return activity.dbHelper.insertGroup(title) } val operations = ArrayList() @@ -598,7 +598,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) { fun updateContact(contact: Contact, photoUpdateStatus: Int): Boolean { return if (contact.source == SMT_PRIVATE) { - activity.dbHelper.update(contact) + activity.dbHelper.updateContact(contact) } else try { activity.toast(R.string.updating) val operations = ArrayList() @@ -942,7 +942,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) { } } - private fun insertLocalContact(contact: Contact) = activity.dbHelper.insert(contact) + 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) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt index 545378d3..af80c3ef 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt @@ -34,7 +34,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont private val COL_GROUPS = "groups" private val GROUPS_TABLE_NAME = "groups" - private val COL_NAME = "name" + private val COL_TITLE = "title" private val FIRST_CONTACT_ID = 1000000 private val FIRST_GROUP_ID = 10000 @@ -78,19 +78,19 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont } private fun createGroupsTable(db: SQLiteDatabase) { - db.execSQL("CREATE TABLE $GROUPS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_NAME TEXT)") + db.execSQL("CREATE TABLE $GROUPS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_TITLE TEXT)") // start autoincrement ID from FIRST_GROUP_ID to avoid conflicts db.execSQL("REPLACE INTO sqlite_sequence (name, seq) VALUES ('$GROUPS_TABLE_NAME', $FIRST_GROUP_ID)") } - fun insert(contact: Contact): Boolean { + fun insertContact(contact: Contact): Boolean { val contactValues = fillContactValues(contact) val id = mDb.insert(CONTACTS_TABLE_NAME, null, contactValues).toInt() return id != -1 } - fun update(contact: Contact): Boolean { + fun updateContact(contact: Contact): Boolean { val contactValues = fillContactValues(contact) val selection = "$COL_ID = ?" val selectionArgs = arrayOf(contact.id.toString()) @@ -145,8 +145,29 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont mDb.update(CONTACTS_TABLE_NAME, contactValues, selection, null) } - fun createGroup(name: String): Group? { - return null + fun insertGroup(group: Group): Boolean { + val contactValues = fillGroupValues(group) + val id = mDb.insert(GROUPS_TABLE_NAME, null, contactValues).toInt() + return id != -1 + } + + fun updateGroup(group: Group): Boolean { + val contactValues = fillGroupValues(group) + val selection = "$COL_ID = ?" + val selectionArgs = arrayOf(group.id.toString()) + return mDb.update(GROUPS_TABLE_NAME, contactValues, selection, selectionArgs) == 1 + } + + fun deleteGroups(ids: Array) { + val args = TextUtils.join(", ", ids) + val selection = "$GROUPS_TABLE_NAME.$COL_ID IN ($args)" + mDb.delete(GROUPS_TABLE_NAME, selection, null) + } + + private fun fillGroupValues(group: Group): ContentValues { + return ContentValues().apply { + put(COL_TITLE, group.title) + } } fun getContacts(selection: String? = null, selectionArgs: Array? = null): ArrayList {