Skip to content

Commit 44093f5

Browse files
committed
refactor: Introduce session scope for DI and remove SessionScope widget
This commit introduces a dedicated session scope for dependency injection using `injectable`. This replaces the previous `SessionScope` InheritedWidget. Key changes: - **`SessionModule`:** A new module is added to provide `StreamFeedsClient` within the session scope. - **`HomeScreen`:** Now manages the session scope lifecycle. It initializes the scope in `initState` and disposes of it in `dispose`. - **Removed `SessionScope` widget:** The `SessionScope` InheritedWidget and its associated extension have been removed. - **Updated `UserFeedScreen`:** Now retrieves `StreamFeedsClient` and the current user directly from the DI container within the session scope. - **`AuthGuard`:** Simplified logging. This change centralizes session-specific dependency management and aligns with standard DI practices.
1 parent c2c2320 commit 44093f5

File tree

8 files changed

+65
-65
lines changed

8 files changed

+65
-65
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'package:injectable/injectable.dart';
2+
import 'package:stream_feeds/stream_feeds.dart';
3+
4+
import '../../app/content/auth_controller.dart';
5+
6+
@module
7+
abstract class SessionModule {
8+
@Singleton(scope: 'session')
9+
StreamFeedsClient authenticatedFeeds(AuthController auth) {
10+
return (auth.value as Authenticated).client;
11+
}
12+
}

sample_app/lib/core/di/di_initializer.config.dart

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample_app/lib/navigation/app_router.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ class AppRouter extends RootStackRouter {
4444
];
4545
}
4646
}
47+

sample_app/lib/navigation/app_router.gr.dart

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sample_app/lib/navigation/guards/auth_guard.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ class AuthGuard extends AutoRouteGuard {
1414
@override
1515
void onNavigation(NavigationResolver resolver, StackRouter router) {
1616
final isAuthenticated = _authController.value is Authenticated;
17-
debugPrint('AuthGuard: isAuthenticated = $isAuthenticated');
18-
1917
// If the user is authenticated, allow navigation to the requested route.
2018
if (isAuthenticated) return resolver.next();
21-
22-
print('AuthGuard: User is not authenticated, redirecting to login.');
2319
// Otherwise, redirect to the Choose user page.
2420
resolver.redirectUntil(const ChooseUserRoute(), replace: true);
2521
}
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
import 'package:auto_route/auto_route.dart';
22
import 'package:flutter/material.dart';
33

4-
import '../../app/content/auth_controller.dart';
4+
import '../../core/di/di_initializer.config.dart';
55
import '../../core/di/di_initializer.dart';
6-
import 'session_scope.dart';
76

87
@RoutePage()
9-
class HomeScreen extends StatelessWidget {
8+
class HomeScreen extends StatefulWidget {
109
const HomeScreen({super.key});
1110

1211
@override
13-
Widget build(BuildContext context) {
14-
final authController = locator<AuthController>();
15-
final state = authController.value;
16-
final (user, client) = switch (state) {
17-
Authenticated(:final user, :final client) => (user, client),
18-
_ => throw Exception('User not authenticated'),
19-
};
12+
State<HomeScreen> createState() => _HomeScreenState();
13+
}
2014

21-
return SessionScope(
22-
user: user,
23-
client: client,
24-
child: const AutoRouter(),
25-
);
15+
class _HomeScreenState extends State<HomeScreen> {
16+
@override
17+
void initState() {
18+
super.initState();
19+
locator.initSessionScope();
2620
}
21+
22+
@override
23+
void dispose() {
24+
locator.popScope();
25+
super.dispose();
26+
}
27+
28+
@override
29+
Widget build(BuildContext context) => const AutoRouter();
2730
}

sample_app/lib/screens/home/session_scope.dart

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

sample_app/lib/screens/user_feed/user_feed_screen.dart

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import '../../../theme/extensions/theme_extensions.dart';
77
import '../../../widgets/user_avatar.dart';
88
import '../../app/content/auth_controller.dart';
99
import '../../core/di/di_initializer.dart';
10-
import '../home/session_scope.dart';
1110
import 'widgets/activity_comments_view.dart';
1211
import 'widgets/activity_content.dart';
1312
import 'widgets/create_activity_bottom_sheet.dart';
@@ -23,19 +22,21 @@ class UserFeedScreen extends StatefulWidget {
2322
}
2423

2524
class _UserFeedScreenState extends State<UserFeedScreen> {
26-
late final feed = context.client.feedFromQuery(
25+
StreamFeedsClient get client => locator<StreamFeedsClient>();
26+
27+
late final feed = client.feedFromQuery(
2728
FeedQuery(
28-
fid: FeedId(group: 'user', id: context.currentUser.id),
29+
fid: FeedId(group: 'user', id: client.user.id),
2930
data: FeedInputData(
3031
visibility: FeedVisibility.public,
31-
members: [FeedMemberRequestData(userId: context.currentUser.id)],
32+
members: [FeedMemberRequestData(userId: client.user.id)],
3233
),
3334
),
3435
);
3536

3637
@override
37-
void didChangeDependencies() {
38-
super.didChangeDependencies();
38+
void initState() {
39+
super.initState();
3940
feed.getOrCreate();
4041
}
4142

@@ -52,6 +53,8 @@ class _UserFeedScreenState extends State<UserFeedScreen> {
5253

5354
@override
5455
Widget build(BuildContext context) {
56+
final currentUser = client.user;
57+
5558
final wideScreen = MediaQuery.sizeOf(context).width > 600;
5659

5760
return StateNotifierBuilder(
@@ -100,7 +103,7 @@ class _UserFeedScreenState extends State<UserFeedScreen> {
100103
text: baseActivity.text ?? '',
101104
attachments: baseActivity.attachments,
102105
data: activity,
103-
currentUserId: context.currentUser.id,
106+
currentUserId: currentUser.id,
104107
onCommentClick: () =>
105108
_onCommentClick(context, activity),
106109
onHeartClick: (isAdding) =>
@@ -123,7 +126,7 @@ class _UserFeedScreenState extends State<UserFeedScreen> {
123126
feedWidget,
124127
onLogout: _onLogout,
125128
onProfileTap: () {
126-
_showProfileBottomSheet(context, context.client, feed);
129+
_showProfileBottomSheet(context, client, feed);
127130
},
128131
);
129132
}
@@ -134,7 +137,7 @@ class _UserFeedScreenState extends State<UserFeedScreen> {
134137
children: [
135138
SizedBox(
136139
width: 250,
137-
child: UserProfileView(feedsClient: context.client, feed: feed),
140+
child: UserProfileView(feedsClient: client, feed: feed),
138141
),
139142
const SizedBox(width: 16),
140143
Expanded(child: feedWidget),
@@ -157,7 +160,7 @@ class _UserFeedScreenState extends State<UserFeedScreen> {
157160
leading: GestureDetector(
158161
onTap: onProfileTap,
159162
child: Center(
160-
child: UserAvatar.appBar(user: context.currentUser),
163+
child: UserAvatar.appBar(user: client.user),
161164
),
162165
),
163166
title: Text(
@@ -202,7 +205,7 @@ class _UserFeedScreenState extends State<UserFeedScreen> {
202205
builder: (context) => ActivityCommentsView(
203206
activityId: activity.id,
204207
feed: feed,
205-
client: context.client,
208+
client: client,
206209
),
207210
);
208211
}
@@ -227,7 +230,7 @@ class _UserFeedScreenState extends State<UserFeedScreen> {
227230
isScrollControlled: true,
228231
backgroundColor: Colors.transparent,
229232
builder: (context) => CreateActivityBottomSheet(
230-
currentUser: context.currentUser,
233+
currentUser: client.user,
231234
feedId: feed.query.fid,
232235
),
233236
);

0 commit comments

Comments
 (0)