wordeck-native/Sources/Core/Notifications/NotificationManager.swift
Till JS 542082772a refactor(big-bang): cards-native → wordeck-native
Code + Identity-Rename zur Vorbereitung auf Apple-Dev-Portal-Aktion
(Bundle ev.mana.wordeck, App-Group group.ev.mana.wordeck, AASA
applinks:wordeck.com). Build bleibt funktional, aber gegen die
neue text-only-API können image-occlusion-Creates 422 zurückgeben —
das wird mit der Wordeck-Native v1.0-Welle (parallele Apple-Aktion)
sauber gemacht.

Umbenennung:
- 41 Files: cardecky/Cardecky → wordeck/Wordeck (Display, Strings,
  Kommentare)
- 57 Files: CardsNative → WordeckNative, CardsAPI → WordeckAPI,
  CardsTheme → WordeckTheme, CardsBrand → WordeckBrand, CardsWidget →
  WordeckWidget, CardsDueWidget → WordeckDueWidget
- Bundle-ID ev.mana.cardecky → ev.mana.wordeck (project.yml,
  Info.plist, entitlements, Keychain-Service, App-Group)
- AASA applinks:cardecky.mana.how → applinks:wordeck.com
- API-Base cardecky-api.mana.how → api.wordeck.com
- 10 Files renamed (App-Entry, API-Extensions, Theme, Widget,
  Entitlements, Tests)
- xcodeproj regenerated via xcodegen → WordeckNative.xcodeproj
- MaskRegionsTests.swift gelöscht (image-occlusion entfällt mit
  Wordeck text-only)

Forgejo-Repo git.mana.how/till/cards-native → wordeck-native umbenannt
(Auto-Redirect aktiv). Lokales Verzeichnis Code/cards-native/ bleibt
vorerst — wird beim nächsten Apple-Setup mit Bundle-Test umbenannt.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-17 23:10:42 +02:00

86 lines
3 KiB
Swift

import Foundation
import Observation
import UserNotifications
/// Lokale tägliche Reminder. Reines `UNUserNotificationCenter`
/// keine Push-Backend-Anbindung, keine Crash-Reporter, kein SaaS
/// (Compliance, siehe `mana/docs/COMPLIANCE.md`).
@MainActor
@Observable
final class NotificationManager {
enum AuthorizationStatus {
case unknown
case authorized
case denied
}
private(set) var authorization: AuthorizationStatus = .unknown
private let identifier = "ev.mana.wordeck.dailyReminder"
private let store = UserDefaults.standard
/// Persistiert User-Pref. Format: ISO-Stunde:Minute (default 18:00).
var reminderHour: Int {
get { store.object(forKey: "reminderHour") as? Int ?? 18 }
set { store.set(newValue, forKey: "reminderHour") }
}
var reminderMinute: Int {
get { store.object(forKey: "reminderMinute") as? Int ?? 0 }
set { store.set(newValue, forKey: "reminderMinute") }
}
var remindersEnabled: Bool {
get { store.bool(forKey: "remindersEnabled") }
set { store.set(newValue, forKey: "remindersEnabled") }
}
func refreshAuthorization() async {
let settings = await UNUserNotificationCenter.current().notificationSettings()
switch settings.authorizationStatus {
case .authorized, .provisional, .ephemeral:
authorization = .authorized
case .denied:
authorization = .denied
default:
authorization = .unknown
}
}
/// Permission anfragen. Beim ersten Aufruf zeigt iOS den System-Prompt.
func requestAuthorization() async -> Bool {
let center = UNUserNotificationCenter.current()
do {
let granted = try await center.requestAuthorization(options: [.alert, .badge, .sound])
authorization = granted ? .authorized : .denied
return granted
} catch {
authorization = .denied
return false
}
}
/// Tägliche Reminder neu planen. Bei `remindersEnabled = false`
/// werden alle bestehenden Notifications gecancelt.
func reschedule() async {
let center = UNUserNotificationCenter.current()
center.removePendingNotificationRequests(withIdentifiers: [identifier])
guard remindersEnabled, authorization == .authorized else { return }
let content = UNMutableNotificationContent()
content.title = "Wordeck"
content.body = "Ein paar Karten warten auf dich."
content.sound = .default
var components = DateComponents()
components.hour = reminderHour
components.minute = reminderMinute
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
do {
try await center.add(request)
} catch {
Log.app.error("Notification schedule failed: \(error.localizedDescription, privacy: .public)")
}
}
}