fix: notification settings persist logic issue

This commit is contained in:
CMK 2021-09-29 18:42:52 +08:00
parent 9104602893
commit 0d7d659e01
4 changed files with 20 additions and 19 deletions

View File

@ -289,6 +289,9 @@ class SettingsViewController: UIViewController, NeedsDependency {
return return
} }
// clear badge before sign-out
context.notificationService.clearNotificationCountForActiveUser()
context.authenticationService.signOutMastodonUser( context.authenticationService.signOutMastodonUser(
domain: activeMastodonAuthenticationBox.domain, domain: activeMastodonAuthenticationBox.domain,
userID: activeMastodonAuthenticationBox.userID userID: activeMastodonAuthenticationBox.userID

View File

@ -18,13 +18,9 @@ extension APIService.CoreData {
setting: Setting, setting: Setting,
policy: Mastodon.API.Subscriptions.Policy policy: Mastodon.API.Subscriptions.Policy
) -> (subscription: Subscription, isCreated: Bool) { ) -> (subscription: Subscription, isCreated: Bool) {
let oldSubscription: Subscription? = { let oldSubscription = setting.subscriptions?.first(where: { subscription in
let request = Subscription.sortedFetchRequest subscription.policyRaw == policy.rawValue
request.predicate = Subscription.predicate(policyRaw: policy.rawValue) })
request.fetchLimit = 1
request.returnsObjectsAsFaults = false
return managedObjectContext.safeFetch(request).first
}()
if let oldSubscription = oldSubscription { if let oldSubscription = oldSubscription {
oldSubscription.setting = setting oldSubscription.setting = setting

View File

@ -27,7 +27,7 @@ final class NotificationService {
let applicationIconBadgeNeedsUpdate = CurrentValueSubject<Void, Never>(Void()) let applicationIconBadgeNeedsUpdate = CurrentValueSubject<Void, Never>(Void())
// output // output
/// [Token: UserID] /// [Token: NotificationViewModel]
let notificationSubscriptionDict: [String: NotificationViewModel] = [:] let notificationSubscriptionDict: [String: NotificationViewModel] = [:]
let unreadNotificationCountDidUpdate = CurrentValueSubject<Void, Never>(Void()) let unreadNotificationCountDidUpdate = CurrentValueSubject<Void, Never>(Void())
let requestRevealNotificationPublisher = PassthroughSubject<Mastodon.Entity.Notification.ID, Never>() let requestRevealNotificationPublisher = PassthroughSubject<Mastodon.Entity.Notification.ID, Never>()

View File

@ -44,19 +44,21 @@ final class SettingService {
.compactMap { [weak self] mastodonAuthenticationBoxes -> AnyPublisher<[MastodonAuthenticationBox], Never>? in .compactMap { [weak self] mastodonAuthenticationBoxes -> AnyPublisher<[MastodonAuthenticationBox], Never>? in
guard let self = self else { return nil } guard let self = self else { return nil }
guard let authenticationService = self.authenticationService else { return nil } guard let authenticationService = self.authenticationService else { return nil }
guard let activeMastodonAuthenticationBox = mastodonAuthenticationBoxes.first else { return nil }
let domain = activeMastodonAuthenticationBox.domain let managedObjectContext = authenticationService.backgroundManagedObjectContext
let userID = activeMastodonAuthenticationBox.userID return managedObjectContext.performChanges {
return authenticationService.backgroundManagedObjectContext.performChanges { for authenticationBox in mastodonAuthenticationBoxes {
let domain = authenticationBox.domain
let userID = authenticationBox.userID
_ = APIService.CoreData.createOrMergeSetting( _ = APIService.CoreData.createOrMergeSetting(
into: authenticationService.backgroundManagedObjectContext, into: managedObjectContext,
property: Setting.Property( property: Setting.Property(
domain: domain, domain: domain,
userID: userID, userID: userID,
appearanceRaw: SettingsItem.AppearanceMode.automatic.rawValue appearanceRaw: SettingsItem.AppearanceMode.automatic.rawValue
) )
) )
} // end for
} }
.map { _ in mastodonAuthenticationBoxes } .map { _ in mastodonAuthenticationBoxes }
.eraseToAnyPublisher() .eraseToAnyPublisher()