diff --git a/app/build.gradle b/app/build.gradle index 619df6f7..04150cd0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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" } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt index 01c694b9..bc734d66 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt @@ -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) } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt index 7dc263ab..60348d67 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/adapters/ConversationsAdapter.kt @@ -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 + val conversationsToRemove = conversations.filter { selectedKeys.contains(it.system_id) } as ArrayList 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 + private fun getSelectedItems() = conversations.filter { selectedKeys.contains(it.system_id) } as ArrayList 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 diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/databases/MessagesDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/databases/MessagesDatabase.kt new file mode 100644 index 00000000..a514dd12 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/databases/MessagesDatabase.kt @@ -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!! + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index a28dd4ba..0ec27820 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -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 { val uri = Sms.CONTENT_URI val projection = arrayOf( @@ -215,7 +221,7 @@ fun Context.getConversations(): ArrayList { 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) } diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/interfaces/ConversationsDao.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/interfaces/ConversationsDao.kt new file mode 100644 index 00000000..2546ff38 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/interfaces/ConversationsDao.kt @@ -0,0 +1,6 @@ +package com.simplemobiletools.smsmessenger.interfaces + +import androidx.room.Dao + +@Dao +interface ConversationsDao diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Conversation.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Conversation.kt index f5cf9da3..06872802 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Conversation.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/Conversation.kt @@ -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 +)