Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions LoopFollow/LiveActivity/LiveActivityManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ final class LiveActivityManager {
let provider = StorageCurrentGlucoseStateProvider()
guard let snapshot = GlucoseSnapshotBuilder.build(from: provider) else { return }

LAAppGroupSettings.setThresholds(
lowMgdl: Storage.shared.lowLine.value,
highMgdl: Storage.shared.highLine.value,
)
writeThresholds()
GlucoseSnapshotStore.shared.save(snapshot)

seq += 1
Expand Down Expand Up @@ -624,10 +621,7 @@ final class LiveActivityManager {

let provider = StorageCurrentGlucoseStateProvider()
if let snapshot = GlucoseSnapshotBuilder.build(from: provider) {
LAAppGroupSettings.setThresholds(
lowMgdl: Storage.shared.lowLine.value,
highMgdl: Storage.shared.highLine.value,
)
writeThresholds()
LAAppGroupSettings.setDisplayName(
Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as? String ?? "LoopFollow",
show: Storage.shared.showDisplayName.value
Expand Down Expand Up @@ -904,6 +898,12 @@ final class LiveActivityManager {
}
}

/// Publishes the range for the selected time-in-range mode, matching the chart and BG header.
private func writeThresholds() {
let thresholds = UnitSettingsStore.shared.effectiveThresholds()
LAAppGroupSettings.setThresholds(lowMgdl: thresholds.low, highMgdl: thresholds.high)
}

private func performRefresh(reason: String) {
let provider = StorageCurrentGlucoseStateProvider()
guard let snapshot = GlucoseSnapshotBuilder.build(from: provider) else {
Expand All @@ -928,12 +928,10 @@ final class LiveActivityManager {
let forceRefreshNeeded = timeSinceLastUpdate >= 5 * 60
// Capture dedup result BEFORE saving so the store comparison is valid.
let snapshotUnchanged = GlucoseSnapshotStore.shared.load() == snapshot
let thresholdsChanged = LAAppGroupSettings.thresholdsMgdl() != UnitSettingsStore.shared.effectiveThresholds()

// Store + Watch: always update, independent of LA state.
LAAppGroupSettings.setThresholds(
lowMgdl: Storage.shared.lowLine.value,
highMgdl: Storage.shared.highLine.value,
)
writeThresholds()
GlucoseSnapshotStore.shared.save(snapshot)
// WatchConnectivityManager.shared.send(snapshot: snapshot)

Expand All @@ -946,7 +944,7 @@ final class LiveActivityManager {
LogManager.shared.log(category: .general, message: "[LA] refresh: LA update skipped — dismissedByUser=true reason=\(reason)")
return
}
guard !snapshotUnchanged || forceRefreshNeeded else { return }
guard !snapshotUnchanged || thresholdsChanged || forceRefreshNeeded else { return }
guard ActivityAuthorizationInfo().areActivitiesEnabled else {
LogManager.shared.log(category: .general, message: "[LA] refresh: LA update skipped — areActivitiesEnabled=false reason=\(reason)")
return
Expand Down
1 change: 1 addition & 0 deletions LoopFollow/ViewControllers/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ class MainViewController: UIViewController, UNUserNotificationCenterDelegate {
.sink { [weak self] _ in
self?.updateBGGraphSettings()
self?.updateBGGraph()
LiveActivityManager.shared.refreshFromCurrentState(reason: "range mode changed")
}
.store(in: &cancellables)

Expand Down
12 changes: 6 additions & 6 deletions LoopFollowLAExtension/LoopFollowLiveActivity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,12 @@ private enum LAColors {
let low = t.low
let high = t.high

if mgdl < low {
let raw = 0.48 + (0.85 - 0.48) * ((low - mgdl) / (low - 54.0))
if mgdl <= low {
let raw = 0.48 + (0.85 - 0.48) * ((low - mgdl) / max(low - 54.0, 1))
let opacity = min(max(raw, 0.48), 0.85)
return Color(uiColor: UIColor.systemRed).opacity(opacity)
} else if mgdl > high {
let raw = 0.44 + (0.85 - 0.44) * ((mgdl - high) / (324.0 - high))
} else if mgdl >= high {
let raw = 0.44 + (0.85 - 0.44) * ((mgdl - high) / max(324.0 - high, 1))
let opacity = min(max(raw, 0.44), 0.85)
return Color(uiColor: UIColor.systemOrange).opacity(opacity)
} else {
Expand All @@ -798,9 +798,9 @@ private enum LAColors {
let low = t.low
let high = t.high

if mgdl < low {
if mgdl <= low {
return Color(uiColor: UIColor.systemRed)
} else if mgdl > high {
} else if mgdl >= high {
return Color(uiColor: UIColor.systemOrange)
} else {
return Color(uiColor: UIColor.systemGreen)
Expand Down
Loading