Notification navigation wip

This commit is contained in:
Justin Mazzocchi 2021-02-04 12:09:05 -08:00
parent 03b17d21ee
commit 1359b80a8e
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
6 changed files with 81 additions and 13 deletions

View file

@ -145,6 +145,7 @@
"main-navigation.notifications" = "Notifications"; "main-navigation.notifications" = "Notifications";
"main-navigation.conversations" = "Messages"; "main-navigation.conversations" = "Messages";
"metatext" = "Metatext"; "metatext" = "Metatext";
"notification.signed-in-as-%@" = "Signed in as %@";
"notifications.all" = "All"; "notifications.all" = "All";
"notifications.mentions" = "Mentions"; "notifications.mentions" = "Mentions";
"ok" = "OK"; "ok" = "OK";

View file

@ -55,7 +55,7 @@ final class NotificationService: UNNotificationServiceExtension {
} }
if appPreferences.notificationPictures { if appPreferences.notificationPictures {
Self.addImage(pushNotification: pushNotification, Self.addImage(url: pushNotification.icon,
bestAttemptContent: bestAttemptContent, bestAttemptContent: bestAttemptContent,
contentHandler: contentHandler) contentHandler: contentHandler)
} else { } else {
@ -71,14 +71,14 @@ final class NotificationService: UNNotificationServiceExtension {
} }
private extension NotificationService { private extension NotificationService {
static func addImage(pushNotification: PushNotification, static func addImage(url: URL,
bestAttemptContent: UNMutableNotificationContent, bestAttemptContent: UNMutableNotificationContent,
contentHandler: @escaping (UNNotificationContent) -> Void) { contentHandler: @escaping (UNNotificationContent) -> Void) {
let fileName = pushNotification.icon.lastPathComponent let fileName = url.lastPathComponent
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) let fileURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(fileName) .appendingPathComponent(fileName)
KingfisherManager.shared.retrieveImage(with: pushNotification.icon) { KingfisherManager.shared.retrieveImage(with: url) {
switch $0 { switch $0 {
case let .success(result): case let .success(result):
let format: ImageFormat let format: ImageFormat

View file

@ -17,6 +17,10 @@ public struct UserNotificationService {
public extension UserNotificationService { public extension UserNotificationService {
typealias Event = UserNotificationClient.DelegateEvent typealias Event = UserNotificationClient.DelegateEvent
typealias Content = UNNotificationContent
typealias MutableContent = UNMutableNotificationContent
typealias Trigger = UNNotificationTrigger
typealias Request = UNNotificationRequest
func isAuthorized(request: Bool) -> AnyPublisher<Bool, Error> { func isAuthorized(request: Bool) -> AnyPublisher<Bool, Error> {
getNotificationSettings() getNotificationSettings()
@ -32,6 +36,24 @@ public extension UserNotificationService {
} }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func add(request: Request) -> AnyPublisher<Never, Error> {
Future<Void, Error> { promise in
userNotificationClient.add(request) { error in
if let error = error {
promise(.failure(error))
} else {
promise(.success(()))
}
}
}
.ignoreOutput()
.eraseToAnyPublisher()
}
func removeDeliveredNotifications(withIdentifiers identifiers: [String]) {
userNotificationClient.removeDeliveredNotifications(identifiers)
}
} }
private extension UserNotificationService { private extension UserNotificationService {

View file

@ -10,16 +10,22 @@ public struct UserNotificationClient {
case openSettingsForNotification(UNNotification?) case openSettingsForNotification(UNNotification?)
} }
public var getNotificationSettings: (@escaping (UNNotificationSettings) -> Void) -> Void public let getNotificationSettings: (@escaping (UNNotificationSettings) -> Void) -> Void
public var requestAuthorization: (UNAuthorizationOptions, @escaping (Bool, Error?) -> Void) -> Void public let requestAuthorization: (UNAuthorizationOptions, @escaping (Bool, Error?) -> Void) -> Void
public var delegateEvents: AnyPublisher<DelegateEvent, Never> public let add: (UNNotificationRequest, ((Error?) -> Void)?) -> Void
public let removeDeliveredNotifications: ([String]) -> Void
public let delegateEvents: AnyPublisher<DelegateEvent, Never>
public init( public init(
getNotificationSettings: @escaping (@escaping (UNNotificationSettings) -> Void) -> Void, getNotificationSettings: @escaping (@escaping (UNNotificationSettings) -> Void) -> Void,
requestAuthorization: @escaping (UNAuthorizationOptions, @escaping (Bool, Error?) -> Void) -> Void, requestAuthorization: @escaping (UNAuthorizationOptions, @escaping (Bool, Error?) -> Void) -> Void,
add: @escaping (UNNotificationRequest, ((Error?) -> Void)?) -> Void,
removeDeliveredNotifications: @escaping ([String]) -> Void,
delegateEvents: AnyPublisher<DelegateEvent, Never>) { delegateEvents: AnyPublisher<DelegateEvent, Never>) {
self.getNotificationSettings = getNotificationSettings self.getNotificationSettings = getNotificationSettings
self.requestAuthorization = requestAuthorization self.requestAuthorization = requestAuthorization
self.add = add
self.removeDeliveredNotifications = removeDeliveredNotifications
self.delegateEvents = delegateEvents self.delegateEvents = delegateEvents
} }
} }
@ -59,6 +65,8 @@ extension UserNotificationClient {
return UserNotificationClient( return UserNotificationClient(
getNotificationSettings: userNotificationCenter.getNotificationSettings, getNotificationSettings: userNotificationCenter.getNotificationSettings,
requestAuthorization: userNotificationCenter.requestAuthorization, requestAuthorization: userNotificationCenter.requestAuthorization,
add: userNotificationCenter.add(_:withCompletionHandler:),
removeDeliveredNotifications: userNotificationCenter.removeDeliveredNotifications(withIdentifiers:),
delegateEvents: subject delegateEvents: subject
.handleEvents(receiveCancel: { delegate = nil }) .handleEvents(receiveCancel: { delegate = nil })
.eraseToAnyPublisher()) .eraseToAnyPublisher())

View file

@ -7,5 +7,7 @@ public extension UserNotificationClient {
static let mock = UserNotificationClient( static let mock = UserNotificationClient(
getNotificationSettings: { _ in }, getNotificationSettings: { _ in },
requestAuthorization: { _, _ in }, requestAuthorization: { _, _ in },
add: { _, completion in completion?(nil) },
removeDeliveredNotifications: { _ in },
delegateEvents: Empty(completeImmediately: false).eraseToAnyPublisher()) delegateEvents: Empty(completeImmediately: false).eraseToAnyPublisher())
} }

View file

@ -36,7 +36,7 @@ public final class RootViewModel: ObservableObject {
.replaceError(with: nil) .replaceError(with: nil)
.assign(to: &$mostRecentlyUsedIdentityId) .assign(to: &$mostRecentlyUsedIdentityId)
identitySelected(id: mostRecentlyUsedIdentityId, immediate: true) identitySelected(id: mostRecentlyUsedIdentityId, immediate: true, notify: false)
allIdentitiesService.identitiesCreated allIdentitiesService.identitiesCreated
.sink { [weak self] in self?.identitySelected(id: $0) } .sink { [weak self] in self?.identitySelected(id: $0) }
@ -50,7 +50,7 @@ public final class RootViewModel: ObservableObject {
public extension RootViewModel { public extension RootViewModel {
func identitySelected(id: Identity.Id?) { func identitySelected(id: Identity.Id?) {
identitySelected(id: id, immediate: false) identitySelected(id: id, immediate: false, notify: false)
} }
func deleteIdentity(id: Identity.Id) { func deleteIdentity(id: Identity.Id) {
@ -80,7 +80,12 @@ public extension RootViewModel {
} }
private extension RootViewModel { private extension RootViewModel {
func identitySelected(id: Identity.Id?, immediate: Bool) { static let identityChangeNotificationUserInfoKey =
"com.metabolist.metatext.identity-change-notification-user-info-key"
static let removeIdentityChangeNotificationAfter = DispatchTimeInterval.seconds(10)
// swiftlint:disable:next function_body_length
func identitySelected(id: Identity.Id?, immediate: Bool, notify: Bool) {
navigationViewModel?.presentingSecondaryNavigation = false navigationViewModel?.presentingSecondaryNavigation = false
guard guard
@ -95,7 +100,9 @@ private extension RootViewModel {
.catch { [weak self] _ -> Empty<Identity, Never> in .catch { [weak self] _ -> Empty<Identity, Never> in
DispatchQueue.main.async { DispatchQueue.main.async {
if self?.navigationViewModel?.identityContext.identity.id == id { if self?.navigationViewModel?.identityContext.identity.id == id {
self?.identitySelected(id: self?.mostRecentlyUsedIdentityId, immediate: false) self?.identitySelected(id: self?.mostRecentlyUsedIdentityId,
immediate: false,
notify: true)
} }
} }
@ -131,6 +138,10 @@ private extension RootViewModel {
.store(in: &self.cancellables) .store(in: &self.cancellables)
} }
if notify {
self.notifyIdentityChange(identityContext: identityContext)
}
return NavigationViewModel(identityContext: identityContext) return NavigationViewModel(identityContext: identityContext)
} }
.assign(to: &$navigationViewModel) .assign(to: &$navigationViewModel)
@ -138,8 +149,15 @@ private extension RootViewModel {
func handle(event: UserNotificationService.Event) { func handle(event: UserNotificationService.Event) {
switch event { switch event {
case let .willPresentNotification(_, completionHandler): case let .willPresentNotification(notification, completionHandler):
completionHandler(.banner) completionHandler(.banner)
if notification.request.content.userInfo[Self.identityChangeNotificationUserInfoKey] as? Bool == true {
DispatchQueue.main.asyncAfter(deadline: .now() + Self.removeIdentityChangeNotificationAfter) {
self.userNotificationService.removeDeliveredNotifications(
withIdentifiers: [notification.request.identifier])
}
}
case let .didReceiveResponse(response, completionHandler): case let .didReceiveResponse(response, completionHandler):
let userInfo = response.notification.request.content.userInfo let userInfo = response.notification.request.content.userInfo
@ -157,6 +175,23 @@ private extension RootViewModel {
} }
func handle(pushNotification: PushNotification, identityId: Identity.Id) { func handle(pushNotification: PushNotification, identityId: Identity.Id) {
// TODO if identityId != navigationViewModel?.identityContext.identity.id {
identitySelected(id: identityId, immediate: false, notify: true)
}
}
func notifyIdentityChange(identityContext: IdentityContext) {
let content = UserNotificationService.MutableContent()
content.body = String.localizedStringWithFormat(
NSLocalizedString("notification.signed-in-as-%@", comment: ""),
identityContext.identity.handle)
content.userInfo[Self.identityChangeNotificationUserInfoKey] = true
let request = UserNotificationService.Request(identifier: UUID().uuidString, content: content, trigger: nil)
userNotificationService.add(request: request)
.sink { _ in } receiveValue: { _ in }
.store(in: &cancellables)
} }
} }