diff --git a/FunnyPuny.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/FunnyPuny.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 2d2da98..f86e8f4 100644 --- a/FunnyPuny.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/FunnyPuny.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -24,7 +24,7 @@ "location" : "https://github.com/firebase/firebase-ios-sdk", "state" : { "branch" : "master", - "revision" : "8a0bb35f019d062d7aefd118cd0d3cf2482ef293" + "revision" : "d6e18b978aab37c810a6e9ce40a756ae02e58562" } }, { @@ -32,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/google/GoogleAppMeasurement.git", "state" : { - "revision" : "71eb6700dd53a851473c48d392f00a3ab26699a6", - "version" : "10.1.0" + "revision" : "9a09ece724128e8d1e14c5133b87c0e236844ac0", + "version" : "10.4.0" } }, { @@ -41,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/google/GoogleDataTransport.git", "state" : { - "revision" : "5056b15c5acbb90cd214fe4d6138bdf5a740e5a8", - "version" : "9.2.0" + "revision" : "f6b558e3f801f2cac336b04f615ce111fa9ddaa0", + "version" : "9.2.1" } }, { @@ -50,8 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/google/GoogleUtilities.git", "state" : { - "revision" : "6db6edb48bdd9943426562c7f042a5492de5ba3d", - "version" : "7.10.0" + "revision" : "0543562f85620b5b7c510c6bcbef75b562a5127b", + "version" : "7.11.0" } }, { @@ -68,8 +68,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/google/gtm-session-fetcher.git", "state" : { - "revision" : "efda500b6d9858d38a76dbfbfa396bd644692e4a", - "version" : "3.0.0" + "revision" : "96d7cc73a71ce950723aa3c50ce4fb275ae180b8", + "version" : "3.1.0" } }, { @@ -132,7 +132,7 @@ "location" : "https://github.com/realm/realm-swift.git", "state" : { "branch" : "master", - "revision" : "b002cf50fbc21b6f52f3b4e74b15edc4007d5cbc" + "revision" : "5d6abe657bfe628e426442aa8143c1ec392706a2" } }, { @@ -167,8 +167,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/kirualex/SwiftyGif.git", "state" : { - "revision" : "db0c122b671bc9760385e0355be00eede3b7bb44", - "version" : "5.4.3" + "revision" : "d6d26061d6553a493781ad3df4a8e275c43fc373", + "version" : "5.4.4" } } ], diff --git a/FunnyPuny/Application/AppDelegate.swift b/FunnyPuny/Application/AppDelegate.swift index 8e88d2f..5550529 100644 --- a/FunnyPuny/Application/AppDelegate.swift +++ b/FunnyPuny/Application/AppDelegate.swift @@ -5,15 +5,25 @@ import FirebaseCore import RealmSwift import SwiftDate import UIKit +import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? + let notificationCenter = UNUserNotificationCenter.current() func application( _: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { + let options: UNAuthorizationOptions = [.alert, .sound, .badge] + notificationCenter.requestAuthorization(options: options) { + didAllow, error in + if !didAllow { + print("User has declined notifications") + } + } + notificationCenter.delegate = self FirebaseApp.configure() SwiftDate.defaultRegion = .local setupRealm() @@ -22,6 +32,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } private func setupRealm() { + let config = Realm.Configuration( + schemaVersion: 2) + Realm.Configuration.defaultConfiguration = config do { let realm = try Realm() print("Realm is located at:", realm.configuration.fileURL!) @@ -36,3 +49,38 @@ class AppDelegate: UIResponder, UIApplicationDelegate { window?.makeKeyAndVisible() } } + +// MARK: Notifications + +extension AppDelegate { + func getRequest() {} + + func scheduleNotification(notificationType: String) { + let content = UNMutableNotificationContent() + content.title = notificationType + content.body = "This is example how to create " + notificationType + content.sound = UNNotificationSound.default + content.badge = 1 + + let date = Date(timeIntervalSinceNow: 3600) + let triggerDaily = Calendar.current.dateComponents([.hour, .minute, .second,], from: date) + let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true) + let identifier = "Local Notification" + let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger) + + notificationCenter.add(request) { error in + if let error { + print("Error \(error.localizedDescription)") + } + } + } +} + +extension AppDelegate: UNUserNotificationCenterDelegate { + func userNotificationCenter(_ center: UNUserNotificationCenter, + willPresent notification: UNNotification, + withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) + { + completionHandler([.alert, .sound]) + } +} diff --git a/FunnyPuny/Model/Habit.swift b/FunnyPuny/Model/Habit.swift index beb5100..6b66ebc 100644 --- a/FunnyPuny/Model/Habit.swift +++ b/FunnyPuny/Model/Habit.swift @@ -9,6 +9,8 @@ class Habit: Object { @Persisted var name: String = "" @Persisted var frequency: List @Persisted var createdDate = Date() + @Persisted var reminderTime: String? + @Persisted var reminderNote: String? convenience init(name: String, frequency: List, createdDate: Date) { self.init() diff --git a/FunnyPuny/Presentation/Common /Constants/DateFormats.swift b/FunnyPuny/Presentation/Common /Constants/DateFormats.swift index f98c62b..a22e1aa 100644 --- a/FunnyPuny/Presentation/Common /Constants/DateFormats.swift +++ b/FunnyPuny/Presentation/Common /Constants/DateFormats.swift @@ -21,6 +21,8 @@ extension Date { case formatLLLLyyyy = "LLLL yyyy" /// 20221018 case formatyyyyMMdd = "yyyyMMdd" + /// 11:36 + case formatHHmm = "HH:mm" } var shortForm: String { diff --git a/FunnyPuny/Presentation/Home/HomeViewController.swift b/FunnyPuny/Presentation/Home/HomeViewController.swift index 2b958d5..e082bd5 100644 --- a/FunnyPuny/Presentation/Home/HomeViewController.swift +++ b/FunnyPuny/Presentation/Home/HomeViewController.swift @@ -24,7 +24,7 @@ class HomeViewController: ViewController { private var viewState: ViewState = .emptyState private var homeView = HomeView() - + let appDelegate = UIApplication.shared.delegate as? AppDelegate override func viewDidLoad() { super.viewDidLoad() title = Texts.home @@ -158,6 +158,18 @@ extension HomeViewController: UITableViewDelegate { tableView.reloadData() } addHapticFeedback(style: .heavy) + let notificationType = "HDHBHDDHBD" + let alert = UIAlertController(title: "", + message: "After 5 seconds " + notificationType + " will appear", + preferredStyle: .alert) + + let okAction = UIAlertAction(title: "OK", style: .default) { action in + + self.appDelegate?.scheduleNotification(notificationType: notificationType) + } + + alert.addAction(okAction) + present(alert, animated: true, completion: nil) } }