mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
3e0a2ef8ed
TestFlight feedback: during a trip the widgets stayed on day 1 until the app was opened again. The app wrote only today's day into the widget snapshot, and only while it ran, so every timeline entry after midnight still showed the day it last ran, with its stops all ahead again. The snapshot now also carries the trip's days from today on (a month at most, sixteen stops a day), each with its date. On iOS every timeline entry takes the day of its own date, so the quarter-hour entry at midnight turns the widget to the next day, and the bookings list drops the days behind; the Android widgets do the same on each update. A snapshot from an app without the days reads as it was written, and an older app binary ignores them.
74 lines
2.7 KiB
Swift
74 lines
2.7 KiB
Swift
import AppIntents
|
|
import WidgetKit
|
|
|
|
/// The trip a widget shows, picked in "Edit Widget": the dashboard's trip by
|
|
/// default ("Automatic"), or any trip the app listed in widget.trips.
|
|
struct TripChoice: AppEntity {
|
|
static var typeDisplayRepresentation: TypeDisplayRepresentation = "widget.intent.trip"
|
|
static var defaultQuery = TripChoiceQuery()
|
|
|
|
/// The id of the automatic choice, which follows the dashboard's trip.
|
|
static let automaticID = "auto"
|
|
|
|
var id: String
|
|
var title: String
|
|
var dates: String?
|
|
|
|
var displayRepresentation: DisplayRepresentation {
|
|
if id == TripChoice.automaticID {
|
|
return DisplayRepresentation(title: "widget.intent.auto", subtitle: "widget.intent.autoHint")
|
|
}
|
|
return DisplayRepresentation(title: "\(title)", subtitle: dates.map { "\($0)" })
|
|
}
|
|
|
|
static var automatic: TripChoice { TripChoice(id: automaticID, title: "", dates: nil) }
|
|
}
|
|
|
|
struct TripChoiceQuery: EntityQuery {
|
|
func entities(for identifiers: [TripChoice.ID]) async throws -> [TripChoice] {
|
|
all().filter { identifiers.contains($0.id) }
|
|
}
|
|
|
|
func suggestedEntities() async throws -> [TripChoice] {
|
|
all()
|
|
}
|
|
|
|
func defaultResult() async -> TripChoice? {
|
|
.automatic
|
|
}
|
|
|
|
/// Automatic first, then the trips the app sent, soonest first.
|
|
private func all() -> [TripChoice] {
|
|
[.automatic] + WidgetSnapshot.tripIndex().map {
|
|
TripChoice(id: String($0.id), title: $0.title, dates: RelativeDay.range($0.start, $0.end))
|
|
}
|
|
}
|
|
}
|
|
|
|
struct SelectTripIntent: WidgetConfigurationIntent {
|
|
static var title: LocalizedStringResource = "widget.intent.title"
|
|
static var description = IntentDescription("widget.intent.description")
|
|
|
|
@Parameter(title: "widget.intent.trip")
|
|
var trip: TripChoice?
|
|
}
|
|
|
|
/// Like TrekProvider, for the widgets that show one trip: the snapshot of the
|
|
/// trip picked for the widget, or the dashboard's.
|
|
struct TrekTripProvider: AppIntentTimelineProvider {
|
|
func placeholder(in context: Context) -> TrekEntry {
|
|
TrekEntry(date: Date(), snapshot: .sample(running: false))
|
|
}
|
|
|
|
func snapshot(for configuration: SelectTripIntent, in context: Context) async -> TrekEntry {
|
|
let stored = WidgetSnapshot.load(trip: configuration.trip?.id)
|
|
let snapshot: WidgetSnapshot? = (stored?.trip != nil || !context.isPreview) ? stored : WidgetSnapshot.sample(running: false)
|
|
let now = Date()
|
|
return TrekEntry(date: now, snapshot: snapshot?.on(now))
|
|
}
|
|
|
|
func timeline(for configuration: SelectTripIntent, in context: Context) async -> Timeline<TrekEntry> {
|
|
TrekProvider.timeline(WidgetSnapshot.load(trip: configuration.trip?.id))
|
|
}
|
|
}
|