Use empty array instead of sentinel value -1 as habitId

This commit is contained in:
Quentin Hibon
2021-08-01 19:32:36 +02:00
parent 67b55a4ecf
commit 91ff5f7a0c
4 changed files with 10 additions and 11 deletions

View File

@@ -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

View File

@@ -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) {