mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Merge pull request #1043 from hiqua/965_illegal_state_exception
Handle target widget type in stacked widget
This commit is contained in:
@@ -109,7 +109,7 @@ abstract class BaseWidgetProvider : AppWidgetProvider() {
|
||||
}
|
||||
|
||||
protected fun getHabitsFromWidgetId(widgetId: Int): List<Habit> {
|
||||
val selectedIds = widgetPrefs.getHabitIdsFromWidgetId(widgetId)!!
|
||||
val selectedIds = widgetPrefs.getHabitIdsFromWidgetId(widgetId)
|
||||
val selectedHabits = ArrayList<Habit>(selectedIds.size)
|
||||
for (id in selectedIds) {
|
||||
val h = habits.getById(id) ?: throw HabitNotFoundException()
|
||||
|
||||
@@ -53,7 +53,7 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
|
||||
AppWidgetManager.INVALID_APPWIDGET_ID
|
||||
)
|
||||
private val habitIds: LongArray
|
||||
private val widgetType: StackWidgetType?
|
||||
private val widgetType: StackWidgetType
|
||||
private var remoteViews = ArrayList<RemoteViews>()
|
||||
override fun onCreate() {}
|
||||
override fun onDestroy() {}
|
||||
@@ -86,7 +86,7 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
|
||||
|
||||
override fun getViewAt(position: Int): RemoteViews? {
|
||||
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(
|
||||
@@ -105,7 +105,7 @@ internal class StackRemoteViewsFactory(private val context: Context, intent: Int
|
||||
StackWidgetType.SCORE -> ScoreWidget(context, widgetId, habit, true)
|
||||
StackWidgetType.HISTORY -> HistoryWidget(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 (habitIdsStr == null) throw RuntimeException("habitIdsStr is null")
|
||||
widgetType = StackWidgetType.getWidgetTypeFromValue(widgetTypeValue)
|
||||
?: throw RuntimeException("unknown widget type value: $widgetTypeValue")
|
||||
habitIds = splitLongs(habitIdsStr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,14 +27,14 @@ enum class StackWidgetType(val value: Int) {
|
||||
|
||||
companion object {
|
||||
fun getWidgetTypeFromValue(value: Int): StackWidgetType? {
|
||||
return when {
|
||||
CHECKMARK.value == value -> CHECKMARK
|
||||
FREQUENCY.value == value -> FREQUENCY
|
||||
SCORE.value == value -> SCORE
|
||||
HISTORY.value == value -> HISTORY
|
||||
STREAKS.value == value -> STREAKS
|
||||
TARGET.value == value -> TARGET
|
||||
else -> throw IllegalStateException()
|
||||
return when (value) {
|
||||
CHECKMARK.value -> CHECKMARK
|
||||
FREQUENCY.value -> FREQUENCY
|
||||
SCORE.value -> SCORE
|
||||
HISTORY.value -> HISTORY
|
||||
STREAKS.value -> STREAKS
|
||||
TARGET.value -> TARGET
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ class WidgetUpdater
|
||||
val modifiedWidgetIds = when (modifiedHabitId) {
|
||||
null -> widgetIds.toList()
|
||||
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))
|
||||
}
|
||||
|
||||
fun getLongArray(key: String, defValue: LongArray): LongArray? {
|
||||
fun getLongArray(key: String, defValue: LongArray): LongArray {
|
||||
val string = getString(key, "")
|
||||
return if (string.isEmpty()) defValue else splitLongs(
|
||||
string
|
||||
|
||||
@@ -27,19 +27,18 @@ class WidgetPreferences @Inject constructor(private val storage: Preferences.Sto
|
||||
storage.putLongArray(getHabitIdKey(widgetId), habitIds)
|
||||
}
|
||||
|
||||
fun getHabitIdsFromWidgetId(widgetId: Int): LongArray? {
|
||||
var habitIds: LongArray?
|
||||
fun getHabitIdsFromWidgetId(widgetId: Int): LongArray {
|
||||
val habitIdKey = getHabitIdKey(widgetId)
|
||||
try {
|
||||
habitIds = storage.getLongArray(habitIdKey, longArrayOf(-1))
|
||||
return try {
|
||||
storage.getLongArray(habitIdKey, longArrayOf())
|
||||
} catch (e: ClassCastException) {
|
||||
// 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.
|
||||
habitIds = LongArray(1)
|
||||
habitIds[0] = storage.getLong(habitIdKey, -1)
|
||||
storage.putLongArray(habitIdKey, habitIds)
|
||||
when (val habitId = storage.getLong(habitIdKey, -1)) {
|
||||
-1L -> longArrayOf()
|
||||
else -> longArrayOf(habitId)
|
||||
}
|
||||
}
|
||||
return habitIds
|
||||
}
|
||||
|
||||
fun removeWidget(id: Int) {
|
||||
|
||||
Reference in New Issue
Block a user