@@ -15,6 +15,7 @@ import 'package:ht_main/headlines-feed/view/headlines_feed_page.dart';
15
15
import 'package:ht_main/headlines-search/view/headlines_search_page.dart' ;
16
16
import 'package:ht_main/l10n/l10n.dart' ;
17
17
import 'package:ht_main/router/routes.dart' ;
18
+ import 'package:ht_main/splash/view/splash_page.dart' ; // Import SplashPage
18
19
19
20
/// Creates and configures the GoRouter instance for the application.
20
21
///
@@ -23,7 +24,7 @@ import 'package:ht_main/router/routes.dart';
23
24
GoRouter createRouter ({required ValueNotifier <AppStatus > authStatusNotifier}) {
24
25
return GoRouter (
25
26
refreshListenable: authStatusNotifier,
26
- initialLocation: Routes .feed,
27
+ initialLocation: Routes .splash, // Set initial location to splash
27
28
debugLogDiagnostics: true , // Enable verbose logging for debugging redirects
28
29
// --- Redirect Logic ---
29
30
redirect: (BuildContext context, GoRouterState state) {
@@ -67,12 +68,48 @@ GoRouter createRouter({required ValueNotifier<AppStatus> authStatusNotifier}) {
67
68
// Check if the 'context=linking' query parameter is present in the URI.
68
69
final isLinkingContext =
69
70
currentUri.queryParameters['context' ] == 'linking' ;
71
+ // Check if the current location is the splash screen path.
72
+ final isGoingToSplash = currentLocation == Routes .splash;
70
73
71
74
// --- Redirect Logic based on AppStatus ---
72
75
73
- // --- Case 1: Unauthenticated User ---
76
+ // --- Case 0: Initial Loading State ---
77
+ // While the app is initializing, force the user to the splash screen.
78
+ if (appStatus == AppStatus .initial) {
79
+ print (' Redirect Decision: AppStatus is INITIAL.' );
80
+ // If already on splash, stay there.
81
+ if (isGoingToSplash) {
82
+ print (' Action: Already on splash screen. Allowing navigation.' );
83
+ return null ;
84
+ }
85
+ // If trying to go anywhere else during initial load, force to splash.
86
+ print (' Action: Forcing navigation to splash screen.' );
87
+ return Routes .splash;
88
+ }
89
+
90
+ // --- Transitioning Away from Splash Screen ---
91
+ // Once the status is determined (not initial), if the user is currently
92
+ // on the splash screen, redirect them to the appropriate main screen.
93
+ if (currentLocation == Routes .splash) {
94
+ print (' Redirect Decision: Transitioning AWAY from Splash Screen.' );
95
+ if (appStatus == AppStatus .unauthenticated) {
96
+ print (' Action: Redirecting to $authenticationPath ' );
97
+ return authenticationPath;
98
+ } else if (appStatus == AppStatus .authenticated ||
99
+ appStatus == AppStatus .anonymous) {
100
+ print (' Action: Redirecting to $feedPath ' );
101
+ return feedPath;
102
+ }
103
+ // Fallback: Should not happen if status is known, but stay on splash if needed.
104
+ print (' Action: Unknown status after initial, staying on splash (fallback).' );
105
+ return null ;
106
+ }
107
+
108
+
109
+ // --- Case 1: Unauthenticated User (After Initial Load) ---
110
+ // If the user is unauthenticated and NOT on the splash screen...
74
111
if (appStatus == AppStatus .unauthenticated) {
75
- print (' Redirect Decision: User is UNauthenticated.' );
112
+ print (' Redirect Decision: User is UNauthenticated (post-initial) .' );
76
113
// If the user is NOT already going to an authentication path...
77
114
if (! isGoingToAuth) {
78
115
// ...redirect them to the main authentication page to sign in or sign up.
@@ -159,25 +196,31 @@ GoRouter createRouter({required ValueNotifier<AppStatus> authStatusNotifier}) {
159
196
);
160
197
return null ; // Allow access
161
198
}
162
- // --- Case 4: Initial/Unknown Status ---
163
- // This might occur briefly during app startup before the status is determined.
164
- // Generally, allow navigation during this phase. If specific routes need
165
- // protection even during startup, add checks here.
199
+ // --- Case 4: Fallback (Should not be reached with initial handling) ---
200
+ // This case is less likely now with explicit initial handling.
201
+ // If somehow the status is unknown after the initial phase, allow navigation.
166
202
else {
167
- print (
168
- ' Redirect Decision: AppStatus is initial/unknown . Allowing navigation.' ,
169
- );
170
- return null ; // Allow access
203
+ print (
204
+ ' Redirect Decision: AppStatus is UNEXPECTED ($ appStatus ) . Allowing navigation (fallback) .' ,
205
+ );
206
+ return null ; // Allow access as a safe default
171
207
}
172
208
173
- // --- Default: No Redirect ---
209
+ // --- Default: No Redirect (Should not be reached if logic is exhaustive) ---
174
210
// If none of the above conditions triggered an explicit redirect, allow navigation.
175
211
// This line should theoretically not be reached if the logic above is exhaustive.
176
212
// print(' Redirect Decision: No specific redirect condition met. Allowing navigation.');
177
213
// return null; // Allow access (already covered by the final return null below)
178
214
},
179
215
// --- Routes ---
180
216
routes: [
217
+ // --- Splash Screen Route ---
218
+ GoRoute (
219
+ path: Routes .splash,
220
+ name: Routes .splashName,
221
+ builder: (context, state) => const SplashPage (),
222
+ ),
223
+ // --- Authentication Routes ---
181
224
GoRoute (
182
225
path: Routes .authentication,
183
226
name: Routes .authenticationName,
0 commit comments