metatext-app-ios-iphone-ipad/ViewModels/Sources/ViewModels/View Models/FiltersViewModel.swift

44 lines
1.3 KiB
Swift
Raw Normal View History

2020-08-29 12:26:26 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
2020-09-05 04:31:43 +02:00
import Foundation
2020-08-31 01:33:11 +02:00
import Mastodon
2020-08-31 20:57:02 +02:00
import ServiceLayer
2020-08-29 12:26:26 +02:00
2020-09-08 04:35:28 +02:00
public final class FiltersViewModel: ObservableObject {
2020-09-01 09:33:49 +02:00
@Published public var activeFilters = [Filter]()
@Published public var expiredFilters = [Filter]()
@Published public var alertItem: AlertItem?
public let identityContext: IdentityContext
2020-08-29 12:26:26 +02:00
private var cancellables = Set<AnyCancellable>()
2021-01-26 01:06:35 +01:00
public init(identityContext: IdentityContext) {
self.identityContext = identityContext
2020-08-29 12:26:26 +02:00
2021-01-26 01:06:35 +01:00
identityContext.service.activeFiltersPublisher()
2020-08-30 02:32:34 +02:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$activeFilters)
2021-01-26 01:06:35 +01:00
identityContext.service.expiredFiltersPublisher()
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
}
}
2020-09-01 09:33:49 +02:00
public extension FiltersViewModel {
2020-08-29 12:26:26 +02:00
func refreshFilters() {
2021-01-26 01:06:35 +01:00
identityContext.service.refreshFilters()
2020-08-29 12:26:26 +02:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { _ in }
.store(in: &cancellables)
}
2020-08-30 02:32:34 +02:00
func delete(filter: Filter) {
2021-01-26 01:06:35 +01:00
identityContext.service.deleteFilter(id: filter.id)
2020-08-30 02:32:34 +02:00
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.sink { _ in }
.store(in: &cancellables)
}
2020-08-29 12:26:26 +02:00
}