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