diff --git a/Mastodon/Scene/Settings/SettingsViewController.swift b/Mastodon/Scene/Settings/SettingsViewController.swift index c4658d7da..3b4e522e6 100644 --- a/Mastodon/Scene/Settings/SettingsViewController.swift +++ b/Mastodon/Scene/Settings/SettingsViewController.swift @@ -289,6 +289,9 @@ class SettingsViewController: UIViewController, NeedsDependency { return } + // clear badge before sign-out + context.notificationService.clearNotificationCountForActiveUser() + context.authenticationService.signOutMastodonUser( domain: activeMastodonAuthenticationBox.domain, userID: activeMastodonAuthenticationBox.userID diff --git a/Mastodon/Service/APIService/CoreData/APIService+CoreData+Subscriptions.swift b/Mastodon/Service/APIService/CoreData/APIService+CoreData+Subscriptions.swift index 6eebc9e56..d5958cf8f 100644 --- a/Mastodon/Service/APIService/CoreData/APIService+CoreData+Subscriptions.swift +++ b/Mastodon/Service/APIService/CoreData/APIService+CoreData+Subscriptions.swift @@ -18,13 +18,9 @@ extension APIService.CoreData { setting: Setting, policy: Mastodon.API.Subscriptions.Policy ) -> (subscription: Subscription, isCreated: Bool) { - let oldSubscription: Subscription? = { - let request = Subscription.sortedFetchRequest - request.predicate = Subscription.predicate(policyRaw: policy.rawValue) - request.fetchLimit = 1 - request.returnsObjectsAsFaults = false - return managedObjectContext.safeFetch(request).first - }() + let oldSubscription = setting.subscriptions?.first(where: { subscription in + subscription.policyRaw == policy.rawValue + }) if let oldSubscription = oldSubscription { oldSubscription.setting = setting diff --git a/Mastodon/Service/NotificationService.swift b/Mastodon/Service/NotificationService.swift index 6437e1b66..463a2def8 100644 --- a/Mastodon/Service/NotificationService.swift +++ b/Mastodon/Service/NotificationService.swift @@ -27,7 +27,7 @@ final class NotificationService { let applicationIconBadgeNeedsUpdate = CurrentValueSubject(Void()) // output - /// [Token: UserID] + /// [Token: NotificationViewModel] let notificationSubscriptionDict: [String: NotificationViewModel] = [:] let unreadNotificationCountDidUpdate = CurrentValueSubject(Void()) let requestRevealNotificationPublisher = PassthroughSubject() diff --git a/Mastodon/Service/SettingService.swift b/Mastodon/Service/SettingService.swift index 5985f6fce..1c030c519 100644 --- a/Mastodon/Service/SettingService.swift +++ b/Mastodon/Service/SettingService.swift @@ -44,19 +44,21 @@ final class SettingService { .compactMap { [weak self] mastodonAuthenticationBoxes -> AnyPublisher<[MastodonAuthenticationBox], Never>? in guard let self = self else { return nil } guard let authenticationService = self.authenticationService else { return nil } - guard let activeMastodonAuthenticationBox = mastodonAuthenticationBoxes.first else { return nil } - let domain = activeMastodonAuthenticationBox.domain - let userID = activeMastodonAuthenticationBox.userID - return authenticationService.backgroundManagedObjectContext.performChanges { - _ = APIService.CoreData.createOrMergeSetting( - into: authenticationService.backgroundManagedObjectContext, - property: Setting.Property( - domain: domain, - userID: userID, - appearanceRaw: SettingsItem.AppearanceMode.automatic.rawValue + let managedObjectContext = authenticationService.backgroundManagedObjectContext + return managedObjectContext.performChanges { + for authenticationBox in mastodonAuthenticationBoxes { + let domain = authenticationBox.domain + let userID = authenticationBox.userID + _ = APIService.CoreData.createOrMergeSetting( + into: managedObjectContext, + property: Setting.Property( + domain: domain, + userID: userID, + appearanceRaw: SettingsItem.AppearanceMode.automatic.rawValue + ) ) - ) + } // end for } .map { _ in mastodonAuthenticationBoxes } .eraseToAnyPublisher()