- Decentralized Room Registry pattern - Each room registers itself with central registry - Dynamic navigation tabs and settings dropdown - New Media Room and Parlor feature folders - Permission-based room filtering support 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
104 lines
3.2 KiB
Dart
104 lines
3.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
import 'package:tatlock_ui/core/auth/auth_provider.dart';
|
|
import 'package:tatlock_ui/features/control_room/router.dart';
|
|
import 'package:tatlock_ui/features/front_hall/router.dart';
|
|
import 'package:tatlock_ui/features/media_room/router.dart';
|
|
import 'package:tatlock_ui/features/parlor/router.dart';
|
|
import 'package:tatlock_ui/features/security/router.dart';
|
|
import 'package:tatlock_ui/features/settings/presentation/pages/settings_page.dart';
|
|
import 'package:tatlock_ui/routing/room_registry.dart';
|
|
import 'package:tatlock_ui/shared/layouts/app_scaffold.dart';
|
|
|
|
part 'app_router.g.dart';
|
|
|
|
/// Initialize room registry with all rooms.
|
|
///
|
|
/// Called once before creating the router.
|
|
/// Order of registration determines tab order in UI.
|
|
void _initializeRoomRegistry() {
|
|
// Skip if already initialized
|
|
if (roomRegistry.all.isNotEmpty) return;
|
|
|
|
// Register rooms in display order
|
|
registerFrontHall();
|
|
registerControlRoom();
|
|
registerSecurity();
|
|
registerParlor();
|
|
registerMediaRoom();
|
|
}
|
|
|
|
/// No-animation page builder for instant transitions
|
|
Page<void> noTransitionPage(BuildContext context, GoRouterState state, Widget child) {
|
|
return NoTransitionPage<void>(
|
|
key: state.pageKey,
|
|
child: child,
|
|
);
|
|
}
|
|
|
|
/// Route paths as constants.
|
|
abstract class AppRoutes {
|
|
static const root = '/';
|
|
static const settings = '/settings';
|
|
}
|
|
|
|
/// Maps defaultRoom preference value to route path.
|
|
String _routeForDefaultRoom(String? defaultRoom) {
|
|
// Look up room in registry, fall back to first room's default route
|
|
final room = roomRegistry.byId(defaultRoom ?? '');
|
|
if (room != null) {
|
|
return room.defaultRoute;
|
|
}
|
|
// Default to first registered room (Front Hall)
|
|
return roomRegistry.all.isNotEmpty
|
|
? roomRegistry.all.first.defaultRoute
|
|
: FrontHallRoutes.base;
|
|
}
|
|
|
|
/// Provides the GoRouter instance.
|
|
@riverpod
|
|
GoRouter appRouter(Ref ref) {
|
|
// Initialize room registry on first access
|
|
_initializeRoomRegistry();
|
|
|
|
final authState = ref.watch(authProvider);
|
|
|
|
return GoRouter(
|
|
initialLocation: AppRoutes.root,
|
|
debugLogDiagnostics: true,
|
|
redirect: (context, state) {
|
|
// Redirect from root to user's default room preference
|
|
if (state.matchedLocation == AppRoutes.root) {
|
|
final defaultRoom = authState.value?.preferences?.defaultRoom;
|
|
return _routeForDefaultRoom(defaultRoom);
|
|
}
|
|
return null;
|
|
},
|
|
routes: [
|
|
// Root redirects to default room (handled by redirect callback above)
|
|
GoRoute(
|
|
path: AppRoutes.root,
|
|
redirect: (context, state) => _routeForDefaultRoom(null),
|
|
),
|
|
// Main app routes (inside shell with app scaffold)
|
|
ShellRoute(
|
|
builder: (context, state, child) => AppScaffold(child: child),
|
|
routes: [
|
|
// All room routes from registry
|
|
...roomRegistry.allRoutes(),
|
|
// Settings page (not a room, just a page)
|
|
GoRoute(
|
|
path: AppRoutes.settings,
|
|
name: 'settings',
|
|
pageBuilder: (context, state) =>
|
|
noTransitionPage(context, state, const SettingsPage()),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
|