@@ -25,6 +25,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
25
25
) {
26
26
on < AppUserChanged > (_onAppUserChanged);
27
27
on < AppLogoutRequested > (_onLogoutRequested);
28
+ on < AppUserAppSettingsChanged > (_onAppUserAppSettingsChanged);
28
29
29
30
_userSubscription = _authenticationRepository.authStateChanges.listen (
30
31
(User ? user) => add (AppUserChanged (user)),
@@ -58,10 +59,43 @@ class AppBloc extends Bloc<AppEvent, AppState> {
58
59
59
60
// Emit user and status update
60
61
emit (state.copyWith (status: status, user: event.user));
62
+
63
+ // If user is authenticated, load their app settings
64
+ if (event.user != null ) {
65
+ try {
66
+ final userAppSettings =
67
+ await _userAppSettingsRepository.read (id: event.user! .id);
68
+ emit (state.copyWith (userAppSettings: userAppSettings));
69
+ } on NotFoundException {
70
+ // If settings not found, create default ones
71
+ final defaultSettings = UserAppSettings (id: event.user! .id);
72
+ await _userAppSettingsRepository.create (item: defaultSettings);
73
+ emit (state.copyWith (userAppSettings: defaultSettings));
74
+ } on HtHttpException catch (e) {
75
+ // Handle HTTP exceptions during settings load
76
+ print ('Error loading user app settings: ${e .message }' );
77
+ emit (state.copyWith ()); // Clear settings on error
78
+ } catch (e) {
79
+ // Handle any other unexpected errors
80
+ print ('Unexpected error loading user app settings: $e ' );
81
+ emit (state.copyWith ()); // Clear settings on error
82
+ }
83
+ } else {
84
+ // If user is unauthenticated, clear app settings
85
+ emit (state.copyWith (clearUserAppSettings: true ));
86
+ }
87
+ }
88
+
89
+ void _onAppUserAppSettingsChanged (
90
+ AppUserAppSettingsChanged event,
91
+ Emitter <AppState > emit,
92
+ ) {
93
+ emit (state.copyWith (userAppSettings: event.userAppSettings));
61
94
}
62
95
63
96
void _onLogoutRequested (AppLogoutRequested event, Emitter <AppState > emit) {
64
97
unawaited (_authenticationRepository.signOut ());
98
+ emit (state.copyWith (clearUserAppSettings: true )); // Clear settings on logout
65
99
}
66
100
67
101
@override
0 commit comments