2026-05-28 16:29:23 +07:00

194 lines
7.6 KiB
Dart

import 'package:go_router/go_router.dart';
import '../app/injection_container.dart';
import '../core/constants/app_constants.dart';
import '../core/storage/secure_storage.dart';
import '../features/activity_log/presentation/screens/activity_log_screen.dart'
as activity;
import '../features/ai_benchmark/presentation/screens/ai_benchmark_screen.dart'
as benchmark;
import '../features/auth/login_screen.dart' as auth_login;
import '../features/auth/register_screen.dart' as auth_register;
import '../features/auth/splash_screen.dart' as auth_splash;
import '../features/call/presentation/screens/call_screen.dart' as call;
import '../features/guardian_dashboard/presentation/screens/guardian_activity_log_screen.dart'
as guardian_logs;
import '../features/guardian_dashboard/presentation/screens/guardian_ai_config_screen.dart'
as guardian_ai;
import '../features/guardian_dashboard/presentation/screens/guardian_map_screen.dart'
as guardian_map;
import '../features/guardian_dashboard/presentation/screens/guardian_send_notification_screen.dart'
as guardian_send;
import '../features/guardian_dashboard/presentation/screens/guardian_settings_screen.dart'
as guardian_settings;
import '../features/guardian_dashboard/presentation/screens/guardian_tools_screen.dart'
as guardian_tools;
import '../features/home/presentation/guardian_dashboard_screen.dart'
as guardian_home;
import '../features/navigation_mode/presentation/screens/navigation_mode_screen.dart'
as nav;
import '../features/notifications/presentation/screens/notification_screen.dart'
as notifications;
import '../features/pairing/presentation/screens/pairing_screens.dart'
as pairing;
import '../features/server_connect/server_connect_server.dart'
as server_connect;
import '../features/settings/presentation/screens/user_settings_screen.dart'
as user_settings;
import '../features/sos/presentation/screens/sos_screen.dart' as sos;
import '../features/walk_guide/presentation/screens/walk_guide_screen.dart'
as walk_guide;
import '../shared/widgets/app_shells.dart';
final GoRouter appRouter = GoRouter(
initialLocation: '/splash',
redirect: (context, state) async {
final path = state.matchedLocation;
final serverUrl = await AppConstants.getServerUrl();
final isEditingServer =
path == '/server-connect' && state.uri.queryParameters['edit'] == '1';
final isPublicRoute = path == '/server-connect' ||
path == '/splash' ||
path == '/login' ||
path == '/register';
if ((serverUrl == null || serverUrl.isEmpty) && path != '/server-connect') {
return '/server-connect';
}
if (!isEditingServer &&
path == '/server-connect' &&
serverUrl != null &&
serverUrl.isNotEmpty) {
return '/splash';
}
if (serverUrl == null || serverUrl.isEmpty) return null;
final storage = sl<SecureStorage>();
final token = await storage.getAccessToken();
final role = await storage.getUserRole();
if ((token == null || token.isEmpty) && !isPublicRoute) {
return '/login';
}
if (token != null && token.isNotEmpty) {
final home = role == 'ROLE_GUARDIAN'
? '/guardian/dashboard'
: role == 'ROLE_USER'
? '/user/walkguide'
: '/login';
if (path == '/splash' || path == '/login' || path == '/register') {
return home;
}
if (path.startsWith('/guardian') && role != 'ROLE_GUARDIAN') {
return '/user/walkguide';
}
if (path.startsWith('/user') && role != 'ROLE_USER') {
return '/guardian/dashboard';
}
}
return null;
},
routes: [
GoRoute(
path: '/server-connect',
builder: (_, state) => server_connect.ServerConnectScreen(
editMode: state.uri.queryParameters['edit'] == '1',
)),
GoRoute(
path: '/splash', builder: (_, __) => const auth_splash.SplashScreen()),
GoRoute(path: '/login', builder: (_, __) => const auth_login.LoginScreen()),
GoRoute(
path: '/register',
builder: (_, __) => const auth_register.RegisterScreen()),
GoRoute(
path: '/incoming-call',
builder: (_, state) {
final extra = state.extra is Map
? Map<String, dynamic>.from(state.extra as Map)
: <String, dynamic>{};
return call.IncomingCallScreen(
callerName: extra['callerName']?.toString() ?? 'Guardian',
callerId: int.tryParse(extra['callerId']?.toString() ?? ''),
channelName: extra['channelName']?.toString(),
agoraToken: extra['agoraToken']?.toString(),
agoraAppId: extra['agoraAppId']?.toString(),
);
}),
ShellRoute(
builder: (_, __, child) => UserShell(child: child),
routes: [
GoRoute(
path: '/user/walkguide',
builder: (_, __) => const walk_guide.WalkGuideScreen()),
GoRoute(path: '/user/sos', builder: (_, __) => const sos.SosScreen()),
GoRoute(
path: '/user/activity',
builder: (_, __) => const activity.ActivityLogScreen()),
GoRoute(
path: '/user/notifications',
builder: (_, __) => const notifications.NotificationScreen()),
GoRoute(
path: '/user/navigation',
builder: (_, __) => const nav.NavigationModeScreen()),
GoRoute(
path: '/user/settings',
builder: (_, __) => const user_settings.UserSettingsScreen()),
GoRoute(
path: '/user/pairing',
builder: (_, __) => const pairing.UserPairingScreen()),
GoRoute(
path: '/user/call', builder: (_, __) => const call.CallScreen()),
GoRoute(
path: '/user/benchmark',
builder: (_, __) => const benchmark.AiBenchmarkScreen()),
],
),
ShellRoute(
builder: (_, __, child) => GuardianShell(child: child),
routes: [
GoRoute(
path: '/guardian/dashboard',
builder: (_, __) => const guardian_home.GuardianDashboardScreen()),
GoRoute(
path: '/guardian/map',
builder: (_, __) => const guardian_map.GuardianMapScreen()),
GoRoute(
path: '/guardian/logs',
builder: (_, __) =>
const guardian_logs.GuardianActivityLogScreen()),
GoRoute(
path: '/guardian/send-notif',
builder: (_, __) => const guardian_send.GuardianSendNotifScreen()),
GoRoute(
path: '/guardian/ai-config',
builder: (_, __) => const guardian_ai.GuardianAiConfigScreen()),
GoRoute(
path: '/guardian/voice-cmd',
builder: (_, __) => const guardian_tools.GuardianVoiceCmdScreen()),
GoRoute(
path: '/guardian/shortcuts',
builder: (_, __) => const guardian_tools.GuardianShortcutScreen()),
GoRoute(
path: '/guardian/geofence',
builder: (_, __) => const guardian_tools.GuardianGeofenceScreen()),
GoRoute(
path: '/guardian/pairing',
builder: (_, __) => const pairing.GuardianPairingScreen()),
GoRoute(
path: '/guardian/settings',
builder: (_, __) =>
const guardian_settings.GuardianSettingsScreen()),
GoRoute(
path: '/guardian/call',
builder: (_, __) => const call.CallScreen(
targetLabel: 'User',
returnRoute: '/guardian/dashboard',
)),
GoRoute(
path: '/guardian/benchmark',
builder: (_, __) => const benchmark.AiBenchmarkScreen()),
],
),
],
);