mirror of
https://github.com/metabolist/metatext.git
synced 2024-12-22 13:37:01 +00:00
48 lines
1.4 KiB
Swift
48 lines
1.4 KiB
Swift
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
import Foundation
|
|
import Combine
|
|
import Mastodon
|
|
|
|
class FiltersViewModel: ObservableObject {
|
|
@Published var activeFilters = [Filter]()
|
|
@Published var expiredFilters = [Filter]()
|
|
@Published var alertItem: AlertItem?
|
|
|
|
private let identityService: IdentityService
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
init(identityService: IdentityService) {
|
|
self.identityService = identityService
|
|
|
|
let now = Date()
|
|
|
|
identityService.activeFiltersObservation(date: now)
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
.assign(to: &$activeFilters)
|
|
|
|
identityService.expiredFiltersObservation(date: now)
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
.assign(to: &$expiredFilters)
|
|
}
|
|
}
|
|
|
|
extension FiltersViewModel {
|
|
func refreshFilters() {
|
|
identityService.refreshFilters()
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
.sink { _ in }
|
|
.store(in: &cancellables)
|
|
}
|
|
|
|
func delete(filter: Filter) {
|
|
identityService.deleteFilter(id: filter.id)
|
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
|
.sink { _ in }
|
|
.store(in: &cancellables)
|
|
}
|
|
|
|
func editFilterViewModel(filter: Filter) -> EditFilterViewModel {
|
|
EditFilterViewModel(filter: filter, identityService: identityService)
|
|
}
|
|
}
|