metatext/Views/FiltersView.swift

68 lines
2.1 KiB
Swift
Raw Normal View History

2020-08-29 10:26:26 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
2021-01-25 23:15:30 +00:00
import Mastodon
2020-08-29 10:26:26 +00:00
import SwiftUI
2020-09-01 07:33:49 +00:00
import ViewModels
2020-08-29 10:26:26 +00:00
struct FiltersView: View {
@StateObject var viewModel: FiltersViewModel
2020-09-08 02:12:38 +00:00
@EnvironmentObject var identification: Identification
2020-08-29 10:26:26 +00:00
var body: some View {
Form {
Section {
NavigationLink(destination: EditFilterView(
2020-09-08 02:12:38 +00:00
viewModel: .init(filter: .new, identification: identification))) {
2020-08-29 10:26:26 +00:00
Label("add", systemImage: "plus.circle")
}
}
2020-08-30 00:32:34 +00:00
section(title: "filters.active", filters: viewModel.activeFilters)
section(title: "filters.expired", filters: viewModel.expiredFilters)
}
.navigationTitle("preferences.filters")
.toolbar {
ToolbarItem(placement: ToolbarItemPlacement.navigationBarTrailing) {
EditButton()
}
}
.alertItem($viewModel.alertItem)
.onAppear(perform: viewModel.refreshFilters)
}
}
private extension FiltersView {
@ViewBuilder
func section(title: LocalizedStringKey, filters: [Filter]) -> some View {
if !filters.isEmpty {
Section(header: Text(title)) {
ForEach(filters) { filter in
2020-08-29 10:26:26 +00:00
NavigationLink(destination: EditFilterView(
2020-09-08 02:12:38 +00:00
viewModel: .init(filter: filter, identification: identification))) {
2020-08-29 10:26:26 +00:00
HStack {
Text(filter.phrase)
Spacer()
Text(ListFormatter.localizedString(byJoining: filter.context.map(\.localized)))
.foregroundColor(.secondary)
}
}
}
2020-08-30 00:32:34 +00:00
.onDelete {
guard let index = $0.first else { return }
viewModel.delete(filter: filters[index])
}
2020-08-29 10:26:26 +00:00
}
}
}
}
#if DEBUG
2020-09-01 07:33:49 +00:00
import PreviewViewModels
2020-08-29 10:26:26 +00:00
struct FiltersView_Previews: PreviewProvider {
static var previews: some View {
2020-09-08 02:12:38 +00:00
FiltersView(viewModel: .init(identification: .preview))
2020-08-29 10:26:26 +00:00
}
}
#endif