Sources/OrgstarWidgets/FlashcardWidget.swift

801e496807e41f08001dbd99622e2fa686a34058
orgstar/Sources/OrgstarWidgets/FlashcardWidget.swift

88 lines · 3520 bytes

 1import OrgApp
 2import SwiftUI
 3import WidgetKit
 4
 5/// How many flashcards are due; tapping it starts the review.
 6struct FlashcardWidget: Widget {
 7    var body: some WidgetConfiguration {
 8        StaticConfiguration(kind: "sh.krz.orgstar.flashcards", provider: FlashcardProvider()) { entry in
 9            FlashcardView(entry: entry)
10        }
11        .configurationDisplayName("Flashcards")
12        .description("How many flashcards are due for review.")
13        #if os(macOS)
14        .supportedFamilies([.systemSmall])
15        #else
16        .supportedFamilies([.systemSmall, .accessoryCircular, .accessoryInline])
17        #endif
18    }
19}
20
21struct FlashcardEntry: TimelineEntry {
22    let date: Date
23    /// Nil until the app has written one.
24    let snapshot: FlashcardSnapshot?
25}
26
27/// The count now, and again each time another card falls due.
28struct FlashcardProvider: TimelineProvider {
29    func placeholder(in context: Context) -> FlashcardEntry {
30        FlashcardEntry(date: .now, snapshot: FlashcardSnapshot(due: [.now.addingTimeInterval(-60)]))
31    }
32
33    func getSnapshot(in context: Context, completion: @escaping (FlashcardEntry) -> Void) {
34        completion(FlashcardEntry(date: .now, snapshot: widgetsLocked ? nil : FlashcardSnapshot.read()))
35    }
36
37    func getTimeline(in context: Context, completion: @escaping (Timeline<FlashcardEntry>) -> Void) {
38        let snapshot = widgetsLocked ? nil : FlashcardSnapshot.read()
39        let dates = [Date.now] + (snapshot?.changes(after: .now) ?? [])
40        completion(Timeline(entries: dates.map { FlashcardEntry(date: $0, snapshot: snapshot) }, policy: .never))
41    }
42}
43
44struct FlashcardView: View {
45    let entry: FlashcardEntry
46    @Environment(\.widgetFamily) private var family
47
48    private var count: Int { entry.snapshot?.count(at: entry.date) ?? 0 }
49
50    var body: some View {
51        content
52            .widgetURL(AppLink.review.url)
53            #if os(macOS)
54            .containerBackground(for: .widget) { Color(nsColor: .windowBackgroundColor) }
55            #else
56            .containerBackground(for: .widget) { family == .systemSmall ? Color(uiColor: .systemBackground) : Color.clear }
57            #endif
58    }
59
60    @ViewBuilder
61    private var content: some View {
62        switch family {
63        case .accessoryInline:
64            Text("\(count) cards due")
65        case .accessoryCircular:
66            ZStack {
67                AccessoryWidgetBackground()
68                VStack(spacing: -2) {
69                    Text("\(count)").font(.title2.weight(.semibold)).widgetAccentable()
70                    Text("cards").font(.caption2)
71                }
72            }
73            .accessibilityLabel("\(count) cards due")
74        default:
75            VStack(alignment: .leading, spacing: 4) {
76                Label("Flashcards", systemImage: "rectangle.on.rectangle.angled").font(.subheadline.weight(.semibold)).widgetAccentable()
77                Spacer(minLength: 0)
78                if entry.snapshot == nil {
79                    Text(widgetsLocked ? "Widgets need orgstar+." : "Open orgstar to count your cards here.").font(.caption).foregroundStyle(.secondary)
80                } else {
81                    Text("\(count)").font(.system(size: 44, weight: .semibold, design: .rounded)).monospacedDigit()
82                    Text(count == 0 ? "Nothing due" : "due for review").font(.caption).foregroundStyle(.secondary)
83                }
84            }
85            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
86        }
87    }
88}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form