Files
buzz/mobile/lib/shared/diagnostics/diagnostic_navigation_observer.dart
Tom Brow a0da08fde7 feat(mobile): add screen breadcrumbs to diagnostic reports
A hang or error report said what the connection was doing but not where
the person was. Reports now carry a trail of the screens they moved to.

A NavigatorObserver on the root navigator and on the conversation pane's
navigator leaves a breadcrumb through the existing reporter when a page is
pushed, replaced, or revealed again by a pop. The app pushes page widgets
without route settings, so a route is named by the first listed page widget
below it, and any other page is "other". Nothing is read from route
settings, arguments or page content, so no channel or community name, ID or
pubkey can reach a breadcrumb. Dialogs, sheets and menus leave none. The
filter keeps the navigation category and drops any entry whose screen is not
on the app's list. A build without a reporting destination does nothing.

Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
Signed-off-by: Tom Brow <tomb@block.xyz>
2026-11-20 11:20:15 -08:00

68 lines
2.3 KiB
Dart

import 'package:flutter/widgets.dart';
import 'diagnostic_reporter.dart';
import 'diagnostic_screens.dart';
/// Leaves a breadcrumb for each screen the person moves to or back to, so a
/// report shows which screens they were on.
///
/// Attach one to each navigator that holds screens: the app's root navigator
/// and the pane navigator that holds an open conversation. Only pages
/// ([PageRoute]) count. Dialogs, sheets and menus are popup routes with no
/// screen of their own, so opening or closing one leaves nothing. A build
/// without a reporting destination does nothing, not even looking at routes.
class DiagnosticNavigationObserver extends NavigatorObserver {
DiagnosticNavigationObserver(this._reporter);
final DiagnosticReporter _reporter;
@override
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) =>
_arrived(route);
@override
void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) =>
_arrived(newRoute);
@override
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) =>
_revealed(route, previousRoute);
@override
void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) =>
_revealed(route, previousRoute);
void _arrived(Route<dynamic>? route) {
if (route is PageRoute<dynamic>) _announce(route);
}
void _revealed(Route<dynamic> left, Route<dynamic>? revealed) {
if (left is PageRoute<dynamic> && revealed is PageRoute<dynamic>) {
_announce(revealed);
}
}
/// Names [route] once the frame has built it, if it is then what the person
/// sees. Several pops in one go, or a route replaced at once, leave only the
/// screen they end on.
void _announce(PageRoute<dynamic> route) {
if (!_reporter.reporting) return;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!_shown(route)) return;
_reporter.leaveBreadcrumb(
DiagnosticBreadcrumb.screen(DiagnosticScreens.of(route)),
);
});
}
/// Whether [route] is current on its navigator and every navigator hosting
/// it.
static bool _shown(Route<dynamic> route) {
if (!route.isCurrent) return false;
final navigator = route.navigator;
if (navigator == null) return false;
final host = ModalRoute.of(navigator.context);
return host == null || _shown(host);
}
}