metatext-app-ios-iphone-ipad/Views/EditFilterView.swift

108 lines
3.8 KiB
Swift
Raw Normal View History

2020-08-29 12:26:26 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import SwiftUI
2020-08-31 01:33:11 +02:00
import struct Mastodon.Filter
2020-09-01 09:33:49 +02:00
import ViewModels
2020-08-29 12:26:26 +02: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 02:32:34 +02:00
set: { viewModel.filter.expiresAt = $0 ? nil : viewModel.date }))
2020-08-29 12:26:26 +02:00
}
if viewModel.filter.expiresAt != nil {
2020-08-30 02:32:34 +02:00
DatePicker(selection: $viewModel.date, in: Date()...) {
2020-08-29 12:26:26 +02: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 21:39:26 +02: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 12:26:26 +02:00
#if DEBUG
2020-09-01 09:33:49 +02:00
import PreviewViewModels
2020-08-29 12:26:26 +02:00
struct EditFilterView_Previews: PreviewProvider {
static var previews: some View {
2020-09-01 09:33:49 +02:00
EditFilterView(viewModel: .mock())
2020-08-29 12:26:26 +02:00
}
}
#endif