Update only widgets containing the habit that was modified

pull/428/head
Alinson S. Xavier 8 years ago
parent 778a7eb6bc
commit 2654521647

@ -23,6 +23,7 @@ import android.appwidget.*
import android.content.* import android.content.*
import org.isoron.androidbase.* import org.isoron.androidbase.*
import org.isoron.uhabits.core.commands.* import org.isoron.uhabits.core.commands.*
import org.isoron.uhabits.core.preferences.*
import org.isoron.uhabits.core.tasks.* import org.isoron.uhabits.core.tasks.*
import javax.inject.* import javax.inject.*
@ -34,11 +35,12 @@ class WidgetUpdater
@Inject constructor( @Inject constructor(
@AppContext private val context: Context, @AppContext private val context: Context,
private val commandRunner: CommandRunner, private val commandRunner: CommandRunner,
private val taskRunner: TaskRunner private val taskRunner: TaskRunner,
private val widgetPrefs: WidgetPreferences
) : CommandRunner.Listener { ) : CommandRunner.Listener {
override fun onCommandExecuted(command: Command, refreshKey: Long?) { override fun onCommandExecuted(command: Command, refreshKey: Long?) {
updateWidgets() updateWidgets(refreshKey)
} }
/** /**
@ -58,22 +60,34 @@ class WidgetUpdater
commandRunner.removeListener(this) commandRunner.removeListener(this)
} }
fun updateWidgets() { fun updateWidgets(modifiedHabitId: Long?) {
taskRunner.execute { taskRunner.execute {
updateWidgets(CheckmarkWidgetProvider::class.java) updateWidgets(modifiedHabitId, CheckmarkWidgetProvider::class.java)
updateWidgets(HistoryWidgetProvider::class.java) updateWidgets(modifiedHabitId, HistoryWidgetProvider::class.java)
updateWidgets(ScoreWidgetProvider::class.java) updateWidgets(modifiedHabitId, ScoreWidgetProvider::class.java)
updateWidgets(StreakWidgetProvider::class.java) updateWidgets(modifiedHabitId, StreakWidgetProvider::class.java)
updateWidgets(FrequencyWidgetProvider::class.java) updateWidgets(modifiedHabitId, FrequencyWidgetProvider::class.java)
} }
} }
fun updateWidgets(providerClass: Class<*>) { private fun updateWidgets(modifiedHabitId: Long?, providerClass: Class<*>) {
val ids = AppWidgetManager.getInstance(context).getAppWidgetIds( val widgetIds = AppWidgetManager.getInstance(context).getAppWidgetIds(
ComponentName(context, providerClass)) ComponentName(context, providerClass))
val modifiedWidgetIds = when (modifiedHabitId) {
null -> widgetIds.toList()
else -> widgetIds.filter { w ->
widgetPrefs.getHabitIdsFromWidgetId(w).contains(modifiedHabitId)
}
}
context.sendBroadcast(Intent(context, providerClass).apply { context.sendBroadcast(Intent(context, providerClass).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids) putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, modifiedWidgetIds.toIntArray())
}) })
} }
fun updateWidgets() {
updateWidgets(null)
}
} }

Loading…
Cancel
Save