@@ -31,7 +31,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
31
31
) {
32
32
on < AppUserChanged > (_onAppUserChanged);
33
33
on < AppSettingsRefreshed > (_onAppSettingsRefreshed);
34
- on < _AppConfigFetchRequested > (_onAppConfigFetchRequested);
34
+ on < AppConfigFetchRequested > (_onAppConfigFetchRequested);
35
35
on < AppUserAccountActionShown > (_onAppUserAccountActionShown); // Added
36
36
on < AppLogoutRequested > (_onLogoutRequested);
37
37
on < AppThemeModeChanged > (_onThemeModeChanged);
@@ -77,7 +77,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
77
77
// Fetch AppConfig regardless of user, as it's global config
78
78
// Or fetch it once at BLoC initialization if it doesn't depend on user at all.
79
79
// For now, fetching after user ensures some app state is set.
80
- add (const _AppConfigFetchRequested ());
80
+ add (const AppConfigFetchRequested ());
81
81
}
82
82
83
83
/// Handles refreshing/loading app settings (theme, font).
@@ -298,31 +298,29 @@ class AppBloc extends Bloc<AppEvent, AppState> {
298
298
}
299
299
300
300
Future <void > _onAppConfigFetchRequested (
301
- _AppConfigFetchRequested event,
301
+ AppConfigFetchRequested event,
302
302
Emitter <AppState > emit,
303
303
) 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 ));
306
310
307
311
try {
308
312
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
318
318
} on HtHttpException catch (e) {
319
- // Failed to fetch AppConfig, log error. App might be partially functional.
320
319
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 ));
323
321
} catch (e) {
324
322
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 ));
326
324
}
327
325
}
328
326
0 commit comments