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-29 10:26:26 +00:00
|
|
|
|
|
|
|
class FiltersViewModel: ObservableObject {
|
2020-08-30 00:32:34 +00:00
|
|
|
@Published var activeFilters = [Filter]()
|
|
|
|
@Published var expiredFilters = [Filter]()
|
2020-08-29 10:26:26 +00:00
|
|
|
@Published var alertItem: AlertItem?
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FiltersViewModel {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|