1
0
mirror of https://github.com/metabolist/metatext synced 2024-12-20 14:32:48 +01:00
metatext-app-ios-iphone-ipad/View Models/FiltersViewModel.swift
Justin Mazzocchi 9b59e2fbea
wip
2020-08-30 16:33:32 -07:00

49 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)
}
}