Fix widget update race condition

pull/3719/head
Alex Baker 5 months ago
parent 38527aef0a
commit e30c583d5a

@ -8,6 +8,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.tasks.R
import org.tasks.injection.ApplicationScope
import timber.log.Timber
@ -25,18 +26,21 @@ class AppWidgetManager @Inject constructor(
?: intArrayOf()
fun reconfigureWidgets(vararg appWidgetIds: Int) = scope.launch(Dispatchers.IO) {
Timber.d("reconfigureWidgets(${appWidgetIds.joinToString()})")
val intent = Intent(context, TasksWidget::class.java)
intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
intent.putExtra(
AppWidgetManager.EXTRA_APPWIDGET_IDS,
appWidgetIds.takeIf { it.isNotEmpty() } ?: widgetIds)
context.sendBroadcast(intent)
updateWidgets()
}
fun updateWidgets() = scope.launch(Dispatchers.IO) {
Timber.d("Updating widgets")
appWidgetManager?.notifyAppWidgetViewDataChanged(widgetIds, R.id.list_view)
val appWidgetIds = widgetIds
Timber.d("updateWidgets: ${appWidgetIds.joinToString()}")
withContext(Dispatchers.Main) {
appWidgetManager?.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.list_view)
}
}
fun exists(id: Int) = appWidgetManager?.getAppWidgetInfo(id) != null

@ -24,7 +24,7 @@ class RequestPinWidgetReceiver : BroadcastReceiver() {
val widgetPreferences = WidgetPreferences(context, preferences, widgetId)
widgetPreferences.setFilter(filter)
widgetPreferences.setColor(color)
appWidgetManager.reconfigureWidgets()
appWidgetManager.reconfigureWidgets(widgetId)
}
companion object {

Loading…
Cancel
Save