Skip to content

Commit 20565b9

Browse files
Robclaude
andcommitted
refactor(settings): clean up decodeDestinations — deduplicate staleThreshold parse, simplify migration predicate, align in-memory settingsVersion with post-migration state
- Extract staleThresholdMinutes parse+coerce to a local val so it is computed once and reused for both staleThresholdMinutes and the lower bound of missedThresholdMinutes (was called twice with identical args). - Simplify `if (itemSettingsVersion < 1 && stored) false else stored` to the left-to-right equivalent `stored && itemSettingsVersion >= 1`. - Change `settingsVersion = itemSettingsVersion` to `settingsVersion = maxOf(1, itemSettingsVersion)` so the in-memory Destination object reflects its post-migration version (1) rather than the raw on-disk value (0). This keeps the in-memory and on-disk representations consistent and makes future migration logic safe to read from either source. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 4a99aa2 commit 20565b9

1 file changed

Lines changed: 6 additions & 11 deletions

File tree

Common/src/main/java/tk/glucodata/OutboundApiSettings.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,8 @@ object OutboundApiSettings {
586586
val item = array.optJSONObject(index) ?: continue
587587
val preset = normalizePreset(item.optString("preset", PRESET_CUSTOM_JSON))
588588
val itemSettingsVersion = item.optInt("settingsVersion", 0)
589+
val staleThreshold = item.optInt("staleThresholdMinutes", DEFAULT_STALE_THRESHOLD_MINUTES)
590+
.coerceIn(1, 120)
589591
destinations += Destination(
590592
id = item.optString("id").ifBlank { UUID.randomUUID().toString() },
591593
enabled = item.optBoolean("enabled", false),
@@ -625,7 +627,7 @@ object OutboundApiSettings {
625627
// since Telegram doesn't notify on edited messages. Force false on first
626628
// migration. Once settingsVersion is saved as 1+, user's explicit choices
627629
// are preserved.
628-
if (itemSettingsVersion < 1 && stored) false else stored
630+
stored && itemSettingsVersion >= 1
629631
},
630632
refreshWindowMinutes = item.optInt(
631633
"refreshWindowMinutes",
@@ -639,23 +641,16 @@ object OutboundApiSettings {
639641
DEFAULT_SUPPRESS_DELTA_BELOW_MGDL
640642
).coerceIn(0, 100),
641643
staleEnabled = item.optBoolean("staleEnabled", DEFAULT_STALE_ENABLED),
642-
staleThresholdMinutes = item.optInt(
643-
"staleThresholdMinutes",
644-
DEFAULT_STALE_THRESHOLD_MINUTES
645-
).coerceIn(1, 120),
644+
staleThresholdMinutes = staleThreshold,
646645
missedThresholdMinutes = item.optInt(
647646
"missedThresholdMinutes",
648647
DEFAULT_MISSED_THRESHOLD_MINUTES
649-
).coerceIn(
650-
item.optInt("staleThresholdMinutes", DEFAULT_STALE_THRESHOLD_MINUTES)
651-
.coerceIn(1, 120) + 1,
652-
240
653-
),
648+
).coerceIn(staleThreshold + 1, 240),
654649
lastMessageIdByRecipient = decodeLongMap(item.optJSONObject("lastMessageIdByRecipient")),
655650
lastSentAtMsByRecipient = decodeLongMap(item.optJSONObject("lastSentAtMsByRecipient")),
656651
lastSentMgdlByRecipient = decodeIntMap(item.optJSONObject("lastSentMgdlByRecipient")),
657652
lastStaleAtMsByRecipient = decodeLongMap(item.optJSONObject("lastStaleAtMsByRecipient")),
658-
settingsVersion = itemSettingsVersion
653+
settingsVersion = maxOf(1, itemSettingsVersion)
659654
)
660655
}
661656
return destinations.distinctBy { it.id.lowercase(Locale.US) }

0 commit comments

Comments
 (0)