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
}
// clear badge before sign-out
context.notificationService.clearNotificationCountForActiveUser()
context.authenticationService.signOutMastodonUser(
domain: activeMastodonAuthenticationBox.domain,
userID: activeMastodonAuthenticationBox.userID

View File

@ -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

View File

@ -27,7 +27,7 @@ final class NotificationService {
let applicationIconBadgeNeedsUpdate = CurrentValueSubject<Void, Never>(Void())
// output
/// [Token: UserID]
/// [Token: NotificationViewModel]
let notificationSubscriptionDict: [String: NotificationViewModel] = [:]
let unreadNotificationCountDidUpdate = CurrentValueSubject<Void, Never>(Void())
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
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()