@@ -70,14 +70,17 @@ class AppBloc extends Bloc<AppEvent, AppState> {
70
70
// Emit user and status update first
71
71
emit (state.copyWith (status: status, user: event.user));
72
72
73
- // Load settings now that we have a user (anonymous or authenticated)
74
73
if (event.user != null ) {
75
- add (const AppSettingsRefreshed ());
74
+ // User is present (authenticated or anonymous)
75
+ add (const AppSettingsRefreshed ()); // Load user-specific settings
76
+ add (const AppConfigFetchRequested ()); // Now attempt to fetch AppConfig
77
+ } else {
78
+ // User is null (unauthenticated or logged out)
79
+ // Clear appConfig if user is logged out, as it might be tied to auth context
80
+ // or simply to ensure fresh fetch on next login.
81
+ // Also ensure status is unauthenticated.
82
+ emit (state.copyWith (appConfig: null , clearAppConfig: true , status: AppStatus .unauthenticated));
76
83
}
77
- // Fetch AppConfig regardless of user, as it's global config
78
- // Or fetch it once at BLoC initialization if it doesn't depend on user at all.
79
- // For now, fetching after user ensures some app state is set.
80
- add (const AppConfigFetchRequested ());
81
84
}
82
85
83
86
/// Handles refreshing/loading app settings (theme, font).
@@ -301,25 +304,42 @@ class AppBloc extends Bloc<AppEvent, AppState> {
301
304
AppConfigFetchRequested event,
302
305
Emitter <AppState > emit,
303
306
) async {
304
- // Avoid refetching if already loaded and not initial, unless a refresh mechanism is added
305
- if (state.appConfig != null && state.status != AppStatus .initial && state.status != AppStatus .configFetchFailed) {
307
+ // Guard: Only fetch if a user (authenticated or anonymous) is present.
308
+ if (state.user == null ) {
309
+ print ('[AppBloc] User is null. Skipping AppConfig fetch because it requires authentication.' );
310
+ // If AppConfig was somehow present without a user, clear it.
311
+ // And ensure status isn't stuck on configFetching if this event was dispatched erroneously.
312
+ if (state.appConfig != null || state.status == AppStatus .configFetching) {
313
+ emit (state.copyWith (appConfig: null , clearAppConfig: true , status: AppStatus .unauthenticated));
314
+ }
306
315
return ;
307
316
}
308
317
318
+ // Avoid refetching if already loaded for the current user session, unless explicitly trying to recover from a failed state.
319
+ if (state.appConfig != null && state.status != AppStatus .configFetchFailed) {
320
+ print ('[AppBloc] AppConfig already loaded for user ${state .user ?.id } and not in a failed state. Skipping fetch.' );
321
+ return ;
322
+ }
323
+
324
+ print ('[AppBloc] Attempting to fetch AppConfig for user: ${state .user !.id }...' );
309
325
emit (state.copyWith (status: AppStatus .configFetching, appConfig: null , clearAppConfig: true ));
310
326
311
327
try {
312
- final appConfig = await _appConfigRepository.read (id: 'app_config' );
313
- // If successful, AppState's status will be updated by user/auth changes,
314
- // or it remains as configFetching until user status is resolved.
315
- // We just need to set the appConfig here.
316
- // The subsequent AppUserChanged event will set the final status (authenticated/anonymous).
317
- emit (state.copyWith (appConfig: appConfig, status: AppStatus .initial)); // Reset status to allow user auth to drive it
328
+ final appConfig = await _appConfigRepository.read (id: 'app_config' ); // API requires auth, so token will be used
329
+ print ('[AppBloc] AppConfig fetched successfully. ID: ${appConfig .id } for user: ${state .user !.id }' );
330
+
331
+ // Determine the correct status based on the existing user's role.
332
+ // This ensures that successfully fetching config doesn't revert auth status to 'initial'.
333
+ final newStatusBasedOnUser = state.user! .role == UserRole .standardUser
334
+ ? AppStatus .authenticated
335
+ : AppStatus .anonymous;
336
+ emit (state.copyWith (appConfig: appConfig, status: newStatusBasedOnUser));
318
337
} on HtHttpException catch (e) {
319
- print ('[AppBloc] Failed to fetch AppConfig: ${e .message }' );
338
+ print ('[AppBloc] Failed to fetch AppConfig (HtHttpException) for user ${ state . user ?. id }: ${ e . runtimeType } - ${e .message }' );
320
339
emit (state.copyWith (status: AppStatus .configFetchFailed, appConfig: null , clearAppConfig: true ));
321
- } catch (e) {
322
- print ('[AppBloc] Unexpected error fetching AppConfig: $e ' );
340
+ } catch (e, s) {
341
+ print ('[AppBloc] Unexpected error fetching AppConfig for user ${state .user ?.id }: $e ' );
342
+ print ('[AppBloc] Stacktrace: $s ' );
323
343
emit (state.copyWith (status: AppStatus .configFetchFailed, appConfig: null , clearAppConfig: true ));
324
344
}
325
345
}
0 commit comments