Last active
April 18, 2020 11:49
-
-
Save cipolleschi/4279d0a607cea904c2ab2e7be4e7fecc to your computer and use it in GitHub Desktop.
Schedule notifications at given times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| protocol NotificationScheduler { | |
| func scheduleNotification(with title: String) | |
| } | |
| class NotificationManager { | |
| let notificationScheduler: NotificationScheduler | |
| init(notificationScheduler: NotificationScheduler) { | |
| self.notificationScheduler = notificationScheduler | |
| } | |
| private func getCurrentHour() -> Int? { | |
| let calendar = Calendar(identifier: .gregorian) | |
| let date = Date() | |
| let hour = calendar.component(Calendar.Component.hour, from: date) | |
| return hour | |
| } | |
| func scheduleMorningNotification() { | |
| guard | |
| let hour = self.getCurrentHour, | |
| hour < 11 | |
| else { | |
| return | |
| } | |
| let title = "Good Morning!" | |
| self.notificationScheduler.scheduleNotification(with: title) | |
| } | |
| func scheduleNoonNotification() { | |
| guard | |
| let hour = self.getCurrentHour, | |
| hour > 11, | |
| hour < 14 | |
| else { | |
| return | |
| } | |
| let title = "Enjoy your lunch!" | |
| self.notificationScheduler.scheduleNotification(with: title) | |
| } | |
| func scheduleDinnerNotification() { | |
| guard | |
| let hour = self.getCurrentHour, | |
| hour > 19 | |
| else { | |
| return | |
| } | |
| let title = "Enjoy your dinner!" | |
| self.notificationScheduler.scheduleNotification(with: title) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment