mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Use empty array instead of sentinel value -1 as habitId
This commit is contained in:
@@ -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()
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user