added base homescreen-widget setup, working on Android, iOS in progress
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"filename" : "marianum_m_white.svg",
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
},
|
||||
"properties" : {
|
||||
"preserves-vector-representation" : true,
|
||||
"template-rendering-intent" : "template"
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
||||
|
||||
<svg
|
||||
width="70.000168mm"
|
||||
height="82.227348mm"
|
||||
viewBox="0 0 70.000168 82.227348"
|
||||
version="1.1"
|
||||
id="svg1"
|
||||
xml:space="preserve"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
xmlns:svg="http://www.w3.org/2000/svg"><defs
|
||||
id="defs1" /><g
|
||||
id="g1"
|
||||
transform="matrix(0.26458333,0,0,0.26458334,107.44411,-80.482198)"><g
|
||||
id="group-R5"
|
||||
transform="translate(-749.41293,290.52252)"><path
|
||||
id="path3"
|
||||
d="m 3499.67,4594.33 c -16.43,-106.19 -29.71,-199.97 -43.79,-293.49 86.83,-19 138.5,-27.61 223.38,-43.82 63.81,-12.18 175.24,-20.4 179.64,-83.23 6.46,-92.69 -124.69,-55.41 -188.38,-43.81 -84.33,15.36 -159.13,28.84 -232.2,43.81 -13.68,-60.72 -26.83,-118.68 -39.43,-179.61 -36.76,-178.32 -73.67,-368.16 -105.11,-551.97 18.09,25.66 30.84,42.72 43.8,65.7 66.7,118.26 140.39,245.04 227.76,354.83 33.49,42.05 76.86,94.81 118.31,113.91 98.42,45.36 166.68,-22.2 170.87,-118.28 3.68,-85.28 -23.09,-181.17 -35.08,-275.99 -12.4,-98.19 -22.89,-194.93 -35.03,-275.98 72.44,102.69 147.93,269.64 240.95,381.12 27.51,33 73.55,80.61 118.27,87.62 218.76,34.33 126.58,-312.17 127.05,-473.13 0.4,-144.9 44.01,-255.37 175.21,-271.59 43.02,-5.31 105.84,11.16 112.7,-26.34 8.67,-47.38 -78.15,-60.52 -125.84,-61.28 -291.34,-4.51 -322.06,262.33 -311.01,573.88 -19.85,-18.57 -35.71,-47.53 -52.57,-74.47 -97.59,-155.88 -203.95,-327.22 -297.92,-503.79 -25.93,-48.79 -53.68,-114.7 -135.8,-83.23 -17.27,6.63 -48.25,44.39 -52.56,56.96 -19.58,57.19 1.55,137.42 8.76,205.89 21.54,203.72 57.81,389.09 78.87,587.01 -26.3,0.51 -43.93,-30.07 -56.96,-48.2 -46.9,-65.27 -86.02,-140.76 -127.04,-214.64 -52.84,-95.15 -108.23,-192.84 -157.71,-293.52 -75.25,-153.09 -188.6,-501.89 -242.12,-678.81 -8.67,-28.67 -17.7,-58.08 -26.3,-87.64 -7.48,-25.72 -10.39,-57.68 -35.05,-74.46 -100.02,18.93 -89.71,104.89 -70.09,205.9 47.35,243.43 170.89,706.45 211.48,946.04 -72.97,-70.97 -153.99,-207.41 -289.14,-236.55 -136.47,-29.44 -217.95,47.68 -271.6,122.66 -17.14,23.96 -41.43,49.54 -26.29,78.84 83.96,35.51 113.37,-65.2 197.15,-74.47 22.65,-2.5 54.56,2.4 74.46,8.78 132.4,42.34 237.57,218.76 297.87,346.07 74.16,156.45 125.32,330.5 148.95,490.64 -65.71,11.4 -142.96,22.15 -219.25,36.52 -109.8,20.72 -158.81,10.75 -201.29,59.86 9.15,41.95 41.41,60.8 70.1,83.24 126.26,-16.84 252.45,-33.77 372.36,-56.97 20.43,89.25 51.98,218.51 74.45,311.05 40.53,26.02 88.88,-8.43 105.17,-35.06"
|
||||
style="fill:#d3d2d2;fill-opacity:1;fill-rule:evenodd;stroke:none"
|
||||
transform="matrix(0.13333333,0,0,-0.13333333,0,632.14667)" /></g></g></svg>
|
||||
|
After Width: | Height: | Size: 2.6 KiB |
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleDisplayName</key>
|
||||
<string>Marianum Stundenplan</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>$(MARKETING_VERSION)</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||
<key>NSExtension</key>
|
||||
<dict>
|
||||
<key>NSExtensionPointIdentifier</key>
|
||||
<string>com.apple.widgetkit-extension</string>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,28 @@
|
||||
import SwiftUI
|
||||
|
||||
/// Marianum-M peeking out of the bottom-right corner. Sized to the longer
|
||||
/// widget edge so it scales with resize; offset nudges a sliver behind the
|
||||
/// edge.
|
||||
struct MarianumWatermark: View {
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
|
||||
var body: some View {
|
||||
GeometryReader { geo in
|
||||
let markSize = min(400, max(160, max(geo.size.width, geo.size.height) * 0.8))
|
||||
let offsetX = markSize * 0.18
|
||||
let offsetY = markSize * 0.18
|
||||
ZStack(alignment: .bottomTrailing) {
|
||||
Color.clear
|
||||
Image("marianum_m")
|
||||
.resizable()
|
||||
.renderingMode(.template)
|
||||
.aspectRatio(contentMode: .fit)
|
||||
.foregroundStyle(.primary)
|
||||
.frame(width: markSize, height: markSize)
|
||||
.opacity(colorScheme == .dark ? 0.025 : 0.014)
|
||||
.offset(x: offsetX, y: offsetY)
|
||||
}
|
||||
}
|
||||
.clipped()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
# iOS Widget Extension — Xcode Setup
|
||||
|
||||
Die Swift-Quellen unter `ios/TimetableWidgetExtension/` müssen einmalig in Xcode als **Widget Extension Target** verdrahtet werden — ohne diesen Schritt bleibt der Code unkompiliert.
|
||||
|
||||
## Schritt 1 — Widget-Extension-Target anlegen
|
||||
|
||||
1. `ios/Runner.xcworkspace` in Xcode öffnen.
|
||||
2. Projekt-Sidebar → `Runner` (Projekt-Root) → **+ Add Target** unten links.
|
||||
3. **iOS → Widget Extension** wählen.
|
||||
4. Eigenschaften:
|
||||
- Product Name: `TimetableWidgetExtension`
|
||||
- Bundle Identifier: `eu.mhsl.marianum.mobile.client.TimetableWidgetExtension`
|
||||
- Language: Swift
|
||||
- Include Configuration Intent: **OFF** (StaticConfiguration reicht)
|
||||
- Embed in: Runner
|
||||
5. Beim Activate-Scheme-Dialog auf **Cancel** klicken.
|
||||
|
||||
## Schritt 2 — Vorhandene Quelldateien ins Target ziehen
|
||||
|
||||
Xcode hat zunächst Dummy-Dateien (`TimetableWidgetExtension.swift`, `TimetableWidgetExtensionBundle.swift`) angelegt. Diese **löschen** (Move to Trash). Dann:
|
||||
|
||||
1. Sidebar → Rechtsklick auf den Ordner `TimetableWidgetExtension` → **Add Files to "Runner"…**
|
||||
2. Im File-Picker zu `ios/TimetableWidgetExtension/` navigieren und alle `.swift`-Dateien, die `Info.plist`, `TimetableWidgetExtension.entitlements` **und den `Assets.xcassets`-Ordner** selektieren (mit `marianum_m`-Asset darin — gleicher Asset-Name wie auf Android-Seite).
|
||||
3. **Wichtig**: bei „Add to targets" nur `TimetableWidgetExtension` ankreuzen, **nicht** Runner.
|
||||
|
||||
## Schritt 3 — App Group aktivieren
|
||||
|
||||
Beide Targets brauchen die App-Group-Berechtigung, damit Hauptapp und Widget über `UserDefaults(suiteName:)` schreiben/lesen können.
|
||||
|
||||
1. **Runner**-Target → **Signing & Capabilities** → **+ Capability** → **App Groups**.
|
||||
- Group-ID hinzufügen: `group.eu.mhsl.marianum.mobile.client.widget`
|
||||
2. Dasselbe für **TimetableWidgetExtension** — mit derselben Group-ID.
|
||||
|
||||
Im Apple-Developer-Portal muss die App-Group bei beiden App-IDs eingetragen sein, sonst schlägt das Provisioning fehl.
|
||||
|
||||
## Schritt 4 — Entitlements verlinken
|
||||
|
||||
1. **Runner** → Build Settings → `CODE_SIGN_ENTITLEMENTS` sollte bereits auf `Runner/Runner.entitlements` zeigen.
|
||||
2. **TimetableWidgetExtension** → Build Settings → `CODE_SIGN_ENTITLEMENTS` → auf `TimetableWidgetExtension/TimetableWidgetExtension.entitlements` setzen.
|
||||
|
||||
## Schritt 5 — Info.plist + Deployment Target
|
||||
|
||||
1. **TimetableWidgetExtension** → Build Settings → `INFOPLIST_FILE` → auf `TimetableWidgetExtension/Info.plist` setzen.
|
||||
2. Build Settings → `IPHONEOS_DEPLOYMENT_TARGET` ≥ 16.0 (Code gated `.containerBackground` mit `if #available(iOS 17, *)`, läuft also auch auf 16).
|
||||
|
||||
## Schritt 6 — Build & Run
|
||||
|
||||
- Scheme `Runner` (nicht das Widget-Scheme) wählen → Run.
|
||||
- Auf Home-Screen langes Drücken → Widget hinzufügen → "Marianum · Heute" / "Marianum · Woche".
|
||||
- Widget-Tap öffnet die App im zuletzt sichtbaren Tab. Eine Tab-Navigation auf den Stundenplan ist bewusst nicht implementiert (Android nutzt Intent-Extras, iOS würde dafür ein URL-Scheme oder AppIntent brauchen — beides bewusst ausgespart).
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- **Widget zeigt „Lade…"** auch nach Refresh: App-Group greift nicht. Prüfen, ob beide Targets dieselbe Group-ID haben und das Provisioning aktualisiert wurde.
|
||||
- **Stale-Daten nach Logout**: `WidgetSync.clear()` schreibt `widget_data_logged_in_v1 = false`; Widget zeigt dann den Login-Placeholder.
|
||||
- **Lessons um 1–2 Stunden verschoben**: Date-Parser-Bug. Sollte gefixt sein in `WidgetData.swift::parseDartDate` — verifizieren, dass die ISO-8601-Strings ohne Z-Suffix als `TimeZone.current` geparsed werden.
|
||||
- **App-Store-Submit später**: `Runner.entitlements` `aps-environment` von `development` auf `production` umbiegen.
|
||||
|
||||
## Was bereits im Repo erledigt ist
|
||||
|
||||
- Alle Swift-Quellen, Info.plist, Entitlements liegen unter `ios/TimetableWidgetExtension/`.
|
||||
- App-Group-ID konsistent zwischen Dart (`WidgetSync.iosAppGroupId`), Swift (`WidgetDataKey.appGroupId`) und der Entitlements-Datei.
|
||||
- `home_widget`-Plugin auf der Dart-Seite konfiguriert; ruft `HomeWidget.setAppGroupId` beim ersten Sync.
|
||||
- `containerBackground` für iOS 17+ gegated, fällt auf iOS 16 sauber zurück.
|
||||
- Date-Parser fixt das fehlende Z-Suffix (Dart schreibt lokale Zeit ohne TZ-Marker).
|
||||
|
||||
## Was am Mac noch zu tun ist
|
||||
|
||||
- Schritte 1–5 oben in Xcode durchklicken (10–15 Min).
|
||||
- `flutter pub get` + `cd ios && pod install`.
|
||||
- Auf physischem Gerät oder iOS-Simulator (≥ 16.0) bauen.
|
||||
- Widget aufs Home-Screen ziehen, prüfen dass Lesson-Zeiten korrekt rendern.
|
||||
@@ -0,0 +1,451 @@
|
||||
import SwiftUI
|
||||
import WidgetKit
|
||||
|
||||
// Layout constants — must mirror WidgetRenderer.kt on Android, otherwise
|
||||
// the platforms drift apart on the same widget size.
|
||||
let FALLBACK_VIRTUAL_MINUTES = 11 * 60
|
||||
let MIN_HOUR_HEIGHT: CGFloat = 18
|
||||
let MAX_HOUR_HEIGHT: CGFloat = 72
|
||||
let MIN_BLOCK_HEIGHT: CGFloat = 16
|
||||
let LESSON_GAP: CGFloat = 1.5
|
||||
|
||||
func realMinutesToVirtual(_ realMin: Int, periods: [WidgetPeriod]) -> CGFloat {
|
||||
guard !periods.isEmpty else { return CGFloat(realMin) }
|
||||
for p in periods where realMin >= p.startMinutes && realMin <= p.endMinutes {
|
||||
return CGFloat(p.virtualStartMinutes + (realMin - p.startMinutes))
|
||||
}
|
||||
let first = periods.first!
|
||||
if realMin < first.startMinutes {
|
||||
return CGFloat(realMin - first.startMinutes + first.virtualStartMinutes)
|
||||
}
|
||||
let last = periods.last!
|
||||
if realMin > last.endMinutes {
|
||||
return CGFloat(last.virtualEndMinutes + (realMin - last.endMinutes))
|
||||
}
|
||||
var prev = first
|
||||
for i in 1..<periods.count {
|
||||
let curr = periods[i]
|
||||
if realMin > prev.endMinutes && realMin < curr.startMinutes {
|
||||
let gap = curr.startMinutes - prev.endMinutes
|
||||
let virtualGap = curr.virtualStartMinutes - prev.virtualEndMinutes
|
||||
if gap > 0 {
|
||||
return CGFloat(prev.virtualEndMinutes) +
|
||||
CGFloat(realMin - prev.endMinutes) * CGFloat(virtualGap) / CGFloat(gap)
|
||||
}
|
||||
return CGFloat(curr.virtualStartMinutes)
|
||||
}
|
||||
prev = curr
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
let BLOCK_SHOW_ROOM_MIN: CGFloat = 18
|
||||
let BLOCK_SHOW_TEACHER_SEPARATE_MIN: CGFloat = 30
|
||||
|
||||
let MIN_SUBJECT_FONT: CGFloat = 9
|
||||
let MAX_SUBJECT_FONT: CGFloat = 14
|
||||
let MIN_SECONDARY_FONT: CGFloat = 7
|
||||
|
||||
func subjectFont(forHourHeight hourHeight: CGFloat) -> CGFloat {
|
||||
let t = max(0, min(1, (hourHeight - MIN_HOUR_HEIGHT) / (MAX_HOUR_HEIGHT - MIN_HOUR_HEIGHT)))
|
||||
return MIN_SUBJECT_FONT + t * (MAX_SUBJECT_FONT - MIN_SUBJECT_FONT)
|
||||
}
|
||||
|
||||
struct TimetableDayView: View {
|
||||
let entry: TimetableEntry
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
if !entry.isLoggedIn {
|
||||
placeholder("Bitte einloggen, um den Stundenplan zu laden")
|
||||
} else if let data = entry.data {
|
||||
content(data: data)
|
||||
} else {
|
||||
placeholder("Lade…")
|
||||
}
|
||||
}
|
||||
.background(MarianumWatermark())
|
||||
.widgetThemeOverride(entry.themeMode)
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func content(data: WidgetTimetableData) -> some View {
|
||||
VStack(alignment: .leading, spacing: 6) {
|
||||
header(data: data)
|
||||
if data.isHoliday {
|
||||
emptyState(text: data.holidayName ?? "Ferien")
|
||||
} else if data.lessons.isEmpty {
|
||||
emptyState(text: "Keine Stunden")
|
||||
} else {
|
||||
GeometryReader { geo in
|
||||
let totalMin = CGFloat(data.periods.last?.virtualEndMinutes ?? FALLBACK_VIRTUAL_MINUTES)
|
||||
TimeGridView(
|
||||
lessons: data.lessons,
|
||||
periods: data.periods,
|
||||
anchorDate: data.anchorDate,
|
||||
hourHeight: max(
|
||||
MIN_HOUR_HEIGHT,
|
||||
min(MAX_HOUR_HEIGHT, geo.size.height / max(totalMin, 60) * 60)
|
||||
),
|
||||
showRoom: true,
|
||||
showTeacher: true,
|
||||
showTimeLabels: true
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func header(data: WidgetTimetableData) -> some View {
|
||||
HStack {
|
||||
Text(dayLabel(for: data.anchorDate))
|
||||
.font(.system(size: 14, weight: .semibold))
|
||||
.foregroundStyle(.primary)
|
||||
Spacer()
|
||||
Text("Stand: \(freshnessLabel(for: data.fetchedAt))")
|
||||
.font(.system(size: 10))
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
|
||||
private func emptyState(text: String) -> some View {
|
||||
VStack {
|
||||
Spacer()
|
||||
Text(text)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
Spacer()
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
|
||||
private func placeholder(_ message: String) -> some View {
|
||||
VStack(spacing: 4) {
|
||||
Text("Marianum Stundenplan")
|
||||
.font(.system(size: 14, weight: .semibold))
|
||||
Text(message)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
}
|
||||
|
||||
struct TimeGridView: View {
|
||||
let lessons: [WidgetLesson]
|
||||
let periods: [WidgetPeriod]
|
||||
let anchorDate: Date
|
||||
let hourHeight: CGFloat
|
||||
let showRoom: Bool
|
||||
let showTeacher: Bool
|
||||
let showTimeLabels: Bool
|
||||
/// Week-widget passes 3 for narrow columns; day-widget keeps 7.
|
||||
var horizontalPadding: CGFloat = 7
|
||||
|
||||
private var totalVirtualMinutes: Int {
|
||||
periods.last?.virtualEndMinutes ?? FALLBACK_VIRTUAL_MINUTES
|
||||
}
|
||||
|
||||
private var totalHeight: CGFloat {
|
||||
CGFloat(totalVirtualMinutes) * hourHeight / 60.0
|
||||
}
|
||||
|
||||
/// Below this per-hour height the two-line label collapses to a single
|
||||
/// period number — time + number overlap otherwise.
|
||||
private var compactLabels: Bool { hourHeight < 26 }
|
||||
|
||||
var body: some View {
|
||||
HStack(alignment: .top, spacing: 0) {
|
||||
if showTimeLabels {
|
||||
timeLabelsColumn
|
||||
.frame(width: 32, alignment: .topTrailing)
|
||||
Rectangle()
|
||||
.fill(Color.primary.opacity(0.13))
|
||||
.frame(width: 1)
|
||||
}
|
||||
ZStack(alignment: .top) {
|
||||
gridLines
|
||||
breakBlocks
|
||||
ForEach(lessons.indices, id: \.self) { idx in
|
||||
lessonBlock(lessons[idx])
|
||||
}
|
||||
if Calendar.current.isDate(anchorDate, inSameDayAs: Date()) {
|
||||
nowIndicator
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, minHeight: totalHeight, alignment: .top)
|
||||
}
|
||||
}
|
||||
|
||||
private var timeLabelsColumn: some View {
|
||||
ZStack(alignment: .topTrailing) {
|
||||
// Hour rules continue through the time-label column so it reads
|
||||
// as a real table column rather than a free-floating tick list.
|
||||
// Hour rules extend through the time-label column so it reads
|
||||
// as a table column rather than a free-floating tick list.
|
||||
ForEach(periodBoundaries(periods), id: \.self) { virtualMin in
|
||||
Rectangle()
|
||||
.fill(Color.primary.opacity(0.08))
|
||||
.frame(height: 1)
|
||||
.offset(y: CGFloat(virtualMin) * hourHeight / 60.0)
|
||||
}
|
||||
ForEach(periods, id: \.startMinutes) { period in
|
||||
VStack(alignment: .trailing, spacing: -2) {
|
||||
if compactLabels {
|
||||
Text("\(period.name).")
|
||||
.font(.system(size: 9, weight: .bold))
|
||||
.foregroundStyle(.primary)
|
||||
.lineLimit(1)
|
||||
} else {
|
||||
Text(formatHm(period.startMinutes))
|
||||
.font(.system(size: 9))
|
||||
.foregroundStyle(.primary)
|
||||
.lineLimit(1)
|
||||
Text("\(period.name).")
|
||||
.font(.system(size: 7, weight: .bold))
|
||||
.foregroundStyle(.secondary)
|
||||
.lineLimit(1)
|
||||
}
|
||||
}
|
||||
.padding(.trailing, 4)
|
||||
.offset(y: CGFloat(period.virtualStartMinutes) * hourHeight / 60.0)
|
||||
}
|
||||
}
|
||||
.frame(height: totalHeight, alignment: .topTrailing)
|
||||
}
|
||||
|
||||
private var gridLines: some View {
|
||||
ZStack(alignment: .top) {
|
||||
// Hour rules continue through the time-label column so it reads
|
||||
// as a real table column rather than a free-floating tick list.
|
||||
ForEach(periodBoundaries(periods), id: \.self) { virtualMin in
|
||||
Rectangle()
|
||||
.fill(Color.primary.opacity(0.08))
|
||||
.frame(height: 1)
|
||||
.offset(y: CGFloat(virtualMin) * hourHeight / 60.0)
|
||||
}
|
||||
}
|
||||
.frame(height: totalHeight)
|
||||
}
|
||||
|
||||
private var breakBlocks: some View {
|
||||
ZStack(alignment: .top) {
|
||||
ForEach(0..<max(0, periods.count - 1), id: \.self) { i in
|
||||
let curr = periods[i]
|
||||
let next = periods[i + 1]
|
||||
let virtualGap = next.virtualStartMinutes - curr.virtualEndMinutes
|
||||
if virtualGap > 0 {
|
||||
Rectangle()
|
||||
.fill(Color.primary.opacity(0.03))
|
||||
.frame(height: CGFloat(virtualGap) * hourHeight / 60.0)
|
||||
.padding(.horizontal, 1)
|
||||
.offset(y: CGFloat(curr.virtualEndMinutes) * hourHeight / 60.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(height: totalHeight)
|
||||
}
|
||||
|
||||
private func formatHm(_ minutes: Int) -> String {
|
||||
String(format: "%02d:%02d", minutes / 60, minutes % 60)
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func lessonBlock(_ lesson: WidgetLesson) -> some View {
|
||||
let cal = Calendar.current
|
||||
let comps = cal.dateComponents([.hour, .minute], from: lesson.start)
|
||||
let startMinutes = (comps.hour ?? 0) * 60 + (comps.minute ?? 0)
|
||||
let durationMinutes = max(15, Int(lesson.end.timeIntervalSince(lesson.start) / 60))
|
||||
let virtualStart = realMinutesToVirtual(startMinutes, periods: periods)
|
||||
let virtualEnd = realMinutesToVirtual(startMinutes + durationMinutes, periods: periods)
|
||||
|
||||
if virtualEnd > virtualStart {
|
||||
let top = virtualStart * hourHeight / 60.0 + LESSON_GAP / 2
|
||||
let height = max(
|
||||
MIN_BLOCK_HEIGHT,
|
||||
(virtualEnd - virtualStart) * hourHeight / 60.0 - LESSON_GAP
|
||||
)
|
||||
let subjectSize = subjectFont(forHourHeight: hourHeight)
|
||||
let secondarySize = max(MIN_SECONDARY_FONT, subjectSize - 2)
|
||||
let room = lesson.room
|
||||
let teacher = lesson.teacher ?? lesson.originalTeacher
|
||||
let hasSecondary = (room?.isEmpty == false) || (teacher?.isEmpty == false)
|
||||
HStack(alignment: .top, spacing: 4) {
|
||||
Text(subjectLabel(lesson))
|
||||
.font(.system(size: subjectSize, weight: .semibold))
|
||||
.foregroundStyle(.white)
|
||||
.lineLimit(hasSecondary ? 1 : 2)
|
||||
.minimumScaleFactor(0.5)
|
||||
if hasSecondary {
|
||||
Spacer(minLength: 0)
|
||||
VStack(alignment: .trailing, spacing: -1) {
|
||||
if showRoom && height >= BLOCK_SHOW_ROOM_MIN {
|
||||
if let room, !room.isEmpty {
|
||||
Text(room)
|
||||
.font(.system(size: secondarySize))
|
||||
.foregroundStyle(.white.opacity(0.85))
|
||||
.lineLimit(1)
|
||||
.minimumScaleFactor(0.5)
|
||||
}
|
||||
if showTeacher,
|
||||
height >= BLOCK_SHOW_TEACHER_SEPARATE_MIN,
|
||||
let teacher,
|
||||
!teacher.isEmpty {
|
||||
Text(teacher)
|
||||
.font(.system(size: secondarySize))
|
||||
.foregroundStyle(.white.opacity(0.7))
|
||||
.lineLimit(1)
|
||||
.minimumScaleFactor(0.5)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, horizontalPadding)
|
||||
.padding(.vertical, 3)
|
||||
.frame(maxWidth: .infinity, alignment: .topLeading)
|
||||
.frame(height: height, alignment: .topLeading)
|
||||
.background(blockColor(lesson))
|
||||
.cornerRadius(6)
|
||||
.overlay(alignment: .bottomLeading) {
|
||||
// Separate fixed-size badge so the +N hint stays readable
|
||||
// when the subject autoshrinks on narrow tiles.
|
||||
if let count = lesson.siblingCount, count > 0 {
|
||||
Text("+\(count)")
|
||||
.font(.system(size: 12, weight: .bold))
|
||||
.foregroundStyle(.white)
|
||||
.padding(.leading, horizontalPadding)
|
||||
.padding(.bottom, 2)
|
||||
}
|
||||
}
|
||||
.overlay {
|
||||
// CrossPainter parity: clip cross to the rounded shape so
|
||||
// the diagonals don't bleed past the corners.
|
||||
if lesson.status == .cancelled {
|
||||
ZStack {
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.stroke(Color.red.opacity(0.78), lineWidth: 1.5)
|
||||
GeometryReader { geo in
|
||||
Path { p in
|
||||
p.move(to: .zero)
|
||||
p.addLine(to: CGPoint(x: geo.size.width, y: geo.size.height))
|
||||
p.move(to: CGPoint(x: geo.size.width, y: 0))
|
||||
p.addLine(to: CGPoint(x: 0, y: geo.size.height))
|
||||
}
|
||||
.stroke(Color.red.opacity(0.78), lineWidth: 3)
|
||||
}
|
||||
}
|
||||
.clipShape(RoundedRectangle(cornerRadius: 6))
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, showRoom ? 2 : 1)
|
||||
.offset(y: top)
|
||||
}
|
||||
}
|
||||
|
||||
private var nowIndicator: some View {
|
||||
let cal = Calendar.current
|
||||
let comps = cal.dateComponents([.hour, .minute], from: Date())
|
||||
let nowMinutes = (comps.hour ?? 0) * 60 + (comps.minute ?? 0)
|
||||
let inside: Bool
|
||||
if let first = periods.first, let last = periods.last {
|
||||
inside = nowMinutes >= first.startMinutes && nowMinutes <= last.endMinutes
|
||||
} else {
|
||||
inside = true
|
||||
}
|
||||
let top = realMinutesToVirtual(nowMinutes, periods: periods) * hourHeight / 60.0
|
||||
return Group {
|
||||
if inside {
|
||||
Rectangle()
|
||||
.fill(Color.red)
|
||||
.frame(height: 2)
|
||||
.offset(y: top)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func subjectLabel(_ lesson: WidgetLesson) -> String {
|
||||
!lesson.subjectShort.isEmpty
|
||||
? lesson.subjectShort
|
||||
: (lesson.subjectLong ?? "—")
|
||||
}
|
||||
|
||||
/// Mirrors lesson_color.dart + custom_event_colors.dart so the widget
|
||||
/// matches the in-app calendar exactly.
|
||||
private func blockColor(_ lesson: WidgetLesson) -> Color {
|
||||
if lesson.status == .event, let custom = lesson.customColor {
|
||||
switch custom {
|
||||
case "orange": return Color(red: 239/255.0, green: 108/255.0, blue: 0/255.0)
|
||||
case "red": return Color(red: 153/255.0, green: 51/255.0, blue: 51/255.0)
|
||||
case "green": return Color(red: 76/255.0, green: 175/255.0, blue: 80/255.0)
|
||||
case "blue": return Color(red: 33/255.0, green: 150/255.0, blue: 243/255.0)
|
||||
default: break
|
||||
}
|
||||
}
|
||||
switch lesson.status {
|
||||
case .regular, .past: return Color(red: 153/255.0, green: 51/255.0, blue: 51/255.0)
|
||||
case .ongoing: return Color(red: 200/255.0, green: 51/255.0, blue: 51/255.0)
|
||||
case .cancelled: return .black
|
||||
case .irregular: return Color(red: 143/255.0, green: 25/255.0, blue: 179/255.0)
|
||||
case .teacherChanged: return Color(red: 41/255.0, green: 99/255.0, blue: 155/255.0)
|
||||
case .event: return Color(red: 239/255.0, green: 108/255.0, blue: 0/255.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Period boundaries deduped: adjacent periods share a line, periods on
|
||||
/// either side of a break get their own (bracketing the break block).
|
||||
func periodBoundaries(_ periods: [WidgetPeriod]) -> [Int] {
|
||||
var seen = Set<Int>()
|
||||
var result: [Int] = []
|
||||
for p in periods {
|
||||
for v in [p.virtualStartMinutes, p.virtualEndMinutes] {
|
||||
if seen.insert(v).inserted { result.append(v) }
|
||||
}
|
||||
}
|
||||
return result.sorted()
|
||||
}
|
||||
|
||||
func dayLabel(for date: Date) -> String {
|
||||
let cal = Calendar.current
|
||||
let today = cal.startOfDay(for: Date())
|
||||
let anchor = cal.startOfDay(for: date)
|
||||
if anchor == today {
|
||||
return "Heute · \(shortDate(date))"
|
||||
}
|
||||
if let tomorrow = cal.date(byAdding: .day, value: 1, to: today), anchor == tomorrow {
|
||||
return "Morgen · \(shortDate(date))"
|
||||
}
|
||||
let formatter = DateFormatter()
|
||||
formatter.locale = Locale(identifier: "de_DE")
|
||||
formatter.dateFormat = "EEEE · dd.MM."
|
||||
return formatter.string(from: date)
|
||||
}
|
||||
|
||||
func shortDate(_ date: Date) -> String {
|
||||
let f = DateFormatter()
|
||||
f.locale = Locale(identifier: "de_DE")
|
||||
f.dateFormat = "dd.MM."
|
||||
return f.string(from: date)
|
||||
}
|
||||
|
||||
func freshnessLabel(for fetchedAt: Date) -> String {
|
||||
let cal = Calendar.current
|
||||
let today = cal.startOfDay(for: Date())
|
||||
let fetchedDay = cal.startOfDay(for: fetchedAt)
|
||||
let timeFmt = DateFormatter()
|
||||
timeFmt.locale = Locale(identifier: "de_DE")
|
||||
timeFmt.dateFormat = "HH:mm"
|
||||
if fetchedDay == today {
|
||||
return timeFmt.string(from: fetchedAt)
|
||||
}
|
||||
if let yesterday = cal.date(byAdding: .day, value: -1, to: today),
|
||||
fetchedDay == yesterday {
|
||||
return "gestern \(timeFmt.string(from: fetchedAt))"
|
||||
}
|
||||
let dateTimeFmt = DateFormatter()
|
||||
dateTimeFmt.locale = Locale(identifier: "de_DE")
|
||||
dateTimeFmt.dateFormat = "dd.MM. HH:mm"
|
||||
return dateTimeFmt.string(from: fetchedAt)
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
import SwiftUI
|
||||
import WidgetKit
|
||||
|
||||
struct TimetableWeekView: View {
|
||||
let entry: TimetableEntry
|
||||
|
||||
var body: some View {
|
||||
ZStack {
|
||||
if !entry.isLoggedIn {
|
||||
placeholder("Bitte einloggen, um den Stundenplan zu laden")
|
||||
} else if let data = entry.data {
|
||||
content(data: data)
|
||||
} else {
|
||||
placeholder("Lade…")
|
||||
}
|
||||
}
|
||||
.background(MarianumWatermark())
|
||||
.widgetThemeOverride(entry.themeMode)
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func content(data: WidgetTimetableData) -> some View {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
header(data: data)
|
||||
dayHeaderRow(data: data)
|
||||
GeometryReader { geo in
|
||||
let totalMin = CGFloat(data.periods.last?.virtualEndMinutes ?? FALLBACK_VIRTUAL_MINUTES)
|
||||
let hourHeight = max(
|
||||
MIN_HOUR_HEIGHT,
|
||||
min(MAX_HOUR_HEIGHT, geo.size.height / max(totalMin, 60) * 60)
|
||||
)
|
||||
let dayColumnWidth = (geo.size.width - 28 - 4) / 5
|
||||
let subjectOnly = dayColumnWidth < 70
|
||||
HStack(alignment: .top, spacing: 0) {
|
||||
timeLabelsColumn(hourHeight: hourHeight, periods: data.periods)
|
||||
.frame(width: 28, alignment: .topTrailing)
|
||||
columnDivider
|
||||
ForEach(0..<5, id: \.self) { offset in
|
||||
column(
|
||||
data: data,
|
||||
offset: offset,
|
||||
hourHeight: hourHeight,
|
||||
subjectOnly: subjectOnly
|
||||
)
|
||||
.frame(maxWidth: .infinity)
|
||||
if offset < 4 { columnDivider }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var columnDivider: some View {
|
||||
Rectangle()
|
||||
.fill(Color.primary.opacity(0.13))
|
||||
.frame(width: 1)
|
||||
}
|
||||
|
||||
private func header(data: WidgetTimetableData) -> some View {
|
||||
let cal = Calendar.current
|
||||
let week = cal.component(.weekOfYear, from: data.anchorDate)
|
||||
let endDate = cal.date(byAdding: .day, value: 4, to: data.anchorDate) ?? data.anchorDate
|
||||
return HStack {
|
||||
Text("KW \(week) · \(shortDate(data.anchorDate))–\(shortDate(endDate))")
|
||||
.font(.system(size: 13, weight: .semibold))
|
||||
.foregroundStyle(.primary)
|
||||
Spacer()
|
||||
Text("Stand: \(freshnessLabel(for: data.fetchedAt))")
|
||||
.font(.system(size: 10))
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
|
||||
private func dayHeaderRow(data: WidgetTimetableData) -> some View {
|
||||
let cal = Calendar.current
|
||||
return HStack(spacing: 0) {
|
||||
Spacer().frame(width: 28)
|
||||
columnDivider
|
||||
ForEach(0..<5, id: \.self) { offset in
|
||||
let day = cal.date(byAdding: .day, value: offset, to: data.anchorDate) ?? data.anchorDate
|
||||
VStack(spacing: 0) {
|
||||
Text(weekday(for: day))
|
||||
.font(.system(size: 11, weight: .bold))
|
||||
.foregroundStyle(.primary)
|
||||
Text(shortDate(day))
|
||||
.font(.system(size: 9))
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
if offset < 4 { columnDivider }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func timeLabelsColumn(hourHeight: CGFloat, periods: [WidgetPeriod]) -> some View {
|
||||
let totalMin = periods.last?.virtualEndMinutes ?? FALLBACK_VIRTUAL_MINUTES
|
||||
let totalHeight = CGFloat(totalMin) * hourHeight / 60.0
|
||||
return ZStack(alignment: .topTrailing) {
|
||||
ForEach(periodBoundaries(periods), id: \.self) { virtualMin in
|
||||
Rectangle()
|
||||
.fill(Color.primary.opacity(0.08))
|
||||
.frame(height: 1)
|
||||
.offset(y: CGFloat(virtualMin) * hourHeight / 60.0)
|
||||
}
|
||||
ForEach(periods, id: \.startMinutes) { period in
|
||||
VStack(alignment: .trailing, spacing: -2) {
|
||||
Text(String(format: "%02d:%02d", period.startMinutes / 60, period.startMinutes % 60))
|
||||
.font(.system(size: 8))
|
||||
.foregroundStyle(.primary)
|
||||
.lineLimit(1)
|
||||
Text("\(period.name).")
|
||||
.font(.system(size: 6, weight: .bold))
|
||||
.foregroundStyle(.secondary)
|
||||
.lineLimit(1)
|
||||
}
|
||||
.offset(y: CGFloat(period.virtualStartMinutes) * hourHeight / 60.0)
|
||||
}
|
||||
}
|
||||
.frame(height: totalHeight, alignment: .topTrailing)
|
||||
}
|
||||
|
||||
private func column(
|
||||
data: WidgetTimetableData,
|
||||
offset: Int,
|
||||
hourHeight: CGFloat,
|
||||
subjectOnly: Bool
|
||||
) -> some View {
|
||||
let cal = Calendar.current
|
||||
let day = cal.date(byAdding: .day, value: offset, to: data.anchorDate) ?? data.anchorDate
|
||||
let lessonsForDay = data.lessons.filter { cal.isDate($0.start, inSameDayAs: day) }
|
||||
return TimeGridView(
|
||||
lessons: lessonsForDay,
|
||||
periods: data.periods,
|
||||
anchorDate: day,
|
||||
hourHeight: hourHeight,
|
||||
showRoom: !subjectOnly,
|
||||
showTeacher: !subjectOnly,
|
||||
showTimeLabels: false,
|
||||
horizontalPadding: 3
|
||||
)
|
||||
}
|
||||
|
||||
private func weekday(for date: Date) -> String {
|
||||
let f = DateFormatter()
|
||||
f.locale = Locale(identifier: "de_DE")
|
||||
f.dateFormat = "EE"
|
||||
return f.string(from: date)
|
||||
}
|
||||
|
||||
private func placeholder(_ message: String) -> some View {
|
||||
VStack(spacing: 4) {
|
||||
Text("Marianum Stundenplan")
|
||||
.font(.system(size: 14, weight: .semibold))
|
||||
Text(message)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>com.apple.security.application-groups</key>
|
||||
<array>
|
||||
<string>group.eu.mhsl.marianum.mobile.client.widget</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,138 @@
|
||||
import SwiftUI
|
||||
import WidgetKit
|
||||
|
||||
@main
|
||||
struct MarianumWidgetBundle: WidgetBundle {
|
||||
var body: some Widget {
|
||||
TimetableDayWidget()
|
||||
TimetableWeekWidget()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Day widget
|
||||
|
||||
struct TimetableDayWidget: Widget {
|
||||
let kind: String = "TimetableDayWidget"
|
||||
|
||||
var body: some WidgetConfiguration {
|
||||
StaticConfiguration(kind: kind, provider: TimetableDayProvider()) { entry in
|
||||
TimetableDayView(entry: entry).widgetContainerBackground()
|
||||
}
|
||||
.configurationDisplayName("Marianum · Heute")
|
||||
.description("Stundenplan und Vertretungen für den anstehenden Schultag.")
|
||||
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
|
||||
}
|
||||
}
|
||||
|
||||
struct TimetableDayProvider: TimelineProvider {
|
||||
func placeholder(in context: Context) -> TimetableEntry {
|
||||
TimetableEntry.placeholder()
|
||||
}
|
||||
|
||||
func getSnapshot(in context: Context, completion: @escaping (TimetableEntry) -> Void) {
|
||||
completion(TimetableEntry.current(variant: .day))
|
||||
}
|
||||
|
||||
func getTimeline(
|
||||
in context: Context,
|
||||
completion: @escaping (Timeline<TimetableEntry>) -> Void
|
||||
) {
|
||||
let entry = TimetableEntry.current(variant: .day)
|
||||
// 30 min mirrors the Dart workmanager cadence. iOS treats this as
|
||||
// advisory; the "Stand:" label tells the user when data is stale.
|
||||
let next = Calendar.current.date(byAdding: .minute, value: 30, to: Date()) ?? Date()
|
||||
completion(Timeline(entries: [entry], policy: .after(next)))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Week widget
|
||||
|
||||
struct TimetableWeekWidget: Widget {
|
||||
let kind: String = "TimetableWeekWidget"
|
||||
|
||||
var body: some WidgetConfiguration {
|
||||
StaticConfiguration(kind: kind, provider: TimetableWeekProvider()) { entry in
|
||||
TimetableWeekView(entry: entry).widgetContainerBackground()
|
||||
}
|
||||
.configurationDisplayName("Marianum · Woche")
|
||||
.description("Stundenplan und Vertretungen für die ganze Schulwoche.")
|
||||
.supportedFamilies([.systemMedium, .systemLarge, .systemExtraLarge])
|
||||
}
|
||||
}
|
||||
|
||||
struct TimetableWeekProvider: TimelineProvider {
|
||||
func placeholder(in context: Context) -> TimetableEntry {
|
||||
TimetableEntry.placeholder()
|
||||
}
|
||||
|
||||
func getSnapshot(in context: Context, completion: @escaping (TimetableEntry) -> Void) {
|
||||
completion(TimetableEntry.current(variant: .week))
|
||||
}
|
||||
|
||||
func getTimeline(
|
||||
in context: Context,
|
||||
completion: @escaping (Timeline<TimetableEntry>) -> Void
|
||||
) {
|
||||
let entry = TimetableEntry.current(variant: .week)
|
||||
let next = Calendar.current.date(byAdding: .minute, value: 30, to: Date()) ?? Date()
|
||||
completion(Timeline(entries: [entry], policy: .after(next)))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Entry
|
||||
|
||||
enum TimetableVariant { case day, week }
|
||||
|
||||
struct TimetableEntry: TimelineEntry {
|
||||
let date: Date
|
||||
let variant: TimetableVariant
|
||||
let data: WidgetTimetableData?
|
||||
let isLoggedIn: Bool
|
||||
let themeMode: String
|
||||
|
||||
static func placeholder() -> TimetableEntry {
|
||||
TimetableEntry(
|
||||
date: Date(),
|
||||
variant: .day,
|
||||
data: nil,
|
||||
isLoggedIn: true,
|
||||
themeMode: "system"
|
||||
)
|
||||
}
|
||||
|
||||
static func current(variant: TimetableVariant) -> TimetableEntry {
|
||||
let isLoggedIn = WidgetDataLoader.isLoggedIn()
|
||||
let data = isLoggedIn
|
||||
? (variant == .day ? WidgetDataLoader.loadDay() : WidgetDataLoader.loadWeek())
|
||||
: nil
|
||||
return TimetableEntry(
|
||||
date: Date(),
|
||||
variant: variant,
|
||||
data: data,
|
||||
isLoggedIn: isLoggedIn,
|
||||
themeMode: WidgetDataLoader.themeMode()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension View {
|
||||
@ViewBuilder
|
||||
func widgetThemeOverride(_ mode: String) -> some View {
|
||||
switch mode {
|
||||
case "light": self.environment(\.colorScheme, .light)
|
||||
case "dark": self.environment(\.colorScheme, .dark)
|
||||
default: self
|
||||
}
|
||||
}
|
||||
|
||||
/// `.containerBackground(_:for:)` is iOS 17+. Older iOS uses the
|
||||
/// implicit `.background(...)` model and renders fine without it.
|
||||
@ViewBuilder
|
||||
func widgetContainerBackground() -> some View {
|
||||
if #available(iOS 17.0, *) {
|
||||
self.containerBackground(.fill.tertiary, for: .widget)
|
||||
} else {
|
||||
self
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
import Foundation
|
||||
|
||||
/// Mirrors lib/widget_data/widget_data.dart. JSON keys must stay in sync —
|
||||
/// the bridge is one-way: Dart writes, Swift reads.
|
||||
enum WidgetLessonStatus: String, Codable {
|
||||
case regular
|
||||
case ongoing
|
||||
case past
|
||||
case cancelled
|
||||
case irregular
|
||||
case teacherChanged
|
||||
case event
|
||||
}
|
||||
|
||||
struct WidgetLesson: Codable {
|
||||
let start: Date
|
||||
let end: Date
|
||||
let subjectShort: String
|
||||
let subjectLong: String?
|
||||
let room: String?
|
||||
let teacher: String?
|
||||
let originalTeacher: String?
|
||||
let status: WidgetLessonStatus
|
||||
let customColor: String?
|
||||
let siblingCount: Int?
|
||||
}
|
||||
|
||||
struct WidgetPeriod: Codable {
|
||||
let name: String
|
||||
let startMinutes: Int
|
||||
let endMinutes: Int
|
||||
let virtualStartMinutes: Int
|
||||
let virtualEndMinutes: Int
|
||||
}
|
||||
|
||||
struct WidgetTimetableData: Codable {
|
||||
let fetchedAt: Date
|
||||
let anchorDate: Date
|
||||
let lessons: [WidgetLesson]
|
||||
let periods: [WidgetPeriod]
|
||||
let isHoliday: Bool
|
||||
let holidayName: String?
|
||||
}
|
||||
|
||||
enum WidgetDataKey {
|
||||
static let appGroupId = "group.eu.mhsl.marianum.mobile.client.widget"
|
||||
static let dayData = "widget_data_day_v1"
|
||||
static let weekData = "widget_data_week_v1"
|
||||
static let loggedIn = "widget_data_logged_in_v1"
|
||||
static let themeMode = "widget_setting_theme_mode_v1"
|
||||
}
|
||||
|
||||
enum WidgetDataLoader {
|
||||
/// Dart's `DateTime.toIso8601String()` on a non-UTC DateTime drops the
|
||||
/// trailing Z and ships local wall-clock time. ISO8601DateFormatter's
|
||||
/// default treats that as UTC and shifts every lesson by the local TZ
|
||||
/// offset — dispatch by suffix instead, mirroring WidgetDataParser.kt.
|
||||
private static func parseDartDate(_ raw: String) -> Date? {
|
||||
let hasTzSuffix = raw.hasSuffix("Z")
|
||||
|| raw.range(of: #"[+-]\d{2}:?\d{2}$"#, options: .regularExpression) != nil
|
||||
if hasTzSuffix {
|
||||
let iso = ISO8601DateFormatter()
|
||||
iso.formatOptions = [.withFullDate, .withFullTime, .withFractionalSeconds]
|
||||
if let d = iso.date(from: raw) { return d }
|
||||
iso.formatOptions = [.withFullDate, .withFullTime]
|
||||
return iso.date(from: raw)
|
||||
}
|
||||
for pattern in [
|
||||
"yyyy-MM-dd'T'HH:mm:ss.SSSSSS",
|
||||
"yyyy-MM-dd'T'HH:mm:ss.SSS",
|
||||
"yyyy-MM-dd'T'HH:mm:ss",
|
||||
] {
|
||||
let f = DateFormatter()
|
||||
f.dateFormat = pattern
|
||||
f.timeZone = TimeZone.current
|
||||
f.locale = Locale(identifier: "en_US_POSIX")
|
||||
if let d = f.date(from: raw) { return d }
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private static func decoder() -> JSONDecoder {
|
||||
let dec = JSONDecoder()
|
||||
dec.dateDecodingStrategy = .custom { decoder in
|
||||
let container = try decoder.singleValueContainer()
|
||||
let raw = try container.decode(String.self)
|
||||
if let d = parseDartDate(raw) { return d }
|
||||
throw DecodingError.dataCorruptedError(
|
||||
in: container,
|
||||
debugDescription: "Unparseable date: \(raw)"
|
||||
)
|
||||
}
|
||||
return dec
|
||||
}
|
||||
|
||||
static func loadDay() -> WidgetTimetableData? {
|
||||
load(key: WidgetDataKey.dayData)
|
||||
}
|
||||
|
||||
static func loadWeek() -> WidgetTimetableData? {
|
||||
load(key: WidgetDataKey.weekData)
|
||||
}
|
||||
|
||||
static func isLoggedIn() -> Bool {
|
||||
let defaults = UserDefaults(suiteName: WidgetDataKey.appGroupId)
|
||||
return defaults?.bool(forKey: WidgetDataKey.loggedIn) ?? false
|
||||
}
|
||||
|
||||
/// "light" / "dark" / "system". The view's `.environment(\.colorScheme)`
|
||||
/// reads this so the App's theme choice wins over the OS-level setting.
|
||||
static func themeMode() -> String {
|
||||
let defaults = UserDefaults(suiteName: WidgetDataKey.appGroupId)
|
||||
return defaults?.string(forKey: WidgetDataKey.themeMode) ?? "system"
|
||||
}
|
||||
|
||||
private static func load(key: String) -> WidgetTimetableData? {
|
||||
guard let defaults = UserDefaults(suiteName: WidgetDataKey.appGroupId),
|
||||
let raw = defaults.string(forKey: key),
|
||||
let data = raw.data(using: .utf8) else {
|
||||
return nil
|
||||
}
|
||||
do {
|
||||
return try decoder().decode(WidgetTimetableData.self, from: data)
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user