implementing contact inserting

This commit is contained in:
tibbi 2018-02-11 19:07:14 +01:00
parent a631a53db6
commit 6305785aa9
3 changed files with 27 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.contacts
import android.app.Application
import com.facebook.stetho.Stetho
import com.simplemobiletools.commons.extensions.checkUseEnglish
import com.squareup.leakcanary.LeakCanary

View File

@ -22,6 +22,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_SURNAME
import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.extensions.config
import com.simplemobiletools.contacts.extensions.dbHelper
import com.simplemobiletools.contacts.models.*
import java.io.ByteArrayOutputStream
import java.util.*
@ -559,9 +560,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
}
private fun insertLocalContact(contact: Contact): Boolean {
return true
}
private fun insertLocalContact(contact: Contact) = activity.dbHelper.insert(contact)
private fun addFullSizePhoto(contactId: Long, fullSizePhotoData: ByteArray) {
val baseUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, contactId)

View File

@ -1,11 +1,15 @@
package com.simplemobiletools.contacts.helpers
import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import com.google.gson.Gson
import com.simplemobiletools.contacts.models.Contact
class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) {
private val MAIN_TABLE_NAME = "contacts"
private val CONTACTS_TABLE_NAME = "contacts"
private val COL_ID = "id"
private val COL_FIRST_NAME = "first_name"
private val COL_MIDDLE_NAME = "middle_name"
@ -32,11 +36,29 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $MAIN_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_FIRST_NAME TEXT, $COL_MIDDLE_NAME TEXT, " +
db.execSQL("CREATE TABLE $CONTACTS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_FIRST_NAME TEXT, $COL_MIDDLE_NAME TEXT, " +
"$COL_SURNAME TEXT, $COL_PHOTO BLOB, $COL_PHONE_NUMBERS TEXT, $COL_EMAILS TEXT, $COL_EVENTS TEXT, $COL_STARRED INTEGER)")
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
}
fun insert(contact: Contact): Boolean {
val contactValues = fillContactValues(contact)
val id = mDb.insert(CONTACTS_TABLE_NAME, null, contactValues).toInt()
return id != -1
}
private fun fillContactValues(contact: Contact): ContentValues {
return ContentValues().apply {
put(COL_FIRST_NAME, contact.firstName)
put(COL_MIDDLE_NAME, contact.middleName)
put(COL_SURNAME, contact.surname)
put(COL_PHONE_NUMBERS, Gson().toJson(contact.phoneNumbers))
put(COL_EMAILS, Gson().toJson(contact.emails))
put(COL_EVENTS, Gson().toJson(contact.events))
put(COL_STARRED, contact.starred)
}
}
}