v0.7.0 — Phase β-6 Native-Polish
Drei Sub-Pakete: Keyboard-Shortcuts, Daily-Reminder-Notifications, WidgetKit-Extension mit App-Group-Daten-Sharing. Siri-Shortcuts und Share-Extension auf β-7 verschoben — niedrige Priorität, die drei großen Brocken decken 90% des Native-Polish ab. Keyboard-Shortcuts: - Hidden Buttons in StudySessionView mit .keyboardShortcut - Space = flip, 1/2/3/4 = again/hard/good/easy - iPad-Magic-Keyboard + macOS-tauglich Daily-Reminders: - NotificationManager @Observable mit UNUserNotificationCenter - Authorization-State + Permission-Request-Flow - UNCalendarNotificationTrigger täglich zur konfigurierten Zeit - SettingsView in AccountView mit Toggle + DatePicker - UserDefaults-Persistierung von Hour/Minute/Enabled WidgetKit-Extension: - WidgetSnapshot Codable mit topDecks (Top-3 by dueCount) + totalDueCount - WidgetSnapshotStore schreibt in group.ev.mana.cards-Container - DeckListStore.refresh schreibt Snapshot + WidgetCenter.reloadAllTimelines - CardsWidgetExtension-Target im project.yml (app-extension) - CardsWidgetBundle + CardsDueWidget mit 5 Familien (small/medium/ accessoryCircular/accessoryInline/accessoryRectangular) - DueProvider TimelineProvider mit 30-min-Refresh - DueWidgetView Family-Switch - WidgetSnapshot.swift shared in beiden Targets via XcodeGen sources - App-Group im Haupt- und Widget-Entitlement 35 Tests grün (keine neuen Tests in β-6 — WidgetKit + Notifications sind System-API-Integrationen, Tests wären überwiegend Mocks). Build inkl. Widget-Extension grün. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
07ada72b0f
commit
a1770fbc6a
15 changed files with 580 additions and 13 deletions
86
Sources/Core/Notifications/NotificationManager.swift
Normal file
86
Sources/Core/Notifications/NotificationManager.swift
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
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.cards.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 = "Cards"
|
||||
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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ import Foundation
|
|||
import ManaCore
|
||||
import Observation
|
||||
import SwiftData
|
||||
import WidgetKit
|
||||
|
||||
/// Orchestriert API + SwiftData-Cache für die Deck-Liste.
|
||||
/// View bindet sich an `state` und `errorMessage`.
|
||||
|
|
@ -35,6 +36,7 @@ final class DeckListStore {
|
|||
do {
|
||||
let decks = try await api.listDecks()
|
||||
try await applyToCache(decks: decks)
|
||||
updateWidgetSnapshot()
|
||||
state = .loaded
|
||||
Log.sync.info("Loaded \(decks.count, privacy: .public) decks from server")
|
||||
} catch let error as AuthError {
|
||||
|
|
@ -96,4 +98,30 @@ final class DeckListStore {
|
|||
|
||||
try context.save()
|
||||
}
|
||||
|
||||
/// Schreibt einen WidgetSnapshot in den shared App-Group-Container
|
||||
/// und fordert WidgetKit auf, alle Widgets neu zu rendern. Wird nach
|
||||
/// jedem erfolgreichen Refresh aufgerufen.
|
||||
private func updateWidgetSnapshot() {
|
||||
let descriptor = FetchDescriptor<CachedDeck>(
|
||||
sortBy: [SortDescriptor(\.dueCount, order: .reverse)]
|
||||
)
|
||||
let allDecks = (try? context.fetch(descriptor)) ?? []
|
||||
let totalDue = allDecks.reduce(0) { $0 + $1.dueCount }
|
||||
let top = allDecks.prefix(3).map { deck in
|
||||
WidgetSnapshot.Entry(
|
||||
id: deck.id,
|
||||
name: deck.name,
|
||||
dueCount: deck.dueCount,
|
||||
colorHex: deck.color
|
||||
)
|
||||
}
|
||||
let snapshot = WidgetSnapshot(
|
||||
updatedAt: .now,
|
||||
totalDueCount: totalDue,
|
||||
topDecks: Array(top)
|
||||
)
|
||||
WidgetSnapshotStore.write(snapshot)
|
||||
WidgetCenter.shared.reloadAllTimelines()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
48
Sources/Core/Sync/WidgetSnapshot.swift
Normal file
48
Sources/Core/Sync/WidgetSnapshot.swift
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import Foundation
|
||||
|
||||
/// Datei-Format für die WidgetKit-Extension. Wird vom Haupt-Target nach
|
||||
/// jedem erfolgreichen `DeckListStore.refresh()` in den shared App-Group-
|
||||
/// Container geschrieben; das Widget liest es im TimelineProvider.
|
||||
///
|
||||
/// Wire ist bewusst stabil + schmal — nur was das Widget rendert.
|
||||
/// Neue Felder dürfen additiv dazukommen, alte Felder bleiben.
|
||||
struct WidgetSnapshot: Codable, Sendable {
|
||||
let updatedAt: Date
|
||||
let totalDueCount: Int
|
||||
let topDecks: [Entry]
|
||||
|
||||
struct Entry: Codable, Sendable, Identifiable {
|
||||
let id: String // deck-id
|
||||
let name: String
|
||||
let dueCount: Int
|
||||
let colorHex: String?
|
||||
}
|
||||
}
|
||||
|
||||
/// Liest und schreibt WidgetSnapshot in den shared App-Group-Container.
|
||||
enum WidgetSnapshotStore {
|
||||
/// App-Group-ID — muss exakt mit dem Entitlement-Eintrag matchen.
|
||||
static let appGroupID = "group.ev.mana.cards"
|
||||
static let snapshotFilename = "widget-snapshot.json"
|
||||
|
||||
static var snapshotURL: URL? {
|
||||
FileManager.default
|
||||
.containerURL(forSecurityApplicationGroupIdentifier: appGroupID)?
|
||||
.appendingPathComponent(snapshotFilename)
|
||||
}
|
||||
|
||||
static func write(_ snapshot: WidgetSnapshot) {
|
||||
guard let url = snapshotURL else { return }
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .iso8601
|
||||
guard let data = try? encoder.encode(snapshot) else { return }
|
||||
try? data.write(to: url, options: .atomic)
|
||||
}
|
||||
|
||||
static func read() -> WidgetSnapshot? {
|
||||
guard let url = snapshotURL, let data = try? Data(contentsOf: url) else { return nil }
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .iso8601
|
||||
return try? decoder.decode(WidgetSnapshot.self, from: data)
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,22 @@ struct AccountView: View {
|
|||
.foregroundStyle(CardsTheme.foreground)
|
||||
}
|
||||
|
||||
NavigationLink {
|
||||
SettingsView()
|
||||
} label: {
|
||||
Label("Einstellungen", systemImage: "gear")
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(.vertical, 12)
|
||||
.background(CardsTheme.surface, in: RoundedRectangle(cornerRadius: 8))
|
||||
.foregroundStyle(CardsTheme.foreground)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 8)
|
||||
.stroke(CardsTheme.border, lineWidth: 1)
|
||||
)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.padding(.horizontal, 32)
|
||||
|
||||
Spacer()
|
||||
|
||||
Button(role: .destructive) {
|
||||
|
|
|
|||
67
Sources/Features/Settings/SettingsView.swift
Normal file
67
Sources/Features/Settings/SettingsView.swift
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import SwiftUI
|
||||
|
||||
/// Settings-Sheet aus AccountView. Heute: Daily-Reminder-Konfiguration.
|
||||
struct SettingsView: View {
|
||||
@State private var notifications = NotificationManager()
|
||||
@State private var reminderDate: Date = .now
|
||||
@State private var requestingAuth = false
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section("Tägliche Erinnerung") {
|
||||
Toggle("Erinnerung aktiv", isOn: Binding(
|
||||
get: { notifications.remindersEnabled },
|
||||
set: { newValue in
|
||||
notifications.remindersEnabled = newValue
|
||||
Task {
|
||||
if newValue, notifications.authorization != .authorized {
|
||||
requestingAuth = true
|
||||
_ = await notifications.requestAuthorization()
|
||||
requestingAuth = false
|
||||
}
|
||||
await notifications.reschedule()
|
||||
}
|
||||
}
|
||||
))
|
||||
.disabled(requestingAuth)
|
||||
|
||||
if notifications.remindersEnabled {
|
||||
DatePicker(
|
||||
"Uhrzeit",
|
||||
selection: $reminderDate,
|
||||
displayedComponents: .hourAndMinute
|
||||
)
|
||||
.onChange(of: reminderDate) { _, newValue in
|
||||
let cal = Calendar.current
|
||||
notifications.reminderHour = cal.component(.hour, from: newValue)
|
||||
notifications.reminderMinute = cal.component(.minute, from: newValue)
|
||||
Task { await notifications.reschedule() }
|
||||
}
|
||||
}
|
||||
|
||||
if notifications.authorization == .denied {
|
||||
Label("Benachrichtigungen sind in den iOS-Einstellungen blockiert.",
|
||||
systemImage: "exclamationmark.circle")
|
||||
.font(.caption)
|
||||
.foregroundStyle(CardsTheme.warning)
|
||||
}
|
||||
}
|
||||
|
||||
Section("Über") {
|
||||
LabeledContent("Server", value: "cardecky-api.mana.how")
|
||||
LabeledContent("Auth", value: "auth.mana.how")
|
||||
}
|
||||
}
|
||||
.navigationTitle("Einstellungen")
|
||||
#if os(iOS)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
#endif
|
||||
.task {
|
||||
await notifications.refreshAuthorization()
|
||||
var comp = DateComponents()
|
||||
comp.hour = notifications.reminderHour
|
||||
comp.minute = notifications.reminderMinute
|
||||
reminderDate = Calendar.current.date(from: comp) ?? .now
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -72,6 +72,7 @@ struct StudySessionView: View {
|
|||
flipHaptic()
|
||||
session.flip()
|
||||
}
|
||||
keyboardShortcuts(session: session)
|
||||
if session.isFlipped {
|
||||
RatingBar { rating in
|
||||
Task { await session.grade(rating) }
|
||||
|
|
@ -157,6 +158,31 @@ struct StudySessionView: View {
|
|||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
|
||||
/// Unsichtbare Buttons mit Keyboard-Shortcuts. Funktionieren auf
|
||||
/// iPad (Magic Keyboard) und macOS. Space = flip, 1-4 = Rating.
|
||||
@ViewBuilder
|
||||
private func keyboardShortcuts(session: StudySession) -> some View {
|
||||
Group {
|
||||
Button("Flip") {
|
||||
flipHaptic()
|
||||
session.flip()
|
||||
}
|
||||
.keyboardShortcut(.space, modifiers: [])
|
||||
|
||||
if session.isFlipped {
|
||||
ForEach(Rating.allCases, id: \.self) { rating in
|
||||
Button(rating.label) {
|
||||
Task { await session.grade(rating) }
|
||||
}
|
||||
.keyboardShortcut(KeyEquivalent(Character(rating.shortcut)), modifiers: [])
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(width: 0, height: 0)
|
||||
.opacity(0)
|
||||
.accessibilityHidden(true)
|
||||
}
|
||||
|
||||
private func flipHaptic() {
|
||||
#if canImport(UIKit)
|
||||
UIImpactFeedbackGenerator(style: .soft).impactOccurred()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue