Last active
April 18, 2020 12:00
-
-
Save cipolleschi/2b50b54980e9a45247312babfa3145d8 to your computer and use it in GitHub Desktop.
Improved notification
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) | |
| } | |
| protocol TimeProvider { | |
| var now: Date { get } | |
| func calendar(with identifier: Calendar.Identifier) -> Calendar | |
| } | |
| class NotificationManager { | |
| let notificationScheduler: NotificationScheduler | |
| let timeProvider: TimeProvider | |
| init(notificationScheduler: NotificationScheduler, timeProvider: TimeProvider) { | |
| self.notificationScheduler = notificationScheduler | |
| self.timeProvider = timeProvider | |
| } | |
| private func getCurrentHour() -> Int? { | |
| let calendar = self.timeProvider.calendar(identifier: .gregorian) | |
| let date = self.timeProvider.now | |
| 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