creating a Widget table

This commit is contained in:
tibbi 2018-12-16 13:13:41 +01:00
parent 1bc7616821
commit 18cd305caf
3 changed files with 44 additions and 1 deletions

View file

@ -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`)")
}
}
}
}

View file

@ -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<Widget>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(widget: Widget): Long
@Query("DELETE FROM widgets WHERE folder_path = :folderPath")
fun deleteNoteWidgets(folderPath: String)
}

View file

@ -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)