Phase 3d: Wetter-Widget (WeatherKit-Entitlement noch offen)

Eigene WeatherCondition statt WeatherKits Typ: die Widget-Ebene soll nicht an
ein Framework gebunden sein, das eine kostenpflichtige Mitgliedschaft und eine
Entitlement voraussetzt. Fällt WeatherKit weg, wird nur die Abbildung getauscht.

Der Zwischenspeicher ist Teil der Funktion, kein Feinschliff: WeatherKit rechnet
nach Abrufen ab, und ein Widget, das bei jedem Öffnen des Panels neu lädt,
verbrennt das Kontingent für Daten, die sich stündlich ändern. 15 Minuten
Gültigkeit, und der Schlüssel führt den Ort mit — sonst zeigt das Widget nach
einem Ortswechsel weiter das alte Wetter, überzeugend, weil Zahlen dastehen.

Ein abgelaufener Stand bleibt als "letzter bekannter" erhalten. Bei einem
Netzfehler ist ein alter Messwert mit sichtbarem Zeitstempel ehrlicher und
nützlicher als ein leeres Feld; er wird abgeblendet und mit Warnzeichen gezeigt.

Apples Namensnennung steht im Datenmodell, nicht in einer Notiz: sie ist
Bedingung der Nutzung von WeatherKit und wird sonst beim Bauen der Oberfläche
übersehen. In der 2x1-Kachel als klickbarer Link, in der 1x1 im Tooltip.

Temperaturen ohne Nachkommastellen — die täuschen eine Genauigkeit vor, die
die Vorhersage nicht hat.

NICHT aktiviert: com.apple.developer.weatherkit. Die Entitlement erzwingt ein
echtes Provisioning-Profil, und die App-ID lässt sich nicht registrieren
("cannot be registered to your development team because it is not available").
Bis das im Portal geklärt ist, bleibt sie auskommentiert — die App baut und
läuft, das Widget meldet ehrlich "Wetter nicht abrufbar".

106 Tests grün.
This commit is contained in:
Guido Schmit
2026-08-10 20:33:32 +02:00
parent d345a4fcab
commit fc92d226b4
12 changed files with 868 additions and 2 deletions

View File

@@ -0,0 +1,31 @@
{
"sourceLanguage" : "en",
"strings" : {
"widget.weather.name" : {
"localizations" : {
"de" : { "stringUnit" : { "state" : "translated", "value" : "Wetter" } },
"en" : { "stringUnit" : { "state" : "translated", "value" : "Weather" } }
}
},
"widget.weather.asOf" : {
"comment" : "%@ ist die Uhrzeit des Messwerts",
"localizations" : {
"de" : { "stringUnit" : { "state" : "translated", "value" : "Stand %@" } },
"en" : { "stringUnit" : { "state" : "translated", "value" : "As of %@" } }
}
},
"widget.weather.unavailable" : {
"localizations" : {
"de" : { "stringUnit" : { "state" : "translated", "value" : "Wetter nicht abrufbar" } },
"en" : { "stringUnit" : { "state" : "translated", "value" : "Weather unavailable" } }
}
},
"widget.weather.locationDenied" : {
"localizations" : {
"de" : { "stringUnit" : { "state" : "translated", "value" : "Kein Standortzugriff.\nOrt in den Einstellungen wählen." } },
"en" : { "stringUnit" : { "state" : "translated", "value" : "No location access.\nPick a place in settings." } }
}
}
},
"version" : "1.0"
}

View File

@@ -0,0 +1,79 @@
import Foundation
/// Hält den zuletzt geladenen Wetterstand je Ort.
///
/// WeatherKit rechnet nach Abrufen ab. Ein Widget, das bei jedem Öffnen des
/// Panels neu lädt, verbrennt das Kontingent für Daten, die sich stündlich
/// ändern der Zwischenspeicher ist deshalb Teil der Funktion und kein
/// Feinschliff.
public struct WeatherCache: Sendable {
/// Wie lange ein Stand als aktuell gilt.
public static let maxAge: TimeInterval = 15 * 60
private var entries: [String: WeatherSnapshot] = [:]
public init() {}
/// Der Stand, wenn er noch frisch genug ist.
public func valid(for place: WeatherPlace, now: Date = Date()) -> WeatherSnapshot? {
guard let entry = entries[Self.key(place)] else { return nil }
return now.timeIntervalSince(entry.asOf) <= Self.maxAge ? entry : nil
}
/// Der letzte bekannte Stand, unabhängig vom Alter.
///
/// Bei einem Netzfehler ist ein alter Messwert mit sichtbarem Zeitstempel
/// deutlich besser als ein leeres Feld man sieht, dass etwas da ist und
/// wie alt es ist.
public func lastKnown(for place: WeatherPlace) -> WeatherSnapshot? {
entries[Self.key(place)]
}
public mutating func store(_ snapshot: WeatherSnapshot, for place: WeatherPlace) {
entries[Self.key(place)] = snapshot
}
/// Der Schlüssel muss den Ort mitführen, sonst zeigt das Widget nach einem
/// Ortswechsel weiter das alte Wetter und zwar überzeugend, weil Zahlen
/// dastehen.
private static func key(_ place: WeatherPlace) -> String {
switch place {
case .current:
"current"
case .fixed(_, let latitude, let longitude):
// Auf vier Nachkommastellen gerundet: das sind gut zehn Meter,
// feiner unterscheidet sich das Wetter ohnehin nicht.
String(format: "%.4f,%.4f", latitude, longitude)
}
}
}
public extension WeatherCondition {
/// Das SF-Symbol zur Lage.
///
/// Nur dort zwischen Tag und Nacht unterscheiden, wo es ein etabliertes
/// Symbolpaar gibt. Eine erzwungene Unterscheidung erzeugt sonst Symbole,
/// die niemand wiedererkennt.
func symbolName(isDaylight: Bool) -> String {
switch self {
case .clear: isDaylight ? "sun.max.fill" : "moon.stars.fill"
case .mostlyClear: isDaylight ? "sun.min.fill" : "moon.fill"
case .partlyCloudy: isDaylight ? "cloud.sun.fill" : "cloud.moon.fill"
case .cloudy: "cloud.fill"
case .fog: "cloud.fog.fill"
case .drizzle: "cloud.drizzle.fill"
case .rain: "cloud.rain.fill"
case .heavyRain: "cloud.heavyrain.fill"
case .sleet: "cloud.sleet.fill"
case .snow: "cloud.snow.fill"
case .heavySnow: "snowflake"
case .hail: "cloud.hail.fill"
case .thunderstorm: "cloud.bolt.rain.fill"
case .windy: "wind"
case .hot: "thermometer.sun.fill"
case .frigid: "thermometer.snowflake"
case .unknown: "questionmark.circle"
}
}
}

View File

@@ -0,0 +1,189 @@
import Foundation
import OSLog
import CoreLocation
import WeatherKit
private let log = Logger(subsystem: "com.scarriffleservices.onyx", category: "Weather")
/// Liefert den aktuellen Standort, oder sagt warum nicht.
///
/// Eigene Klasse statt eines Aufrufs mittendrin: `CLLocationManager` meldet
/// asynchron über einen Delegaten, und die Berechtigung kann sich jederzeit
/// ändern. Beides gehört an eine Stelle.
@MainActor
final class LocationProvider: NSObject, CLLocationManagerDelegate {
private let manager = CLLocationManager()
private var pending: [CheckedContinuation<CLLocation?, Never>] = []
override init() {
super.init()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyKilometer
}
var authorization: CLAuthorizationStatus { manager.authorizationStatus }
var isDenied: Bool {
authorization == .denied || authorization == .restricted
}
func requestAuthorization() {
guard authorization == .notDetermined else { return }
manager.requestWhenInUseAuthorization()
}
/// Eine einzelne Ortung. Liefert `nil`, wenn keine Berechtigung vorliegt
/// oder nichts hereinkommt.
func currentLocation() async -> CLLocation? {
guard !isDenied else { return nil }
if authorization == .notDetermined {
requestAuthorization()
// Auf die Entscheidung des Nutzers warten hieße, hier zu blockieren.
// Beim nächsten Durchlauf steht sie fest.
return nil
}
if let cached = manager.location, cached.timestamp.timeIntervalSinceNow > -900 {
return cached
}
return await withCheckedContinuation { continuation in
pending.append(continuation)
manager.requestLocation()
}
}
private func resume(with location: CLLocation?) {
let waiting = pending
pending.removeAll()
waiting.forEach { $0.resume(returning: location) }
}
nonisolated func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
Task { @MainActor in self.resume(with: locations.last) }
}
nonisolated func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) {
Task { @MainActor in
log.error("Ortung fehlgeschlagen: \(error.localizedDescription, privacy: .public)")
self.resume(with: nil)
}
}
}
/// Wetter aus Apples WeatherKit.
@MainActor
public final class WeatherKitSource {
private let service = WeatherService.shared
private let location = LocationProvider()
private let geocoder = CLGeocoder()
public init() {}
public var locationIsDenied: Bool { location.isDenied }
public func requestLocationAuthorization() { location.requestAuthorization() }
public func load(_ place: WeatherPlace) async -> WeatherState {
let coordinate: CLLocation
let placeName: String
switch place {
case .current:
guard let here = await location.currentLocation() else {
return location.isDenied
? .locationDenied
: .unavailable(lastKnown: nil, reason: "Standort nicht verfügbar")
}
coordinate = here
placeName = await name(for: here)
case .fixed(let name, let latitude, let longitude):
coordinate = CLLocation(latitude: latitude, longitude: longitude)
placeName = name
}
do {
let weather = try await service.weather(for: coordinate)
return .ready(Self.snapshot(from: weather,
placeName: placeName,
attribution: try? await attribution()))
} catch {
log.error("WeatherKit: \(error.localizedDescription, privacy: .public)")
return .unavailable(lastKnown: nil, reason: error.localizedDescription)
}
}
/// Apples Namensnennung. Keine Kür sie ist Bedingung der Nutzung.
private func attribution() async throws -> WeatherAttribution {
let legal = try await service.attribution
return WeatherAttribution(name: "Apple Weather",
legalPageURL: legal.legalPageURL,
logoURL: legal.combinedMarkDarkURL)
}
private func name(for location: CLLocation) async -> String {
guard let placemark = try? await geocoder.reverseGeocodeLocation(location).first else {
return "Aktueller Ort"
}
return placemark.locality ?? placemark.subAdministrativeArea
?? placemark.administrativeArea ?? "Aktueller Ort"
}
// MARK: - Umwandlung
private static func snapshot(from weather: Weather,
placeName: String,
attribution: WeatherAttribution?) -> WeatherSnapshot {
let current = weather.currentWeather
let today = weather.dailyForecast.first
return WeatherSnapshot(
placeName: placeName,
temperature: current.temperature,
apparentTemperature: current.apparentTemperature,
condition: map(current.condition),
isDaylight: current.isDaylight,
humidity: current.humidity,
windSpeed: current.wind.speed,
high: today?.highTemperature,
low: today?.lowTemperature,
hourly: weather.hourlyForecast.forecast
.filter { $0.date >= Date() }
.prefix(12)
.map {
HourlyPoint(date: $0.date,
temperature: $0.temperature,
condition: map($0.condition),
precipitationChance: $0.precipitationChance)
},
asOf: current.date,
attribution: attribution)
}
/// WeatherKit kennt deutlich mehr Lagen als hier unterschieden werden.
/// Feiner aufzulösen bringt nichts: das Widget hat ein Symbol und ein Wort
/// Platz, und mäßiger Sprühregen" liest dort niemand.
private static func map(_ condition: WeatherKit.WeatherCondition) -> WeatherCondition {
switch condition {
case .clear: .clear
case .mostlyClear: .mostlyClear
case .partlyCloudy, .mostlyCloudy: .partlyCloudy
case .cloudy: .cloudy
case .foggy, .haze, .smoky: .fog
case .drizzle, .freezingDrizzle: .drizzle
case .rain, .sunShowers, .freezingRain: .rain
case .heavyRain: .heavyRain
case .sleet, .wintryMix, .hail: .sleet
case .snow, .flurries, .sunFlurries, .blowingSnow: .snow
case .heavySnow, .blizzard: .heavySnow
case .thunderstorms, .isolatedThunderstorms, .scatteredThunderstorms,
.strongStorms, .tropicalStorm, .hurricane: .thunderstorm
case .windy, .breezy, .blowingDust: .windy
case .hot: .hot
case .frigid: .frigid
@unknown default: .unknown
}
}
}

View File

@@ -0,0 +1,125 @@
import Foundation
/// Wetterlage, unabhängig von der Quelle.
///
/// Eine eigene Aufzählung statt WeatherKits Typ: die Widget-Ebene soll nicht
/// gegen ein Framework gebunden sein, das eine kostenpflichtige Mitgliedschaft
/// und eine Entitlement voraussetzt. Fällt WeatherKit einmal weg, wird nur die
/// Abbildung ausgetauscht.
public enum WeatherCondition: String, Equatable, Sendable, CaseIterable {
case clear
case mostlyClear
case partlyCloudy
case cloudy
case fog
case drizzle
case rain
case heavyRain
case sleet
case snow
case heavySnow
case hail
case thunderstorm
case windy
case hot
case frigid
case unknown
}
/// Der Ort, für den Wetter angezeigt wird.
public enum WeatherPlace: Equatable, Sendable, Codable {
/// Aktueller Standort über CoreLocation.
case current
/// Fester Ort, in den Widget-Einstellungen gewählt.
case fixed(name: String, latitude: Double, longitude: Double)
public var isCurrent: Bool { self == .current }
}
public struct HourlyPoint: Equatable, Sendable, Identifiable {
public let date: Date
public let temperature: Measurement<UnitTemperature>
public let condition: WeatherCondition
public let precipitationChance: Double
public var id: Date { date }
public init(date: Date, temperature: Measurement<UnitTemperature>,
condition: WeatherCondition, precipitationChance: Double) {
self.date = date
self.temperature = temperature
self.condition = condition
self.precipitationChance = precipitationChance
}
}
/// Was Apple bei Nutzung von WeatherKit sichtbar verlangt.
///
/// Das ist keine Kür: die Nennung samt Link auf die Rechtshinweise ist
/// Bedingung der Nutzung. Deshalb steht sie im Datenmodell und nicht in einer
/// Notiz, die man beim Bauen der Oberfläche übersieht.
public struct WeatherAttribution: Equatable, Sendable {
public let name: String
public let legalPageURL: URL
public let logoURL: URL?
public init(name: String, legalPageURL: URL, logoURL: URL?) {
self.name = name
self.legalPageURL = legalPageURL
self.logoURL = logoURL
}
}
public struct WeatherSnapshot: Equatable, Sendable {
public let placeName: String
public let temperature: Measurement<UnitTemperature>
public let apparentTemperature: Measurement<UnitTemperature>
public let condition: WeatherCondition
public let isDaylight: Bool
public let humidity: Double
public let windSpeed: Measurement<UnitSpeed>
public let high: Measurement<UnitTemperature>?
public let low: Measurement<UnitTemperature>?
public let hourly: [HourlyPoint]
public let asOf: Date
public let attribution: WeatherAttribution?
public init(placeName: String,
temperature: Measurement<UnitTemperature>,
apparentTemperature: Measurement<UnitTemperature>,
condition: WeatherCondition,
isDaylight: Bool,
humidity: Double,
windSpeed: Measurement<UnitSpeed>,
high: Measurement<UnitTemperature>?,
low: Measurement<UnitTemperature>?,
hourly: [HourlyPoint],
asOf: Date,
attribution: WeatherAttribution?) {
self.placeName = placeName
self.temperature = temperature
self.apparentTemperature = apparentTemperature
self.condition = condition
self.isDaylight = isDaylight
self.humidity = humidity
self.windSpeed = windSpeed
self.high = high
self.low = low
self.hourly = hourly
self.asOf = asOf
self.attribution = attribution
}
}
/// Wie beim Kalender: jeder Fehlerfall bekommt eine eigene Antwort in der
/// Oberfläche. Keine Daten" ist keine.
public enum WeatherState: Equatable, Sendable {
case ready(WeatherSnapshot)
/// Noch nie geladen.
case idle
case loading
/// Ortungsberechtigung fehlt betrifft nur `.current`.
case locationDenied
/// Kein Netz oder Dienst nicht erreichbar. Der letzte Stand bleibt sichtbar.
case unavailable(lastKnown: WeatherSnapshot?, reason: String)
}

View File

@@ -0,0 +1,264 @@
import SwiftUI
import AppKit
import OnyxDesign
import OnyxWidgetKit
/// Hält den Wetterstand für das Widget.
@MainActor
@Observable
public final class WeatherModel {
public private(set) var state: WeatherState = .idle
public var place: WeatherPlace {
didSet { guard place != oldValue else { return }; load(force: true) }
}
private let source: WeatherKitSource
private var cache = WeatherCache()
private var task: Task<Void, Never>?
public init(source: WeatherKitSource = WeatherKitSource(), place: WeatherPlace = .current) {
self.source = source
self.place = place
}
public var locationIsDenied: Bool { source.locationIsDenied }
public func requestLocationAuthorization() { source.requestLocationAuthorization() }
/// Lädt, wenn der Zwischenspeicher abgelaufen ist.
///
/// `force` gilt nur für einen Ortswechsel nicht für jedes Öffnen des
/// Panels. WeatherKit rechnet nach Abrufen ab, und das Wetter ändert sich
/// nicht im Sekundentakt.
public func load(force: Bool = false) {
if !force, let fresh = cache.valid(for: place) {
state = .ready(fresh)
return
}
task?.cancel()
let place = place
task = Task { [weak self] in
guard let self else { return }
if case .ready = state {} else { state = .loading }
let result = await source.load(place)
guard !Task.isCancelled else { return }
switch result {
case .ready(let snapshot):
cache.store(snapshot, for: place)
state = .ready(snapshot)
case .unavailable(_, let reason):
// Der letzte bekannte Stand bleibt sichtbar. Ein alter Messwert
// mit Zeitstempel ist ehrlicher und nützlicher als ein leeres Feld.
state = .unavailable(lastKnown: cache.lastKnown(for: place), reason: reason)
default:
state = result
}
}
}
public func stop() {
task?.cancel()
task = nil
}
}
// MARK: - Widget
public struct WeatherWidget: OnyxWidget {
public let id = "weather"
public var displayName: String { String(localized: "widget.weather.name", bundle: .module) }
public let symbolName = "cloud.sun"
public let supportedSizes: [WidgetSize] = [.small, .medium]
private let model: WeatherModel
public init(model: WeatherModel) { self.model = model }
public func makeView(size: WidgetSize) -> AnyView {
AnyView(WeatherWidgetView(model: model, size: size))
}
}
private struct WeatherWidgetView: View {
let model: WeatherModel
let size: WidgetSize
var body: some View {
Group {
switch model.state {
case .ready(let snapshot):
content(snapshot, isStale: false)
case .unavailable(let lastKnown, _):
if let lastKnown {
content(lastKnown, isStale: true)
} else {
Notice(symbol: "wifi.slash", text: "widget.weather.unavailable")
}
case .locationDenied:
Notice(symbol: "location.slash", text: "widget.weather.locationDenied")
case .loading, .idle:
ProgressView()
.controlSize(.small)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.task { model.load() }
.onDisappear { model.stop() }
}
@ViewBuilder
private func content(_ snapshot: WeatherSnapshot, isStale: Bool) -> some View {
if size == .small {
CompactWeather(snapshot: snapshot, isStale: isStale)
} else {
WideWeather(snapshot: snapshot, isStale: isStale)
}
}
}
private struct Notice: View {
let symbol: String
let text: LocalizedStringKey
var body: some View {
VStack(spacing: 6) {
Image(systemName: symbol)
.font(.system(size: 15))
.foregroundStyle(Onyx.Color.textTertiary)
Text(text, bundle: .module)
.font(Onyx.Font.caption)
.foregroundStyle(Onyx.Color.textSecondary)
.multilineTextAlignment(.center)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
private struct CompactWeather: View {
let snapshot: WeatherSnapshot
let isStale: Bool
var body: some View {
VStack(alignment: .leading, spacing: 2) {
Image(systemName: snapshot.condition.symbolName(isDaylight: snapshot.isDaylight))
.font(.system(size: 18))
.foregroundStyle(Onyx.Color.textPrimary)
.symbolRenderingMode(.hierarchical)
Spacer(minLength: 0)
Text(snapshot.temperature.onyxFormatted)
.font(Onyx.Font.metric)
.foregroundStyle(Onyx.Color.textPrimary)
Text(snapshot.placeName)
.font(.system(size: 9))
.foregroundStyle(Onyx.Color.textTertiary)
.lineLimit(1)
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.opacity(isStale ? 0.55 : 1)
.help(helpText)
}
private var helpText: Text {
// Apples Namensnennung ist Bedingung der Nutzung. In der 1×1-Kachel ist
// dafür kein Platz sie steht hier und zusätzlich in den Einstellungen.
Text(verbatim: [snapshot.placeName,
snapshot.attribution.map { "Wetterdaten: \($0.name)" }]
.compactMap { $0 }.joined(separator: " · "))
}
}
private struct WideWeather: View {
let snapshot: WeatherSnapshot
let isStale: Bool
var body: some View {
VStack(alignment: .leading, spacing: 6) {
HStack(alignment: .top, spacing: 8) {
Image(systemName: snapshot.condition.symbolName(isDaylight: snapshot.isDaylight))
.font(.system(size: 22))
.symbolRenderingMode(.hierarchical)
.foregroundStyle(Onyx.Color.textPrimary)
VStack(alignment: .leading, spacing: 1) {
Text(snapshot.temperature.onyxFormatted)
.font(Onyx.Font.metric)
.foregroundStyle(Onyx.Color.textPrimary)
Text(snapshot.placeName)
.font(.system(size: 10))
.foregroundStyle(Onyx.Color.textSecondary)
.lineLimit(1)
}
Spacer(minLength: 0)
if let high = snapshot.high, let low = snapshot.low {
VStack(alignment: .trailing, spacing: 1) {
Text("\(high.onyxFormatted)")
Text("\(low.onyxFormatted)")
}
.font(Onyx.Font.metricSmall)
.foregroundStyle(Onyx.Color.textTertiary)
}
}
if !snapshot.hourly.isEmpty {
HStack(spacing: 0) {
ForEach(snapshot.hourly.prefix(5)) { point in
VStack(spacing: 2) {
Text(point.date.formatted(.dateTime.hour()))
.font(.system(size: 8))
.foregroundStyle(Onyx.Color.textTertiary)
Image(systemName: point.condition.symbolName(isDaylight: true))
.font(.system(size: 9))
.foregroundStyle(Onyx.Color.textSecondary)
Text(point.temperature.onyxFormatted)
.font(.system(size: 9))
.monospacedDigit()
.foregroundStyle(Onyx.Color.textSecondary)
}
.frame(maxWidth: .infinity)
}
}
}
Spacer(minLength: 0)
HStack(spacing: 4) {
if isStale {
Image(systemName: "exclamationmark.triangle.fill")
.font(.system(size: 8))
.foregroundStyle(Onyx.Color.warning)
}
Text(stampText)
.font(.system(size: 8))
.foregroundStyle(Onyx.Color.textTertiary)
Spacer(minLength: 0)
if let attribution = snapshot.attribution {
// Pflichtangabe, klickbar auf Apples Rechtshinweise.
Link(attribution.name, destination: attribution.legalPageURL)
.font(.system(size: 8))
.foregroundStyle(Onyx.Color.textTertiary)
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
}
private var stampText: String {
let time = snapshot.asOf.formatted(date: .omitted, time: .shortened)
return String(format: String(localized: "widget.weather.asOf", bundle: .module), time)
}
}
extension Measurement where UnitType == UnitTemperature {
/// Ganzzahlig und in der Einheit, die zur Region des Nutzers passt.
/// Nachkommastellen bei der Außentemperatur täuschen eine Genauigkeit vor,
/// die die Vorhersage nicht hat.
var onyxFormatted: String {
formatted(.measurement(width: .narrow,
usage: .weather,
numberFormatStyle: .number.precision(.fractionLength(0))))
}
}