← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


commit 259001b0d79770345c214898428f18dfff514e36
parent dbc94ee1ebc9ac84b94fab5fe2728d15cbfa3ea5
Author: Michael Camilleri <[email protected]>
Date:   Tue, 20 Oct 2026 13:38:20 +0900

Keep the timeline's fetch from resetting to empty

After filing a report, the timeline sometimes showed 'Nothing recorded
yet.' until the user switched to the Settings tab and back. The list's
sectioned fetch started with a predicate that matched nothing, and
widen() then set the real predicate on the fetch results. Dismissing
the survey sheet rebuilt the list with a fresh fetch request, which
could bring back the empty predicate. The task that set the real one
didn't run again until the view reappeared.

This commit splits the list in two. ReportList still works out how far
back the timeline reaches, but now only records it. A new ReportDays
view builds its fetch in init from that start, so each new copy of the
view fetches the right reports rather than relying on a predicate set
after the fact.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Diffstat

MReportage/Views/Home/HomeView.swift | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------

1 file changed, 71 insertions(+), 23 deletions(-)

diff --git a/Reportage/Views/Home/HomeView.swift b/Reportage/Views/Home/HomeView.swift

@@ -109,6 +109,8 @@ extension ReportEntity {
     }
 }
 
+/// Works out how far back the timeline reaches, and reaches further as it's
+/// scrolled to its end.
 struct ReportList: View {
     let describer: ReportDescriber
     let onStart: (ReportKind) -> Void
@@ -117,8 +119,6 @@ struct ReportList: View {
     let onOpen: (UUID) -> Void
 
     @Environment(\.reportStore) private var store
-    @SectionedFetchRequest(fetchRequest: ReportList.request, sectionIdentifier: \ReportEntity.dayKey)
-    private var days: SectionedFetchResults<String, ReportEntity>
     /// How many of the newest reports the list covers. Grouping into days
     /// loads every report the fetch covers, and regroups them all whenever
     /// one changes, so the list starts with the newest and reaches further
@@ -129,18 +129,77 @@ struct ReportList: View {
     private static let page = 300
     private static let morePage = 500
 
-    private enum ListStart: Hashable {
-        /// Not worked out yet: the list is empty until it is.
-        case unknown
-        /// Reports from this date on, with older ones beyond.
-        case date(Date)
-        /// Every report.
-        case all
+    var body: some View {
+        ReportDays(
+            start: start,
+            describer: describer,
+            onStart: onStart,
+            onQuickLog: onQuickLog,
+            onOpen: onOpen,
+            onReachEnd: { reach += Self.morePage }
+        )
+        .task(id: reach) { await widen() }
+    }
+
+    private func widen() async {
+        guard let store, let date = try? await store.listStart(covering: reach) else {
+            start = .all
+            return
+        }
+        start = .date(date)
+    }
+}
+
+private enum ListStart: Hashable {
+    /// Not worked out yet: the list is empty until it is.
+    case unknown
+    /// Reports from this date on, with older ones beyond.
+    case date(Date)
+    /// Every report.
+    case all
+
+    var predicate: NSPredicate? {
+        switch self {
+        case .unknown: NSPredicate(value: false)
+        case .date(let date): NSPredicate(format: "date >= %@", date as NSDate)
+        case .all: nil
+        }
+    }
+}
+
+/// The timeline's reports, grouped by day. The fetch is built from `start`
+/// each time the view is, rather than changed after the fact, so a new
+/// copy of the view never brings back an empty fetch.
+private struct ReportDays: View {
+    let start: ListStart
+    let describer: ReportDescriber
+    let onStart: (ReportKind) -> Void
+    let onQuickLog: () -> Void
+    let onOpen: (UUID) -> Void
+    /// Reaches further back once the list is scrolled to its end.
+    let onReachEnd: () -> Void
+
+    @SectionedFetchRequest private var days: SectionedFetchResults<String, ReportEntity>
+
+    init(
+        start: ListStart, describer: ReportDescriber,
+        onStart: @escaping (ReportKind) -> Void, onQuickLog: @escaping () -> Void,
+        onOpen: @escaping (UUID) -> Void, onReachEnd: @escaping () -> Void
+    ) {
+        self.start = start
+        self.describer = describer
+        self.onStart = onStart
+        self.onQuickLog = onQuickLog
+        self.onOpen = onOpen
+        self.onReachEnd = onReachEnd
+        _days = SectionedFetchRequest(
+            fetchRequest: Self.request(predicate: start.predicate),
+            sectionIdentifier: \ReportEntity.dayKey)
     }
 
-    private static var request: NSFetchRequest<ReportEntity> {
+    private static func request(predicate: NSPredicate?) -> NSFetchRequest<ReportEntity> {
         let request = ReportEntity.fetchRequest()
-        request.predicate = NSPredicate(value: false)
+        request.predicate = predicate
         request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
         request.fetchBatchSize = 40
         request.relationshipKeyPathsForPrefetching = ["responses", "health"]
@@ -181,23 +240,12 @@ struct ReportList: View {
                     // A new row each time the list reaches further, so it
                     // keeps reaching while its end is still in view.
                     .id(start)
-                    .onAppear { reach += Self.morePage }
+                    .onAppear(perform: onReachEnd)
             }
         }
         .listStyle(.plain)
         .scrollContentBackground(.hidden)
         .background(Theme.paper)
-        .task(id: reach) { await widen() }
-    }
-
-    private func widen() async {
-        guard let store, let date = try? await store.listStart(covering: reach) else {
-            start = .all
-            days.nsPredicate = nil
-            return
-        }
-        start = .date(date)
-        days.nsPredicate = NSPredicate(format: "date >= %@", date as NSDate)
     }
 
     /// A report is tapped to see it in full, where it can be edited or