metatext/ViewModels/Sources/ViewModels/PostingReadingPreferencesViewModel.swift

32 lines
943 B
Swift
Raw Normal View History

2020-08-07 10:14:14 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
2020-08-31 18:57:02 +00:00
import ServiceLayer
2020-08-07 10:14:14 +00:00
2020-09-01 07:33:49 +00:00
public class PostingReadingPreferencesViewModel: ObservableObject {
@Published public var preferences: Identity.Preferences
@Published public var alertItem: AlertItem?
2020-08-07 10:14:14 +00:00
2020-08-08 23:08:47 +00:00
private let identityService: IdentityService
2020-08-07 10:14:14 +00:00
private var cancellables = Set<AnyCancellable>()
2020-08-08 23:08:47 +00:00
init(identityService: IdentityService) {
self.identityService = identityService
preferences = identityService.identity.preferences
2020-08-07 10:14:14 +00:00
2020-08-15 00:14:21 +00:00
identityService.$identity
.map(\.preferences)
2020-08-07 10:14:14 +00:00
.dropFirst()
.removeDuplicates()
.assign(to: &$preferences)
2020-08-15 00:14:21 +00:00
$preferences
.dropFirst()
2020-08-08 23:08:47 +00:00
.flatMap(identityService.updatePreferences)
2020-08-07 10:14:14 +00:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
2020-08-26 09:19:38 +00:00
.sink { _ in }
2020-08-07 10:14:14 +00:00
.store(in: &cancellables)
}
}