This repository has been archived on 2026-10-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
hope-voice-ios/Sources/Features/Settings/ConnectionTestView.swift
T

88 lines
3.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import SwiftUI
struct ConnectionTestView: View {
@ObservedObject var settings: SettingsStore
@State private var isBusy = false
@State private var result: Result<TokenResponse, Error>?
@State private var urlError: String?
var body: some View {
Form {
Section("Server") {
LabeledContent("URL") {
Text(settings.trimmedServerURL)
.font(.footnote.monospaced())
.foregroundStyle(.secondary)
}
if let urlError {
Label(urlError, systemImage: "xmark.octagon")
.foregroundStyle(.red)
}
}
Section {
Button(action: runTest) {
HStack {
Spacer()
if isBusy {
ProgressView()
} else {
Text("Test connection")
}
Spacer()
}
}
.disabled(isBusy || settings.trimmedServerURL.isEmpty)
}
if let result {
switch result {
case .success(let response):
Section("Success") {
LabeledContent("Room") {
Text(response.room)
.font(.footnote.monospaced())
}
LabeledContent("User") {
Text(response.user.displayName ?? response.user.id)
}
Label("Connected to your server. Start a conversation to check Hope’s voice and replies.", systemImage: "checkmark.circle")
.foregroundStyle(.green)
}
case .failure(let error):
Section("Error") {
Label(error.localizedDescription, systemImage: "xmark.octagon")
.foregroundStyle(.red)
.font(.footnote)
}
}
}
}
.navigationTitle("Connection test")
.navigationBarTitleDisplayMode(.inline)
}
private func runTest() {
result = nil
urlError = nil
switch ServerConfig.parse(settings.trimmedServerURL) {
case .success(let config):
isBusy = true
Task {
let client = TokenClient(config: config)
do {
let response = try await client.fetchToken()
result = .success(response)
} catch {
result = .failure(error)
}
isBusy = false
}
case .failure(let error):
urlError = error.localizedDescription
}
}
}
#Preview {
ConnectionTestView(settings: SettingsStore())
}