Skip to content

Commit c75c596

Browse files
committed
fix(app): improve app config fetching logic
- Avoid refetching unnecessary - Handle config fetch failures - Reset status on success
1 parent 00b1533 commit c75c596

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
3131
) {
3232
on<AppUserChanged>(_onAppUserChanged);
3333
on<AppSettingsRefreshed>(_onAppSettingsRefreshed);
34-
on<_AppConfigFetchRequested>(_onAppConfigFetchRequested);
34+
on<AppConfigFetchRequested>(_onAppConfigFetchRequested);
3535
on<AppUserAccountActionShown>(_onAppUserAccountActionShown); // Added
3636
on<AppLogoutRequested>(_onLogoutRequested);
3737
on<AppThemeModeChanged>(_onThemeModeChanged);
@@ -77,7 +77,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
7777
// Fetch AppConfig regardless of user, as it's global config
7878
// Or fetch it once at BLoC initialization if it doesn't depend on user at all.
7979
// For now, fetching after user ensures some app state is set.
80-
add(const _AppConfigFetchRequested());
80+
add(const AppConfigFetchRequested());
8181
}
8282

8383
/// Handles refreshing/loading app settings (theme, font).
@@ -298,31 +298,29 @@ class AppBloc extends Bloc<AppEvent, AppState> {
298298
}
299299

300300
Future<void> _onAppConfigFetchRequested(
301-
_AppConfigFetchRequested event,
301+
AppConfigFetchRequested event,
302302
Emitter<AppState> emit,
303303
) async {
304-
// Avoid refetching if already loaded, unless a refresh mechanism is added
305-
if (state.appConfig != null && state.status != AppStatus.initial) return;
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) {
306+
return;
307+
}
308+
309+
emit(state.copyWith(status: AppStatus.configFetching, appConfig: null, clearAppConfig: true));
306310

307311
try {
308312
final appConfig = await _appConfigRepository.read(id: 'app_config');
309-
emit(state.copyWith(appConfig: appConfig));
310-
} on NotFoundException {
311-
// If AppConfig is not found on the backend, use a local default.
312-
// The AppConfig model has default values for its nested configurations.
313-
emit(state.copyWith(appConfig: const AppConfig(id: 'app_config')));
314-
// Optionally, one might want to log this or attempt to create it on backend.
315-
print(
316-
'[AppBloc] AppConfig not found on backend, using local default.',
317-
);
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
318318
} on HtHttpException catch (e) {
319-
// Failed to fetch AppConfig, log error. App might be partially functional.
320319
print('[AppBloc] Failed to fetch AppConfig: ${e.message}');
321-
// Emit state with null appConfig or keep existing if partially loaded before
322-
emit(state.copyWith(appConfig: null, clearAppConfig: true));
320+
emit(state.copyWith(status: AppStatus.configFetchFailed, appConfig: null, clearAppConfig: true));
323321
} catch (e) {
324322
print('[AppBloc] Unexpected error fetching AppConfig: $e');
325-
emit(state.copyWith(appConfig: null, clearAppConfig: true));
323+
emit(state.copyWith(status: AppStatus.configFetchFailed, appConfig: null, clearAppConfig: true));
326324
}
327325
}
328326

0 commit comments

Comments
 (0)