feat: show received notification status

This commit is contained in:
CMK 2021-04-27 17:45:11 +08:00
parent ed9c2ddd8f
commit 125f6d0a27
5 changed files with 83 additions and 1 deletions

View File

@ -167,6 +167,16 @@ extension MainTabBarController {
notificationViewController.navigationController?.tabBarItem.image = image
}
.store(in: &disposeBag)
context.notificationService.requestRevealNotificationPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] notificationID in
guard let self = self else { return }
self.coordinator.switchToTabBar(tab: .notification)
let threadViewModel = RemoteThreadViewModel(context: self.context, notificationID: notificationID)
self.coordinator.present(scene: .thread(viewModel: threadViewModel), from: nil, transition: .show)
}
.store(in: &disposeBag)
}
}

View File

@ -47,4 +47,43 @@ final class RemoteThreadViewModel: ThreadViewModel {
}
.store(in: &disposeBag)
}
// FIXME: multiple account supports
init(context: AppContext, notificationID: Mastodon.Entity.Notification.ID) {
super.init(context: context, optionalStatus: nil)
guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else {
return
}
let domain = activeMastodonAuthenticationBox.domain
context.apiService.notification(
notificationID: notificationID,
mastodonAuthenticationBox: activeMastodonAuthenticationBox
)
.retry(3)
.sink { completion in
switch completion {
case .failure(let error):
// TODO: handle error
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote notification %s fetch failed: %s", ((#file as NSString).lastPathComponent), #line, #function, notificationID, error.localizedDescription)
case .finished:
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote notification %s fetched", ((#file as NSString).lastPathComponent), #line, #function, notificationID)
}
} receiveValue: { [weak self] response in
guard let self = self else { return }
guard let statusID = response.value.status?.id else { return }
let managedObjectContext = context.managedObjectContext
let request = Status.sortedFetchRequest
request.fetchLimit = 1
request.predicate = Status.predicate(domain: domain, id: statusID)
guard let status = managedObjectContext.safeFetch(request).first else {
assertionFailure()
return
}
self.rootItem.value = .root(statusObjectID: status.objectID, attribute: Item.StatusAttribute())
}
.store(in: &disposeBag)
}
}

View File

@ -78,6 +78,34 @@ extension APIService {
notificationID: notificationID,
authorization: authorization
)
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Notification>, Error> in
guard let status = response.value.status else {
return Just(response)
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
}
return APIService.Persist.persistStatus(
managedObjectContext: self.backgroundManagedObjectContext,
domain: domain,
query: nil,
response: response.map { _ in [status] },
persistType: .lookUp,
requestMastodonUserID: nil,
log: OSLog.api
)
.setFailureType(to: Error.self)
.tryMap { result -> Mastodon.Response.Content<Mastodon.Entity.Notification> in
switch result {
case .success:
return response
case .failure(let error):
throw error
}
}
.eraseToAnyPublisher()
}
.eraseToAnyPublisher()
}
}

View File

@ -29,6 +29,7 @@ final class NotificationService {
/// [Token: UserID]
let notificationSubscriptionDict: [String: NotificationViewModel] = [:]
let hasUnreadPushNotification = CurrentValueSubject<Bool, Never>(false)
let requestRevealNotificationPublisher = PassthroughSubject<Mastodon.Entity.Notification.ID, Never>()
init(
apiService: APIService,

View File

@ -61,6 +61,8 @@ extension AppDelegate {
// MARK: - UNUserNotificationCenterDelegate
extension AppDelegate: UNUserNotificationCenterDelegate {
// notification present in the foreground
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
@ -78,6 +80,7 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
completionHandler([.sound])
}
// response to user action for notification
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
@ -93,7 +96,7 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
let notificationID = String(mastodonPushNotification.notificationID)
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: [Push Notification] notification %s", ((#file as NSString).lastPathComponent), #line, #function, notificationID)
appContext.notificationService.handlePushNotification(notificationID: notificationID)
appContext.notificationService.requestRevealNotificationPublisher.send(notificationID)
completionHandler()
}
@ -105,6 +108,7 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
return mastodonPushNotification
}
}
extension AppContext {