Standardize notification handling on Combine instead of selectors

This commit is contained in:
Maurice Parker
2020-07-18 15:20:15 -05:00
parent 5845925b3a
commit 876f978347
5 changed files with 116 additions and 154 deletions

View File

@@ -63,22 +63,22 @@ class AccountsPreferencesModel: ObservableObject {
@Published var showDeleteConfirmation: Bool = false
// Subscriptions
var notificationSubscriptions = Set<AnyCancellable>()
var cancellables = Set<AnyCancellable>()
init() {
sortedAccounts = AccountManager.shared.sortedAccounts
NotificationCenter.default.publisher(for: .UserDidAddAccount).sink(receiveValue: { [weak self] _ in
NotificationCenter.default.publisher(for: .UserDidAddAccount).sink { [weak self] _ in
self?.sortedAccounts = AccountManager.shared.sortedAccounts
}).store(in: &notificationSubscriptions)
}.store(in: &cancellables)
NotificationCenter.default.publisher(for: .UserDidDeleteAccount).sink(receiveValue: { [weak self] _ in
NotificationCenter.default.publisher(for: .UserDidDeleteAccount).sink { [weak self] _ in
self?.selectedConfiguredAccountID = nil
self?.sortedAccounts = AccountManager.shared.sortedAccounts
self?.selectedConfiguredAccountID = AccountManager.shared.defaultAccount.accountID
}).store(in: &notificationSubscriptions)
}.store(in: &cancellables)
NotificationCenter.default.publisher(for: .AccountStateDidChange).sink(receiveValue: { [weak self] notification in
NotificationCenter.default.publisher(for: .AccountStateDidChange).sink { [weak self] notification in
guard let account = notification.object as? Account else {
return
}
@@ -87,7 +87,7 @@ class AccountsPreferencesModel: ObservableObject {
self?.accountIsActive = account.isActive
self?.accountName = account.name ?? ""
}
}).store(in: &notificationSubscriptions)
}.store(in: &cancellables)
}
}