More cleanup

This commit is contained in:
Philipp Heckel 2022-05-20 09:18:57 -04:00
parent 6c3e8278f9
commit 2ff702057c
4 changed files with 44 additions and 29 deletions

View file

@ -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()
}
}

View file

@ -116,7 +116,7 @@ class Store: ObservableObject {
}
func delete(notifications: Set<Notification>) {
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

View file

@ -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)

View file

@ -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<Notification>()