Files
iOS/Sources/App/Frontend/WebView/FrontendView.swift
Bruno Pantaleão Gonçalves 8aa807c61c WebView wrapper improvements (#4811)
<!-- 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 -->
- Reduce UIKit usage
- Each server has it's own self-healing webview with connectivity
handling
## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## 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-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-06-23 14:39:49 +02:00

43 lines
1.6 KiB
Swift

import Shared
import SwiftUI
import UIKit
/// Hosts the UIKit `WebViewController` as a SwiftUI view. One instance per server: `ContainerView` keys the
/// host by server identity, so a server switch builds a fresh controller and discards the previous one.
struct FrontendView: UIViewControllerRepresentable {
let server: Server
var restorationType: WebViewRestorationType?
var onWebViewController: ((WebViewController) -> Void)?
let overlayState: WebFrontendOverlayState
init(
server: Server,
restorationType: WebViewRestorationType? = nil,
onWebViewController: ((WebViewController) -> Void)? = nil,
overlayState: WebFrontendOverlayState
) {
self.server = server
self.restorationType = restorationType
self.onWebViewController = onWebViewController
self.overlayState = overlayState
}
func makeUIViewController(context: Context) -> WebViewController {
let webViewController = makeWebViewController()
onWebViewController?(webViewController)
return webViewController
}
func updateUIViewController(_ webViewController: WebViewController, context: Context) {
// No-op: a server change recreates this view (keyed by server in `ContainerView`), never updates it.
}
// Non-private for tests.
func makeWebViewController() -> WebViewController {
let controller = WebViewController(server: server)
controller.initialURL = restorationType?.initialURL
controller.overlayState = overlayState
return controller
}
}