Version casi lista
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import Foundation
|
||||
import UserNotifications
|
||||
|
||||
@MainActor
|
||||
final class NotificationService {
|
||||
static let shared = NotificationService()
|
||||
|
||||
private init() {}
|
||||
|
||||
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 schedulePlanningReminderIfNeeded(nextWeekPlan: WeekPlan?, language: AppLanguage) {
|
||||
let center = UNUserNotificationCenter.current()
|
||||
let identifier = "mealmood.next-week-planning"
|
||||
|
||||
let isComplete = nextWeekPlan?.slots.allSatisfy { $0.dishId != nil } ?? false
|
||||
if isComplete {
|
||||
center.removePendingNotificationRequests(withIdentifiers: [identifier])
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user