mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-22 00:01:00 +00:00
Notification registration improvements
This commit is contained in:
parent
741545fc1f
commit
366ea17d04
3 changed files with 35 additions and 16 deletions
|
@ -210,7 +210,9 @@ public extension IdentityDatabase {
|
|||
func fetchIdentitiesWithOutdatedDeviceTokens(deviceToken: Data) -> AnyPublisher<[Identity], Error> {
|
||||
databaseWriter.readPublisher(
|
||||
value: IdentityInfo.request(IdentityRecord.order(IdentityRecord.Columns.lastUsedAt.desc))
|
||||
.filter(IdentityRecord.Columns.lastRegisteredDeviceToken != deviceToken)
|
||||
.filter(IdentityRecord.Columns.authenticated == true
|
||||
&& IdentityRecord.Columns.pending == false
|
||||
&& IdentityRecord.Columns.lastRegisteredDeviceToken != deviceToken)
|
||||
.fetchAll)
|
||||
.map { $0.map(Identity.init(info:)) }
|
||||
.eraseToAnyPublisher()
|
||||
|
|
|
@ -5,7 +5,7 @@ import Foundation
|
|||
import UserNotifications
|
||||
|
||||
public struct UserNotificationService {
|
||||
let events: AnyPublisher<UserNotificationClient.DelegateEvent, Never>
|
||||
public let events: AnyPublisher<Event, Never>
|
||||
|
||||
private let userNotificationClient: UserNotificationClient
|
||||
|
||||
|
@ -16,12 +16,14 @@ public struct UserNotificationService {
|
|||
}
|
||||
|
||||
public extension UserNotificationService {
|
||||
func isAuthorized() -> AnyPublisher<Bool, Error> {
|
||||
typealias Event = UserNotificationClient.DelegateEvent
|
||||
|
||||
func isAuthorized(request: Bool) -> AnyPublisher<Bool, Error> {
|
||||
getNotificationSettings()
|
||||
.map(\.authorizationStatus)
|
||||
.flatMap { status -> AnyPublisher<Bool, Error> in
|
||||
if status == .notDetermined {
|
||||
return requestProvisionalAuthorization().eraseToAnyPublisher()
|
||||
if request, status == .notDetermined {
|
||||
return requestAuthorization().eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
return Just(status == .authorized || status == .provisional)
|
||||
|
@ -40,9 +42,9 @@ private extension UserNotificationService {
|
|||
.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
func requestProvisionalAuthorization() -> AnyPublisher<Bool, Error> {
|
||||
func requestAuthorization() -> AnyPublisher<Bool, Error> {
|
||||
Future<Bool, Error> { promise in
|
||||
userNotificationClient.requestAuthorization([.alert, .sound, .badge, .provisional]) { granted, error in
|
||||
userNotificationClient.requestAuthorization([.alert, .sound, .badge]) { granted, error in
|
||||
if let error = error {
|
||||
promise(.failure(error))
|
||||
} else {
|
||||
|
|
|
@ -32,13 +32,17 @@ public final class RootViewModel: ObservableObject {
|
|||
.sink { [weak self] in self?.identitySelected(id: $0) }
|
||||
.store(in: &cancellables)
|
||||
|
||||
userNotificationService.isAuthorized()
|
||||
userNotificationService.isAuthorized(request: false)
|
||||
.filter { $0 }
|
||||
.zip(registerForRemoteNotifications())
|
||||
.map { $1 }
|
||||
.flatMap(allIdentitiesService.updatePushSubscriptions(deviceToken:))
|
||||
.sink { _ in } receiveValue: { _ in }
|
||||
.store(in: &cancellables)
|
||||
|
||||
userNotificationService.events
|
||||
.sink { [weak self] in self?.handle(event: $0) }
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -112,17 +116,28 @@ private extension RootViewModel {
|
|||
.sink { _ in } receiveValue: { _ in }
|
||||
.store(in: &self.cancellables)
|
||||
|
||||
self.userNotificationService.isAuthorized()
|
||||
.filter { $0 }
|
||||
.zip(self.registerForRemoteNotifications())
|
||||
.filter { identityContext.identity.lastRegisteredDeviceToken != $1 }
|
||||
.map { ($1, identityContext.identity.pushSubscriptionAlerts) }
|
||||
.flatMap(identityContext.service.createPushSubscription(deviceToken:alerts:))
|
||||
.sink { _ in } receiveValue: { _ in }
|
||||
.store(in: &self.cancellables)
|
||||
if identityContext.identity.authenticated && !identityContext.identity.pending {
|
||||
self.userNotificationService.isAuthorized(request: true)
|
||||
.filter { $0 }
|
||||
.zip(self.registerForRemoteNotifications())
|
||||
.filter { identityContext.identity.lastRegisteredDeviceToken != $1 }
|
||||
.map { ($1, identityContext.identity.pushSubscriptionAlerts) }
|
||||
.flatMap(identityContext.service.createPushSubscription(deviceToken:alerts:))
|
||||
.sink { _ in } receiveValue: { _ in }
|
||||
.store(in: &self.cancellables)
|
||||
}
|
||||
|
||||
return NavigationViewModel(identityContext: identityContext)
|
||||
}
|
||||
.assign(to: &$navigationViewModel)
|
||||
}
|
||||
|
||||
func handle(event: UserNotificationService.Event) {
|
||||
switch event {
|
||||
case let .willPresentNotification(_, completionHandler):
|
||||
completionHandler(.banner)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue