metatext/ViewModels/Sources/ViewModels/FiltersViewModel.swift

50 lines
1.5 KiB
Swift
Raw Normal View History

2020-08-29 10:26:26 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import Foundation
import Combine
2020-08-30 23:33:11 +00:00
import Mastodon
2020-08-31 18:57:02 +00:00
import ServiceLayer
2020-08-29 10:26:26 +00:00
2020-09-01 07:33:49 +00:00
public class FiltersViewModel: ObservableObject {
@Published public var activeFilters = [Filter]()
@Published public var expiredFilters = [Filter]()
@Published public var alertItem: AlertItem?
2020-08-29 10:26:26 +00:00
private let identityService: IdentityService
private var cancellables = Set<AnyCancellable>()
init(identityService: IdentityService) {
self.identityService = identityService
2020-08-30 00:32:34 +00:00
let now = Date()
identityService.activeFiltersObservation(date: now)
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$activeFilters)
identityService.expiredFiltersObservation(date: now)
2020-08-29 10:26:26 +00:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
2020-08-30 00:32:34 +00:00
.assign(to: &$expiredFilters)
2020-08-29 10:26:26 +00:00
}
}
2020-09-01 07:33:49 +00:00
public extension FiltersViewModel {
2020-08-29 10:26:26 +00:00
func refreshFilters() {
identityService.refreshFilters()
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { _ in }
.store(in: &cancellables)
}
2020-08-30 00:32:34 +00:00
func delete(filter: Filter) {
identityService.deleteFilter(id: filter.id)
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { _ in }
.store(in: &cancellables)
}
2020-08-29 10:26:26 +00:00
func editFilterViewModel(filter: Filter) -> EditFilterViewModel {
EditFilterViewModel(filter: filter, identityService: identityService)
}
}