feat: add unread notification application shortcut

This commit is contained in:
CMK 2022-07-22 18:39:36 +08:00
parent 100d189e8e
commit b61e7518b5
2 changed files with 40 additions and 0 deletions

View File

@ -12,9 +12,12 @@ import CoreData
import CoreDataStack
import MastodonSDK
import AppShared
import MastodonLocalization
final class NotificationService {
public static let unreadShortcutItemIdentifier = "org.joinmastodon.app.NotificationService.unread-shortcut"
var disposeBag = Set<AnyCancellable>()
let workingQueue = DispatchQueue(label: "org.joinmastodon.app.NotificationService.working-queue")
@ -74,6 +77,9 @@ final class NotificationService {
UserDefaults.shared.notificationBadgeCount = count
UIApplication.shared.applicationIconBadgeNumber = count
Task { @MainActor in
UIApplication.shared.shortcutItems = try? await self.unreadApplicationShortcutItems()
}
self.unreadNotificationCountDidUpdate.send()
}
@ -100,6 +106,38 @@ extension NotificationService {
}
}
extension NotificationService {
public func unreadApplicationShortcutItems() async throws -> [UIApplicationShortcutItem] {
guard let authenticationService = self.authenticationService else { return [] }
let managedObjectContext = authenticationService.managedObjectContext
return try await managedObjectContext.perform {
var items: [UIApplicationShortcutItem] = []
for object in authenticationService.mastodonAuthentications.value {
guard let authentication = managedObjectContext.object(with: object.objectID) as? MastodonAuthentication else { continue }
let accessToken = authentication.userAccessToken
let count = UserDefaults.shared.getNotificationCountWithAccessToken(accessToken: accessToken)
guard count > 0 else { continue }
let title = "@\(authentication.user.acctWithDomain)"
let subtitle = L10n.A11y.Plural.Count.Unread.notification(count)
let item = UIApplicationShortcutItem(
type: NotificationService.unreadShortcutItemIdentifier,
localizedTitle: title,
localizedSubtitle: subtitle,
icon: nil,
userInfo: [
"accessToken": accessToken as NSSecureCoding
]
)
items.append(item)
}
return items
}
}
}
extension NotificationService {
func dequeueNotificationViewModel(

View File

@ -142,6 +142,8 @@ extension SceneDelegate {
logger.debug("\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(shortcutItem.type)")
switch shortcutItem.type {
case NotificationService.unreadShortcutItemIdentifier:
coordinator?.switchToTabBar(tab: .notification)
case "org.joinmastodon.app.new-post":
if coordinator?.tabBarController.topMost is ComposeViewController {
logger.debug("\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): composing…")