From ab3922be22892dfc243341b15141925d33abce43 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 17 Sep 2022 23:43:30 +0200 Subject: [PATCH] adding some initial Room database related code --- app/build.gradle | 4 +++ .../launcher/databases/AppsDatabase.kt | 27 +++++++++++++++++++ .../launcher/models/AppLauncher.kt | 16 ++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt diff --git a/app/build.gradle b/app/build.gradle index 3e26d4c3..bc8dc17f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -44,4 +44,8 @@ android { dependencies { implementation 'com.github.SimpleMobileTools:Simple-Commons:a0121a8d50' + + kapt "androidx.room:room-compiler:2.4.3" + implementation "androidx.room:room-runtime:2.4.3" + annotationProcessor "androidx.room:room-compiler:2.4.3" } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt new file mode 100644 index 00000000..7b89046c --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/databases/AppsDatabase.kt @@ -0,0 +1,27 @@ +package com.simplemobiletools.launcher.databases + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import com.simplemobiletools.launcher.models.AppLauncher + +@Database(entities = [AppLauncher::class], version = 1) +abstract class AppsDatabase : RoomDatabase() { + + companion object { + private var db: AppsDatabase? = null + + fun getInstance(context: Context): AppsDatabase { + if (db == null) { + synchronized(AppsDatabase::class) { + if (db == null) { + db = Room.databaseBuilder(context.applicationContext, AppsDatabase::class.java, "apps.db") + .build() + } + } + } + return db!! + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppLauncher.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppLauncher.kt index bcef7b1d..7b08d24e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppLauncher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/AppLauncher.kt @@ -1,16 +1,30 @@ package com.simplemobiletools.launcher.models import android.graphics.drawable.Drawable +import androidx.room.* import com.simplemobiletools.commons.helpers.SORT_BY_TITLE import com.simplemobiletools.commons.helpers.SORT_DESCENDING -data class AppLauncher(val id: Int, var title: String, val packageName: String, var order: Int, val drawable: Drawable? = null) : Comparable { +@Entity(tableName = "apps", indices = [(Index(value = ["package_name"], unique = true))]) +data class AppLauncher( + @PrimaryKey(autoGenerate = true) var id: Long?, + @ColumnInfo(name = "title") var title: String, + @ColumnInfo(name = "package_name") var packageName: String, + @ColumnInfo(name = "order") var order: Int, + + @Ignore var drawable: Drawable? +) : Comparable { + + constructor() : this(null, "", "", 0, null) + companion object { var sorting = 0 } override fun equals(other: Any?) = packageName.equals((other as AppLauncher).packageName, true) + override fun hashCode() = super.hashCode() + fun getBubbleText() = title override fun compareTo(other: AppLauncher): Int {