Skip to content

Commit 84430bd

Browse files
committed
feat(app): load user app settings on login
- Load settings after user login - Create default settings if not found - Handle settings load errors - Clear settings on logout
1 parent f0470b6 commit 84430bd

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
2525
) {
2626
on<AppUserChanged>(_onAppUserChanged);
2727
on<AppLogoutRequested>(_onLogoutRequested);
28+
on<AppUserAppSettingsChanged>(_onAppUserAppSettingsChanged);
2829

2930
_userSubscription = _authenticationRepository.authStateChanges.listen(
3031
(User? user) => add(AppUserChanged(user)),
@@ -58,10 +59,43 @@ class AppBloc extends Bloc<AppEvent, AppState> {
5859

5960
// Emit user and status update
6061
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));
6194
}
6295

6396
void _onLogoutRequested(AppLogoutRequested event, Emitter<AppState> emit) {
6497
unawaited(_authenticationRepository.signOut());
98+
emit(state.copyWith(clearUserAppSettings: true)); // Clear settings on logout
6599
}
66100

67101
@override

lib/app/bloc/app_event.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,17 @@ class AppLogoutRequested extends AppEvent {
2323
/// {@macro app_logout_requested}
2424
const AppLogoutRequested();
2525
}
26+
27+
/// {@template app_user_app_settings_changed}
28+
/// Event to notify that user application settings have changed.
29+
/// {@endtemplate}
30+
final class AppUserAppSettingsChanged extends AppEvent {
31+
/// {@macro app_user_app_settings_changed}
32+
const AppUserAppSettingsChanged(this.userAppSettings);
33+
34+
/// The updated user application settings.
35+
final UserAppSettings userAppSettings;
36+
37+
@override
38+
List<Object?> get props => [userAppSettings];
39+
}

lib/app/bloc/app_state.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class AppState extends Equatable {
2121
this.status = AppStatus.initial,
2222
this.user,
2323
this.environment,
24+
this.userAppSettings,
2425
});
2526

2627
/// The current authentication status of the application.
@@ -32,17 +33,24 @@ class AppState extends Equatable {
3233
/// The current application environment (e.g., production, development, demo).
3334
final local_config.AppEnvironment? environment;
3435

36+
/// The current user application settings. Null if not loaded or unauthenticated.
37+
final UserAppSettings? userAppSettings;
38+
3539
/// Creates a copy of the current state with updated values.
3640
AppState copyWith({
3741
AppStatus? status,
3842
User? user,
3943
local_config.AppEnvironment? environment,
44+
UserAppSettings? userAppSettings,
4045
bool clearEnvironment = false,
46+
bool clearUserAppSettings = false,
4147
}) {
4248
return AppState(
4349
status: status ?? this.status,
4450
user: user ?? this.user,
4551
environment: clearEnvironment ? null : environment ?? this.environment,
52+
userAppSettings:
53+
clearUserAppSettings ? null : userAppSettings ?? this.userAppSettings,
4654
);
4755
}
4856

@@ -51,5 +59,6 @@ class AppState extends Equatable {
5159
status,
5260
user,
5361
environment,
62+
userAppSettings,
5463
];
5564
}

0 commit comments

Comments
 (0)