properly remove the widget ID from the database on deletion

This commit is contained in:
tibbi 2018-12-16 20:55:56 +01:00
parent 069d991fac
commit 9e2847fb8f
2 changed files with 12 additions and 4 deletions

View File

@ -30,9 +30,8 @@ class MyWidgetProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
super.onUpdate(context, appWidgetManager, appWidgetIds) super.onUpdate(context, appWidgetManager, appWidgetIds)
Thread { Thread {
val widgets = context.widgetsDB.getWidgets()
val config = context.config val config = context.config
widgets.forEach { context.widgetsDB.getWidgets().forEach {
val views = RemoteViews(context.packageName, R.layout.widget).apply { val views = RemoteViews(context.packageName, R.layout.widget).apply {
setBackgroundColor(R.id.widget_holder, config.widgetBgColor) setBackgroundColor(R.id.widget_holder, config.widgetBgColor)
setVisibleIf(R.id.widget_folder_name, config.showWidgetFolderName) setVisibleIf(R.id.widget_folder_name, config.showWidgetFolderName)
@ -61,4 +60,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

@ -14,6 +14,6 @@ interface WidgetsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(widget: Widget): Long fun insertOrUpdate(widget: Widget): Long
@Query("DELETE FROM widgets WHERE folder_path = :folderPath") @Query("DELETE FROM widgets WHERE widget_id = :widgetId")
fun deleteNoteWidgets(folderPath: String) fun deleteWidgetId(widgetId: Int)
} }