|
| 1 | +// LoopFollow |
| 2 | +// FutureCarbsCondition.swift |
| 3 | + |
| 4 | +import Foundation |
| 5 | + |
| 6 | +/// Fires once when a future-dated carb entry's scheduled time arrives. |
| 7 | +/// |
| 8 | +/// **How it works:** |
| 9 | +/// 1. Each alarm tick scans `recentCarbs` for entries whose `date` is in the future. |
| 10 | +/// New ones are added to a persistent "pending" list regardless of lookahead distance, |
| 11 | +/// capturing the moment they were first observed (`observedAt`). |
| 12 | +/// 2. When a pending entry's `carbDate` passes (i.e. `carbDate <= now`), verify the |
| 13 | +/// carb still exists in `recentCarbs` **and** that the original distance |
| 14 | +/// (`carbDate − observedAt`) was within the max lookahead window. If both hold, |
| 15 | +/// fire the alarm. Otherwise silently remove the entry. |
| 16 | +/// 3. Stale entries (observed > 2 hours ago) whose carb no longer exists in |
| 17 | +/// `recentCarbs` are cleaned up automatically. |
| 18 | +struct FutureCarbsCondition: AlarmCondition { |
| 19 | + static let type: AlarmType = .futureCarbs |
| 20 | + init() {} |
| 21 | + |
| 22 | + func evaluate(alarm: Alarm, data: AlarmData, now: Date) -> Bool { |
| 23 | + // ──────────────────────────────── |
| 24 | + // 0. Pull settings |
| 25 | + // ──────────────────────────────── |
| 26 | + let maxLookaheadMin = alarm.threshold ?? 45 // max lookahead in minutes |
| 27 | + let minGrams = alarm.delta ?? 5 // ignore carbs below this |
| 28 | + |
| 29 | + let nowTI = now.timeIntervalSince1970 |
| 30 | + let maxLookaheadSec = maxLookaheadMin * 60 |
| 31 | + |
| 32 | + var pending = Storage.shared.pendingFutureCarbs.value |
| 33 | + let tolerance: TimeInterval = 5 // seconds, for matching carb entries |
| 34 | + |
| 35 | + // ──────────────────────────────── |
| 36 | + // 1. Scan for new future carbs |
| 37 | + // ──────────────────────────────── |
| 38 | + for carb in data.recentCarbs { |
| 39 | + let carbTI = carb.date.timeIntervalSince1970 |
| 40 | + |
| 41 | + // Must be in the future and meet the minimum grams threshold. |
| 42 | + // We track ALL future carbs (not just those within the lookahead |
| 43 | + // window) so that carbs originally outside the window cannot |
| 44 | + // drift in later with a fresh observedAt. |
| 45 | + guard carbTI > nowTI, |
| 46 | + carb.grams >= minGrams |
| 47 | + else { continue } |
| 48 | + |
| 49 | + // Already tracked? |
| 50 | + let alreadyTracked = pending.contains { entry in |
| 51 | + abs(entry.carbDate - carbTI) < tolerance && entry.grams == carb.grams |
| 52 | + } |
| 53 | + if !alreadyTracked { |
| 54 | + pending.append(PendingFutureCarb( |
| 55 | + carbDate: carbTI, |
| 56 | + grams: carb.grams, |
| 57 | + observedAt: nowTI |
| 58 | + )) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // ──────────────────────────────── |
| 63 | + // 2. Check if any pending entry is due |
| 64 | + // ──────────────────────────────── |
| 65 | + var fired = false |
| 66 | + |
| 67 | + pending.removeAll { entry in |
| 68 | + let stillExists = data.recentCarbs.contains { carb in |
| 69 | + abs(carb.date.timeIntervalSince1970 - entry.carbDate) < tolerance |
| 70 | + && carb.grams == entry.grams |
| 71 | + } |
| 72 | + |
| 73 | + // Cleanup stale entries (observed > 2 hours ago) only if |
| 74 | + // the carb no longer exists — prevents eviction and |
| 75 | + // re-observation with a fresh observedAt. |
| 76 | + if nowTI - entry.observedAt > 7200, !stillExists { |
| 77 | + return true |
| 78 | + } |
| 79 | + |
| 80 | + // Not yet due |
| 81 | + guard entry.carbDate <= nowTI else { return false } |
| 82 | + |
| 83 | + // Carb was deleted — remove silently |
| 84 | + if !stillExists { return true } |
| 85 | + |
| 86 | + // Carb was originally outside the lookahead window — remove without firing |
| 87 | + if entry.carbDate - entry.observedAt > maxLookaheadSec { return true } |
| 88 | + |
| 89 | + // Fire (one per tick) |
| 90 | + if !fired { |
| 91 | + fired = true |
| 92 | + return true |
| 93 | + } |
| 94 | + |
| 95 | + return false |
| 96 | + } |
| 97 | + |
| 98 | + // ──────────────────────────────── |
| 99 | + // 3. Persist and return |
| 100 | + // ──────────────────────────────── |
| 101 | + Storage.shared.pendingFutureCarbs.value = pending |
| 102 | + return fired |
| 103 | + } |
| 104 | +} |
0 commit comments