Skip to content

Commit e788b31

Browse files
committed
refactor: The BLoCs for the headlines details and account page are now provided at the GoRouter level. The BlocProvider has been removed from the individual pages, and the HeadlinesFilterPage is now correctly accessing the HeadlinesFeedBloc from the context.
1 parent bacd19f commit e788b31

File tree

6 files changed

+30
-104
lines changed

6 files changed

+30
-104
lines changed

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
analyzer:
22
errors:
33
avoid_catches_without_on_clauses: ignore
4+
lines_longer_than_80_chars: ignore
45
include: package:very_good_analysis/analysis_options.7.0.0.yaml
56
linter:
67
rules:

lib/account/view/account_page.dart

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,14 @@ import 'package:ht_main/l10n/l10n.dart';
88
import 'package:ht_main/router/routes.dart';
99
import 'package:ht_main/shared/constants/app_spacing.dart';
1010

11-
/// {@template account_page}
12-
/// Page widget for the Account feature.
13-
/// Provides the [AccountBloc] to its descendants.
14-
/// {@endtemplate}
15-
class AccountPage extends StatelessWidget {
16-
/// {@macro account_page}
17-
const AccountPage({super.key});
18-
19-
@override
20-
Widget build(BuildContext context) {
21-
return const _AccountView();
22-
}
23-
}
2411

2512
/// {@template account_view}
2613
/// Displays the user's account information and actions.
2714
/// Adapts UI based on authentication status (authenticated vs. anonymous).
2815
/// {@endtemplate}
29-
class _AccountView extends StatelessWidget {
16+
class AccountPage extends StatelessWidget {
3017
/// {@macro account_view}
31-
const _AccountView();
18+
const AccountPage({super.key});
3219

3320
@override
3421
Widget build(BuildContext context) {

lib/authentication/view/authentication_page.dart

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import 'package:flutter/material.dart';
55
import 'package:flutter_bloc/flutter_bloc.dart';
66
import 'package:go_router/go_router.dart';
7-
import 'package:ht_authentication_repository/ht_authentication_repository.dart';
87
import 'package:ht_main/authentication/bloc/authentication_bloc.dart';
98
import 'package:ht_main/l10n/l10n.dart';
109
import 'package:ht_main/router/routes.dart';
@@ -38,41 +37,6 @@ class AuthenticationPage extends StatelessWidget {
3837
/// Whether this page is being shown in the account linking context.
3938
final bool isLinkingContext;
4039

41-
@override
42-
Widget build(BuildContext context) {
43-
// Provide the BLoC here if it's not already provided higher up
44-
// For this refactor, assuming it's provided by the route or App setup
45-
return BlocProvider(
46-
// Ensure BLoC is created only once per instance of this page if needed
47-
// If BLoC needs to persist across navigations, provide it higher up.
48-
create:
49-
(context) => AuthenticationBloc(
50-
authenticationRepository:
51-
context.read<HtAuthenticationRepository>(),
52-
),
53-
child: _AuthenticationView(
54-
headline: headline,
55-
subHeadline: subHeadline,
56-
showAnonymousButton: showAnonymousButton,
57-
isLinkingContext: isLinkingContext,
58-
),
59-
);
60-
}
61-
}
62-
63-
class _AuthenticationView extends StatelessWidget {
64-
const _AuthenticationView({
65-
required this.headline,
66-
required this.subHeadline,
67-
required this.showAnonymousButton,
68-
required this.isLinkingContext,
69-
});
70-
71-
final String headline;
72-
final String subHeadline;
73-
final bool showAnonymousButton;
74-
final bool isLinkingContext;
75-
7640
@override
7741
Widget build(BuildContext context) {
7842
final l10n = context.l10n;

lib/headline-details/view/headline_details_page.dart

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
//
2-
// ignore_for_file: avoid_redundant_argument_values, lines_longer_than_80_chars
2+
// ignore_for_file: avoid_redundant_argument_values
33

44
import 'package:flutter/material.dart';
55
import 'package:flutter_bloc/flutter_bloc.dart';
66
import 'package:ht_headlines_client/ht_headlines_client.dart';
7-
import 'package:ht_headlines_repository/ht_headlines_repository.dart';
87
import 'package:ht_main/headline-details/bloc/headline_details_bloc.dart';
98
import 'package:ht_main/l10n/l10n.dart';
109
import 'package:ht_main/shared/shared.dart';
@@ -16,27 +15,6 @@ class HeadlineDetailsPage extends StatelessWidget {
1615

1716
final String headlineId;
1817

19-
static Route<void> route({required String headlineId}) {
20-
return MaterialPageRoute<void>(
21-
builder: (_) => HeadlineDetailsPage(headlineId: headlineId),
22-
);
23-
}
24-
25-
@override
26-
Widget build(BuildContext context) {
27-
return BlocProvider(
28-
create:
29-
(context) => HeadlineDetailsBloc(
30-
headlinesRepository: context.read<HtHeadlinesRepository>(),
31-
)..add(HeadlineDetailsRequested(headlineId: headlineId)),
32-
child: const _HeadlineDetailsView(),
33-
);
34-
}
35-
}
36-
37-
class _HeadlineDetailsView extends StatelessWidget {
38-
const _HeadlineDetailsView();
39-
4018
@override
4119
Widget build(BuildContext context) {
4220
final l10n = context.l10n;
@@ -63,7 +41,7 @@ class _HeadlineDetailsView extends StatelessWidget {
6341
message: state.message,
6442
onRetry: () {
6543
context.read<HeadlineDetailsBloc>().add(
66-
HeadlineDetailsRequested(headlineId: '1'),
44+
HeadlineDetailsRequested(headlineId: headlineId),
6745
);
6846
},
6947
),

lib/headlines-feed/view/headlines_feed_page.dart

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,24 @@ import 'package:ht_main/shared/widgets/failure_state_widget.dart';
1515
import 'package:ht_main/shared/widgets/loading_state_widget.dart';
1616
// Import Source
1717

18-
/// {@template headlines_feed_page}
19-
/// The main page displaying the feed of news headlines.
20-
///
21-
/// Provides the [HeadlinesFeedBloc] and renders the [_HeadlinesFeedView]
22-
/// which handles the UI based on the BLoC state.
23-
/// {@endtemplate}
24-
class HeadlinesFeedPage extends StatelessWidget {
25-
/// {@macro headlines_feed_page}
26-
const HeadlinesFeedPage({super.key});
27-
28-
@override
29-
Widget build(BuildContext context) {
30-
return const _HeadlinesFeedView();
31-
}
32-
}
33-
3418
/// {@template headlines_feed_view}
3519
/// The core view widget for the headlines feed.
3620
///
3721
/// Handles displaying the list of headlines, loading states, error states,
3822
/// pagination (infinity scroll), and pull-to-refresh functionality. It also
3923
/// includes the AppBar with actions for notifications and filtering.
4024
/// {@endtemplate}
41-
class _HeadlinesFeedView extends StatefulWidget {
25+
class HeadlinesFeedPage extends StatefulWidget {
4226
/// {@macro headlines_feed_view}
43-
const _HeadlinesFeedView();
27+
const HeadlinesFeedPage({super.key});
4428

4529
@override
46-
State<_HeadlinesFeedView> createState() => _HeadlinesFeedViewState();
30+
State<HeadlinesFeedPage> createState() => _HeadlinesFeedPageState();
4731
}
4832

49-
/// State for the [_HeadlinesFeedView]. Manages the [ScrollController] for
33+
/// State for the [HeadlinesFeedPage]. Manages the [ScrollController] for
5034
/// pagination and listens to scroll events.
51-
class _HeadlinesFeedViewState extends State<_HeadlinesFeedView> {
35+
class _HeadlinesFeedPageState extends State<HeadlinesFeedPage> {
5236
final _scrollController = ScrollController();
5337

5438
@override

lib/router/router.dart

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//
2-
// ignore_for_file: avoid_print, lines_longer_than_80_chars
31

42
import 'package:flutter/material.dart';
53
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -12,9 +10,11 @@ import 'package:ht_main/account/bloc/account_bloc.dart';
1210
import 'package:ht_main/account/view/account_page.dart';
1311
import 'package:ht_main/app/bloc/app_bloc.dart';
1412
import 'package:ht_main/app/view/app_shell.dart';
13+
import 'package:ht_main/authentication/bloc/authentication_bloc.dart';
1514
import 'package:ht_main/authentication/view/authentication_page.dart';
1615
import 'package:ht_main/authentication/view/email_link_sent_page.dart';
1716
import 'package:ht_main/authentication/view/email_sign_in_page.dart';
17+
import 'package:ht_main/headline-details/bloc/headline_details_bloc.dart';
1818
import 'package:ht_main/headline-details/view/headline_details_page.dart';
1919
import 'package:ht_main/headlines-feed/bloc/headlines_feed_bloc.dart';
2020
import 'package:ht_main/headlines-feed/view/category_filter_page.dart'; // Import new page
@@ -234,11 +234,17 @@ GoRouter createRouter({
234234
showAnonymousButton = true; // Show anon button for initial sign-in
235235
}
236236

237-
return AuthenticationPage(
238-
headline: headline,
239-
subHeadline: subHeadline,
240-
showAnonymousButton: showAnonymousButton,
241-
isLinkingContext: isLinkingContext,
237+
return BlocProvider(
238+
create:
239+
(context) => AuthenticationBloc(
240+
authenticationRepository: htAuthenticationRepository,
241+
),
242+
child: AuthenticationPage(
243+
headline: headline,
244+
subHeadline: subHeadline,
245+
showAnonymousButton: showAnonymousButton,
246+
isLinkingContext: isLinkingContext,
247+
),
242248
);
243249
},
244250
routes: [
@@ -301,7 +307,13 @@ GoRouter createRouter({
301307
name: Routes.articleDetailsName,
302308
builder: (context, state) {
303309
final id = state.pathParameters['id']!;
304-
return HeadlineDetailsPage(headlineId: id);
310+
return BlocProvider(
311+
create:
312+
(context) => HeadlineDetailsBloc(
313+
headlinesRepository: htHeadlinesRepository,
314+
)..add(HeadlineDetailsRequested(headlineId: id)),
315+
child: HeadlineDetailsPage(headlineId: id),
316+
);
305317
},
306318
),
307319
// Sub-route for notifications (placeholder) - MOVED HERE

0 commit comments

Comments
 (0)