mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 17:26:24 +00:00
b1037dec62
Four widgets instead of one plain text block, drawn in the phone tokens (warm paper by day, near black by night) with contour lines and a route arc as the travel motif: - Trip (small, medium, large, lock screen): the cover photo, a countdown ring and a plane on its arc before the trip; the day of the trip as segments, the weather and the next stops on frosted glass while it runs - Today (medium, large): the day as a timeline in its category colours, with a now line, the day's progress and the weather - Bookings (small, medium, large, lock screen): flights, trains, hotels and tables ahead as coloured tiles, the next one featured, status dots - To-dos (small, medium, large, lock screen): to-dos and packing as nested rings, the most pressing open to-dos with priority and due dates The app now sends the destination, a small cover JPEG, the weather where the trip is, the bookings ahead, the open to-dos and the packing count; every new field is optional on both sides, so older binaries and servers keep working. Timelines step every quarter hour so the now line moves without the app. client/ios/WidgetPreviews renders every widget in every size, light and dark, on the CI's Mac, uploaded as the widget-previews artifact.
176 lines
6.7 KiB
Swift
176 lines
6.7 KiB
Swift
import SwiftUI
|
|
import WidgetKit
|
|
|
|
/// The home-screen and lock-screen widgets. Each reads the snapshot the app
|
|
/// last wrote (WidgetSnapshot) and draws it with the views in *Content.swift,
|
|
/// which know nothing of WidgetKit so the preview renderer can draw them too.
|
|
struct TrekEntry: TimelineEntry {
|
|
let date: Date
|
|
let snapshot: WidgetSnapshot?
|
|
}
|
|
|
|
struct TrekProvider: TimelineProvider {
|
|
func placeholder(in context: Context) -> TrekEntry {
|
|
TrekEntry(date: Date(), snapshot: .sample(running: false))
|
|
}
|
|
|
|
/// The gallery shows the user's own trip when the app has written one, a
|
|
/// sample trip before that.
|
|
func getSnapshot(in context: Context, completion: @escaping (TrekEntry) -> Void) {
|
|
let stored = WidgetSnapshot.load()
|
|
let snapshot: WidgetSnapshot? = (stored?.trip != nil || !context.isPreview) ? stored : WidgetSnapshot.sample(running: false)
|
|
completion(TrekEntry(date: Date(), snapshot: snapshot))
|
|
}
|
|
|
|
/// An entry every quarter hour for the next six hours, so the now line and
|
|
/// the next stop move on without the app; the app reloads the timeline
|
|
/// whenever it writes a new snapshot.
|
|
func getTimeline(in context: Context, completion: @escaping (Timeline<TrekEntry>) -> Void) {
|
|
let snapshot = WidgetSnapshot.load()
|
|
let now = Date()
|
|
let calendar = Calendar.current
|
|
let minute = calendar.component(.minute, from: now)
|
|
let firstStep = calendar.date(byAdding: .minute, value: 15 - minute % 15, to: now) ?? now
|
|
var entries = [TrekEntry(date: now, snapshot: snapshot)]
|
|
for step in 0..<24 {
|
|
if let date = calendar.date(byAdding: .minute, value: step * 15, to: firstStep) {
|
|
entries.append(TrekEntry(date: calendar.date(bySetting: .second, value: 0, of: date) ?? date, snapshot: snapshot))
|
|
}
|
|
}
|
|
completion(Timeline(entries: entries, policy: .atEnd))
|
|
}
|
|
}
|
|
|
|
extension TrekFamily {
|
|
init(_ family: WidgetFamily) {
|
|
switch family {
|
|
case .systemSmall: self = .small
|
|
case .systemMedium: self = .medium
|
|
case .accessoryCircular: self = .circular
|
|
case .accessoryRectangular: self = .rectangular
|
|
case .accessoryInline: self = .inline
|
|
default: self = .large
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The widget's ground: the content on its background, through the container
|
|
/// background on iOS 17 (which the system can tint and remove), the plain way before.
|
|
struct TrekContainer<Content: View, Background: View>: View {
|
|
@Environment(\.widgetFamily) private var family
|
|
let link: URL?
|
|
@ViewBuilder var content: () -> Content
|
|
@ViewBuilder var background: () -> Background
|
|
|
|
var body: some View {
|
|
let accessory = TrekFamily(family).isAccessory
|
|
Group {
|
|
if #available(iOSApplicationExtension 17.0, *) {
|
|
content().containerBackground(for: .widget) {
|
|
if accessory { Color.clear } else { background() }
|
|
}
|
|
} else {
|
|
content().padding(accessory ? 0 : 16).background(accessory ? nil : background())
|
|
}
|
|
}
|
|
.widgetURL(link)
|
|
}
|
|
}
|
|
|
|
// MARK: - Trip
|
|
|
|
struct NextTripWidget: Widget {
|
|
var body: some WidgetConfiguration {
|
|
// The kind stays "NextTrip" so widgets placed before the redesign keep working.
|
|
StaticConfiguration(kind: "NextTrip", provider: TrekProvider()) { entry in
|
|
TripWidgetView(entry: entry)
|
|
}
|
|
.configurationDisplayName("Trip")
|
|
.description("Counts down to your next trip, then shows the day and what comes next.")
|
|
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .accessoryCircular, .accessoryRectangular, .accessoryInline])
|
|
}
|
|
}
|
|
|
|
struct TripWidgetView: View {
|
|
@Environment(\.widgetFamily) private var family
|
|
let entry: TrekEntry
|
|
|
|
var body: some View {
|
|
let snapshot = entry.snapshot
|
|
TrekContainer(link: snapshot?.trip.flatMap { WidgetSnapshot.link($0.path) }) {
|
|
TripContent(snapshot: snapshot, family: TrekFamily(family), now: entry.date)
|
|
} background: {
|
|
if snapshot?.trip != nil { TripBackdrop(trip: snapshot?.trip) } else { TrekPaper() }
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Today
|
|
|
|
struct TodayWidget: Widget {
|
|
var body: some WidgetConfiguration {
|
|
StaticConfiguration(kind: "TripToday", provider: TrekProvider()) { entry in
|
|
TrekContainer(link: entry.snapshot?.trip.flatMap { WidgetSnapshot.link($0.path) }) {
|
|
TodayFamilyView(entry: entry)
|
|
} background: {
|
|
TrekPaper()
|
|
}
|
|
}
|
|
.configurationDisplayName("Today")
|
|
.description("Today's plan as a timeline, with the weather and what is next.")
|
|
.supportedFamilies([.systemMedium, .systemLarge])
|
|
}
|
|
}
|
|
|
|
private struct TodayFamilyView: View {
|
|
@Environment(\.widgetFamily) private var family
|
|
let entry: TrekEntry
|
|
var body: some View { TodayContent(snapshot: entry.snapshot, family: TrekFamily(family), now: entry.date) }
|
|
}
|
|
|
|
// MARK: - Bookings
|
|
|
|
struct BookingsWidget: Widget {
|
|
var body: some WidgetConfiguration {
|
|
StaticConfiguration(kind: "TripBookings", provider: TrekProvider()) { entry in
|
|
TrekContainer(link: entry.snapshot?.bookings?.first.flatMap { WidgetSnapshot.link($0.path) }) {
|
|
BookingsFamilyView(entry: entry)
|
|
} background: {
|
|
TrekPaper()
|
|
}
|
|
}
|
|
.configurationDisplayName("Bookings")
|
|
.description("Your next flights, trains, hotels and tables, and when they are due.")
|
|
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .accessoryRectangular, .accessoryInline])
|
|
}
|
|
}
|
|
|
|
private struct BookingsFamilyView: View {
|
|
@Environment(\.widgetFamily) private var family
|
|
let entry: TrekEntry
|
|
var body: some View { BookingsContent(snapshot: entry.snapshot, family: TrekFamily(family), now: entry.date) }
|
|
}
|
|
|
|
// MARK: - To-dos
|
|
|
|
struct TodosWidget: Widget {
|
|
var body: some WidgetConfiguration {
|
|
StaticConfiguration(kind: "TripTodos", provider: TrekProvider()) { entry in
|
|
TrekContainer(link: entry.snapshot?.todos.flatMap { WidgetSnapshot.link($0.path) }) {
|
|
TodosFamilyView(entry: entry)
|
|
} background: {
|
|
TrekPaper()
|
|
}
|
|
}
|
|
.configurationDisplayName("To-dos")
|
|
.description("What is still open for the trip, and how far the packing has come.")
|
|
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge, .accessoryCircular, .accessoryRectangular])
|
|
}
|
|
}
|
|
|
|
private struct TodosFamilyView: View {
|
|
@Environment(\.widgetFamily) private var family
|
|
let entry: TrekEntry
|
|
var body: some View { TodosContent(snapshot: entry.snapshot, family: TrekFamily(family), now: entry.date) }
|
|
}
|