Berechtigungen zeigen ihren Zustand live, Dock-Symbol wird wählbar
Die Einstellungen zeigten weiter "Anfragen", obwohl die Ortung längst lief: authorizationStatus ist eine Abfrage, keine Beobachtung, und ich hatte sie einmalig beim Öffnen gelesen. Jetzt meldet locationManagerDidChangeAuthorization die Änderung, WeatherModel führt daraus einen beobachtbaren Wert, und die Einstellungen aktualisieren sich, während das Fenster offen steht. Nach der Zustimmung wird sofort geladen statt beim nächsten Anlauf. Der Knopf "Systemeinstellungen öffnen" ganz unten zeigte fest auf den Kalenderbereich — wer ihn beim Standort drückte, landete am falschen Ort. Es gibt ihn nicht mehr; stattdessen führt jede Berechtigung in ihren eigenen Bereich, und der Knopf erscheint nur im Zustand "abgelehnt". Bei "noch nie gefragt" steht dort Anfragen, bei "erteilt" gar nichts: drei Zustände, drei Antworten. Genau daran ist die erste Fassung gescheitert. Dock-Symbol ist jetzt eine Einstellung, standardmäßig aus. Onyx läuft als LSUIElement — das war Absicht und keine Auslassung: es hat kein Hauptfenster und wäre im Dock ein Symbol, das beim Anklicken nichts öffnet, und es stünde im Programmumschalter zwischen den Programmen, mit denen man arbeitet. Wer es trotzdem will, schaltet es ein; die Wahl wird beim Start wieder angewendet.
This commit is contained in:
@@ -15,6 +15,8 @@ final class LocationProvider: NSObject, CLLocationManagerDelegate {
|
||||
|
||||
private let manager = CLLocationManager()
|
||||
private var pending: [CheckedContinuation<CLLocation?, Never>] = []
|
||||
/// Wird gerufen, wenn der Nutzer die Erlaubnis erteilt oder entzieht.
|
||||
var onAuthorizationChange: (() -> Void)?
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
@@ -59,6 +61,14 @@ final class LocationProvider: NSObject, CLLocationManagerDelegate {
|
||||
Task { @MainActor in self.resume(with: locations.last) }
|
||||
}
|
||||
|
||||
/// Ohne diesen Rückruf merkt niemand, dass der Nutzer gerade zugestimmt
|
||||
/// hat: `authorizationStatus` ist eine Abfrage, keine Beobachtung. Die
|
||||
/// Einstellungen zeigten deshalb weiter „Anfragen", obwohl die Ortung
|
||||
/// längst lief.
|
||||
nonisolated func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
|
||||
Task { @MainActor in self.onAuthorizationChange?() }
|
||||
}
|
||||
|
||||
nonisolated func locationManager(_ manager: CLLocationManager,
|
||||
didFailWithError error: Error) {
|
||||
Task { @MainActor in
|
||||
@@ -78,6 +88,12 @@ public final class WeatherKitSource {
|
||||
|
||||
public init() {}
|
||||
|
||||
/// Weiterreichen, damit das Modell einen beobachtbaren Wert daraus machen kann.
|
||||
public var onLocationAuthorizationChange: (() -> Void)? {
|
||||
get { location.onAuthorizationChange }
|
||||
set { location.onAuthorizationChange = newValue }
|
||||
}
|
||||
|
||||
public var locationIsDenied: Bool { location.isDenied }
|
||||
public var locationIsUndetermined: Bool { location.isUndetermined }
|
||||
public var locationIsAuthorized: Bool { !location.isDenied && !location.isUndetermined }
|
||||
|
||||
@@ -3,12 +3,21 @@ import AppKit
|
||||
import OnyxDesign
|
||||
import OnyxWidgetKit
|
||||
|
||||
/// Der Berechtigungszustand der Ortung — als beobachtbarer Wert.
|
||||
public enum LocationAuthorization: Equatable, Sendable {
|
||||
case granted, denied, undetermined
|
||||
}
|
||||
|
||||
/// Hält den Wetterstand für das Widget.
|
||||
@MainActor
|
||||
@Observable
|
||||
public final class WeatherModel {
|
||||
|
||||
public private(set) var state: WeatherState = .idle
|
||||
/// Beobachtbar, damit die Einstellungen mitbekommen, wenn der Nutzer
|
||||
/// zustimmt. Eine Abfrage auf `CLLocationManager` täte das nicht — sie
|
||||
/// liefert nur den Wert zum Zeitpunkt des Lesens.
|
||||
public private(set) var locationAuthorization: LocationAuthorization = .undetermined
|
||||
public var place: WeatherPlace {
|
||||
didSet { guard place != oldValue else { return }; load(force: true) }
|
||||
}
|
||||
@@ -20,22 +29,30 @@ public final class WeatherModel {
|
||||
public init(source: WeatherKitSource = WeatherKitSource(), place: WeatherPlace = .current) {
|
||||
self.source = source
|
||||
self.place = place
|
||||
syncAuthorization()
|
||||
source.onLocationAuthorizationChange = { [weak self] in
|
||||
guard let self else { return }
|
||||
syncAuthorization()
|
||||
// Nach der Zustimmung sofort laden statt auf den nächsten Anlauf
|
||||
// zu warten.
|
||||
if locationAuthorization == .granted { load(force: true) }
|
||||
}
|
||||
}
|
||||
|
||||
public var locationIsDenied: Bool { source.locationIsDenied }
|
||||
public var locationIsUndetermined: Bool { source.locationIsUndetermined }
|
||||
public var locationIsAuthorized: Bool { source.locationIsAuthorized }
|
||||
private func syncAuthorization() {
|
||||
locationAuthorization = if source.locationIsDenied { .denied }
|
||||
else if source.locationIsUndetermined { .undetermined }
|
||||
else { .granted }
|
||||
}
|
||||
|
||||
/// Fragt die Ortungsberechtigung an und lädt danach.
|
||||
public var locationIsDenied: Bool { locationAuthorization == .denied }
|
||||
public var locationIsUndetermined: Bool { locationAuthorization == .undetermined }
|
||||
public var locationIsAuthorized: Bool { locationAuthorization == .granted }
|
||||
|
||||
/// Fragt die Ortungsberechtigung an. Das Ergebnis kommt über den
|
||||
/// Delegaten zurück und aktualisiert `locationAuthorization` von selbst.
|
||||
public func requestLocationAuthorization() {
|
||||
source.requestLocationAuthorization()
|
||||
// Die Entscheidung fällt asynchron. Kurz warten und dann laden — die
|
||||
// Alternative wäre ein Delegat quer durch drei Schichten für einen
|
||||
// Vorgang, der einmal im Leben der App passiert.
|
||||
Task { [weak self] in
|
||||
try? await Task.sleep(for: .seconds(2))
|
||||
self?.load(force: true)
|
||||
}
|
||||
}
|
||||
|
||||
/// Lädt, wenn der Zwischenspeicher abgelaufen ist.
|
||||
|
||||
Reference in New Issue
Block a user