From 61900c12e1d9e92e032d9375a946dabf6641185a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Thu, 12 Nov 2026 10:55:15 +0100 Subject: [PATCH 1/2] Rename "Schedule for later" to "Adjust meal time" The meal time setting in the Trio remote Meal screen works for both past and future times, so the toggle is now called "Adjust meal time" under a "Meal Time" heading, and the bolus note is reworded to match. --- LoopFollow/Remote/TRC/MealView.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/LoopFollow/Remote/TRC/MealView.swift b/LoopFollow/Remote/TRC/MealView.swift index 9d9ed0e7e..c7c8c7e5d 100644 --- a/LoopFollow/Remote/TRC/MealView.swift +++ b/LoopFollow/Remote/TRC/MealView.swift @@ -127,8 +127,8 @@ struct MealView: View { } } - Section(header: Text("Schedule")) { - Toggle("Schedule for later", isOn: $isScheduling) + Section(header: Text("Meal Time")) { + Toggle("Adjust meal time", isOn: $isScheduling) if isScheduling { DatePicker( "Select Time", @@ -141,7 +141,7 @@ struct MealView: View { .datePickerStyle(CompactDatePickerStyle()) if bolusAmount.doubleValue(for: .internationalUnit()) > 0 { - Text("Note: The meal will be scheduled, but the bolus is enacted immediately.") + Text("Note: The meal uses the adjusted time, but the bolus is enacted immediately.") } } } From d6d9ba15c256e3befe8768849b2fd1033f28d143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Bj=C3=B6rkert?= Date: Sun, 22 Nov 2026 14:17:08 +0100 Subject: [PATCH 2/2] Date an adjusted meal time to its occurrence closest to now and show the date in the confirmation when it is not today --- LoopFollow/Remote/TRC/MealView.swift | 42 ++++++++++++++++++------ Tests/MealTimeTests.swift | 48 ++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 Tests/MealTimeTests.swift diff --git a/LoopFollow/Remote/TRC/MealView.swift b/LoopFollow/Remote/TRC/MealView.swift index c7c8c7e5d..42656b8cc 100644 --- a/LoopFollow/Remote/TRC/MealView.swift +++ b/LoopFollow/Remote/TRC/MealView.swift @@ -211,9 +211,14 @@ struct MealView: View { var message = "Are you sure you want to send the meal data" if let selectedTime = selectedTime { + let mealDate = MealTime.nearestDate(to: selectedTime, now: Date()) let timeFormatter = DateFormatter() timeFormatter.timeStyle = .short - let timeString = timeFormatter.string(from: selectedTime) + if !Calendar.current.isDateInToday(mealDate) { + timeFormatter.dateStyle = .short + timeFormatter.doesRelativeDateFormatting = true + } + let timeString = timeFormatter.string(from: mealDate) message += " for \(timeString)?" } else { message += " now?" @@ -303,14 +308,7 @@ struct MealView: View { var scheduledDate: Date? = nil if isScheduling, let selectedTime = selectedTime { - let calendar = Calendar.current - let now = Date() - let selectedDateComponents = calendar.dateComponents([.hour, .minute], from: selectedTime) - let currentSecond = calendar.component(.second, from: now) - scheduledDate = calendar.date(bySettingHour: selectedDateComponents.hour ?? 0, - minute: selectedDateComponents.minute ?? 0, - second: currentSecond, - of: now) ?? now + scheduledDate = MealTime.nearestDate(to: selectedTime, now: Date()) } pushNotificationManager.sendMealPushNotification( @@ -385,3 +383,29 @@ struct MealView: View { showAlert = true } } + +enum MealTime { + /// The picker gives only an hour and minute. The meal time is the occurrence of + /// that time closest to now, at most 12 hours before or after, so 23:50 picked + /// at 00:10 is yesterday evening. The seconds come from now. + static func nearestDate(to selectedTime: Date, now: Date, calendar: Calendar = .current) -> Date { + let components = calendar.dateComponents([.hour, .minute], from: selectedTime) + let second = calendar.component(.second, from: now) + guard let today = calendar.date(bySettingHour: components.hour ?? 0, + minute: components.minute ?? 0, + second: second, + of: now) + else { + return now + } + + let offset = today.timeIntervalSince(now) + if offset > 12 * 3600 { + return calendar.date(byAdding: .day, value: -1, to: today) ?? today + } + if offset < -12 * 3600 { + return calendar.date(byAdding: .day, value: 1, to: today) ?? today + } + return today + } +} diff --git a/Tests/MealTimeTests.swift b/Tests/MealTimeTests.swift new file mode 100644 index 000000000..bec889cce --- /dev/null +++ b/Tests/MealTimeTests.swift @@ -0,0 +1,48 @@ +// LoopFollow +// MealTimeTests.swift + +import Foundation +@testable import LoopFollow +import Testing + +struct MealTimeTests { + private var calendar: Calendar { + var calendar = Calendar(identifier: .gregorian) + calendar.timeZone = TimeZone(identifier: "Europe/Stockholm")! + return calendar + } + + private func date(_ day: Int, _ hour: Int, _ minute: Int, _ second: Int = 0) -> Date { + calendar.date(from: DateComponents(year: 2026, month: 11, day: day, hour: hour, minute: minute, second: second))! + } + + @Test("a late-evening time picked just after midnight is yesterday") + func lateEveningAfterMidnight() { + let resolved = MealTime.nearestDate(to: date(22, 23, 50), now: date(22, 0, 10), calendar: calendar) + #expect(resolved == date(21, 23, 50)) + } + + @Test("a time just after midnight picked late in the evening is tomorrow") + func earlyMorningBeforeMidnight() { + let resolved = MealTime.nearestDate(to: date(22, 0, 10), now: date(22, 23, 50), calendar: calendar) + #expect(resolved == date(23, 0, 10)) + } + + @Test("an hour ahead stays today") + func hourAhead() { + let resolved = MealTime.nearestDate(to: date(22, 13, 0), now: date(22, 12, 0), calendar: calendar) + #expect(resolved == date(22, 13, 0)) + } + + @Test("an hour back stays today") + func hourBack() { + let resolved = MealTime.nearestDate(to: date(22, 11, 0), now: date(22, 12, 0), calendar: calendar) + #expect(resolved == date(22, 11, 0)) + } + + @Test("the seconds come from now") + func secondsFromNow() { + let resolved = MealTime.nearestDate(to: date(22, 11, 0, 0), now: date(22, 12, 0, 37), calendar: calendar) + #expect(resolved == date(22, 11, 0, 37)) + } +}