creating a room DB for storing conversations

This commit is contained in:
tibbi 2020-05-30 18:04:34 +02:00
parent 4e89e8b7ac
commit f638fa06f2
7 changed files with 72 additions and 11 deletions

View File

@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
@ -56,8 +57,12 @@ android {
}
dependencies {
implementation 'com.simplemobiletools:commons:5.28.19'
implementation 'com.simplemobiletools:commons:5.28.24'
implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'com.klinkerapps:android-smsmms:5.2.6'
implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a'
kapt "androidx.room:room-compiler:2.2.5"
implementation "androidx.room:room-runtime:2.2.5"
annotationProcessor "androidx.room:room-compiler:2.2.5"
}

View File

@ -168,7 +168,7 @@ class MainActivity : SimpleActivity() {
ConversationsAdapter(this, conversations, conversations_list, conversations_fastscroller) {
Intent(this, ThreadActivity::class.java).apply {
putExtra(THREAD_ID, (it as Conversation).id)
putExtra(THREAD_ID, (it as Conversation).system_id)
putExtra(THREAD_TITLE, it.title)
startActivity(this)
}

View File

@ -59,9 +59,9 @@ class ConversationsAdapter(activity: SimpleActivity, var conversations: ArrayLis
override fun getIsItemSelectable(position: Int) = true
override fun getItemSelectionKey(position: Int) = conversations.getOrNull(position)?.id
override fun getItemSelectionKey(position: Int) = conversations.getOrNull(position)?.system_id
override fun getItemKeyPosition(key: Int) = conversations.indexOfFirst { it.id == key }
override fun getItemKeyPosition(key: Int) = conversations.indexOfFirst { it.system_id == key }
override fun onActionModeCreated() {}
@ -129,10 +129,10 @@ class ConversationsAdapter(activity: SimpleActivity, var conversations: ArrayLis
return
}
val conversationsToRemove = conversations.filter { selectedKeys.contains(it.id) } as ArrayList<Conversation>
val conversationsToRemove = conversations.filter { selectedKeys.contains(it.system_id) } as ArrayList<Conversation>
val positions = getSelectedItemPositions()
conversationsToRemove.forEach {
activity.deleteConversation(it.id)
activity.deleteConversation(it.system_id)
}
conversations.removeAll(conversationsToRemove)
@ -164,7 +164,7 @@ class ConversationsAdapter(activity: SimpleActivity, var conversations: ArrayLis
}
}
private fun getSelectedItems() = conversations.filter { selectedKeys.contains(it.id) } as ArrayList<Conversation>
private fun getSelectedItems() = conversations.filter { selectedKeys.contains(it.system_id) } as ArrayList<Conversation>
override fun onViewRecycled(holder: ViewHolder) {
super.onViewRecycled(holder)
@ -175,7 +175,7 @@ class ConversationsAdapter(activity: SimpleActivity, var conversations: ArrayLis
private fun setupView(view: View, conversation: Conversation) {
view.apply {
conversation_frame.isSelected = selectedKeys.contains(conversation.id)
conversation_frame.isSelected = selectedKeys.contains(conversation.system_id)
conversation_address.text = conversation.title
conversation_body_short.text = conversation.snippet

View File

@ -0,0 +1,30 @@
package com.simplemobiletools.smsmessenger.databases
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.simplemobiletools.smsmessenger.interfaces.ConversationsDao
import com.simplemobiletools.smsmessenger.models.Conversation
@Database(entities = [(Conversation::class)], version = 1)
abstract class MessagesDatabase : RoomDatabase() {
abstract fun ConversationsDao(): ConversationsDao
companion object {
private var db: MessagesDatabase? = null
fun getInstance(context: Context): MessagesDatabase {
if (db == null) {
synchronized(MessagesDatabase::class) {
if (db == null) {
db = Room.databaseBuilder(context.applicationContext, MessagesDatabase::class.java, "conversations.db")
.build()
}
}
}
return db!!
}
}
}

View File

@ -22,7 +22,9 @@ import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.SimpleContact
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.ThreadActivity
import com.simplemobiletools.smsmessenger.databases.MessagesDatabase
import com.simplemobiletools.smsmessenger.helpers.*
import com.simplemobiletools.smsmessenger.interfaces.ConversationsDao
import com.simplemobiletools.smsmessenger.models.*
import com.simplemobiletools.smsmessenger.receivers.MarkAsReadReceiver
import java.util.*
@ -30,6 +32,10 @@ import kotlin.collections.ArrayList
val Context.config: Config get() = Config.newInstance(applicationContext)
fun Context.getMessagessDB() = MessagesDatabase.getInstance(this)
val Context.conversationsDB: ConversationsDao get() = getMessagessDB().ConversationsDao()
fun Context.getMessages(threadId: Int): ArrayList<Message> {
val uri = Sms.CONTENT_URI
val projection = arrayOf(
@ -215,7 +221,7 @@ fun Context.getConversations(): ArrayList<Conversation> {
val photoUri = if (phoneNumbers.size == 1) simpleContactHelper.getPhotoUriFromPhoneNumber(phoneNumbers.first()) else ""
val isGroupConversation = phoneNumbers.size > 1
val read = cursor.getIntValue(Threads.READ) == 1
val conversation = Conversation(id, snippet, date.toInt(), read, title, photoUri, isGroupConversation, phoneNumbers.first())
val conversation = Conversation(0, id, snippet, date.toInt(), read, title, photoUri, isGroupConversation, phoneNumbers.first())
conversations.add(conversation)
}

View File

@ -0,0 +1,6 @@
package com.simplemobiletools.smsmessenger.interfaces
import androidx.room.Dao
@Dao
interface ConversationsDao

View File

@ -1,5 +1,19 @@
package com.simplemobiletools.smsmessenger.models
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
@Entity(tableName = "conversations", indices = [(Index(value = ["id"], unique = true))])
data class Conversation(
val id: Int, val snippet: String, val date: Int, val read: Boolean, var title: String, var photoUri: String,
val isGroupConversation: Boolean, val phoneNumber: String)
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "system_id") var system_id: Int,
@ColumnInfo(name = "snippet") var snippet: String,
@ColumnInfo(name = "date") var date: Int,
@ColumnInfo(name = "read") var read: Boolean,
@ColumnInfo(name = "title") var title: String,
@ColumnInfo(name = "photo_uri") var photoUri: String,
@ColumnInfo(name = "is_group_conversation") var isGroupConversation: Boolean,
@ColumnInfo(name = "phone_number") var phoneNumber: String
)