From 2ed8a4e150a84ef0ada487c7757ce10220839403 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 11 Feb 2018 19:21:23 +0100 Subject: [PATCH] create the function for retrieving contacts from the local database --- .../contacts/helpers/DBHelper.kt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) 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 4aedbb14..8c677d66 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/DBHelper.kt @@ -5,7 +5,13 @@ import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import com.google.gson.Gson +import com.google.gson.reflect.TypeToken +import com.simplemobiletools.commons.extensions.getIntValue +import com.simplemobiletools.commons.extensions.getStringValue import com.simplemobiletools.contacts.models.Contact +import com.simplemobiletools.contacts.models.Email +import com.simplemobiletools.contacts.models.Event +import com.simplemobiletools.contacts.models.PhoneNumber class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) { @@ -61,4 +67,35 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont put(COL_STARRED, contact.starred) } } + + fun getContacts(): ArrayList { + val contacts = ArrayList() + val projection = arrayOf(COL_ID, COL_FIRST_NAME, COL_MIDDLE_NAME, COL_SURNAME, COL_PHONE_NUMBERS, COL_EMAILS, COL_EVENTS, COL_STARRED) + val cursor = mDb.query(CONTACTS_TABLE_NAME, projection, null, null, null, null, null) + cursor.use { + while (cursor.moveToNext()) { + val id = cursor.getIntValue(COL_ID) + val firstName = cursor.getStringValue(COL_FIRST_NAME) + val middleName = cursor.getStringValue(COL_MIDDLE_NAME) + val surname = cursor.getStringValue(COL_SURNAME) + + val phoneNumbersJson = cursor.getStringValue(COL_PHONE_NUMBERS) + val phoneNumbersToken = object : TypeToken>() {}.type + val phoneNumbers = Gson().fromJson>(phoneNumbersJson, phoneNumbersToken) ?: ArrayList(1) + + val emailsJson = cursor.getStringValue(COL_EMAILS) + val emailsToken = object : TypeToken>() {}.type + val emails = Gson().fromJson>(emailsJson, emailsToken) ?: ArrayList(1) + + val eventsJson = cursor.getStringValue(COL_EVENTS) + val eventsToken = object : TypeToken>() {}.type + val events = Gson().fromJson>(eventsJson, eventsToken) ?: ArrayList(1) + + val starred = cursor.getIntValue(COL_STARRED) + val contact = Contact(id, firstName, middleName, surname, "", phoneNumbers, emails, events, SMT_PRIVATE, starred, id, "") + contacts.add(contact) + } + } + return contacts + } }