-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.dart
More file actions
362 lines (324 loc) · 11.8 KB
/
Copy pathroutes.dart
File metadata and controls
362 lines (324 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:pilgrims_3d/presentation/screens/about_screen.dart';
import 'package:pilgrims_3d/presentation/screens/all_routes_map_screen.dart';
import 'package:pilgrims_3d/presentation/screens/nearby_routes_map_screen.dart';
import 'package:pilgrims_3d/presentation/screens/ar_screen.dart';
import 'package:pilgrims_3d/presentation/screens/create_poi_screen.dart';
import 'package:pilgrims_3d/presentation/screens/model3d_screen.dart';
import 'package:pilgrims_3d/presentation/screens/my_pois_screen.dart';
import 'package:pilgrims_3d/presentation/screens/navigation_map_screen.dart';
import 'package:pilgrims_3d/presentation/screens/one_route_map_screen.dart';
import 'package:pilgrims_3d/presentation/screens/route_stages_screen.dart';
import 'package:pilgrims_3d/presentation/screens/relevant_pois_screen.dart';
import 'package:pilgrims_3d/presentation/screens/poi_detail_screen.dart';
import 'package:pilgrims_3d/presentation/screens/create_route_screen.dart';
import 'package:pilgrims_3d/presentation/screens/my_routes_screen.dart';
import 'package:pilgrims_3d/core/config/env.dart';
import 'package:pilgrims_3d/core/utils/initial_location_stub.dart'
if (dart.library.html) 'package:pilgrims_3d/core/utils/initial_location_web.dart';
// Screens
import '../../presentation/screens/home_screen.dart';
import '../../presentation/screens/events_screen.dart';
import '../../presentation/screens/audios_screen.dart';
import '../../presentation/screens/login_screen.dart';
// Imports para pantallas adicionales comentadas (evitar imports no usados)
import '../../presentation/screens/terms_screen.dart';
import '../../presentation/screens/privacy_policy_screen.dart';
import '../../presentation/screens/delete_account_screen.dart';
import '../../presentation/screens/survey_screen.dart';
/// Global Navigator Key para acceder al contexto desde cualquier lugar
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
/// Configuración de rutas de la aplicación usando GoRouter
class AppRouter {
// Flag global para indicar si la sesión actual es de invitado
static bool guestMode = false;
static final GoRouter router = GoRouter(
navigatorKey: navigatorKey,
initialLocation: getInitialLocation(),
// Rutas de la aplicación
routes: [
// 🔓 RUTAS PÚBLICAS (sin autenticación)
GoRoute(
path: '/login',
name: 'login',
builder: (context, state) => LoginScreen(),
),
GoRoute(
path: '/poi/:poiId',
name: 'poiDetail',
builder: (context, state) {
final poiId = state.pathParameters['poiId'];
if (poiId == null || poiId.isEmpty) {
return const HomeScreen();
}
return PoiDetailScreen(poiId: poiId);
},
),
GoRoute(
path: '/terms',
name: 'terms',
builder: (context, state) => const TermsAndConditionsScreen(),
),
GoRoute(
path: '/privacy',
name: 'privacy',
builder: (context, state) => const PrivacyPolicyScreen(),
),
GoRoute(
path: '/delete_account',
name: 'deleteAccount',
builder: (context, state) => const DeleteAccountScreen(),
),
// 🔒 RUTAS PRIVADAS (requieren autenticación)
GoRoute(
path: '/',
name: 'home',
builder: (context, state) => const HomeScreen(),
),
GoRoute(
path: '/events',
name: 'events',
builder: (context, state) => const EventsScreen(),
),
GoRoute(
path: '/audios',
name: 'audios',
builder: (context, state) => const AudiosScreen(),
),
GoRoute(
path: '/about',
name: 'about',
builder: (context, state) => const AboutScreen(),
),
GoRoute(
path: '/survey',
name: 'survey',
builder: (context, state) => const SurveyScreen(),
),
GoRoute(
path: '/route',
name: 'route',
redirect: (context, state) {
final args = state.extra as Map<String, dynamic>?;
if (args == null ||
args['routeId'] == null ||
args['routeName'] == null) {
return '/';
}
return null;
},
builder: (context, state) {
final args = state.extra as Map<String, dynamic>?;
return RouteStagesScreen(
routeName: args?['routeName'] ?? '',
routeId: args?['routeId'] ?? '',
);
},
),
GoRoute(
path: '/map',
name: 'map',
redirect: (context, state) {
final args = state.extra as Map<String, dynamic>?;
if (args == null || args['routeId'] == null) {
return '/';
}
return null;
},
builder: (context, state) {
final args = state.extra as Map<String, dynamic>?;
final routeId = args?['routeId'] ?? '';
final routeName = args?['routeName'] ?? routeId;
final pois = args?['pois'] as List<dynamic>? ?? [];
return OneRouteMapScreen(
routeId: routeId,
routeName: routeName,
pois: pois,
);
},
),
GoRoute(
path: '/navigationMap',
name: 'navigationMap',
redirect: (context, state) {
final args = state.extra as Map<String, dynamic>?;
if (args == null ||
args['destinationLatitude'] == null ||
args['destinationLongitude'] == null) {
return '/';
}
return null;
},
builder: (context, state) {
final args = state.extra as Map<String, dynamic>?;
final destinationLatitude =
(args?['destinationLatitude'] as num?)?.toDouble() ?? 0.0;
final destinationLongitude =
(args?['destinationLongitude'] as num?)?.toDouble() ?? 0.0;
final destinationName = args?['destinationName'] as String?;
return NavigationMapScreen(
destinationLatitude: destinationLatitude,
destinationLongitude: destinationLongitude,
destinationName: destinationName,
);
},
),
GoRoute(
path: '/nearbyRoutesMap',
name: 'nearbyRoutesMap',
builder: (context, state) {
final args = state.extra as Map<String, dynamic>?;
final languageId = args?['languageId'] as String? ?? '1';
final distanceKm = (args?['distanceKm'] as num?)?.toDouble() ?? 2.0;
return NearbyRoutesMapScreen(
languageId: languageId,
distanceKm: distanceKm,
);
},
),
GoRoute(
path: '/allRoutesMap',
name: 'allRoutesMap',
builder: (context, state) {
final args = state.extra as Map<String, dynamic>?;
final routeType = args?['routeType'] ?? 'all';
final languageId = args?['languageId'] ?? '1';
return AllRoutesMapScreen(
routeType: routeType,
languageId: languageId,
);
},
),
GoRoute(
path: '/model-viewer',
name: 'modelViewer',
builder: (context, state) {
final modelUrl =
state.uri.queryParameters['modelUrl'] ??
'https://zenodo.org/api/files/2bc7f5fc-94c3-48ad-aec1-d2d6d9c649c0/23343bb4baae43bb87399264ade89547.glb';
return ModelViewerScreen(modelUrl: modelUrl);
},
),
GoRoute(
path: '/create_poi',
name: 'createPoi',
redirect: (context, state) {
final loggedIn = FirebaseAuth.instance.currentUser != null;
if (!loggedIn || AppRouter.guestMode) return '/login';
return null;
},
builder: (context, state) => const CreatePOIScreen(),
),
GoRoute(
path: '/my_pois',
name: 'myPois',
redirect: (context, state) {
final loggedIn = FirebaseAuth.instance.currentUser != null;
if (!loggedIn || AppRouter.guestMode) return '/login';
return null;
},
builder: (context, state) => const MyPoisScreen(),
),
GoRoute(
path: '/create_route',
name: 'createRoute',
redirect: (context, state) {
final loggedIn = FirebaseAuth.instance.currentUser != null;
if (!loggedIn || AppRouter.guestMode) return '/login';
return null;
},
builder: (context, state) => const MapRouteCreatorScreen(),
),
GoRoute(
path: '/my_routes',
name: 'myRoutes',
redirect: (context, state) {
final loggedIn = FirebaseAuth.instance.currentUser != null;
if (!loggedIn || AppRouter.guestMode) return '/login';
return null;
},
builder: (context, state) => const MyRoutesScreen(),
),
GoRoute(
path: '/relevant_pois',
name: 'relevantPois',
builder: (context, state) => const RelevantPoisScreen(),
),
GoRoute(
path: '/arScreen',
name: 'arScreen',
builder: (context, state) {
final modelUrl = state.uri.queryParameters['modelUrl'];
return ARScreen(modelUrl: modelUrl);
},
),
],
// Redirect global para proteger rutas
redirect: (context, state) {
final loggedIn = FirebaseAuth.instance.currentUser != null;
// En web: uri.path; en mobile: matchedLocation
final location =
state.uri.path.isNotEmpty ? state.uri.path : state.matchedLocation;
final loggingIn = location == '/login';
final isPublicRoute = _isPublicRoute(location);
final isGuest = AppRouter.guestMode;
debugPrint(
'🔐 Redirect check: location=$location, loggedIn=$loggedIn, isPublic=$isPublicRoute',
);
// Si no está logueado ni en modo invitado y no está en una ruta pública, redirigir a login
if (!loggedIn && !isGuest && !isPublicRoute) {
debugPrint('❌ Redirigiendo a login de: $location');
return '/login';
}
// Si está logueado o es invitado y está en login, redirigir a home
if ((loggedIn || isGuest) && loggingIn) {
debugPrint('→ Redirigiendo a home (usuario ya autenticado)');
return '/';
}
debugPrint('✅ Permitiendo: $location');
return null; // No redirigir
},
// Manejo de errores
errorBuilder:
(context, state) => Scaffold(
appBar: AppBar(title: const Text('Error')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.error_outline, size: 80, color: Colors.red),
const SizedBox(height: 16),
Text(
'Página no encontrada',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
Text(state.matchedLocation),
const SizedBox(height: 24),
ElevatedButton.icon(
onPressed: () => context.go('/'),
icon: const Icon(Icons.home),
label: const Text('Volver al inicio'),
),
],
),
),
),
);
/// Verifica si una ruta es pública (no requiere autenticación)
static bool _isPublicRoute(String location) {
const publicRoutes = ['/login', '/terms', '/privacy', '/delete_account'];
if (location.startsWith('/poi/')) return true;
return publicRoutes.contains(location);
}
/// Construye un deep link hacia un POI concreto
static Uri buildPoiDeepLink(String poiId) {
final path = router.namedLocation(
'poiDetail',
pathParameters: {'poiId': poiId},
);
return Uri(scheme: appLinkScheme, host: appLinkHost, path: path);
}
}