From 18cd305caf31eb3d33d330f43b1eab41c6637e28 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 16 Dec 2018 13:13:41 +0100 Subject: [PATCH] creating a Widget table --- .../gallery/pro/databases/GalleryDatabase.kt | 14 +++++++++++++- .../gallery/pro/interfaces/WidgetsDao.kt | 19 +++++++++++++++++++ .../gallery/pro/models/Widget.kt | 12 ++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/WidgetsDao.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Widget.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/databases/GalleryDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/databases/GalleryDatabase.kt index d6412cda5..7f0257f9b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/databases/GalleryDatabase.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/databases/GalleryDatabase.kt @@ -8,16 +8,20 @@ import androidx.room.migration.Migration import androidx.sqlite.db.SupportSQLiteDatabase import com.simplemobiletools.gallery.pro.interfaces.DirectoryDao import com.simplemobiletools.gallery.pro.interfaces.MediumDao +import com.simplemobiletools.gallery.pro.interfaces.WidgetsDao import com.simplemobiletools.gallery.pro.models.Directory import com.simplemobiletools.gallery.pro.models.Medium +import com.simplemobiletools.gallery.pro.models.Widget -@Database(entities = [Directory::class, Medium::class], version = 5) +@Database(entities = [Directory::class, Medium::class, Widget::class], version = 6) abstract class GalleryDatabase : RoomDatabase() { abstract fun DirectoryDao(): DirectoryDao abstract fun MediumDao(): MediumDao + abstract fun WidgetsDao(): WidgetsDao + companion object { private var db: GalleryDatabase? = null @@ -27,6 +31,7 @@ abstract class GalleryDatabase : RoomDatabase() { if (db == null) { db = Room.databaseBuilder(context.applicationContext, GalleryDatabase::class.java, "gallery.db") .addMigrations(MIGRATION_4_5) + .addMigrations(MIGRATION_5_6) .build() } } @@ -43,5 +48,12 @@ abstract class GalleryDatabase : RoomDatabase() { database.execSQL("ALTER TABLE media ADD COLUMN video_duration INTEGER default 0 NOT NULL") } } + + private val MIGRATION_5_6 = object : Migration(5, 6) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("CREATE TABLE IF NOT EXISTS `widgets` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `widget_id` INTEGER NOT NULL, `folder_path` TEXT NOT NULL)") + database.execSQL("CREATE UNIQUE INDEX `index_widgets_widget_id` ON `widgets` (`widget_id`)") + } + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/WidgetsDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/WidgetsDao.kt new file mode 100644 index 000000000..72409ca35 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/WidgetsDao.kt @@ -0,0 +1,19 @@ +package com.simplemobiletools.gallery.pro.interfaces + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import com.simplemobiletools.gallery.pro.models.Widget + +@Dao +interface WidgetsDao { + @Query("SELECT * FROM widgets") + fun getWidgets(): List + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertOrUpdate(widget: Widget): Long + + @Query("DELETE FROM widgets WHERE folder_path = :folderPath") + fun deleteNoteWidgets(folderPath: String) +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Widget.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Widget.kt new file mode 100644 index 000000000..2ef525334 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Widget.kt @@ -0,0 +1,12 @@ +package com.simplemobiletools.gallery.pro.models + +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.Index +import androidx.room.PrimaryKey + +@Entity(tableName = "widgets", indices = [(Index(value = ["widget_id"], unique = true))]) +data class Widget( + @PrimaryKey(autoGenerate = true) var id: Int?, + @ColumnInfo(name = "widget_id") var widgetId: Int, + @ColumnInfo(name = "folder_path") var folderPath: String)