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

110 lines
3.1 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
public struct FiltersListView: View {
@Environment(\.dismiss) private var dismiss
2023-01-27 19:36:40 +00:00
@EnvironmentObject private var theme: Theme
@EnvironmentObject private var account: CurrentAccount
@EnvironmentObject private var client: 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 {
if !isLoading && filters.isEmpty {
EmptyView()
} else {
Section {
if isLoading && filters.isEmpty {
ProgressView()
} else {
ForEach(filters) { filter in
NavigationLink(destination: EditFilterView(filter: filter)) {
VStack(alignment: .leading) {
Text(filter.title)
.font(.scaledSubheadline)
2023-01-27 19:36:40 +00:00
Text("\(filter.context.map { $0.name }.joined(separator: ", "))")
.font(.scaledBody)
.foregroundColor(.gray)
if filter.hasExpiry() {
if filter.isExpired() {
Text("filter.expired")
.font(.footnote)
.foregroundColor(.gray)
} else {
Text("filter.expiry-\(filter.expiresAt!.relativeFormatted)")
.font(.footnote)
.foregroundColor(.gray)
}
}
}
}
}
.onDelete { indexes in
deleteFilter(indexes: indexes)
}
}
}
.listRowBackground(theme.primaryBackgroundColor)
}
2023-01-27 19:36:40 +00:00
Section {
NavigationLink(destination: EditFilterView(filter: nil)) {
Label("filter.new", systemImage: "plus")
}
}
.listRowBackground(theme.primaryBackgroundColor)
}
.toolbar {
toolbarContent
}
.navigationTitle("filter.filters")
.navigationBarTitleDisplayMode(.inline)
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
.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()
}
}
}
}