Skip to content

Commit a24fd8f

Browse files
committed
feat(app): Revamp AppConfig fetching logic
- Fetch AppConfig only when user is present - Clear AppConfig on logout/auth changes - Prevent redundant fetches - Handle config fetch failures better
1 parent c75c596 commit a24fd8f

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,17 @@ class AppBloc extends Bloc<AppEvent, AppState> {
7070
// Emit user and status update first
7171
emit(state.copyWith(status: status, user: event.user));
7272

73-
// Load settings now that we have a user (anonymous or authenticated)
7473
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));
7683
}
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());
8184
}
8285

8386
/// Handles refreshing/loading app settings (theme, font).
@@ -301,25 +304,42 @@ class AppBloc extends Bloc<AppEvent, AppState> {
301304
AppConfigFetchRequested event,
302305
Emitter<AppState> emit,
303306
) 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+
}
306315
return;
307316
}
308317

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}...');
309325
emit(state.copyWith(status: AppStatus.configFetching, appConfig: null, clearAppConfig: true));
310326

311327
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));
318337
} 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}');
320339
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');
323343
emit(state.copyWith(status: AppStatus.configFetchFailed, appConfig: null, clearAppConfig: true));
324344
}
325345
}

0 commit comments

Comments
 (0)