Skip to content

Commit

Permalink
Send alarm notification only if user entered sleep mode
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirBrejcha committed Aug 26, 2023
1 parent 12c3716 commit 326ac72
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Core/Sources/Core/NotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ public final class NotificationManager: NSObject {
public func cleanDelivered() {
center.removeAllDeliveredNotifications()
}

public func removeNotification(by id: String) {
center.removePendingNotificationRequests(withIdentifiers: [id])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public protocol HasScheduleSleepNotifications {

public protocol ScheduleSleepNotifications: AnyObject {
func callAsFunction()
func setAlarmNotification(time: Date)
func cancelAlarmNotification()
}

class ScheduleSleepNotificationsImpl:
Expand Down Expand Up @@ -56,15 +58,15 @@ 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!"
notificationManager.createNotification(
title: wakeUpTitle,
body: wakeUpBody,
components: wakeUpComponents,
identifier: "time-to-wake-\(index)"
identifier: id
)
}

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Rise.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -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 = "";
Expand Down
5 changes: 4 additions & 1 deletion Rise/Presentation/Pages/Sleep/SleepViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class SleepViewController:
& HasChangeScreenBrightness
& HasPlayWhileSleepingMelody
& HasPlayBeforeAlarmMelody
& HasScheduleSleepNotifications

typealias Params = Date
typealias View = SleepView
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 326ac72

Please sign in to comment.