Merge pull request #1043 from hiqua/965_illegal_state_exception

Handle target widget type in stacked widget
feature/sync^2^2^2
Alinson S. Xavier 4 years ago committed by GitHub
commit e756a639ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -109,7 +109,7 @@ abstract class BaseWidgetProvider : AppWidgetProvider() {
} }
protected fun getHabitsFromWidgetId(widgetId: Int): List<Habit> { protected fun getHabitsFromWidgetId(widgetId: Int): List<Habit> {
val selectedIds = widgetPrefs.getHabitIdsFromWidgetId(widgetId)!! val selectedIds = widgetPrefs.getHabitIdsFromWidgetId(widgetId)
val selectedHabits = ArrayList<Habit>(selectedIds.size) val selectedHabits = ArrayList<Habit>(selectedIds.size)
for (id in selectedIds) { for (id in selectedIds) {
val h = habits.getById(id) ?: throw HabitNotFoundException() val h = habits.getById(id) ?: throw HabitNotFoundException()

@ -53,7 +53,7 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
AppWidgetManager.INVALID_APPWIDGET_ID AppWidgetManager.INVALID_APPWIDGET_ID
) )
private val habitIds: LongArray private val habitIds: LongArray
private val widgetType: StackWidgetType? private val widgetType: StackWidgetType
private var remoteViews = ArrayList<RemoteViews>() private var remoteViews = ArrayList<RemoteViews>()
override fun onCreate() {} override fun onCreate() {}
override fun onDestroy() {} override fun onDestroy() {}
@ -86,7 +86,7 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
override fun getViewAt(position: Int): RemoteViews? { override fun getViewAt(position: Int): RemoteViews? {
Log.i("StackRemoteViewsFactory", "getViewAt $position") Log.i("StackRemoteViewsFactory", "getViewAt $position")
return if (position < 0 || position > remoteViews.size) null else remoteViews[position] return if (0 <= position && position < remoteViews.size) remoteViews[position] else null
} }
private fun constructWidget( private fun constructWidget(
@ -105,7 +105,7 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
StackWidgetType.SCORE -> ScoreWidget(context, widgetId, habit, true) StackWidgetType.SCORE -> ScoreWidget(context, widgetId, habit, true)
StackWidgetType.HISTORY -> HistoryWidget(context, widgetId, habit, true) StackWidgetType.HISTORY -> HistoryWidget(context, widgetId, habit, true)
StackWidgetType.STREAKS -> StreakWidget(context, widgetId, habit, true) StackWidgetType.STREAKS -> StreakWidget(context, widgetId, habit, true)
else -> throw IllegalStateException() StackWidgetType.TARGET -> TargetWidget(context, widgetId, habit, true)
} }
} }
@ -157,6 +157,7 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
if (widgetTypeValue < 0) throw RuntimeException("invalid widget type") if (widgetTypeValue < 0) throw RuntimeException("invalid widget type")
if (habitIdsStr == null) throw RuntimeException("habitIdsStr is null") if (habitIdsStr == null) throw RuntimeException("habitIdsStr is null")
widgetType = StackWidgetType.getWidgetTypeFromValue(widgetTypeValue) widgetType = StackWidgetType.getWidgetTypeFromValue(widgetTypeValue)
?: throw RuntimeException("unknown widget type value: $widgetTypeValue")
habitIds = splitLongs(habitIdsStr) habitIds = splitLongs(habitIdsStr)
} }
} }

@ -27,14 +27,14 @@ enum class StackWidgetType(val value: Int) {
companion object { companion object {
fun getWidgetTypeFromValue(value: Int): StackWidgetType? { fun getWidgetTypeFromValue(value: Int): StackWidgetType? {
return when { return when (value) {
CHECKMARK.value == value -> CHECKMARK CHECKMARK.value -> CHECKMARK
FREQUENCY.value == value -> FREQUENCY FREQUENCY.value -> FREQUENCY
SCORE.value == value -> SCORE SCORE.value -> SCORE
HISTORY.value == value -> HISTORY HISTORY.value -> HISTORY
STREAKS.value == value -> STREAKS STREAKS.value -> STREAKS
TARGET.value == value -> TARGET TARGET.value -> TARGET
else -> throw IllegalStateException() else -> null
} }
} }

@ -95,7 +95,7 @@ class WidgetUpdater
val modifiedWidgetIds = when (modifiedHabitId) { val modifiedWidgetIds = when (modifiedHabitId) {
null -> widgetIds.toList() null -> widgetIds.toList()
else -> widgetIds.filter { w -> else -> widgetIds.filter { w ->
widgetPrefs.getHabitIdsFromWidgetId(w)!!.contains(modifiedHabitId) widgetPrefs.getHabitIdsFromWidgetId(w).contains(modifiedHabitId)
} }
} }

@ -258,7 +258,7 @@ open class Preferences(private val storage: Storage) {
putString(key, joinLongs(values)) putString(key, joinLongs(values))
} }
fun getLongArray(key: String, defValue: LongArray): LongArray? { fun getLongArray(key: String, defValue: LongArray): LongArray {
val string = getString(key, "") val string = getString(key, "")
return if (string.isEmpty()) defValue else splitLongs( return if (string.isEmpty()) defValue else splitLongs(
string string

@ -27,19 +27,18 @@ class WidgetPreferences @Inject constructor(private val storage: Preferences.Sto
storage.putLongArray(getHabitIdKey(widgetId), habitIds) storage.putLongArray(getHabitIdKey(widgetId), habitIds)
} }
fun getHabitIdsFromWidgetId(widgetId: Int): LongArray? { fun getHabitIdsFromWidgetId(widgetId: Int): LongArray {
var habitIds: LongArray?
val habitIdKey = getHabitIdKey(widgetId) val habitIdKey = getHabitIdKey(widgetId)
try { return try {
habitIds = storage.getLongArray(habitIdKey, longArrayOf(-1)) storage.getLongArray(habitIdKey, longArrayOf())
} catch (e: ClassCastException) { } catch (e: ClassCastException) {
// Up to Loop 1.7.11, this preference was not an array, but a single // Up to Loop 1.7.11, this preference was not an array, but a single
// long. Trying to read the old preference causes a cast exception. // long. Trying to read the old preference causes a cast exception.
habitIds = LongArray(1) when (val habitId = storage.getLong(habitIdKey, -1)) {
habitIds[0] = storage.getLong(habitIdKey, -1) -1L -> longArrayOf()
storage.putLongArray(habitIdKey, habitIds) else -> longArrayOf(habitId)
}
} }
return habitIds
} }
fun removeWidget(id: Int) { fun removeWidget(id: Int) {

Loading…
Cancel
Save