build 6, watchdog
This commit is contained in:
+67
-8
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:collection';
|
||||
import 'dart:convert';
|
||||
import 'dart:io' show Platform;
|
||||
@@ -99,6 +100,20 @@ class _WebShellState extends State<WebShell> with WidgetsBindingObserver {
|
||||
bool _recovering = false;
|
||||
int _loadRetries = 0;
|
||||
|
||||
/// Watchdog: the page reports its first screen through the `route` handler.
|
||||
/// No report within [_readyTimeout] → fresh WebView; after
|
||||
/// [_maxWatchdogRestarts] in a row the error screen with Try again shows.
|
||||
static const _readyTimeout = Duration(seconds: 6);
|
||||
static const _maxWatchdogRestarts = 3;
|
||||
bool _pageReady = false;
|
||||
Timer? _readyTimer;
|
||||
int _watchdogRestarts = 0;
|
||||
|
||||
/// After a long time in the background start from a fresh page anyway:
|
||||
/// iOS often leaves a WebView behind that no longer draws.
|
||||
static const _staleAfter = Duration(minutes: 30);
|
||||
DateTime? _backgroundSince;
|
||||
|
||||
/// Selected tab while the bar is shown; null = hidden (survey flow, welcome).
|
||||
int? _tab;
|
||||
|
||||
@@ -129,19 +144,29 @@ class _WebShellState extends State<WebShell> with WidgetsBindingObserver {
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
_readyTimer?.cancel();
|
||||
_server.stop();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed && _ready) _recover();
|
||||
if (state == AppLifecycleState.paused || state == AppLifecycleState.hidden) {
|
||||
_backgroundSince ??= DateTime.now();
|
||||
} else if (state == AppLifecycleState.resumed) {
|
||||
final since = _backgroundSince;
|
||||
_backgroundSince = null;
|
||||
if (!_ready) return;
|
||||
final stale = since != null && DateTime.now().difference(since) > _staleAfter;
|
||||
// Give the WebView a moment to come back to the screen before probing it.
|
||||
Future<void>.delayed(const Duration(milliseconds: 600), () => _recover(forceNewWebView: stale));
|
||||
}
|
||||
}
|
||||
|
||||
/// Back from the background: iOS may have taken the server's socket and
|
||||
/// killed the page's process while the app was suspended.
|
||||
/// killed (or frozen) the page's process while the app was suspended.
|
||||
Future<void> _recover({bool forceNewWebView = false}) async {
|
||||
if (_recovering) return;
|
||||
if (_recovering || !mounted) return;
|
||||
_recovering = true;
|
||||
try {
|
||||
await _server.ensureRunning();
|
||||
@@ -154,14 +179,20 @@ class _WebShellState extends State<WebShell> with WidgetsBindingObserver {
|
||||
}
|
||||
}
|
||||
|
||||
/// Alive = the page runs script AND draws: it answers after an animation
|
||||
/// frame and has content. A WebView that lost its renderer never answers.
|
||||
Future<bool> _pageAlive() async {
|
||||
final c = _controller;
|
||||
if (c == null) return false;
|
||||
if (c == null || !_pageReady) return false;
|
||||
try {
|
||||
final n = await c
|
||||
.evaluateJavascript(source: "(document.getElementById('root') || {}).childElementCount || 0")
|
||||
.timeout(const Duration(seconds: 2));
|
||||
return n is num && n > 0;
|
||||
final res = await c
|
||||
.callAsyncJavaScript(
|
||||
functionBody: "return await new Promise((resolve) => requestAnimationFrame(() => "
|
||||
"resolve((document.getElementById('root') || {}).childElementCount || 0)));",
|
||||
)
|
||||
.timeout(const Duration(seconds: 3));
|
||||
final n = res?.value;
|
||||
return res?.error == null && n is num && n > 0;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
@@ -171,10 +202,31 @@ class _WebShellState extends State<WebShell> with WidgetsBindingObserver {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_controller = null;
|
||||
_pageReady = false;
|
||||
_webKey = UniqueKey();
|
||||
});
|
||||
}
|
||||
|
||||
/// Started with every WebView: no first screen in time → start a fresh one.
|
||||
void _armWatchdog() {
|
||||
_readyTimer?.cancel();
|
||||
_readyTimer = Timer(_readyTimeout, () {
|
||||
if (!mounted || _pageReady || _error != null) return;
|
||||
if (_watchdogRestarts >= _maxWatchdogRestarts) {
|
||||
setState(() => _error = 'SkyContour could not load its screen.');
|
||||
return;
|
||||
}
|
||||
_watchdogRestarts++;
|
||||
_recover(forceNewWebView: true);
|
||||
});
|
||||
}
|
||||
|
||||
/// A tap on the native tab bar must change the page; if the page is dead, restart it.
|
||||
Future<void> _probeAfterTap() async {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 300));
|
||||
if (mounted && _ready && !await _pageAlive()) _recover(forceNewWebView: true);
|
||||
}
|
||||
|
||||
/// The page itself failed to load (server not answering): bind again, retry a few times.
|
||||
void _onMainFrameError() {
|
||||
if (_loadRetries >= 3) return;
|
||||
@@ -241,6 +293,10 @@ class _WebShellState extends State<WebShell> with WidgetsBindingObserver {
|
||||
|
||||
/// The web app reports its screen: {name, tab: 'sites'|'settings'|null}.
|
||||
Future<dynamic> _handleRoute(List<dynamic> args) async {
|
||||
// First report = the page is up and drawing (watchdog satisfied).
|
||||
_pageReady = true;
|
||||
_watchdogRestarts = 0;
|
||||
_readyTimer?.cancel();
|
||||
final info = args.isNotEmpty && args[0] is Map ? args[0] as Map : const {};
|
||||
final index = _tabs.indexWhere((t) => t.id == info['tab']);
|
||||
final labels = info['labels'] is Map
|
||||
@@ -285,6 +341,7 @@ class _WebShellState extends State<WebShell> with WidgetsBindingObserver {
|
||||
HapticFeedback.selectionClick();
|
||||
final id = _tabs[index].id;
|
||||
_controller?.evaluateJavascript(source: "window.__sfzNav && window.__sfzNav('$id')");
|
||||
_probeAfterTap();
|
||||
}
|
||||
|
||||
/// Kept for older web builds that saved the raw panorama PNG to the gallery.
|
||||
@@ -313,6 +370,7 @@ class _WebShellState extends State<WebShell> with WidgetsBindingObserver {
|
||||
style: FilledButton.styleFrom(backgroundColor: _accent, foregroundColor: Colors.black),
|
||||
onPressed: () {
|
||||
_loadRetries = 0;
|
||||
_watchdogRestarts = 0;
|
||||
if (_ready) {
|
||||
_recover(forceNewWebView: true);
|
||||
} else {
|
||||
@@ -366,6 +424,7 @@ class _WebShellState extends State<WebShell> with WidgetsBindingObserver {
|
||||
),
|
||||
onWebViewCreated: (controller) {
|
||||
_controller = controller;
|
||||
_armWatchdog();
|
||||
controller.addJavaScriptHandler(handlerName: 'route', callback: _handleRoute);
|
||||
controller.addJavaScriptHandler(handlerName: 'keepAwake', callback: _handleKeepAwake);
|
||||
controller.addJavaScriptHandler(handlerName: 'haptic', callback: _handleHaptic);
|
||||
|
||||
Reference in New Issue
Block a user