Skip to content

Instantly share code, notes, and snippets.

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

  • Save cipolleschi/4279d0a607cea904c2ab2e7be4e7fecc to your computer and use it in GitHub Desktop.

Select an option

Save cipolleschi/4279d0a607cea904c2ab2e7be4e7fecc to your computer and use it in GitHub Desktop.
Schedule notifications at given times
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