More cleanup
This commit is contained in:
parent
6c3e8278f9
commit
2ff702057c
4 changed files with 44 additions and 29 deletions
|
|
@ -90,10 +90,7 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
|
||||||
) {
|
) {
|
||||||
let userInfo = notification.request.content.userInfo
|
let userInfo = notification.request.content.userInfo
|
||||||
Log.d(tag, "Notification received via userNotificationCenter(willPresent)", userInfo)
|
Log.d(tag, "Notification received via userNotificationCenter(willPresent)", userInfo)
|
||||||
//store.saveNotification(fromUserInfo: userInfo)
|
completionHandler([[.banner, .sound]])
|
||||||
//_ = try? store.context.fetch(Subscription.fetchRequest())
|
|
||||||
|
|
||||||
completionHandler([[.alert, .sound]])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func userNotificationCenter(
|
func userNotificationCenter(
|
||||||
|
|
@ -103,7 +100,6 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
|
||||||
) {
|
) {
|
||||||
let userInfo = response.notification.request.content.userInfo
|
let userInfo = response.notification.request.content.userInfo
|
||||||
Log.d(tag, "Notification received via userNotificationCenter(didReceive)", userInfo)
|
Log.d(tag, "Notification received via userNotificationCenter(didReceive)", userInfo)
|
||||||
store.saveNotification(fromUserInfo: userInfo)
|
|
||||||
completionHandler()
|
completionHandler()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,7 @@ class Store: ObservableObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
func delete(notifications: Set<Notification>) {
|
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 {
|
do {
|
||||||
notifications.forEach { notification in
|
notifications.forEach { notification in
|
||||||
context.delete(notification)
|
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() {
|
func rollbackAndRefresh() {
|
||||||
// Hack: We refresh all objects, since failing to store a notification usually means
|
// 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
|
// that the app extension stored the notification first. This is a way to update the
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,7 @@ class ApiService {
|
||||||
message: String,
|
message: String,
|
||||||
title: String,
|
title: String,
|
||||||
priority: Int = 3,
|
priority: Int = 3,
|
||||||
tags: [String] = [],
|
tags: [String] = []
|
||||||
completionHandler: @escaping (Notification?, Error?) -> Void
|
|
||||||
) {
|
) {
|
||||||
guard let url = URL(string: subscription.urlString()) else { return }
|
guard let url = URL(string: subscription.urlString()) else { return }
|
||||||
var request = URLRequest(url: url)
|
var request = URLRequest(url: url)
|
||||||
|
|
|
||||||
|
|
@ -44,18 +44,7 @@ struct NotificationListView: View {
|
||||||
Menu {
|
Menu {
|
||||||
editButton
|
editButton
|
||||||
Button("Send test notification") {
|
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"]
|
self.sendTestNotification()
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Button("Clear all notifications") {
|
Button("Clear all notifications") {
|
||||||
self.showAlert = true
|
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?"),
|
message: Text("Do you really want to delete all of the notifications in this topic?"),
|
||||||
primaryButton: .destructive(
|
primaryButton: .destructive(
|
||||||
Text("Permanently delete"),
|
Text("Permanently delete"),
|
||||||
action: {
|
action: deleteAll
|
||||||
//Database.current.deleteNotificationsForSubscription(subscription: subscription)
|
),
|
||||||
//viewModel.notifications = Database.current.getNotifications(subscription: subscription)
|
|
||||||
//subscription.loadNotifications()
|
|
||||||
}),
|
|
||||||
secondaryButton: .cancel())
|
secondaryButton: .cancel())
|
||||||
case .unsubscribe:
|
case .unsubscribe:
|
||||||
return Alert(
|
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?"),
|
message: Text("Do you really want to unsubscribe from this topic and delete all of the notifications you received?"),
|
||||||
primaryButton: .destructive(
|
primaryButton: .destructive(
|
||||||
Text("Unsubscribe"),
|
Text("Unsubscribe"),
|
||||||
action: {
|
action: unsubscribe
|
||||||
subscriptionManager.unsubscribe(subscription)
|
),
|
||||||
self.presentationMode.wrappedValue.dismiss()
|
|
||||||
}),
|
|
||||||
secondaryButton: .cancel())
|
secondaryButton: .cancel())
|
||||||
case .selected:
|
case .selected:
|
||||||
return Alert(
|
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() {
|
private func deleteSelected() {
|
||||||
store.delete(notifications: selection)
|
store.delete(notifications: selection)
|
||||||
selection = Set<Notification>()
|
selection = Set<Notification>()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue