metatext/Views/EditFilterView.swift

108 lines
3.8 KiB
Swift
Raw Normal View History

2020-08-29 10:26:26 +00:00
// Copyright © 2020 Metabolist. All rights reserved.
import SwiftUI
2020-08-30 23:33:11 +00:00
import struct Mastodon.Filter
2020-09-01 07:33:49 +00:00
import ViewModels
2020-08-29 10:26:26 +00:00
struct EditFilterView: View {
@StateObject var viewModel: EditFilterViewModel
@Environment(\.presentationMode) var presentationMode
var body: some View {
Form {
Section(header: Text("filter.keyword-or-phrase")) {
TextField("filter.keyword-or-phrase", text: $viewModel.filter.phrase)
}
Section {
if viewModel.isNew || viewModel.filter.expiresAt == nil {
Toggle("filter.never-expires", isOn: .init(
get: { viewModel.filter.expiresAt == nil },
2020-08-30 00:32:34 +00:00
set: { viewModel.filter.expiresAt = $0 ? nil : viewModel.date }))
2020-08-29 10:26:26 +00:00
}
if viewModel.filter.expiresAt != nil {
2020-08-30 00:32:34 +00:00
DatePicker(selection: $viewModel.date, in: Date()...) {
2020-08-29 10:26:26 +00:00
Text("filter.expire-after")
}
}
}
Section(header: Text("filter.contexts")) {
ForEach(Filter.Context.allCasesExceptUnknown) { context in
Toggle(context.localized, isOn: .init(
get: { viewModel.filter.context.contains(context) },
set: { _ in viewModel.toggleSelection(context: context) }))
}
}
Section {
Toggle(isOn: $viewModel.filter.irreversible, label: {
VStack(alignment: .leading, spacing: 8) {
Text("filter.irreversible")
Text("filter.irreversible-explanation")
.font(.footnote)
.foregroundColor(.secondary)
}
})
}
Section {
Toggle(isOn: $viewModel.filter.wholeWord, label: {
VStack(alignment: .leading, spacing: 8) {
Text("filter.whole-word")
Text("filter.whole-word-explanation")
.font(.footnote)
.foregroundColor(.secondary)
}
})
}
}
.alertItem($viewModel.alertItem)
.onReceive(viewModel.saveCompleted) { presentationMode.wrappedValue.dismiss() }
.navigationTitle(viewModel.isNew ? "filter.add-new" : "filter.edit")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Group {
if viewModel.saving {
ProgressView()
} else {
Button(viewModel.isNew ? "add" : "filter.save-changes",
action: viewModel.save)
.disabled(viewModel.isSaveDisabled)
}
}
}
}
}
}
2020-08-31 19:39:26 +00:00
extension Filter.Context {
var localized: String {
switch self {
case .home:
return NSLocalizedString("filter.context.home", comment: "")
case .notifications:
return NSLocalizedString("filter.context.notifications", comment: "")
case .public:
return NSLocalizedString("filter.context.public", comment: "")
case .thread:
return NSLocalizedString("filter.context.thread", comment: "")
case .account:
return NSLocalizedString("filter.context.account", comment: "")
case .unknown:
return NSLocalizedString("filter.context.unknown", comment: "")
}
}
}
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 EditFilterView_Previews: PreviewProvider {
static var previews: some View {
2020-09-08 02:12:38 +00:00
EditFilterView(viewModel: .init(filter: .new, identification: .preview))
2020-08-29 10:26:26 +00:00
}
}
#endif