mirror of
https://github.com/metabolist/metatext.git
synced 2024-12-23 14:00:29 +00:00
44 lines
1.4 KiB
Swift
44 lines
1.4 KiB
Swift
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
import Foundation
|
|
import Combine
|
|
|
|
class PostingReadingPreferencesViewModel: ObservableObject {
|
|
@Published var preferences: Identity.Preferences
|
|
@Published var alertItem: AlertItem?
|
|
let handle: String
|
|
|
|
private let identityRepository: IdentityRepository
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
init(identityRepository: IdentityRepository) {
|
|
self.identityRepository = identityRepository
|
|
preferences = identityRepository.identity.preferences
|
|
handle = identityRepository.identity.handle
|
|
|
|
identityRepository.$identity.map(\.preferences)
|
|
.dropFirst()
|
|
.removeDuplicates()
|
|
.handleEvents(receiveOutput: { [weak self] in
|
|
if $0.useServerPostingReadingPreferences {
|
|
self?.refreshServerPreferences()
|
|
}
|
|
})
|
|
.assign(to: &$preferences)
|
|
|
|
$preferences.dropFirst()
|
|
.flatMap(identityRepository.updatePreferences)
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
.sink(receiveValue: {})
|
|
.store(in: &cancellables)
|
|
}
|
|
}
|
|
|
|
extension PostingReadingPreferencesViewModel {
|
|
private func refreshServerPreferences() {
|
|
identityRepository.refreshServerPreferences()
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
.sink(receiveValue: {})
|
|
.store(in: &cancellables)
|
|
}
|
|
}
|