Skip to content

Instantly share code, notes, and snippets.

@cipolleschi
Last active April 18, 2020 12:00
Show Gist options
  • Select an option

  • Save cipolleschi/2b50b54980e9a45247312babfa3145d8 to your computer and use it in GitHub Desktop.

Select an option

Save cipolleschi/2b50b54980e9a45247312babfa3145d8 to your computer and use it in GitHub Desktop.
Improved notification
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