cards-native/Sources/Core/Notifications/NotificationManager.swift
Till JS 2eb7f61f9c fix: ITMS-90129 — DisplayName Cards → Cardecky + Build 2
Apple lehnte Build 0.1.0(1) ab mit ITMS-90129: "The bundle uses a
bundle name or display name that is already taken." Im App-Store
gibt es schon Apps mit dem DisplayName "Cards" (u.a. Apples eigene
Grußkarten-App war so benannt). App-Store-Connect-App heißt sowieso
"Cardecky" — Brand-Konsistenz: DisplayName durchgehend "Cardecky".

Geändert:
- project.yml Main-App: CFBundleDisplayName Cards → Cardecky,
  CFBundleVersion 1 → 2 (Apple lehnt doppelte Build-Nummern ab)
- project.yml Widget: CFBundleDisplayName Cards Widget → Cardecky Widget,
  INFOPLIST_KEY_CFBundleDisplayName analog
- project.yml Share-Extension: CFBundleVersion 1 → 2
- LoginView Heading: "Cards" → "Cardecky"
- NotificationManager.content.title: "Cards" → "Cardecky"
- UITest: erwartet "Cardecky" statt "Cards"

Archive verifiziert: CFBundleDisplayName=Cardecky, CFBundleVersion=2,
ARCHIVE SUCCEEDED. Nach erneutem Upload via Xcode Organizer sollte
TestFlight den Build akzeptieren.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 15:05:33 +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: Sendable {
case unknown
case authorized
case denied
}
private(set) var authorization: AuthorizationStatus = .unknown
private let identifier = "ev.mana.cardecky.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 = "Cardecky"
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)")
}
}
}