From 326ac72a07d1cf02e3bb0d92e44dd91a00069e0b Mon Sep 17 00:00:00 2001 From: Vladimir Brejcha Date: Sat, 26 Aug 2023 21:35:07 +0300 Subject: [PATCH] Send alarm notification only if user entered sleep mode --- Core/Sources/Core/NotificationManager.swift | 4 ++++ .../UseCases/ScheduleSleepNotifications.swift | 17 ++++++++++++++--- Rise.xcodeproj/project.pbxproj | 4 ++-- .../Pages/Sleep/SleepViewController.swift | 5 ++++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Core/Sources/Core/NotificationManager.swift b/Core/Sources/Core/NotificationManager.swift index c3c3c19..9a9c244 100644 --- a/Core/Sources/Core/NotificationManager.swift +++ b/Core/Sources/Core/NotificationManager.swift @@ -87,4 +87,8 @@ public final class NotificationManager: NSObject { public func cleanDelivered() { center.removeAllDeliveredNotifications() } + + public func removeNotification(by id: String) { + center.removePendingNotificationRequests(withIdentifiers: [id]) + } } diff --git a/DomainLayer/Sources/DomainLayer/UseCases/ScheduleSleepNotifications.swift b/DomainLayer/Sources/DomainLayer/UseCases/ScheduleSleepNotifications.swift index 4b2bdd1..88a918b 100644 --- a/DomainLayer/Sources/DomainLayer/UseCases/ScheduleSleepNotifications.swift +++ b/DomainLayer/Sources/DomainLayer/UseCases/ScheduleSleepNotifications.swift @@ -11,6 +11,8 @@ public protocol HasScheduleSleepNotifications { public protocol ScheduleSleepNotifications: AnyObject { func callAsFunction() + func setAlarmNotification(time: Date) + func cancelAlarmNotification() } class ScheduleSleepNotificationsImpl: @@ -56,7 +58,7 @@ class ScheduleSleepNotificationsImpl: ) } - func generateWakeUpNotification(for date: Date, index: Int) { + func generateWakeUpNotification(for date: Date, id: String) { let wakeUpComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date) let wakeUpTitle = NotificationText.textTitleWakeUp.randomElement() ?? "🌞 Rise and Shine: A New Day Beckons!" let wakeUpBody = NotificationText.textBodyWakeUp.randomElement() ?? "☀️ Good morning! It's a brand-new day filled with possibilities. Time to rise and shine, and make the most of it!" @@ -64,7 +66,7 @@ class ScheduleSleepNotificationsImpl: title: wakeUpTitle, body: wakeUpBody, components: wakeUpComponents, - identifier: "time-to-wake-\(index)" + identifier: id ) } @@ -73,9 +75,18 @@ class ScheduleSleepNotificationsImpl: .forNextDays(numberOfDays: 10, startToday: true) for (i, day) in schedule.enumerated() { generateSleepNotification(for: day.toBed, index: i) - generateWakeUpNotification(for: day.wakeUp, index: i) } } + + private let wakeNotificationId = "time-to-wake" + + func setAlarmNotification(time: Date) { + generateWakeUpNotification(for: time, id: wakeNotificationId) + } + + func cancelAlarmNotification() { + notificationManager.removeNotification(by: wakeNotificationId) + } } public struct NotificationText { diff --git a/Rise.xcodeproj/project.pbxproj b/Rise.xcodeproj/project.pbxproj index cb718a1..ab2e507 100644 --- a/Rise.xcodeproj/project.pbxproj +++ b/Rise.xcodeproj/project.pbxproj @@ -1038,7 +1038,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.2; + MARKETING_VERSION = 1.2.1; PRODUCT_BUNDLE_IDENTIFIER = com.vladimirkorolev.rise; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; @@ -1065,7 +1065,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.2; + MARKETING_VERSION = 1.2.1; PRODUCT_BUNDLE_IDENTIFIER = com.vladimirkorolev.rise; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/Rise/Presentation/Pages/Sleep/SleepViewController.swift b/Rise/Presentation/Pages/Sleep/SleepViewController.swift index f248629..a3334fa 100644 --- a/Rise/Presentation/Pages/Sleep/SleepViewController.swift +++ b/Rise/Presentation/Pages/Sleep/SleepViewController.swift @@ -28,6 +28,7 @@ final class SleepViewController: & HasChangeScreenBrightness & HasPlayWhileSleepingMelody & HasPlayBeforeAlarmMelody + & HasScheduleSleepNotifications typealias Params = Date typealias View = SleepView @@ -103,6 +104,7 @@ final class SleepViewController: super.init(nibName: nil, bundle: nil) deps.preventAppSleep(true) deps.manageActiveSleep.alarmAt = alarmTime + deps.scheduleSleepNotifications.setAlarmNotification(time: alarmTime) } @available(*, unavailable) @@ -123,7 +125,7 @@ final class SleepViewController: Date().HHmmString }, wakeUpInDataSource: { [weak self] in - if let timeLeft = self?.alarmTime.fixIfNeeded().timeIntervalSince(Date()).HHmmString { + if let timeLeft = self?.alarmTime.fixIfNeeded().timeIntervalSince(Date.now).HHmmString { return FloatingLabel.Model(text: "Wake up in \(timeLeft)", alpha: 1) } else { return FloatingLabel.Model(text: "", alpha: 0) @@ -134,6 +136,7 @@ final class SleepViewController: self?.restoreBrightness() self?.playWhileSleepingMelody.stop() self?.playBeforeAlarmMelody.stop() + self?.deps.scheduleSleepNotifications.cancelAlarmNotification() self?.out(.showAfterSleep) }, keepAppOpenedHandler: { [weak self] in