Skip to content

Commit f106c4f

Browse files
committed
refactor(router): share AccountBloc instance
- Instantiate AccountBloc once - Pass to details pages - Remove duplicate provider - Simplify auth logic
1 parent 8ec05da commit f106c4f

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

lib/router/router.dart

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ GoRouter createRouter({
6262
htUserContentPreferencesRepository,
6363
required HtDataRepository<AppConfig> htAppConfigRepository,
6464
}) {
65+
// Instantiate AccountBloc once to be shared
66+
final accountBloc = AccountBloc(
67+
authenticationRepository: htAuthenticationRepository,
68+
userContentPreferencesRepository: htUserContentPreferencesRepository,
69+
);
70+
6571
return GoRouter(
6672
refreshListenable: authStatusNotifier,
6773
initialLocation: Routes.feed,
@@ -178,21 +184,27 @@ GoRouter createRouter({
178184
);
179185
return null; // Allow access
180186
}
181-
// **Sub-Case 2.3: Navigating Within the Main App Sections (Feed, Search, Account)**
182-
// Allow anonymous users to access the main content sections and their sub-routes.
183-
else if (isGoingToFeed || isGoingToSearch || isGoingToAccount) {
184-
// Added checks for search and account
187+
// **Sub-Case 2.3: Navigating Within the Main App Sections (Feed, Search, Account) or Details Pages**
188+
// Allow anonymous users to access the main content sections, their sub-routes, and details pages.
189+
else if (isGoingToFeed ||
190+
isGoingToSearch ||
191+
isGoingToAccount ||
192+
currentLocation == Routes.categoryDetails ||
193+
currentLocation == Routes.sourceDetails ||
194+
currentLocation.startsWith('${Routes.feed}/${Routes.articleDetailsName.split('/:id').first}') ||
195+
currentLocation.startsWith('${Routes.search}/${Routes.searchArticleDetailsName.split('/:id').first}') ||
196+
currentLocation.startsWith('${Routes.account}/${Routes.accountSavedHeadlines}/${Routes.accountArticleDetailsName.split('/:id').first}')) {
185197
print(
186-
' Action: Allowing navigation within main app section ($currentLocation).', // Updated log message
198+
' Action: Allowing navigation to main app section or details page ($currentLocation).',
187199
);
188200
return null; // Allow access
189201
}
190-
// **Sub-Case 2.4: Fallback for Unexpected Paths** // Now correctly handles only truly unexpected paths
202+
// **Sub-Case 2.4: Fallback for Unexpected Paths**
191203
// If an anonymous user tries to navigate anywhere else unexpected,
192204
// redirect them to the main content feed as a safe default.
193205
else {
194206
print(
195-
' Action: Unexpected path ($currentLocation), redirecting to $feedPath', // Updated path constant
207+
' Action: Unexpected path ($currentLocation), redirecting to $feedPath',
196208
);
197209
return feedPath; // Redirect to feed
198210
}
@@ -299,12 +311,14 @@ GoRouter createRouter({
299311
builder: (context, state) {
300312
final args = state.extra as EntityDetailsPageArguments?;
301313
if (args == null) {
302-
// Handle missing arguments, perhaps redirect or show error
303314
return const Scaffold(
304315
body: Center(child: Text('Error: Missing category details arguments')),
305316
);
306317
}
307-
return EntityDetailsPage(args: args);
318+
return BlocProvider.value(
319+
value: accountBloc,
320+
child: EntityDetailsPage(args: args),
321+
);
308322
},
309323
),
310324
GoRoute(
@@ -317,7 +331,10 @@ GoRouter createRouter({
317331
body: Center(child: Text('Error: Missing source details arguments')),
318332
);
319333
}
320-
return EntityDetailsPage(args: args);
334+
return BlocProvider.value(
335+
value: accountBloc,
336+
child: EntityDetailsPage(args: args),
337+
);
321338
},
322339
),
323340
// --- Main App Shell ---
@@ -326,6 +343,7 @@ GoRouter createRouter({
326343
// Return the shell widget which contains the AdaptiveScaffold
327344
return MultiBlocProvider(
328345
providers: [
346+
BlocProvider.value(value: accountBloc), // Use the shared instance
329347
BlocProvider(
330348
create:
331349
(context) => HeadlinesFeedBloc(
@@ -346,16 +364,7 @@ GoRouter createRouter({
346364
context.read<HtDataRepository<Country>>(),
347365
),
348366
),
349-
BlocProvider(
350-
create:
351-
(context) => AccountBloc(
352-
authenticationRepository:
353-
context.read<HtAuthRepository>(),
354-
userContentPreferencesRepository:
355-
context
356-
.read<HtDataRepository<UserContentPreferences>>(),
357-
),
358-
),
367+
// Removed separate AccountBloc creation here
359368
],
360369
child: AppShell(navigationShell: navigationShell),
361370
);

0 commit comments

Comments
 (0)