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() { func startUnlock() {
guard !isPerformingRequest else { guard !isPerformingRequest else {
return return

View File

@ -73,7 +73,8 @@ struct ContentView: View {
.animation(.easeInOut, value: coordinator.state.color) .animation(.easeInOut, value: coordinator.state.color)
.sheet(isPresented: $showSettingsSheet) { .sheet(isPresented: $showSettingsSheet) {
SettingsView( SettingsView(
keyManager: coordinator.keyManager, keyManager: coordinator.keyManager,
coordinator: coordinator,
serverAddress: $coordinator.serverPath, serverAddress: $coordinator.serverPath,
localAddress: $coordinator.localAddress) localAddress: $coordinator.localAddress)
} }

View File

@ -1,9 +1,14 @@
import SwiftUI import SwiftUI
import SwiftData
import SFSafeSymbols
struct SettingsView: View { struct SettingsView: View {
let keyManager: KeyManagement let keyManager: KeyManagement
@ObservedObject
var coordinator: RequestCoordinator
@Binding @Binding
var serverAddress: String var serverAddress: String
@ -20,6 +25,19 @@ struct SettingsView: View {
TextField("Server address", text: $serverAddress) TextField("Server address", text: $serverAddress)
.foregroundColor(.secondary) .foregroundColor(.secondary)
.padding(.leading, 8) .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) }.padding(.vertical, 8)
VStack(alignment: .leading) { VStack(alignment: .leading) {
Text("Local address") Text("Local address")
@ -44,11 +62,20 @@ struct SettingsView: View {
} }
} }
struct SettingsView_Previews: PreviewProvider { #Preview {
static var previews: some View { do {
SettingsView( 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(), keyManager: KeyManagement(),
coordinator: .init(modelContext: container.mainContext),
serverAddress: .constant("https://example.com"), serverAddress: .constant("https://example.com"),
localAddress: .constant("192.168.178.42")) localAddress: .constant("192.168.178.42"))
} catch {
fatalError("Failed to create model container.")
} }
} }