diff --git a/Sources/Views/ServerFormView.swift b/Sources/Views/ServerFormView.swift index 967e64a..400d77a 100644 --- a/Sources/Views/ServerFormView.swift +++ b/Sources/Views/ServerFormView.swift @@ -20,6 +20,8 @@ struct ServerFormView: View { @State private var hostname: String @State private var apiKey: String @State private var connectionOK: Bool = false + @State private var testingConnection: Bool = false + @State private var connectionError: String = "" @Environment(\.dismiss) private var dismiss @@ -55,6 +57,12 @@ struct ServerFormView: View { SecureField("API Key", text: $apiKey) .textFieldStyle(RoundedBorderTextFieldStyle()) + if !connectionError.isEmpty { + Text(connectionError) + .foregroundColor(.red) + .font(.caption) + } + HStack { Button("Cancel") { dismiss() @@ -65,6 +73,8 @@ struct ServerFormView: View { await testConnection() } } + .disabled(hostname.isEmpty || apiKey.isEmpty || testingConnection) + Button("Save") { saveServer() updateServer() @@ -102,9 +112,21 @@ struct ServerFormView: View { let host = hostname.trimmingCharacters(in: .whitespacesAndNewlines) let key = apiKey.trimmingCharacters(in: .whitespacesAndNewlines) + await MainActor.run { + testingConnection = true + connectionError = "" + } + let reachable = await PingService.ping(hostname: host, apiKey: key) await MainActor.run { - connectionOK = reachable + testingConnection = false + if reachable { + connectionOK = true + connectionError = "" + } else { + connectionOK = false + connectionError = "Connection failed. Check hostname and API key." + } } // // guard let url = URL(string: "https://\(host)/api/v2/ping") else {