mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2025-02-04 00:17:30 +01:00
Better handling of notifications
This commit is contained in:
parent
90199fc88b
commit
3d96d6997d
@ -57,9 +57,9 @@ struct IceCubesApp: App {
|
|||||||
.edgesIgnoringSafeArea(.bottom)
|
.edgesIgnoringSafeArea(.bottom)
|
||||||
.background(TransparentBackground())
|
.background(TransparentBackground())
|
||||||
})
|
})
|
||||||
.onChange(of: pushNotificationsService.routedNotification) { notification in
|
.onChange(of: pushNotificationsService.handleNotification) { notification in
|
||||||
if notification != nil {
|
if notification != nil {
|
||||||
pushNotificationsService.routedNotification = nil
|
pushNotificationsService.handleNotification = nil
|
||||||
selectedTab = .notifications
|
selectedTab = .notifications
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,13 +54,16 @@ struct NotificationsTab: View {
|
|||||||
routerPath.path = []
|
routerPath.path = []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onChange(of: pushNotificationsService.routedNotification) { notification in
|
.onChange(of: pushNotificationsService.handleNotification) { notification in
|
||||||
if let notification {
|
if let notification, let type = notification.supportedType {
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
if let account = notification.accountId {
|
switch type {
|
||||||
routerPath.navigate(to: .accountDetail(id: account))
|
case .follow, .follow_request:
|
||||||
} else if let status = notification.statusId {
|
routerPath.navigate(to: .accountDetailWithAccount(account: notification.account))
|
||||||
routerPath.navigate(to: .statusDetail(id: status))
|
default:
|
||||||
|
if let status = notification.status {
|
||||||
|
routerPath.navigate(to: .statusDetailWithStatus(status: status))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,18 +26,12 @@ public class PushNotificationsService: NSObject, ObservableObject {
|
|||||||
static let keychainPrivateKey = "notifications_private_key"
|
static let keychainPrivateKey = "notifications_private_key"
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct RoutedNotification: Equatable {
|
|
||||||
public let accessToken: String
|
|
||||||
public let statusId: String?
|
|
||||||
public let accountId: String?
|
|
||||||
}
|
|
||||||
|
|
||||||
public static let shared = PushNotificationsService()
|
public static let shared = PushNotificationsService()
|
||||||
|
|
||||||
public private(set) var subscriptions: [PushNotificationSubscriptionSettings] = []
|
public private(set) var subscriptions: [PushNotificationSubscriptionSettings] = []
|
||||||
|
|
||||||
@Published public var pushToken: Data?
|
@Published public var pushToken: Data?
|
||||||
@Published public var routedNotification: RoutedNotification?
|
@Published public var handleNotification: Models.Notification?
|
||||||
|
|
||||||
override init() {
|
override init() {
|
||||||
super.init()
|
super.init()
|
||||||
@ -137,21 +131,16 @@ public class PushNotificationsService: NSObject, ObservableObject {
|
|||||||
extension PushNotificationsService: UNUserNotificationCenterDelegate {
|
extension PushNotificationsService: UNUserNotificationCenterDelegate {
|
||||||
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
|
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
|
||||||
guard let plaintext = response.notification.request.content.userInfo["plaintext"] as? Data,
|
guard let plaintext = response.notification.request.content.userInfo["plaintext"] as? Data,
|
||||||
let mastodonPushNotification = try? JSONDecoder().decode(MastodonPushNotification.self, from: plaintext) else {
|
let mastodonPushNotification = try? JSONDecoder().decode(MastodonPushNotification.self, from: plaintext),
|
||||||
|
let account = subscriptions.first(where: { $0.account.token.accessToken == mastodonPushNotification.accessToken }) else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if let type = Models.Notification.NotificationType(rawValue: mastodonPushNotification.notificationType) {
|
do {
|
||||||
switch type {
|
let client = Client(server: account.account.server, oauthToken: account.account.token)
|
||||||
case .follow, .follow_request:
|
let notification: Models.Notification =
|
||||||
self.routedNotification = .init(accessToken: mastodonPushNotification.accessToken,
|
try await client.get(endpoint: Notifications.notification(id:String(mastodonPushNotification.notificationID)))
|
||||||
statusId: nil,
|
self.handleNotification = notification
|
||||||
accountId: String(mastodonPushNotification.notificationID))
|
} catch { }
|
||||||
default:
|
|
||||||
self.routedNotification = .init(accessToken: mastodonPushNotification.accessToken,
|
|
||||||
statusId: String(mastodonPushNotification.notificationID),
|
|
||||||
accountId: nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public struct Notification: Decodable, Identifiable {
|
public struct Notification: Decodable, Identifiable, Equatable {
|
||||||
public enum NotificationType: String, CaseIterable {
|
public enum NotificationType: String, CaseIterable {
|
||||||
case follow, follow_request, mention, reblog, status, favourite, poll, update
|
case follow, follow_request, mention, reblog, status, favourite, poll, update
|
||||||
}
|
}
|
||||||
|
@ -5,12 +5,15 @@ public enum Notifications: Endpoint {
|
|||||||
maxId: String?,
|
maxId: String?,
|
||||||
types: [String]?,
|
types: [String]?,
|
||||||
limit: Int)
|
limit: Int)
|
||||||
|
case notification(id: String)
|
||||||
case clear
|
case clear
|
||||||
|
|
||||||
public func path() -> String {
|
public func path() -> String {
|
||||||
switch self {
|
switch self {
|
||||||
case .notifications:
|
case .notifications:
|
||||||
return "notifications"
|
return "notifications"
|
||||||
|
case let .notification(id):
|
||||||
|
return "notifications/\(id)"
|
||||||
case .clear:
|
case .clear:
|
||||||
return "notifications/clear"
|
return "notifications/clear"
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user