Skip to content

Commit 58df938

Browse files
committed
refactor(app): use ValueNotifier for auth status
- Replaced GoRouterRefreshStream - Used ValueNotifier for status - Simplified AppView state
1 parent 716a7c0 commit 58df938

File tree

4 files changed

+46
-80
lines changed

4 files changed

+46
-80
lines changed

lib/app/view/app.dart

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,118 @@
1-
// Required for StreamSubscription
2-
31
import 'package:flex_color_scheme/flex_color_scheme.dart';
4-
// Required for Listenable
52
import 'package:flutter/material.dart';
63
import 'package:flutter_bloc/flutter_bloc.dart';
7-
import 'package:go_router/go_router.dart'; // Import GoRouter
4+
import 'package:go_router/go_router.dart';
85
import 'package:google_fonts/google_fonts.dart';
96
import 'package:ht_authentication_repository/ht_authentication_repository.dart';
107
import 'package:ht_headlines_repository/ht_headlines_repository.dart';
118
import 'package:ht_main/app/bloc/app_bloc.dart';
129
import 'package:ht_main/l10n/l10n.dart';
13-
// Import the createRouter function and the helper stream
14-
import 'package:ht_main/router/go_router_refresh_stream.dart';
1510
import 'package:ht_main/router/router.dart';
16-
// Routes class is still needed if createRouter uses it, which it does
1711

1812
class App extends StatelessWidget {
1913
const App({
2014
required HtHeadlinesRepository htHeadlinesRepository,
2115
required HtAuthenticationRepository htAuthenticationRepository,
22-
// AppBloc is no longer passed in constructor
2316
super.key,
2417
}) : _htHeadlinesRepository = htHeadlinesRepository,
2518
_htAuthenticationRepository = htAuthenticationRepository;
2619

2720
final HtHeadlinesRepository _htHeadlinesRepository;
2821
final HtAuthenticationRepository _htAuthenticationRepository;
29-
// No longer storing AppBloc instance here
3022

3123
@override
3224
Widget build(BuildContext context) {
33-
// Provide repositories globally
3425
return MultiRepositoryProvider(
3526
providers: [
3627
RepositoryProvider.value(value: _htHeadlinesRepository),
3728
RepositoryProvider.value(value: _htAuthenticationRepository),
3829
],
39-
// Create AppBloc here using BlocProvider
4030
child: BlocProvider(
4131
create: (context) => AppBloc(
4232
authenticationRepository: context.read<HtAuthenticationRepository>(),
4333
),
44-
// _AppView now reads AppBloc from context
4534
child: const _AppView(),
4635
),
4736
);
4837
}
4938
}
5039

51-
// Change _AppView to StatefulWidget to manage GoRouter lifecycle
5240
class _AppView extends StatefulWidget {
53-
const _AppView(); // No longer needs appBloc passed
41+
const _AppView();
5442

5543
@override
5644
State<_AppView> createState() => _AppViewState();
5745
}
5846

5947
class _AppViewState extends State<_AppView> {
60-
// Store the router and the refresh stream listener
48+
// Store the router and the status notifier
6149
late final GoRouter _router;
62-
late final GoRouterRefreshStream _refreshListener;
50+
late final ValueNotifier<AppStatus> _statusNotifier;
6351

6452
@override
6553
void initState() {
6654
super.initState();
6755
// Get the AppBloc instance from the BlocProvider above
6856
final appBloc = context.read<AppBloc>();
69-
// Create the refresh listener using the AppBloc stream
70-
_refreshListener = GoRouterRefreshStream(appBloc.stream);
71-
// Create the router instance by calling the function from router.dart
72-
_router = createRouter(refreshListenable: _refreshListener);
57+
58+
// Create the ValueNotifier, initialized with the current status
59+
_statusNotifier = ValueNotifier<AppStatus>(appBloc.state.status);
60+
61+
// Create the router instance, passing the ValueNotifier as the listenable
62+
_router = createRouter(authStatusNotifier: _statusNotifier);
7363
}
7464

7565
@override
7666
void dispose() {
77-
// Dispose the refresh listener when the widget is disposed
78-
_refreshListener.dispose();
67+
// Remove subscription cancellation
68+
// _blocSubscription.cancel();
69+
// Dispose the ValueNotifier
70+
_statusNotifier.dispose();
7971
super.dispose();
8072
}
8173

8274
@override
8375
Widget build(BuildContext context) {
84-
// Use BlocBuilder to react to theme changes from AppBloc
85-
return BlocBuilder<AppBloc, AppState>(
86-
// Use buildWhen for optimization if only theme affects MaterialApp
87-
buildWhen: (previous, current) => previous.themeMode != current.themeMode,
88-
builder: (context, state) {
89-
return MaterialApp.router(
90-
debugShowCheckedModeBanner: false,
91-
// Apply theme based on AppBloc state
92-
themeMode: state.themeMode,
93-
theme: lightTheme(),
94-
darkTheme: darkTheme(),
95-
// Use the router created and stored in the state
96-
routerConfig: _router,
97-
localizationsDelegates: AppLocalizations.localizationsDelegates,
98-
supportedLocales: AppLocalizations.supportedLocales,
99-
);
76+
// Wrap the part of the tree that needs to react to AppBloc state changes
77+
// (specifically for updating the ValueNotifier) with a BlocListener.
78+
// The BlocBuilder remains for theme changes.
79+
return BlocListener<AppBloc, AppState>(
80+
// Only listen when the status actually changes
81+
listenWhen: (previous, current) => previous.status != current.status,
82+
listener: (context, state) {
83+
// Update the ValueNotifier when the AppBloc status changes.
84+
// This triggers the GoRouter's refreshListenable.
85+
_statusNotifier.value = state.status;
10086
},
87+
child: BlocBuilder<AppBloc, AppState>(
88+
buildWhen: (previous, current) =>
89+
previous.themeMode != current.themeMode,
90+
builder: (context, state) {
91+
return MaterialApp.router(
92+
debugShowCheckedModeBanner: false,
93+
themeMode: state.themeMode,
94+
theme: lightTheme(),
95+
darkTheme: darkTheme(),
96+
routerConfig: _router,
97+
localizationsDelegates: AppLocalizations.localizationsDelegates,
98+
supportedLocales: AppLocalizations.supportedLocales,
99+
);
100+
},
101+
),
101102
);
102103
}
103104
}
104105

105-
// --- Themes (unchanged) ---
106106
ThemeData lightTheme() {
107107
return FlexThemeData.light(
108-
scheme: FlexScheme.greyLaw,
108+
scheme: FlexScheme.material,
109109
fontFamily: GoogleFonts.notoSans().fontFamily,
110110
);
111111
}
112112

113113
ThemeData darkTheme() {
114114
return FlexThemeData.dark(
115-
scheme: FlexScheme.greyLaw,
115+
scheme: FlexScheme.material,
116116
fontFamily: GoogleFonts.notoSans().fontFamily,
117117
);
118118
}

lib/router/go_router_refresh_stream.dart

Lines changed: 0 additions & 31 deletions
This file was deleted.

lib/router/router.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,20 @@ import 'package:ht_main/headlines-feed/view/headlines_feed_page.dart';
1111
import 'package:ht_main/headlines-search/view/headlines_search_page.dart';
1212
import 'package:ht_main/router/routes.dart'; // Keep Routes
1313

14-
// No longer defining a global router instance here.
15-
1614
/// Creates and configures the GoRouter instance for the application.
1715
///
18-
/// Requires a [refreshListenable] (typically based on AppBloc's stream)
19-
/// to trigger route re-evaluation when authentication state changes.
20-
GoRouter createRouter({required Listenable refreshListenable}) {
16+
/// Requires an [authStatusNotifier] to trigger route re-evaluation when
17+
/// authentication state changes.
18+
GoRouter createRouter({required ValueNotifier<AppStatus> authStatusNotifier}) {
2119
return GoRouter(
22-
refreshListenable: refreshListenable,
20+
refreshListenable: authStatusNotifier,
2321
initialLocation: Routes.headlinesFeed,
2422
debugLogDiagnostics: true, // Enable verbose logging for debugging redirects
2523
// --- Redirect Logic ---
2624
redirect: (BuildContext context, GoRouterState state) {
2725
// Use context.read<AppBloc>() here. It's safe because refreshListenable
2826
// ensures this runs within a valid context after AppBloc state changes.
2927
final appStatus = context.read<AppBloc>().state.status;
30-
// final user = context.read<AppBloc>().state.user; // AppState does not have user
3128
final currentLocation = state.matchedLocation;
3229

3330
print(

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: ht_main
22
description: main headlines toolkit mobile app.
3-
version: 0.32.0
3+
version: 0.32.1
44
publish_to: none
55
repository: https://github.com/headlines-toolkit/ht-main
66
environment:

0 commit comments

Comments
 (0)