import SwiftUI
import WidgetKit
struct RideWidgetEntry: TimelineEntry {
let date: Date
var attributes: RidePickupAttributes?
var content: RidePickupAttributes.ContentState?
var stale = false
}
struct RidePickupProvider: TimelineProvider {
func placeholder(in context: Context) -> RideWidgetEntry { .init(date: .now) }
func getSnapshot(in context: Context, completion: @escaping (RideWidgetEntry) -> Void) {
if context.isPreview {
let ride = RidePickupSnapshot.example()
completion(.init(date: .now, attributes: ride.attributes, content: ride.activityState))
} else {
Task.detached { completion(currentEntries().first ?? .init(date: .now)) }
}
}
func getTimeline(in context: Context, completion: @escaping (Timeline<RideWidgetEntry>) -> Void) {
Task.detached { completion(Timeline(entries: currentEntries(), policy: .never)) }
}
private func currentEntries() -> [RideWidgetEntry] {
let now = Date.now
guard let report = RideWidgetStore.read() else { return [.init(date: now)] }
let deadline = report.staleDate ?? report.content.estimateDate
let stale = deadline.map { $0 <= now } == true
let current = RideWidgetEntry(date: now, attributes: report.attributes, content: report.content, stale: stale)
let cleared = RideWidgetEntry(date: report.validUntil)
guard !stale, let deadline, deadline < report.validUntil else { return [current, cleared] }
return [current, .init(date: deadline, attributes: report.attributes, content: report.content, stale: true), cleared]
}
}
struct RidePickupWidget: Widget {
var body: some WidgetConfiguration {
StaticConfiguration(kind: RidePickupAttributes.homeWidgetKind, provider: RidePickupProvider()) { entry in
RideWidgetTile(entry: entry)
.containerBackground(LumoStyle.surface, for: .widget)
.widgetURL(URL(string: "lumo-widget-demo://ride"))
}
.configurationDisplayName("Lumo Ride Pickup")
.description("Check pickup status, meeting point and the vehicle for your active ride.")
.supportedFamilies([.systemSmall, .systemMedium])
}
}
private struct RideWidgetTile: View {
let entry: RideWidgetEntry
@Environment(\.widgetFamily) private var family
private var compact: Bool { family == .systemSmall }
var body: some View {
VStack(alignment: .leading, spacing: 7) {
LumoHeading(title: "Ride Pickup", icon: "car.side", tone: LumoStyle.teal)
if let state = entry.content, let pickup = entry.attributes?.pickupLabel {
ViewThatFits(in: .horizontal) {
HStack(spacing: 8) {
status(state)
Spacer(minLength: 0)
if !entry.stale && state.timer != nil { estimate(state).fixedSize() }
}
VStack(alignment: .leading, spacing: 4) {
status(state)
if !entry.stale && state.timer != nil { estimate(state) }
}
}
Label(pickup, systemImage: "location")
.font(.caption)
.foregroundStyle(.secondary)
.lineLimit(2)
if !compact {
let vehicle = [state.vehicle, state.plate].compactMap { $0 }.joined(separator: " · ")
if !vehicle.isEmpty {
Text(vehicle).font(.caption2).foregroundStyle(.secondary).lineLimit(1)
}
}
} else {
Text("No active ride").font(.subheadline.weight(.semibold))
Text("Open Lumo to start tracking.").font(.caption).foregroundStyle(.secondary).lineLimit(2)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
}
private func status(_ state: RidePickupAttributes.ContentState) -> some View {
Text(entry.stale ? "Check pickup estimate" : state.status.label)
.font(.subheadline.weight(.semibold))
.lineLimit(2)
}
private func estimate(_ state: RidePickupAttributes.ContentState) -> some View {
RidePickupETA(state: state, stale: false).foregroundStyle(LumoStyle.teal)
}
}
#Preview(as: .systemMedium) {
RidePickupWidget()
} timeline: {
RideWidgetEntry(date: .now, attributes: RidePickupSnapshot.example().attributes,
content: RidePickupSnapshot.example().activityState)
RideWidgetEntry(date: .now)
}