iOS/Sources/App/WebView/ConnectivityCheck/ConnectivityCheckState.swift
Bruno Pantaleão Gonçalves 0553c44d53
Add connection troubleshooting to error view (#4245)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->
<img width="2360" height="1640" alt="Simulator Screenshot - iPad Air
11-inch (M3) - 2026-01-21 at 13 27 07"
src="https://github.com/user-attachments/assets/1f7aac7c-2b1b-4252-b81d-e2ca27c40700"
/>

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-01-21 15:19:29 +01:00

71 lines
1.8 KiB
Swift

import Foundation
import Shared
enum ConnectivityCheckType: String, CaseIterable {
case dns = "DNS Resolution"
case port = "Port Reachability"
case tls = "TLS Certificate"
case server = "Server Connection"
var localizedName: String {
switch self {
case .dns:
return L10n.Connectivity.Check.dns
case .port:
return L10n.Connectivity.Check.port
case .tls:
return L10n.Connectivity.Check.tls
case .server:
return L10n.Connectivity.Check.server
}
}
}
enum ConnectivityCheckResult: Equatable {
case pending
case running
case success(message: String?)
case failure(error: String)
case skipped
var isCompleted: Bool {
switch self {
case .success, .failure, .skipped:
return true
case .pending, .running:
return false
}
}
}
struct ConnectivityCheck: Identifiable {
let id = UUID()
let type: ConnectivityCheckType
var result: ConnectivityCheckResult
init(type: ConnectivityCheckType, result: ConnectivityCheckResult = .pending) {
self.type = type
self.result = result
}
}
class ConnectivityCheckState: ObservableObject {
@Published var checks: [ConnectivityCheck] = []
@Published var isRunning: Bool = false
init() {
self.checks = ConnectivityCheckType.allCases.map { ConnectivityCheck(type: $0) }
}
func updateCheck(type: ConnectivityCheckType, result: ConnectivityCheckResult) {
if let index = checks.firstIndex(where: { $0.type == type }) {
checks[index].result = result
}
}
func reset() {
checks = ConnectivityCheckType.allCases.map { ConnectivityCheck(type: $0) }
isRunning = false
}
}