Files
m8chat-app2/lib/app/router.dart
T
help4bis 729822c97e feat(analytics): port SessionJourney tracking from GardenBuddy (v1.12.0+16)
Rolls the session/journey/friction tracking module out to M8Chat app2, per
plan ancient-petting-hejlsberg.md §5 step 4. Same pattern as GardenBuddy's
same-day port: SessionJourney.init() wired after WidgetsFlutterBinding.
ensureInitialized() and after the existing FlutterError.onError /
PlatformDispatcher.instance.onError handlers, which it chains rather than
replaces. SessionJourneyNavigatorObserver registered on the GoRouter
instance; SessionJourneyGestureLayer wraps MaterialApp.router for rage-tap
detection.

This repo has no shared offline-cache module like GardenBuddy's
core/offline/, so persistence is a small scoped sj_local_store.dart using
the package:web dependency already present in the repo (localStorage on
web, in-memory fallback for the test VM), rather than adding a new package.

app_key m8chat.au-app was reserved today in track.php v2.0.1's
allowed_sites/allowed_origins for https://app2.m8chat.au, confirmed by grep
before use.

Verified live: build deployed to app2.m8chat.au (md5-matched), Playwright
navigation produced real session/visitor/screen_view rows in
admin_flutterstats with app_key=m8chat.au-app, nginx logs show no 5xx and
no real visitor served a broken build during the test window.
2026-09-25 14:34:18 +10:00

144 lines
5.1 KiB
Dart

// Version: 1.2.0 | Created: 2026-04-01 | Updated: 2026-09-25
// All route definitions in one place.
// GoRouter is created ONCE (keepAlive) and re-evaluates redirects via
// refreshListenable — avoids the GoRouter recreation bug from ref.watch.
// v1.2.0: registered SessionJourneyNavigatorObserver (session/journey
// tracking, plan ancient-petting-hejlsberg.md §3/§5).
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../core/analytics/session_journey.dart';
import '../core/auth/auth_notifier.dart';
import '../core/auth/auth_state.dart';
import '../features/auth/presentation/login_screen.dart';
import '../features/calls/presentation/call_screen.dart';
import '../features/chat/presentation/chat_screen.dart';
import '../features/jitsi/presentation/jitsi_screen.dart';
import '../features/jitsi/presentation/welcome_screen.dart';
import '../features/profile/presentation/profile_screen.dart';
import '../features/rooms/presentation/rooms_screen.dart';
import '../features/spaces/presentation/spaces_screen.dart';
part 'router.g.dart';
/// Route path constants — use these instead of raw strings.
abstract final class AppRoutes {
static const String root = '/';
static const String welcome = '/welcome';
static const String login = '/login';
static const String rooms = '/rooms';
static const String chat = '/rooms/:roomId';
static const String call = '/calls/:roomId';
static const String profile = '/profile';
static const String spaces = '/spaces';
static const String jitsi = '/jitsi';
}
/// ChangeNotifier that GoRouter listens to for redirect re-evaluation.
/// Notified by ref.listen whenever authProvider changes.
class _AuthRefreshNotifier extends ChangeNotifier {
void notify() => notifyListeners();
}
/// Routes that are accessible without authentication.
const _publicRoutes = {AppRoutes.welcome, AppRoutes.login, AppRoutes.jitsi};
@Riverpod(keepAlive: true)
GoRouter router(Ref ref) {
// Create once — notifier triggers re-redirect without recreating the router.
final notifier = _AuthRefreshNotifier();
ref.listen<AuthState>(authProvider, (_, __) => notifier.notify());
ref.onDispose(notifier.dispose);
return GoRouter(
initialLocation: AppRoutes.root,
refreshListenable: notifier,
debugLogDiagnostics: false,
observers: [SessionJourneyNavigatorObserver()],
redirect: (BuildContext context, GoRouterState state) {
// Read (not watch) so redirect does not itself trigger provider changes.
final authState = ref.read(authProvider);
final isLoggedIn = authState is AuthAuthenticated;
final currentPath = state.matchedLocation;
final isPublicRoute = _publicRoutes.contains(currentPath);
// While restoring session, stay on splash.
if (authState is AuthInitial || authState is AuthLoading) return null;
// Unauthenticated users hitting a protected route → welcome screen.
if (!isLoggedIn && !isPublicRoute) return AppRoutes.welcome;
// Authenticated users on welcome or login → go straight to rooms.
if (isLoggedIn &&
(currentPath == AppRoutes.welcome ||
currentPath == AppRoutes.login ||
currentPath == AppRoutes.root)) {
return AppRoutes.rooms;
}
return null;
},
routes: [
GoRoute(
path: AppRoutes.root,
builder: (context, state) => const _SplashRedirectPage(),
),
GoRoute(
path: AppRoutes.welcome,
builder: (context, state) => const WelcomeScreen(),
),
GoRoute(
path: AppRoutes.login,
builder: (context, state) => const LoginScreen(),
),
GoRoute(
path: AppRoutes.rooms,
builder: (context, state) => const RoomsScreen(),
),
GoRoute(
path: AppRoutes.chat,
builder: (context, state) {
// safe: path param is guaranteed by route definition
final roomId = state.pathParameters['roomId']!;
return ChatScreen(roomId: roomId);
},
),
GoRoute(
path: AppRoutes.call,
builder: (context, state) {
final roomId = state.pathParameters['roomId']!;
final isVideo = state.uri.queryParameters['video'] != '0';
return CallScreen(roomId: roomId, isVideo: isVideo);
},
),
GoRoute(
path: AppRoutes.profile,
builder: (context, state) => const ProfileScreen(),
),
GoRoute(
path: AppRoutes.spaces,
builder: (context, state) => const SpacesScreen(),
),
GoRoute(
path: AppRoutes.jitsi,
builder: (context, state) {
final meetingUrl = state.extra as String? ?? '';
return JitsiScreen(meetingUrl: meetingUrl);
},
),
],
);
}
/// Invisible page shown at `/` while GoRouter's redirect evaluates auth state.
class _SplashRedirectPage extends StatelessWidget {
const _SplashRedirectPage();
@override
Widget build(BuildContext context) {
return const Scaffold(body: Center(child: CircularProgressIndicator()));
}
}