1
0
mirror of https://github.com/metabolist/metatext synced 2024-12-12 01:16:46 +01:00
metatext-app-ios-iphone-ipad/Views/FiltersView.swift
2020-09-01 00:33:49 -07:00

67 lines
2.1 KiB
Swift

// Copyright © 2020 Metabolist. All rights reserved.
import SwiftUI
import struct Mastodon.Filter
import ViewModels
struct FiltersView: View {
@StateObject var viewModel: FiltersViewModel
var body: some View {
Form {
Section {
NavigationLink(destination: EditFilterView(
viewModel: viewModel.editFilterViewModel(filter: .new))) {
Label("add", systemImage: "plus.circle")
}
}
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
NavigationLink(destination: EditFilterView(
viewModel: viewModel.editFilterViewModel(filter: filter))) {
HStack {
Text(filter.phrase)
Spacer()
Text(ListFormatter.localizedString(byJoining: filter.context.map(\.localized)))
.foregroundColor(.secondary)
}
}
}
.onDelete {
guard let index = $0.first else { return }
viewModel.delete(filter: filters[index])
}
}
}
}
}
#if DEBUG
import PreviewViewModels
struct FiltersView_Previews: PreviewProvider {
static var previews: some View {
FiltersView(viewModel: .mock())
}
}
#endif