IceCubesApp/Packages/Account/Sources/Account/Filters/FiltersListView.swift

117 lines
3.3 KiB
Swift
Raw Normal View History

import DesignSystem
2023-01-27 19:36:40 +00:00
import Env
import Models
2023-01-27 19:36:40 +00:00
import Network
import SwiftUI
2023-09-19 07:18:20 +00:00
@MainActor
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
@Environment(CurrentAccount.self) private var account
@Environment(Client.self) private var client
2023-01-27 19:36:40 +00:00
@State private var isLoading: Bool = true
@State private var filters: [ServerFilter] = []
2023-01-27 19:36:40 +00:00
public init() {}
public var body: some View {
NavigationStack {
Form {
2023-09-16 12:15:03 +00:00
if !isLoading, filters.isEmpty {
EmptyView()
} else {
Section {
2023-09-16 12:15:03 +00:00
if isLoading, filters.isEmpty {
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: ", "))")
.font(.scaledBody)
2023-12-04 14:49:44 +00:00
.foregroundStyle(.secondary)
if filter.hasExpiry() {
if filter.isExpired() {
Text("filter.expired")
.font(.footnote)
2023-12-04 14:49:44 +00:00
.foregroundStyle(.secondary)
} else {
Text("filter.expiry-\(filter.expiresAt!.relativeFormatted)")
.font(.footnote)
2023-12-04 14:49:44 +00:00
.foregroundStyle(.secondary)
}
}
}
}
}
.onDelete { indexes in
deleteFilter(indexes: indexes)
}
}
}
2023-12-19 08:48:12 +00:00
#if !os(visionOS)
.listRowBackground(theme.primaryBackgroundColor)
2023-12-19 08:48:12 +00:00
#endif
}
2023-01-27 19:36:40 +00:00
Section {
NavigationLink(destination: EditFilterView(filter: nil)) {
Label("filter.new", systemImage: "plus")
}
}
2023-12-19 08:48:12 +00:00
#if !os(visionOS)
.listRowBackground(theme.primaryBackgroundColor)
2023-12-19 08:48:12 +00:00
#endif
}
.toolbar {
toolbarContent
}
.navigationTitle("filter.filters")
.navigationBarTitleDisplayMode(.inline)
2023-12-19 08:48:12 +00:00
#if !os(visionOS)
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
2023-12-19 08:48:12 +00:00
#endif
.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
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
@ToolbarContentBuilder
private var toolbarContent: some ToolbarContent {
ToolbarItem(placement: .navigationBarTrailing) {
2023-03-03 19:10:20 +00:00
Button {
dismiss()
2023-03-03 19:10:20 +00:00
} label: {
Text("action.done").bold()
}
}
}
}