2022-12-03 02:40:14 +00:00
|
|
|
//
|
|
|
|
// EditFilterView.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 12/2/22.
|
|
|
|
// Copyright © 2022 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import Pachyderm
|
|
|
|
|
|
|
|
struct EditFilterView: View {
|
|
|
|
private static let expiresInOptions: [MenuPicker<TimeInterval>.Option] = {
|
|
|
|
let f = DateComponentsFormatter()
|
|
|
|
f.maximumUnitCount = 1
|
|
|
|
f.unitsStyle = .full
|
|
|
|
f.allowedUnits = [.weekOfMonth, .day, .hour, .minute]
|
|
|
|
|
|
|
|
let durations: [TimeInterval] = [
|
|
|
|
30 * 60,
|
|
|
|
60 * 60,
|
|
|
|
6 * 60 * 60,
|
|
|
|
24 * 60 * 60,
|
|
|
|
3 * 24 * 60 * 60,
|
|
|
|
7 * 24 * 60 * 60,
|
|
|
|
]
|
|
|
|
return durations.map { .init(value: $0, title: f.string(from: $0)!) }
|
|
|
|
}()
|
|
|
|
|
2022-12-03 17:29:07 +00:00
|
|
|
@ObservedObject var filter: EditedFilter
|
|
|
|
let updateFilter: (EditedFilter) -> Void
|
2022-12-03 02:40:14 +00:00
|
|
|
@EnvironmentObject private var mastodonController: MastodonController
|
|
|
|
@State private var edited = false
|
|
|
|
|
2022-12-03 17:29:07 +00:00
|
|
|
init(filter: EditedFilter, updateFilter: @escaping (EditedFilter) -> Void) {
|
2022-12-03 02:40:14 +00:00
|
|
|
self.filter = filter
|
|
|
|
self.updateFilter = updateFilter
|
2022-12-03 17:29:07 +00:00
|
|
|
if let expiresIn = filter.expiresIn {
|
2022-12-03 02:40:14 +00:00
|
|
|
self.expiresIn = Self.expiresInOptions.min(by: { a, b in
|
2022-12-03 17:29:07 +00:00
|
|
|
let aDist = abs(a.value - expiresIn)
|
|
|
|
let bDist = abs(b.value - expiresIn)
|
2022-12-03 02:40:14 +00:00
|
|
|
return aDist < bDist
|
|
|
|
})!.value
|
|
|
|
} else {
|
|
|
|
self.expiresIn = 24 * 60 * 60
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@State private var expiresIn: TimeInterval {
|
|
|
|
didSet {
|
2022-12-03 17:29:07 +00:00
|
|
|
if expires.wrappedValue {
|
|
|
|
filter.expiresIn = expiresIn
|
2022-12-03 02:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private var expires: Binding<Bool> {
|
|
|
|
Binding {
|
2022-12-03 17:29:07 +00:00
|
|
|
filter.expiresIn != nil
|
2022-12-03 02:40:14 +00:00
|
|
|
} set: { newValue in
|
2022-12-03 17:29:07 +00:00
|
|
|
filter.expiresIn = newValue ? expiresIn : nil
|
2022-12-03 02:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Form {
|
2022-12-03 17:29:07 +00:00
|
|
|
if mastodonController.instanceFeatures.filtersV2 {
|
|
|
|
Section {
|
|
|
|
TextField("Title", text: Binding(get: {
|
|
|
|
filter.title ?? ""
|
|
|
|
}, set: { newValue in
|
|
|
|
filter.title = newValue
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 02:40:14 +00:00
|
|
|
Section {
|
2022-12-03 17:29:07 +00:00
|
|
|
ForEach(Array($filter.keywords.enumerated()), id: \.offset) { keyword in
|
|
|
|
VStack {
|
|
|
|
TextField("Phrase", text: keyword.element.keyword)
|
|
|
|
Toggle("Whole Word", isOn: keyword.element.wholeWord)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onDelete(perform: mastodonController.instanceFeatures.filtersV2 ? { indices in
|
|
|
|
filter.keywords.remove(atOffsets: indices)
|
|
|
|
} : nil)
|
|
|
|
|
|
|
|
if mastodonController.instanceFeatures.filtersV2 {
|
|
|
|
Button {
|
|
|
|
let new = EditedFilter.Keyword(id: nil, keyword: "", wholeWord: true)
|
|
|
|
withAnimation {
|
|
|
|
filter.keywords.append(new)
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Label("Add Keyword", systemImage: "plus")
|
|
|
|
}
|
|
|
|
}
|
2022-12-03 02:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Section {
|
2022-12-03 17:29:07 +00:00
|
|
|
if mastodonController.instanceFeatures.filtersV2 {
|
|
|
|
Picker(selection: $filter.action) {
|
|
|
|
ForEach(FilterV2.Action.allCases, id: \.self) { action in
|
|
|
|
Text(action.displayName).tag(action)
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Text("Action")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-03 02:40:14 +00:00
|
|
|
Toggle("Expires", isOn: expires)
|
2022-12-03 17:29:07 +00:00
|
|
|
|
2022-12-03 02:40:14 +00:00
|
|
|
if expires.wrappedValue {
|
|
|
|
Picker(selection: $expiresIn) {
|
|
|
|
ForEach(Self.expiresInOptions, id: \.value) { option in
|
|
|
|
Text(option.title).tag(option.value)
|
|
|
|
}
|
|
|
|
} label: {
|
|
|
|
Text("Duration")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Section {
|
2022-12-03 17:29:07 +00:00
|
|
|
ForEach(FilterV1.Context.allCases, id: \.rawValue) { context in
|
2022-12-03 02:40:14 +00:00
|
|
|
Toggle(isOn: Binding(get: {
|
|
|
|
filter.contexts.contains(context)
|
|
|
|
}, set: { newValue in
|
|
|
|
if newValue {
|
|
|
|
if !filter.contexts.contains(context) {
|
|
|
|
filter.contexts.append(context)
|
|
|
|
}
|
|
|
|
} else if filter.contexts.count > 1 {
|
|
|
|
filter.contexts.removeAll(where: { $0 == context })
|
|
|
|
}
|
|
|
|
})) {
|
|
|
|
Text(context.displayName)
|
|
|
|
}
|
|
|
|
.toggleStyle(FilterContextToggleStyle())
|
|
|
|
}
|
|
|
|
} header: {
|
|
|
|
Text("Contexts")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.navigationTitle("Edit Filter")
|
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
|
|
.onReceive(filter.objectWillChange, perform: { _ in
|
|
|
|
edited = true
|
|
|
|
})
|
|
|
|
.onDisappear {
|
|
|
|
if edited {
|
2022-12-03 17:29:07 +00:00
|
|
|
updateFilter(filter)
|
2022-12-03 02:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct FilterContextToggleStyle: ToggleStyle {
|
|
|
|
func makeBody(configuration: Configuration) -> some View {
|
|
|
|
Button {
|
|
|
|
configuration.isOn.toggle()
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
configuration.label
|
|
|
|
.foregroundColor(.primary)
|
|
|
|
Spacer()
|
|
|
|
if configuration.isOn {
|
|
|
|
Image(systemName: "checkmark")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//struct EditFilterView_Previews: PreviewProvider {
|
|
|
|
// static var previews: some View {
|
|
|
|
// EditFilterView()
|
|
|
|
// }
|
|
|
|
//}
|