Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions Levain/Features/Shared/RootTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SwiftData
import SwiftUI

struct RootTabView: View {
@Environment(\.scenePhase) private var scenePhase
@Environment(\.modelContext) private var modelContext
@EnvironmentObject private var environment: AppEnvironment
@EnvironmentObject private var router: AppRouter
Expand Down Expand Up @@ -50,6 +51,19 @@ struct RootTabView: View {
.onOpenURL { url in
router.open(url: url, modelContext: modelContext)
}
.onChange(of: scenePhase) { _, newPhase in
guard newPhase == .active, didBootstrap else { return }
guard ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != "1" else { return }
guard AppLaunchOptions.shouldSuppressNotifications == false else { return }

let appSettings = loadOrCreateAppSettings()
let notificationService = environment.prepareNotificationServiceIfNeeded()
syncNotificationsForLifecycle(
notificationService: notificationService,
appSettings: appSettings,
showDisabledBanner: false
)
}
}

// MARK: - Tab content + aligned bottom bar
Expand Down Expand Up @@ -145,7 +159,11 @@ struct RootTabView: View {

guard AppLaunchOptions.shouldSuppressNotifications == false else { return }

scheduleNotificationBootstrap(notificationService: notificationService, appSettings: appSettings)
syncNotificationsForLifecycle(
notificationService: notificationService,
appSettings: appSettings,
showDisabledBanner: true
)
}

@MainActor
Expand All @@ -160,14 +178,11 @@ struct RootTabView: View {
}

@MainActor
private func scheduleNotificationBootstrap(
private func syncNotificationsForLifecycle(
notificationService: NotificationService,
appSettings: AppSettings
appSettings: AppSettings,
showDisabledBanner: Bool
) {
let needsLaunchSync = appSettings.lastNotificationSync == nil
let needsAuthorizationPrompt = appSettings.hasRequestedNotificationPermission == false
guard needsLaunchSync || needsAuthorizationPrompt else { return }

Task(priority: .utility) {
let authorizationState = await notificationService.requestAuthorizationIfNeeded(settings: appSettings)
if authorizationState == .authorized {
Expand All @@ -176,11 +191,11 @@ struct RootTabView: View {
try? modelContext.save()
}
if authorizationState == .denied {
if appSettings.lastNotificationSync == nil {
appSettings.lastNotificationSync = .now
try? modelContext.save()
appSettings.lastNotificationSync = .now
try? modelContext.save()
if showDisabledBanner {
router.showNotificationsDisabledBanner()
}
router.showNotificationsDisabledBanner()
}
Comment on lines 181 to 199

Copilot AI Apr 16, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syncNotificationsForLifecycle ora esegue sempre requestAuthorizationIfNeeded e, se autorizzato, un resyncAll (fetch di tutti i modelli + remove/schedule) ogni volta che viene chiamata (bootstrap + ogni ritorno in foreground via scenePhase). Questo può introdurre overhead/battery churn e un numero elevato di operazioni su UNUserNotificationCenter anche quando non è cambiato nulla. Suggerimento: reintrodurre un guard basato su appSettings.lastNotificationSync (es. throttle temporale) e/o chiamare resyncAll solo quando needsLaunchSync/needsAuthorizationPrompt o dopo mutazioni di dati rilevanti.

Copilot uses AI. Check for mistakes.
}
}
Expand Down
24 changes: 22 additions & 2 deletions Levain/Services/BakeReminderPlanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ enum BakeReminderPlanner {
}

var reminders: [BakeReminder] = []
if let activeStep = bake.activeStep, activeStep.isOverdue(now: now) {
reminders.append(contentsOf: overdueReminders(for: bake, step: activeStep, now: now))
}

for step in bake.sortedSteps where step.isTerminal == false {
if step.isWindowBased, step.status == .running {
Expand All @@ -39,9 +42,10 @@ enum BakeReminderPlanner {
return reminders
}

static func identifiers(for bake: Bake) -> [String] {
static func identifiers(for bake: Bake, now: Date = .now) -> [String] {
bake.steps.flatMap { step in
ReminderPhase.allCases.map { identifier(for: bake, step: step, phase: $0) }
ReminderPhase.allCases.map { identifier(for: bake, step: step, phase: $0) } +
RollingOverdueReminderPlanner.identifierDates(now: now).map { overdueIdentifier(for: bake, step: step, on: $0) }
}
}

Expand Down Expand Up @@ -75,6 +79,18 @@ enum BakeReminderPlanner {
return reminders
}

private static func overdueReminders(for bake: Bake, step: BakeStep, now: Date) -> [BakeReminder] {
RollingOverdueReminderPlanner.reminderDates(overdueSince: step.windowEnd, now: now).map { fireDate in
BakeReminder(
identifier: overdueIdentifier(for: bake, step: step, on: fireDate),
title: "\(step.displayName) · \(bake.name)",
body: "Non hai ancora completato \(step.displayName) di \(bake.name).",
route: AppRouter.DeepLink.bake(id: bake.id, stepID: step.id),
fireDate: fireDate
)
}
}

private enum ReminderPhase: String, CaseIterable {
case start
case windowClose
Expand All @@ -83,4 +99,8 @@ enum BakeReminderPlanner {
private static func identifier(for bake: Bake, step: BakeStep, phase: ReminderPhase) -> String {
"bake-\(phase.rawValue)-\(bake.id.uuidString)-\(step.id.uuidString)"
}

private static func overdueIdentifier(for bake: Bake, step: BakeStep, on fireDate: Date) -> String {
"bake-overdue-\(bake.id.uuidString)-\(step.id.uuidString)-\(RollingOverdueReminderPlanner.identifierSuffix(for: fireDate))"
}
}
32 changes: 30 additions & 2 deletions Levain/Services/KefirReminderPlanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,24 @@ enum KefirReminderPlanner {
)
}

reminders.append(contentsOf:
RollingOverdueReminderPlanner.reminderDates(overdueSince: dueAt, now: now).map { fireDate in
KefirReminderRequest(
identifier: overdueIdentifier(for: batch, on: fireDate),
fireDate: fireDate,
title: batch.name,
body: overdueBody(for: batch),
route: route
)
}
)

return reminders
}

static func identifiers(for batch: KefirBatch) -> [String] {
Phase.allCases.map { identifier(for: batch, phase: $0) }
static func identifiers(for batch: KefirBatch, now: Date = .now) -> [String] {
Phase.allCases.map { identifier(for: batch, phase: $0) } +
RollingOverdueReminderPlanner.identifierDates(now: now).map { overdueIdentifier(for: batch, on: $0) }
}

private static func identifier(for batch: KefirBatch, phase: Phase) -> String {
Expand Down Expand Up @@ -77,4 +90,19 @@ enum KefirReminderPlanner {
return "È il momento di riattivare il batch."
}
}

private static func overdueBody(for batch: KefirBatch) -> String {
switch batch.storageMode {
case .roomTemperature:
return "Non hai ancora rinnovato \(batch.name)."
case .fridge:
return "Non hai ancora controllato \(batch.name)."
case .freezer:
return "Non hai ancora riattivato \(batch.name)."
}
}

private static func overdueIdentifier(for batch: KefirBatch, on fireDate: Date) -> String {
"kefir-overdue-\(batch.id.uuidString)-\(RollingOverdueReminderPlanner.identifierSuffix(for: fireDate))"
}
}
Loading
Loading