feat(companion): kiosk session survives remote ⇄ dashboard navigation

The WEB_VIEW route recreated the WebView on every return from the
remote screen — full reload + re-login every time. The kiosk WebView is
now retained in a holder and reattached live (no race, no reload);
clients/bridges/listeners are re-bound to the new composition on
reattach, and the instance is dropped on retry, disconnect, or server
change so errored/stale sessions still reload for real.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Dorian 2026-07-24 01:19:57 +01:00
parent 1bcf6ccadb
commit 1e20b79c3c
2 changed files with 46 additions and 4 deletions

View File

@ -123,6 +123,23 @@ private fun isSameHost(url: String, base: String): Boolean {
}
}
/** Kiosk WebView retained across navigation (remote dashboard) so leaving
* the kiosk and coming back reattaches the LIVE page no reload, no
* re-login, no reconnect. Dropped on retry/disconnect/server change. */
private object KioskWebView {
var instance: WebView? = null
var url: String? = null
fun drop() {
instance?.let {
(it.parent as? ViewGroup)?.removeView(it)
it.destroy()
}
instance = null
url = null
}
}
/** True when a TCP listener answers at [base]'s host:port within [timeoutMs]. */
private fun tcpAnswers(base: String, timeoutMs: Int): Boolean = try {
val u = android.net.Uri.parse(base)
@ -213,6 +230,13 @@ fun WebViewScreen(
var startUrl by remember(serverUrl) { mutableStateOf<String?>(null) }
var raceNonce by remember { mutableIntStateOf(0) }
LaunchedEffect(serverUrl, meshFallbackUrl, raceNonce) {
// A retained live session exists — reattach instantly: no race, no
// reload, no re-login (remote ⇄ dashboard round trip).
if (KioskWebView.instance != null && KioskWebView.url == serverUrl) {
isLoading = false
startUrl = serverUrl
return@LaunchedEffect
}
val picked = pickStartUrl(serverUrl, meshFallbackUrl)
// Starting on the mesh: don't bounce back to it on error (it IS it).
if (picked != serverUrl) triedMeshFallback = true
@ -328,7 +352,10 @@ fun WebViewScreen(
text = stringResource(R.string.retry),
onClick = {
// Re-race LAN vs mesh — the network we're on may have
// changed since the last pick.
// changed since the last pick. Drop the retained view:
// an errored session must genuinely reload.
KioskWebView.drop()
webView = null
hasError = false
isLoading = true
triedMeshFallback = false
@ -342,7 +369,10 @@ fun WebViewScreen(
GlassButton(
text = stringResource(R.string.disconnect),
onClick = onDisconnect,
onClick = {
KioskWebView.drop()
onDisconnect()
},
modifier = Modifier.fillMaxWidth().height(48.dp),
)
}
@ -359,7 +389,15 @@ fun WebViewScreen(
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context ->
WebView(context).apply {
// Reattach the retained kiosk WebView (remote ⇄ dashboard
// must not reload the node UI). Everything configured
// below is idempotent, and re-running it rebinds clients,
// bridges and listeners to THIS composition's state —
// stale closures from the previous visit are replaced.
if (KioskWebView.url != serverUrl) KioskWebView.drop()
val reused = KioskWebView.instance
(reused ?: WebView(context)).apply {
(parent as? ViewGroup)?.removeView(this)
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
@ -665,7 +703,11 @@ fun WebViewScreen(
}
webView = this
loadUrl(initialUrl)
if (reused == null) {
KioskWebView.instance = this
KioskWebView.url = serverUrl
loadUrl(initialUrl)
}
}
},
)