Add connection check in settings

This commit is contained in:
Christoph Hagen 2023-12-27 21:57:32 +01:00
parent 9086c6a916
commit e9d870bd12
3 changed files with 49 additions and 4 deletions

View File

@ -41,6 +41,23 @@ final class RequestCoordinator: ObservableObject {
}
}
func checkConnection(using route: TransmissionType? = nil) {
guard !isPerformingRequest else {
return
}
isPerformingRequest = true
Task {
let route = route ?? connectionType.transmissionTypes.first!
let (finalResult, _) = await performChallenge(route: route)
DispatchQueue.main.async {
self.state = finalResult.result
self.isPerformingRequest = false
}
print("Finished connection test: \(finalResult)")
scheduleReturnToReadyState()
}
}
func startUnlock() {
guard !isPerformingRequest else {
return

View File

@ -74,6 +74,7 @@ struct ContentView: View {
.sheet(isPresented: $showSettingsSheet) {
SettingsView(
keyManager: coordinator.keyManager,
coordinator: coordinator,
serverAddress: $coordinator.serverPath,
localAddress: $coordinator.localAddress)
}

View File

@ -1,9 +1,14 @@
import SwiftUI
import SwiftData
import SFSafeSymbols
struct SettingsView: View {
let keyManager: KeyManagement
@ObservedObject
var coordinator: RequestCoordinator
@Binding
var serverAddress: String
@ -20,6 +25,19 @@ struct SettingsView: View {
TextField("Server address", text: $serverAddress)
.foregroundColor(.secondary)
.padding(.leading, 8)
HStack {
Button("Test") {
coordinator.checkConnection(using: .throughServer)
}.padding(8)
if coordinator.state == .deviceAvailable {
Image(systemSymbol: .checkmarkCircle)
.foregroundColor(.green)
} else if coordinator.state != .notChecked {
Text(coordinator.state.description)
.font(.caption)
.foregroundStyle(.secondary)
}
}
}.padding(.vertical, 8)
VStack(alignment: .leading) {
Text("Local address")
@ -44,11 +62,20 @@ struct SettingsView: View {
}
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
SettingsView(
#Preview {
do {
let config = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try ModelContainer(for: HistoryItem.self, configurations: config)
let item = HistoryItem.mock
container.mainContext.insert(item)
try container.mainContext.save()
return SettingsView(
keyManager: KeyManagement(),
coordinator: .init(modelContext: container.mainContext),
serverAddress: .constant("https://example.com"),
localAddress: .constant("192.168.178.42"))
} catch {
fatalError("Failed to create model container.")
}
}