144 lines
5.3 KiB
Swift
144 lines
5.3 KiB
Swift
//
|
|
// LocalNotification.swift
|
|
// Feels
|
|
//
|
|
// Created by Trey Tartt on 1/8/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UserNotifications
|
|
|
|
class LocalNotification {
|
|
public enum ActionType: String {
|
|
case horrible = "HORRIBLE_ACTION"
|
|
case bad = "BAD_ACTION"
|
|
case average = "AVERAGE_ACTION"
|
|
case good = "GOOD_ACTION"
|
|
case great = "GREAT_ACTION"
|
|
}
|
|
static let categoryName = "MOOD_UPDATE"
|
|
|
|
public class func testIfEnabled(completion: @escaping (Result<Bool, Error>) -> Void) {
|
|
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
|
|
if success {
|
|
completion(.success(true))
|
|
} else if let error = error {
|
|
completion(.failure(error))
|
|
}
|
|
}
|
|
}
|
|
|
|
public class func scheduleReminder(atTime time: Date) {
|
|
let _ = LocalNotification.createNotificationCategory()
|
|
|
|
let notificationContent = UNMutableNotificationContent()
|
|
notificationContent.title = "How was your day?"
|
|
notificationContent.badge = NSNumber(value: 1)
|
|
notificationContent.sound = .default
|
|
notificationContent.categoryIdentifier = LocalNotification.categoryName
|
|
|
|
let calendar = Calendar.current
|
|
let time = calendar.dateComponents([.hour,.minute], from: time)
|
|
|
|
var datComp = DateComponents()
|
|
datComp.hour = time.hour
|
|
datComp.minute = time.minute
|
|
|
|
let trigger = UNCalendarNotificationTrigger(dateMatching: datComp, repeats: true)
|
|
let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: trigger)
|
|
UNUserNotificationCenter.current().add(request) { (error : Error?) in
|
|
if let theError = error {
|
|
print(theError.localizedDescription)
|
|
}
|
|
}
|
|
}
|
|
|
|
private class func createNotificationCategory() -> UNNotificationCategory {
|
|
let moodCategory =
|
|
UNNotificationCategory(identifier: LocalNotification.categoryName,
|
|
actions: [horribleAction, badAction, averageAction, goodAction, greatAction],
|
|
intentIdentifiers: [],
|
|
hiddenPreviewsBodyPlaceholder: "",
|
|
options: .customDismissAction)
|
|
// Register the notification type.
|
|
let notificationCenter = UNUserNotificationCenter.current()
|
|
notificationCenter.setNotificationCategories([moodCategory])
|
|
|
|
return moodCategory
|
|
}
|
|
|
|
private class var horribleAction: UNNotificationAction {
|
|
let acceptAction = UNNotificationAction(identifier: ActionType.horrible.rawValue,
|
|
title: "Horrible",
|
|
options: [])
|
|
return acceptAction
|
|
}
|
|
|
|
private class var badAction: UNNotificationAction {
|
|
let acceptAction = UNNotificationAction(identifier: ActionType.bad.rawValue,
|
|
title: "Bad",
|
|
options: [])
|
|
return acceptAction
|
|
}
|
|
|
|
private class var averageAction: UNNotificationAction {
|
|
let acceptAction = UNNotificationAction(identifier: ActionType.average.rawValue,
|
|
title: "Average",
|
|
options: [])
|
|
return acceptAction
|
|
}
|
|
|
|
private class var goodAction: UNNotificationAction {
|
|
let acceptAction = UNNotificationAction(identifier: ActionType.good.rawValue,
|
|
title: "Good",
|
|
options: [])
|
|
return acceptAction
|
|
}
|
|
|
|
private class var greatAction: UNNotificationAction {
|
|
let acceptAction = UNNotificationAction(identifier: ActionType.great.rawValue,
|
|
title: "Great",
|
|
options: [])
|
|
return acceptAction
|
|
}
|
|
|
|
public class func removeNotificaiton() {
|
|
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
|
|
}
|
|
}
|
|
|
|
class NotificationDelegate: NSObject, ObservableObject, UNUserNotificationCenterDelegate {
|
|
@Published var notificationCounter = 0
|
|
|
|
override init() {
|
|
super.init()
|
|
UNUserNotificationCenter.current().delegate = self
|
|
}
|
|
|
|
func requestAuthorization() {
|
|
|
|
}
|
|
|
|
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
|
|
completionHandler([.badge, .banner, .sound])
|
|
}
|
|
|
|
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
|
|
if let action = LocalNotification.ActionType(rawValue: response.actionIdentifier) {
|
|
switch action {
|
|
case .horrible:
|
|
PersistenceController.shared.add(mood: .horrible, forDate: Date())
|
|
case .bad:
|
|
PersistenceController.shared.add(mood: .bad, forDate: Date())
|
|
case .average:
|
|
PersistenceController.shared.add(mood: .average, forDate: Date())
|
|
case .good:
|
|
PersistenceController.shared.add(mood: .good, forDate: Date())
|
|
case .great:
|
|
PersistenceController.shared.add(mood: .great, forDate: Date())
|
|
}
|
|
}
|
|
completionHandler()
|
|
}
|
|
}
|