← All repositories

reportage

A life-logging app for iPhone

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


Shared/Capture/PassiveLog.swift (1262B)

1import Foundation
2
3/// Logs the moment: a report with its context and no answers, from a quick
4/// log or a notification's Instant Report action. It appears in the timeline straight away and can be
5/// filled in at any time, or left as it is.
6struct PassiveLog: Sendable {
7    let store: ReportStore
8    var capture: @Sendable () async -> Context = { await ContextCapture().capture() }
9
10    /// Saves the report at once, so it's in the timeline while its context
11    /// is captured, then adds the context. Returns the report's ID.
12    @discardableResult
13    func log(
14        kind: ReportKind = .manual,
15        trigger: ReportTrigger,
16        date: Date = Date(),
17        timeZone: TimeZone = .current,
18        promptedAt: Date? = nil
19    ) async throws -> UUID {
20        let report = Report(
21            id: UUID(),
22            date: date,
23            timeZone: timeZone.identifier,
24            kind: kind,
25            trigger: trigger,
26            state: .passive,
27            promptedAt: promptedAt,
28            modifiedAt: date
29        )
30        try await store.insert(report)
31        let context = await capture()
32        if context != Context() {
33            try await store.setContext(context, for: report.id)
34        }
35        return report.id
36    }
37}