Refactoring

This commit is contained in:
Justin Mazzocchi 2020-08-14 17:14:21 -07:00
parent 888604497c
commit c64fa26b09
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
2 changed files with 11 additions and 16 deletions

View file

@ -85,6 +85,11 @@ extension IdentityService {
func updatePreferences(_ preferences: Identity.Preferences) -> AnyPublisher<Void, Error> {
identityDatabase.updatePreferences(preferences, forIdentityID: identity.id)
.zip(Just(self).first().setFailureType(to: Error.self))
.filter { $1.identity.preferences.useServerPostingReadingPreferences }
.map { _ in () }
.flatMap(refreshServerPreferences)
.eraseToAnyPublisher()
}
func createPushSubscription(deviceToken: String, alerts: PushSubscription.Alerts) -> AnyPublisher<Void, Error> {

View file

@ -4,19 +4,8 @@ import Foundation
import Combine
class PostingReadingPreferencesViewModel: ObservableObject {
@Published var preferences: Identity.Preferences {
didSet {
if preferences.useServerPostingReadingPreferences {
identityService.refreshServerPreferences()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.store(in: &cancellables)
}
}
}
@Published var preferences: Identity.Preferences
@Published var alertItem: AlertItem?
let handle: String
private let identityService: IdentityService
private var cancellables = Set<AnyCancellable>()
@ -24,17 +13,18 @@ class PostingReadingPreferencesViewModel: ObservableObject {
init(identityService: IdentityService) {
self.identityService = identityService
preferences = identityService.identity.preferences
handle = identityService.identity.handle
identityService.$identity.map(\.preferences)
identityService.$identity
.map(\.preferences)
.dropFirst()
.removeDuplicates()
.assign(to: &$preferences)
$preferences.dropFirst()
$preferences
.dropFirst()
.flatMap(identityService.updatePreferences)
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink(receiveValue: {})
.sink {}
.store(in: &cancellables)
}
}