MotorSignal-App/lib/core/router.dart

312 lines
10 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../features/dashboard/dashboard_screen.dart';
import '../features/map/map_screen.dart';
import '../features/settings/settings_screen.dart';
import '../features/settings/diagnostics_screen.dart';
import '../features/garage/garage_screen.dart';
import '../features/garage/add_motorcycle_screen.dart';
import '../features/garage/motorcycle_detail_screen.dart';
import 'widgets/main_layout.dart';
import '../features/settings/field_test_report_screen.dart';
import '../features/settings/field_test_live_log_screen.dart';
import '../features/field_test/field_test_pre_ride_check_screen.dart';
import '../features/settings/oauth_callback_screen.dart';
import '../features/settings/account_screen.dart';
import '../features/entitlements/pro_screen.dart';
import '../features/activity/activity_screen.dart';
import '../features/social/social_hub_screen.dart';
import '../features/social/rider_profile_screen.dart';
import '../features/social/friends_screen.dart';
import '../features/social/clubs/clubs_hub_screen.dart';
import '../features/social/clubs/create_club_screen.dart';
import '../features/social/clubs/club_detail_screen.dart';
import '../features/social/feed/social_feed_screen.dart';
import '../features/group_rides/group_rides_hub_screen.dart';
import '../features/group_rides/create_group_ride_screen.dart';
import '../features/group_rides/group_ride_detail_screen.dart';
import '../features/group_rides/live_group_ride_screen.dart';
import '../features/routes/route_planner_screen.dart';
import '../features/routes/route_detail_screen.dart';
import '../features/performance/performance_launch_screen.dart';
// ignore: unused_element
final _rootNavigatorKey = GlobalKey<NavigatorState>();
final appRouter = GoRouter(
initialLocation: '/',
errorBuilder: (context, state) {
final uri = state.uri;
if (uri.toString().contains('oauth') || uri.toString().contains('code=')) {
return OAuthCallbackScreen(uri: uri);
}
return Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Text(
'Navigation Error: ${state.error}',
style: const TextStyle(color: Colors.white),
),
),
);
},
redirect: (context, state) {
final uri = state.uri;
final uriStr = uri.toString();
if ((uri.scheme == 'motorsignal' && uri.host == 'oauth') ||
uri.host == 'oauth' ||
uriStr.contains('/oauth/callback')) {
if (state.matchedLocation != '/oauth/callback') {
return '/oauth/callback?${uri.query}';
}
}
if (uri.scheme == 'motorsignal') {
if (uri.host == 'group-rides') {
return '/group-rides${uri.path}';
}
if (uri.host == 'clubs') {
return '/social/clubs${uri.path}';
}
if (uri.path.isNotEmpty && uri.path != '/') {
return uri.path;
}
}
return null;
},
routes: [
GoRoute(
path: '/oauth/callback',
builder: (context, state) => OAuthCallbackScreen(uri: state.uri),
),
StatefulShellRoute.indexedStack(
builder: (context, state, navigationShell) {
return MainLayout(navigationShell: navigationShell);
},
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const DashboardScreen(),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/map',
builder: (context, state) => const MapScreen(),
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/activity',
builder: (context, state) {
final tabParam = state.uri.queryParameters['tab'];
final tab = tabParam == 'records'
? ActivityTab.records
: ActivityTab.rides;
return ActivityScreen(initialTab: tab);
},
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/garage',
builder: (context, state) => const GarageScreen(),
routes: [
GoRoute(
path: 'add',
builder: (context, state) => const AddMotorcycleScreen(),
),
GoRoute(
path: ':id',
builder: (context, state) {
final id = int.parse(state.pathParameters['id']!);
return MotorcycleDetailScreen(motorcycleId: id);
},
),
],
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/social',
builder: (context, state) {
final tabParam = state.uri.queryParameters['tab'];
final initialTab = tabParam == 'hub' || tabParam == 'network'
? SocialHubTab.hub
: SocialHubTab.feed;
return SocialHubScreen(initialTab: initialTab);
},
routes: [
GoRoute(
path: 'profile',
builder: (context, state) => const RiderProfileScreen(),
),
GoRoute(
path: 'friends',
builder: (context, state) => const FriendsScreen(),
),
GoRoute(
path: 'clubs',
builder: (context, state) => const ClubsHubScreen(),
routes: [
GoRoute(
path: 'create',
builder: (context, state) => const CreateClubScreen(),
),
GoRoute(
path: ':id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return ClubDetailScreen(clubId: id);
},
),
],
),
GoRoute(
path: 'feed',
builder: (context, state) => const SocialFeedScreen(),
),
],
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreen(),
routes: [
GoRoute(
path: 'account',
builder: (context, state) => const AccountScreen(),
),
GoRoute(
path: 'pro',
builder: (context, state) => const ProScreen(),
),
GoRoute(
path: 'profile',
builder: (context, state) => const RiderProfileScreen(),
),
GoRoute(
path: 'friends',
builder: (context, state) => const FriendsScreen(),
),
GoRoute(
path: 'diagnostics',
builder: (context, state) => const DiagnosticsScreen(),
),
GoRoute(
path: 'field-test-report',
builder: (context, state) {
final rideId = state.uri.queryParameters['rideId'] != null
? int.tryParse(state.uri.queryParameters['rideId']!)
: null;
return FieldTestReportScreen(rideId: rideId);
},
),
GoRoute(
path: 'field-test-live-log',
builder: (context, state) => const FieldTestLiveLogScreen(),
),
GoRoute(
path: 'field-test-pre-ride-check',
builder: (context, state) =>
const FieldTestPreRideCheckScreen(),
),
],
),
],
),
],
),
GoRoute(
path: '/account',
builder: (context, state) => const AccountScreen(),
),
GoRoute(
path: '/pro',
builder: (context, state) => const ProScreen(),
),
GoRoute(
path: '/history',
builder: (context, state) =>
const ActivityScreen(initialTab: ActivityTab.rides),
),
GoRoute(
path: '/records',
builder: (context, state) =>
const ActivityScreen(initialTab: ActivityTab.records),
),
GoRoute(
path: '/group-rides',
builder: (context, state) => const GroupRidesHubScreen(),
routes: [
GoRoute(
path: 'create',
builder: (context, state) {
final clubId = state.uri.queryParameters['clubId'];
return CreateGroupRideScreen(clubId: clubId);
},
),
GoRoute(
path: ':id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return GroupRideDetailScreen(rideId: id);
},
routes: [
GoRoute(
path: 'live',
builder: (context, state) {
final id = state.pathParameters['id']!;
return LiveGroupRideScreen(rideId: id);
},
),
],
),
],
),
GoRoute(
path: '/routes/planner',
builder: (context, state) {
final initialRouteId = state.uri.queryParameters['routeId'];
final attachToGroupRideId = state.uri.queryParameters['groupRideId'];
return RoutePlannerScreen(
initialRouteId: initialRouteId,
attachToGroupRideId: attachToGroupRideId,
);
},
),
GoRoute(
path: '/routes/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return RouteDetailScreen(routeId: id);
},
),
GoRoute(
path: '/launch',
builder: (context, state) => const PerformanceLaunchScreen(),
),
GoRoute(
path: '/social/feed',
builder: (context, state) => const SocialFeedScreen(),
),
GoRoute(
path: '/feed',
builder: (context, state) => const SocialFeedScreen(),
),
],
);