Skip to content

Commit 4821119

Browse files
committed
refactor: Refactor router and auth flow
- Updated auth repository - Use generic data repository - Implemented code verification flow - Added account sub-routes
1 parent 6c90036 commit 4821119

File tree

2 files changed

+99
-47
lines changed

2 files changed

+99
-47
lines changed

lib/router/router.dart

Lines changed: 88 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_bloc/flutter_bloc.dart';
33
import 'package:go_router/go_router.dart';
4-
import 'package:ht_authentication_repository/ht_authentication_repository.dart';
5-
import 'package:ht_categories_repository/ht_categories_repository.dart';
6-
import 'package:ht_countries_repository/ht_countries_repository.dart';
7-
import 'package:ht_headlines_repository/ht_headlines_repository.dart';
4+
import 'package:ht_auth_repository/ht_auth_repository.dart'; // Auth Repository
5+
import 'package:ht_data_repository/ht_data_repository.dart'; // Generic Data Repository
86
import 'package:ht_main/account/bloc/account_bloc.dart';
97
import 'package:ht_main/account/view/account_page.dart';
108
import 'package:ht_main/app/bloc/app_bloc.dart';
119
import 'package:ht_main/app/view/app_shell.dart';
1210
import 'package:ht_main/authentication/bloc/authentication_bloc.dart';
1311
import 'package:ht_main/authentication/view/authentication_page.dart';
14-
import 'package:ht_main/authentication/view/email_link_sent_page.dart';
15-
import 'package:ht_main/authentication/view/email_sign_in_page.dart';
12+
import 'package:ht_main/authentication/view/email_code_verification_page.dart';
13+
import 'package:ht_main/authentication/view/request_code_page.dart'; // Will be renamed to request_code_page.dart later
1614
import 'package:ht_main/headline-details/bloc/headline_details_bloc.dart';
1715
import 'package:ht_main/headline-details/view/headline_details_page.dart';
1816
import 'package:ht_main/headlines-feed/bloc/categories_filter_bloc.dart'; // Import new BLoC
@@ -34,21 +32,23 @@ import 'package:ht_main/settings/view/article_settings_page.dart'; // Added
3432
import 'package:ht_main/settings/view/feed_settings_page.dart'; // Added
3533
import 'package:ht_main/settings/view/notification_settings_page.dart'; // Added
3634
import 'package:ht_main/settings/view/settings_page.dart'; // Added
37-
import 'package:ht_preferences_repository/ht_preferences_repository.dart'; // Added
38-
import 'package:ht_sources_repository/ht_sources_repository.dart';
35+
import 'package:ht_shared/ht_shared.dart'; // Shared models, FromJson, ToJson, etc.
3936

4037
/// Creates and configures the GoRouter instance for the application.
4138
///
4239
/// Requires an [authStatusNotifier] to trigger route re-evaluation when
4340
/// authentication state changes.
4441
GoRouter createRouter({
4542
required ValueNotifier<AppStatus> authStatusNotifier,
46-
required HtAuthenticationRepository htAuthenticationRepository,
47-
required HtHeadlinesRepository htHeadlinesRepository,
48-
required HtCategoriesRepository htCategoriesRepository,
49-
required HtCountriesRepository htCountriesRepository,
50-
required HtSourcesRepository htSourcesRepository,
51-
required HtPreferencesRepository htPreferencesRepository, // Added
43+
required HtAuthRepository htAuthenticationRepository,
44+
required HtDataRepository<Headline> htHeadlinesRepository,
45+
required HtDataRepository<Category> htCategoriesRepository,
46+
required HtDataRepository<Country> htCountriesRepository,
47+
required HtDataRepository<Source> htSourcesRepository,
48+
required HtDataRepository<UserAppSettings> htUserAppSettingsRepository,
49+
required HtDataRepository<UserContentPreferences>
50+
htUserContentPreferencesRepository,
51+
required HtDataRepository<AppConfig> htAppConfigRepository,
5252
}) {
5353
return GoRouter(
5454
refreshListenable: authStatusNotifier,
@@ -78,11 +78,11 @@ GoRouter createRouter({
7878
// Base paths for major sections.
7979
const authenticationPath = Routes.authentication; // '/authentication'
8080
const feedPath = Routes.feed; // Updated path constant
81-
// Specific authentication sub-routes crucial for the email linking flow.
82-
const emailSignInPath =
83-
'$authenticationPath/${Routes.emailSignIn}'; // '/authentication/email-sign-in'
84-
const emailLinkSentPath =
85-
'$authenticationPath/${Routes.emailLinkSent}'; // '/authentication/email-link-sent'
81+
// Specific authentication sub-routes crucial for the email code verification flow.
82+
const requestCodePath =
83+
'$authenticationPath/${Routes.requestCode}'; // '/authentication/request-code'
84+
const verifyCodePath =
85+
'$authenticationPath/${Routes.verifyCode}'; // '/authentication/verify-code'
8686

8787
// --- Helper Booleans ---
8888
// Check if the navigation target is within the authentication section.
@@ -155,13 +155,14 @@ GoRouter createRouter({
155155
return feedPath; // Redirect to feed
156156
}
157157
}
158-
// **Sub-Case 2.2: Navigating to Specific Email Linking Sub-Routes**
159-
// Explicitly allow access to the necessary pages for the email linking process,
158+
// **Sub-Case 2.2: Navigating to Specific Email Code Verification Sub-Routes**
159+
// Explicitly allow access to the necessary pages for the email code verification process,
160160
// even if the 'context=linking' parameter is lost during navigation between these pages.
161-
else if (currentLocation == emailSignInPath ||
162-
currentLocation == emailLinkSentPath) {
161+
else if (currentLocation == requestCodePath ||
162+
currentLocation.startsWith(verifyCodePath)) {
163+
// Use startsWith for parameterized path
163164
print(
164-
' Action: Allowing navigation to email linking sub-route ($currentLocation).',
165+
' Action: Allowing navigation to email code verification sub-route ($currentLocation).',
165166
);
166167
return null; // Allow access
167168
}
@@ -217,9 +218,8 @@ GoRouter createRouter({
217218
// print(' Redirect Decision: No specific redirect condition met. Allowing navigation.');
218219
// return null; // Allow access (already covered by the final return null below)
219220
},
220-
// --- Routes ---
221+
// --- Authentication Routes ---
221222
routes: [
222-
// --- Authentication Routes ---
223223
GoRoute(
224224
path: Routes.authentication,
225225
name: Routes.authenticationName,
@@ -247,7 +247,7 @@ GoRouter createRouter({
247247
return BlocProvider(
248248
create:
249249
(context) => AuthenticationBloc(
250-
authenticationRepository: htAuthenticationRepository,
250+
authenticationRepository: context.read<HtAuthRepository>(),
251251
),
252252
child: AuthenticationPage(
253253
headline: headline,
@@ -259,18 +259,26 @@ GoRouter createRouter({
259259
},
260260
routes: [
261261
GoRoute(
262-
path: Routes.emailSignIn,
263-
name: Routes.emailSignInName,
262+
path: Routes.requestCode, // Use new path
263+
name: Routes.requestCodeName, // Use new name
264264
builder: (context, state) {
265265
// Extract the linking context flag from 'extra', default to false.
266266
final isLinking = (state.extra as bool?) ?? false;
267-
return EmailSignInPage(isLinkingContext: isLinking);
267+
return EmailSignInPage(
268+
isLinkingContext: isLinking,
269+
); // Page will be renamed later
268270
},
269271
),
270272
GoRoute(
271-
path: Routes.emailLinkSent,
272-
name: Routes.emailLinkSentName,
273-
builder: (context, state) => const EmailLinkSentPage(),
273+
path:
274+
'${Routes.verifyCode}/:email', // Use new path with email parameter
275+
name: Routes.verifyCodeName, // Use new name
276+
builder: (context, state) {
277+
final email = state.pathParameters['email']!; // Extract email
278+
return EmailCodeVerificationPage(
279+
email: email,
280+
); // Use renamed page
281+
},
274282
),
275283
],
276284
),
@@ -283,19 +291,25 @@ GoRouter createRouter({
283291
BlocProvider(
284292
create:
285293
(context) => HeadlinesFeedBloc(
286-
headlinesRepository: htHeadlinesRepository,
294+
headlinesRepository:
295+
context.read<HtDataRepository<Headline>>(),
287296
)..add(const HeadlinesFeedFetchRequested()),
288297
),
289298
BlocProvider(
290299
create:
291300
(context) => HeadlinesSearchBloc(
292-
headlinesRepository: htHeadlinesRepository,
301+
headlinesRepository:
302+
context.read<HtDataRepository<Headline>>(),
293303
),
294304
),
295305
BlocProvider(
296306
create:
297307
(context) => AccountBloc(
298-
authenticationRepository: htAuthenticationRepository,
308+
authenticationRepository:
309+
context.read<HtAuthRepository>(),
310+
userContentPreferencesRepository:
311+
context
312+
.read<HtDataRepository<UserContentPreferences>>(),
299313
),
300314
),
301315
],
@@ -320,7 +334,8 @@ GoRouter createRouter({
320334
return BlocProvider(
321335
create:
322336
(context) => HeadlineDetailsBloc(
323-
headlinesRepository: htHeadlinesRepository,
337+
headlinesRepository:
338+
context.read<HtDataRepository<Headline>>(),
324339
)..add(HeadlineDetailsRequested(headlineId: id)),
325340
child: HeadlineDetailsPage(headlineId: id),
326341
);
@@ -364,7 +379,8 @@ GoRouter createRouter({
364379
create:
365380
(context) => CategoriesFilterBloc(
366381
categoriesRepository:
367-
context.read<HtCategoriesRepository>(),
382+
context
383+
.read<HtDataRepository<Category>>(),
368384
),
369385
child: const CategoryFilterPage(),
370386
),
@@ -381,7 +397,8 @@ GoRouter createRouter({
381397
create:
382398
(context) => SourcesFilterBloc(
383399
sourcesRepository:
384-
context.read<HtSourcesRepository>(),
400+
context
401+
.read<HtDataRepository<Source>>(),
385402
),
386403
child: const SourceFilterPage(),
387404
),
@@ -398,7 +415,8 @@ GoRouter createRouter({
398415
create:
399416
(context) => CountriesFilterBloc(
400417
countriesRepository:
401-
context.read<HtCountriesRepository>(),
418+
context
419+
.read<HtDataRepository<Country>>(),
402420
),
403421
child: const CountryFilterPage(),
404422
),
@@ -436,8 +454,11 @@ GoRouter createRouter({
436454
return BlocProvider(
437455
create:
438456
(context) => SettingsBloc(
439-
preferencesRepository:
440-
context.read<HtPreferencesRepository>(),
457+
userAppSettingsRepository:
458+
context
459+
.read<
460+
HtDataRepository<UserAppSettings>
461+
>(),
441462
)..add(
442463
const SettingsLoadRequested(),
443464
), // Load on entry
@@ -473,6 +494,31 @@ GoRouter createRouter({
473494
),
474495
],
475496
),
497+
// New routes for Account sub-pages
498+
GoRoute(
499+
path:
500+
Routes
501+
.accountContentPreferences, // Relative path 'content-preferences'
502+
name: Routes.accountContentPreferencesName,
503+
builder: (context, state) {
504+
// TODO(fulleni): Replace with actual ContentPreferencesPage
505+
return const Placeholder(
506+
child: Center(child: Text('CONTENT PREFERENCES PAGE')),
507+
);
508+
},
509+
),
510+
GoRoute(
511+
path:
512+
Routes
513+
.accountSavedHeadlines, // Relative path 'saved-headlines'
514+
name: Routes.accountSavedHeadlinesName,
515+
builder: (context, state) {
516+
// TODO(fulleni): Replace with actual SavedHeadlinesPage
517+
return const Placeholder(
518+
child: Center(child: Text('SAVED HEADLINES PAGE')),
519+
);
520+
},
521+
),
476522
],
477523
),
478524
],

lib/router/routes.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ abstract final class Routes {
4848
static const accountLinking = 'linking'; // Query param context, not a path
4949
static const accountLinkingName = 'accountLinking'; // Name for context
5050

51-
// routes for email sign-in flow
52-
static const emailSignIn = 'email-sign-in';
53-
static const emailSignInName = 'emailSignIn';
54-
static const emailLinkSent = 'email-link-sent';
55-
static const emailLinkSentName = 'emailLinkSent';
51+
// routes for email code verification flow
52+
static const requestCode = 'request-code';
53+
static const requestCodeName = 'requestCode';
54+
static const verifyCode = 'verify-code';
55+
static const verifyCodeName = 'verifyCode';
5656

5757
// --- Settings Sub-Routes (relative to /account/settings) ---
5858
static const settingsAppearance = 'appearance';
@@ -66,4 +66,10 @@ abstract final class Routes {
6666
// Add names for notification sub-selection routes if needed later
6767
// static const settingsNotificationCategories = 'categories';
6868
// static const settingsNotificationCategoriesName = 'settingsNotificationCategories';
69+
70+
// --- Account Sub-Routes (relative to /account) ---
71+
static const accountContentPreferences = 'content-preferences';
72+
static const accountContentPreferencesName = 'accountContentPreferences';
73+
static const accountSavedHeadlines = 'saved-headlines';
74+
static const accountSavedHeadlinesName = 'accountSavedHeadlines';
6975
}

0 commit comments

Comments
 (0)