properly clean up removed widgets from the database

This commit is contained in:
tibbi 2018-12-16 21:01:08 +01:00
parent af199908d9
commit 42c14576be
3 changed files with 13 additions and 1 deletions

View File

@ -48,4 +48,13 @@ class MyWidgetProvider : AppWidgetProvider() {
} }
}.start() }.start()
} }
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
super.onDeleted(context, appWidgetIds)
Thread {
appWidgetIds.forEach {
context.widgetsDB.deleteWidgetId(it)
}
}.start()
}
} }

View File

@ -16,4 +16,7 @@ interface WidgetsDao {
@Query("DELETE FROM widgets WHERE note_id = :noteId") @Query("DELETE FROM widgets WHERE note_id = :noteId")
fun deleteNoteWidgets(noteId: Long) fun deleteNoteWidgets(noteId: Long)
@Query("DELETE FROM widgets WHERE widget_id = :widgetId")
fun deleteWidgetId(widgetId: Int)
} }

View File

@ -7,6 +7,6 @@ import androidx.room.PrimaryKey
@Entity(tableName = "widgets", indices = [(Index(value = ["widget_id"], unique = true))]) @Entity(tableName = "widgets", indices = [(Index(value = ["widget_id"], unique = true))])
data class Widget( data class Widget(
@PrimaryKey(autoGenerate = true) var id: Int?, @PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "widget_id") var widgetId: Int, @ColumnInfo(name = "widget_id") var widgetId: Int,
@ColumnInfo(name = "note_id") var noteId: Long) @ColumnInfo(name = "note_id") var noteId: Long)