Files
FamilyMealPlanner/MealMood/Services/NotificationService.swift
T
alexandrev-tibco 4ffa15b06a 2.0: CloudKit private-database sync replaces KV snapshot sync
Foundation for 2.1 family sharing (CKShare needs these models + container).
SwiftData cannot share across Apple IDs today, so 2.0 ships true multi-device
sync for the same account instead:

- Models made CloudKit-compatible: dropped @Attribute(.unique) on all 5,
  inline defaults on every attribute, WeekPlan.slots stored as optional
  relationship (name preserved → lightweight migration) with non-optional
  slotList facade; ~73 call sites renamed.
- Container: cloudKitDatabase .automatic, falling back to the local-only
  store when CloudKit is unavailable; failures recorded to Crashlytics.
- Entitlements: iCloud CloudKit service (container already existed);
  remote-notification background mode for push-driven sync.
- Legacy KV snapshot sync (ICloudSyncService) stays inert when CloudKit is
  active — kept only for 1.x devices.
- DeduplicationService collapses cross-device duplicates deterministically on
  launch (settings singleton, default tags with tagId remapping, same-week
  plans).
- Note: AppSettings.isPremium now syncs across same-Apple-ID devices; StoreKit
  (PremiumSyncService + Transaction.updates) remains the source of truth and
  reconciles on every launch/foreground.

All 21 unit tests pass, including ICloudSyncPremiumIsolationTests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BkanrydYtrme8wipTzWssG
2026-07-12 18:13:45 +02:00

121 lines
4.8 KiB
Swift

import Foundation
import UserNotifications
@MainActor
final class NotificationService {
static let shared = NotificationService()
static let remindersEnabledKey = "notifications_reminders_enabled"
private static let allReminderIdentifiers = [
"mealmood.next-week-planning",
"mealmood.sunday-planning"
]
private init() {}
/// User-level preference for weekly planning reminders (independent of OS permission).
var remindersEnabled: Bool {
get {
UserDefaults.standard.object(forKey: Self.remindersEnabledKey) as? Bool ?? true
}
set {
UserDefaults.standard.set(newValue, forKey: Self.remindersEnabledKey)
if !newValue { cancelAllReminders() }
}
}
func authorizationStatus() async -> UNAuthorizationStatus {
await UNUserNotificationCenter.current().notificationSettings().authorizationStatus
}
/// Explicit permission request for contextual prompts (onboarding, settings toggle).
/// Returns whether notifications are authorized after the request.
@discardableResult
func requestPermission() async -> Bool {
let center = UNUserNotificationCenter.current()
let settings = await center.notificationSettings()
switch settings.authorizationStatus {
case .notDetermined:
return (try? await center.requestAuthorization(options: [.alert, .sound, .badge])) ?? false
case .authorized, .provisional, .ephemeral:
return true
default:
return false
}
}
func requestPermissionIfNeeded() async {
let center = UNUserNotificationCenter.current()
let settings = await center.notificationSettings()
guard settings.authorizationStatus == .notDetermined else { return }
_ = try? await center.requestAuthorization(options: [.alert, .sound, .badge])
}
func cancelAllReminders() {
UNUserNotificationCenter.current()
.removePendingNotificationRequests(withIdentifiers: Self.allReminderIdentifiers)
}
func schedulePlanningReminderIfNeeded(nextWeekPlan: WeekPlan?, language: AppLanguage) {
guard remindersEnabled else {
cancelAllReminders()
return
}
let center = UNUserNotificationCenter.current()
let identifier = "mealmood.next-week-planning"
let isComplete = nextWeekPlan?.slotList.allSatisfy { $0.dishId != nil || $0.isEatingOut } ?? false
if isComplete {
center.removePendingNotificationRequests(withIdentifiers: [identifier])
} else {
let nextWeekStart = Date().startOfWeek().addingDays(7)
let reminderDay = nextWeekStart.addingDays(-1)
let calendar = Calendar.current
var components = calendar.dateComponents([.year, .month, .day], from: reminderDay)
components.hour = 19
components.minute = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
let content = UNMutableNotificationContent()
content.title = localizedString("notification_planning_title", language: language)
content.body = localizedString("notification_planning_body", language: language)
content.sound = .default
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.removePendingNotificationRequests(withIdentifiers: [identifier])
center.add(request)
}
scheduleSundayReminderIfNeeded(nextWeekPlan: nextWeekPlan, language: language)
}
private func scheduleSundayReminderIfNeeded(nextWeekPlan: WeekPlan?, language: AppLanguage) {
let center = UNUserNotificationCenter.current()
let identifier = "mealmood.sunday-planning"
// Cancel if next week is already planned
let hasAnySlot = nextWeekPlan?.slotList.contains { $0.dishId != nil || $0.isEatingOut } ?? false
if hasAnySlot {
center.removePendingNotificationRequests(withIdentifiers: [identifier])
return
}
// Schedule for next Sunday at 17:00
var components = DateComponents()
components.weekday = 1 // Sunday
components.hour = 17
components.minute = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let content = UNMutableNotificationContent()
content.title = localizedString("notification_sunday_title", language: language)
content.body = localizedString("notification_sunday_body", language: language)
content.sound = .default
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.removePendingNotificationRequests(withIdentifiers: [identifier])
center.add(request)
}
}