89 lines
3.2 KiB
Swift
89 lines
3.2 KiB
Swift
import SwiftUI
|
|
import ProxyCore
|
|
import GRDB
|
|
|
|
struct MapLocalView: View {
|
|
@State private var rules: [MapLocalRule] = []
|
|
@State private var showAddRule = false
|
|
@State private var observation: AnyDatabaseCancellable?
|
|
|
|
private let rulesRepo = RulesRepository()
|
|
|
|
var body: some View {
|
|
List {
|
|
Section {
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Text("Map Local")
|
|
.font(.headline)
|
|
Text("Intercept requests and replace the response with local content. Define custom mock responses for matched URLs.")
|
|
.font(.subheadline)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.padding()
|
|
}
|
|
.listRowInsets(EdgeInsets())
|
|
|
|
Section("Rules") {
|
|
if rules.isEmpty {
|
|
EmptyStateView(
|
|
icon: "doc.on.doc",
|
|
title: "No Map Local Rules",
|
|
subtitle: "Tap + to create a new rule."
|
|
)
|
|
} else {
|
|
ForEach(rules) { rule in
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text(rule.name ?? rule.urlPattern)
|
|
.font(.subheadline.weight(.medium))
|
|
Text(rule.urlPattern)
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
Text("Status: \(rule.responseStatus)")
|
|
.font(.caption2)
|
|
.foregroundStyle(.tertiary)
|
|
}
|
|
}
|
|
.onDelete { indexSet in
|
|
for index in indexSet {
|
|
if let id = rules[index].id {
|
|
try? rulesRepo.deleteMapLocalRule(id: id)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.navigationTitle("Map Local")
|
|
.toolbar {
|
|
ToolbarItem(placement: .topBarTrailing) {
|
|
Button { showAddRule = true } label: {
|
|
Image(systemName: "plus")
|
|
}
|
|
}
|
|
}
|
|
.sheet(isPresented: $showAddRule) {
|
|
NavigationStack {
|
|
Form {
|
|
// TODO: Add map local rule creation form
|
|
Text("Map Local rule creation")
|
|
}
|
|
.navigationTitle("New Map Local Rule")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Cancel") { showAddRule = false }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.task {
|
|
observation = rulesRepo.observeMapLocalRules()
|
|
.start(in: DatabaseManager.shared.dbPool) { error in
|
|
print("Map Local observation error: \(error)")
|
|
} onChange: { newRules in
|
|
rules = newRules
|
|
}
|
|
}
|
|
}
|
|
}
|