Skip to content

Commit

Permalink
Use empty array instead of sentinel value -1 as habitId
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqua committed Aug 3, 2021
1 parent 67b55a4 commit 91ff5f7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 91ff5f7

Please sign in to comment.