2023-01-25 20:18:34 +00:00
|
|
|
import DesignSystem
|
2023-01-27 19:36:40 +00:00
|
|
|
import Env
|
2023-01-25 20:18:34 +00:00
|
|
|
import Models
|
2023-01-27 19:36:40 +00:00
|
|
|
import Network
|
|
|
|
import SwiftUI
|
2023-01-25 20:18:34 +00:00
|
|
|
|
2023-09-19 07:18:20 +00:00
|
|
|
@MainActor
|
2023-01-25 20:18:34 +00:00
|
|
|
public struct FiltersListView: View {
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
2023-01-27 19:36:40 +00:00
|
|
|
|
2023-09-18 19:03:52 +00:00
|
|
|
@Environment(Theme.self) private var theme
|
2023-09-18 05:01:23 +00:00
|
|
|
@Environment(CurrentAccount.self) private var account
|
|
|
|
@Environment(Client.self) private var client
|
2023-01-27 19:36:40 +00:00
|
|
|
|
2023-01-25 20:18:34 +00:00
|
|
|
@State private var isLoading: Bool = true
|
|
|
|
@State private var filters: [ServerFilter] = []
|
2023-01-27 19:36:40 +00:00
|
|
|
|
|
|
|
public init() {}
|
|
|
|
|
2023-01-25 20:18:34 +00:00
|
|
|
public var body: some View {
|
|
|
|
NavigationStack {
|
|
|
|
Form {
|
2023-09-16 12:15:03 +00:00
|
|
|
if !isLoading, filters.isEmpty {
|
2023-01-27 11:18:17 +00:00
|
|
|
EmptyView()
|
|
|
|
} else {
|
|
|
|
Section {
|
2023-09-16 12:15:03 +00:00
|
|
|
if isLoading, filters.isEmpty {
|
2023-01-27 11:18:17 +00:00
|
|
|
ProgressView()
|
|
|
|
} else {
|
|
|
|
ForEach(filters) { filter in
|
|
|
|
NavigationLink(destination: EditFilterView(filter: filter)) {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
Text(filter.title)
|
|
|
|
.font(.scaledSubheadline)
|
2023-09-16 12:15:03 +00:00
|
|
|
Text("\(filter.context.map(\.name).joined(separator: ", "))")
|
2023-01-27 11:18:17 +00:00
|
|
|
.font(.scaledBody)
|
2023-12-04 14:49:44 +00:00
|
|
|
.foregroundStyle(.secondary)
|
2023-03-03 07:15:34 +00:00
|
|
|
if filter.hasExpiry() {
|
|
|
|
if filter.isExpired() {
|
|
|
|
Text("filter.expired")
|
|
|
|
.font(.footnote)
|
2023-12-04 14:49:44 +00:00
|
|
|
.foregroundStyle(.secondary)
|
2023-03-03 07:15:34 +00:00
|
|
|
} else {
|
|
|
|
Text("filter.expiry-\(filter.expiresAt!.relativeFormatted)")
|
|
|
|
.font(.footnote)
|
2023-12-04 14:49:44 +00:00
|
|
|
.foregroundStyle(.secondary)
|
2023-03-03 07:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-27 11:18:17 +00:00
|
|
|
}
|
2023-01-25 20:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-27 11:18:17 +00:00
|
|
|
.onDelete { indexes in
|
|
|
|
deleteFilter(indexes: indexes)
|
|
|
|
}
|
2023-01-25 20:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-19 08:48:12 +00:00
|
|
|
#if !os(visionOS)
|
2023-01-27 11:18:17 +00:00
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
2023-12-19 08:48:12 +00:00
|
|
|
#endif
|
2023-01-25 20:18:34 +00:00
|
|
|
}
|
2023-01-27 19:36:40 +00:00
|
|
|
|
2023-01-25 20:18:34 +00:00
|
|
|
Section {
|
|
|
|
NavigationLink(destination: EditFilterView(filter: nil)) {
|
|
|
|
Label("filter.new", systemImage: "plus")
|
|
|
|
}
|
|
|
|
}
|
2023-12-19 08:48:12 +00:00
|
|
|
#if !os(visionOS)
|
2023-01-25 20:18:34 +00:00
|
|
|
.listRowBackground(theme.primaryBackgroundColor)
|
2023-12-19 08:48:12 +00:00
|
|
|
#endif
|
2023-01-25 20:18:34 +00:00
|
|
|
}
|
|
|
|
.toolbar {
|
|
|
|
toolbarContent
|
|
|
|
}
|
|
|
|
.navigationTitle("filter.filters")
|
|
|
|
.navigationBarTitleDisplayMode(.inline)
|
2023-12-19 08:48:12 +00:00
|
|
|
#if !os(visionOS)
|
2023-01-25 20:18:34 +00:00
|
|
|
.scrollContentBackground(.hidden)
|
|
|
|
.background(theme.secondaryBackgroundColor)
|
2023-12-19 08:48:12 +00:00
|
|
|
#endif
|
2023-01-25 20:18:34 +00:00
|
|
|
.task {
|
|
|
|
do {
|
|
|
|
isLoading = true
|
|
|
|
filters = try await client.get(endpoint: ServerFilters.filters, forceVersion: .v2)
|
|
|
|
isLoading = false
|
|
|
|
} catch {
|
|
|
|
isLoading = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-27 19:36:40 +00:00
|
|
|
|
2023-01-25 20:18:34 +00:00
|
|
|
private func deleteFilter(indexes: IndexSet) {
|
|
|
|
if let index = indexes.first {
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
let response = try await client.delete(endpoint: ServerFilters.filter(id: filters[index].id),
|
|
|
|
forceVersion: .v2)
|
|
|
|
if response?.statusCode == 200 {
|
|
|
|
filters.remove(at: index)
|
|
|
|
}
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-27 19:36:40 +00:00
|
|
|
|
2023-01-25 20:18:34 +00:00
|
|
|
@ToolbarContentBuilder
|
|
|
|
private var toolbarContent: some ToolbarContent {
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) {
|
2023-03-03 19:10:20 +00:00
|
|
|
Button {
|
2023-01-25 20:18:34 +00:00
|
|
|
dismiss()
|
2023-03-03 19:10:20 +00:00
|
|
|
} label: {
|
|
|
|
Text("action.done").bold()
|
2023-01-25 20:18:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|