From 2ff702057c8fad65057d6edebb3cf1dd39efd2de Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Fri, 20 May 2022 09:18:57 -0400 Subject: [PATCH] More cleanup --- ntfy/App/AppDelegate.swift | 6 +--- ntfy/Persistence/Store.swift | 16 ++++++++- ntfy/Utils/ApiService.swift | 3 +- ntfy/Views/NotificationListView.swift | 48 +++++++++++++++------------ 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/ntfy/App/AppDelegate.swift b/ntfy/App/AppDelegate.swift index d0dba19..799d541 100644 --- a/ntfy/App/AppDelegate.swift +++ b/ntfy/App/AppDelegate.swift @@ -90,10 +90,7 @@ extension AppDelegate: UNUserNotificationCenterDelegate { ) { let userInfo = notification.request.content.userInfo Log.d(tag, "Notification received via userNotificationCenter(willPresent)", userInfo) - //store.saveNotification(fromUserInfo: userInfo) - //_ = try? store.context.fetch(Subscription.fetchRequest()) - - completionHandler([[.alert, .sound]]) + completionHandler([[.banner, .sound]]) } func userNotificationCenter( @@ -103,7 +100,6 @@ extension AppDelegate: UNUserNotificationCenterDelegate { ) { let userInfo = response.notification.request.content.userInfo Log.d(tag, "Notification received via userNotificationCenter(didReceive)", userInfo) - store.saveNotification(fromUserInfo: userInfo) completionHandler() } } diff --git a/ntfy/Persistence/Store.swift b/ntfy/Persistence/Store.swift index 6265744..af2f9b1 100644 --- a/ntfy/Persistence/Store.swift +++ b/ntfy/Persistence/Store.swift @@ -116,7 +116,7 @@ class Store: ObservableObject { } func delete(notifications: Set) { - Log.d(Store.tag, "Deleting \(notifications.count) notification(s)", notifications) + Log.d(Store.tag, "Deleting \(notifications.count) notification(s)") do { notifications.forEach { notification in context.delete(notification) @@ -128,6 +128,20 @@ class Store: ObservableObject { } } + func delete(allNotificationsFor subscription: Subscription) { + guard let notifications = subscription.notifications else { return } + Log.d(Store.tag, "Deleting all \(notifications.count) notification(s) for subscription \(subscription.urlString())") + do { + notifications.forEach { notification in + context.delete(notification as! Notification) + } + try context.save() + } catch let error { + Log.w(Store.tag, "Cannot delete notification(s)", error) + rollbackAndRefresh() + } + } + func rollbackAndRefresh() { // Hack: We refresh all objects, since failing to store a notification usually means // that the app extension stored the notification first. This is a way to update the diff --git a/ntfy/Utils/ApiService.swift b/ntfy/Utils/ApiService.swift index 8592bed..7f93e71 100644 --- a/ntfy/Utils/ApiService.swift +++ b/ntfy/Utils/ApiService.swift @@ -18,8 +18,7 @@ class ApiService { message: String, title: String, priority: Int = 3, - tags: [String] = [], - completionHandler: @escaping (Notification?, Error?) -> Void + tags: [String] = [] ) { guard let url = URL(string: subscription.urlString()) else { return } var request = URLRequest(url: url) diff --git a/ntfy/Views/NotificationListView.swift b/ntfy/Views/NotificationListView.swift index 865c609..dd0db01 100644 --- a/ntfy/Views/NotificationListView.swift +++ b/ntfy/Views/NotificationListView.swift @@ -44,18 +44,7 @@ struct NotificationListView: View { Menu { editButton Button("Send test notification") { - let possibleTags = ["warning", "skull", "success", "triangular_flag_on_post", "de", "us", "dog", "cat", "rotating_light", "bike", "backup", "rsync", "this-s-a-tag", "ios"] - let priority = Int.random(in: 1..<6) - let tags = Array(possibleTags.shuffled().prefix(Int.random(in: 0..<4))) - ApiService.shared.publish( - subscription: subscription, - message: "This is a test notification from the ntfy iOS app. It has a priority of \(priority). If you send another one, it may look different.", - title: "Test: You can set a title if you like", - priority: priority, - tags: tags - ) { _,_ in - print("Success") - } + self.sendTestNotification() } Button("Clear all notifications") { self.showAlert = true @@ -90,11 +79,8 @@ struct NotificationListView: View { message: Text("Do you really want to delete all of the notifications in this topic?"), primaryButton: .destructive( Text("Permanently delete"), - action: { - //Database.current.deleteNotificationsForSubscription(subscription: subscription) - //viewModel.notifications = Database.current.getNotifications(subscription: subscription) - //subscription.loadNotifications() - }), + action: deleteAll + ), secondaryButton: .cancel()) case .unsubscribe: return Alert( @@ -102,10 +88,8 @@ struct NotificationListView: View { message: Text("Do you really want to unsubscribe from this topic and delete all of the notifications you received?"), primaryButton: .destructive( Text("Unsubscribe"), - action: { - subscriptionManager.unsubscribe(subscription) - self.presentationMode.wrappedValue.dismiss() - }), + action: unsubscribe + ), secondaryButton: .cancel()) case .selected: return Alert( @@ -148,6 +132,28 @@ struct NotificationListView: View { } } + private func sendTestNotification() { + let possibleTags = ["warning", "skull", "success", "triangular_flag_on_post", "de", "us", "dog", "cat", "rotating_light", "bike", "backup", "rsync", "this-s-a-tag", "ios"] + let priority = Int.random(in: 1..<6) + let tags = Array(possibleTags.shuffled().prefix(Int.random(in: 0..<4))) + ApiService.shared.publish( + subscription: subscription, + message: "This is a test notification from the ntfy iOS app. It has a priority of \(priority). If you send another one, it may look different.", + title: "Test: You can set a title if you like", + priority: priority, + tags: tags + ) + } + + private func unsubscribe() { + subscriptionManager.unsubscribe(subscription) + presentationMode.wrappedValue.dismiss() + } + + private func deleteAll() { + store.delete(allNotificationsFor: subscription) + } + private func deleteSelected() { store.delete(notifications: selection) selection = Set()