@@ -15,18 +15,23 @@ class AppBloc extends Bloc<AppEvent, AppState> {
15
15
AppBloc ({
16
16
required HtAuthRepository authenticationRepository,
17
17
required HtDataRepository <UserAppSettings > userAppSettingsRepository,
18
- }) : _authenticationRepository = authenticationRepository,
19
- _userAppSettingsRepository = userAppSettingsRepository,
20
- // Initialize with default state, load settings after user is known
21
- // Provide a default UserAppSettings instance
22
- super (
23
- const AppState (
24
- settings: UserAppSettings (id: 'default' ),
25
- selectedBottomNavigationIndex: 0 ,
26
- ),
27
- ) {
18
+ required HtDataRepository <AppConfig > appConfigRepository, // Added
19
+ }) : _authenticationRepository = authenticationRepository,
20
+ _userAppSettingsRepository = userAppSettingsRepository,
21
+ _appConfigRepository = appConfigRepository, // Added
22
+ // Initialize with default state, load settings after user is known
23
+ // Provide a default UserAppSettings instance
24
+ super (
25
+ // AppConfig will be null initially, fetched later
26
+ const AppState (
27
+ settings: UserAppSettings (id: 'default' ),
28
+ selectedBottomNavigationIndex: 0 ,
29
+ appConfig: null ,
30
+ ),
31
+ ) {
28
32
on < AppUserChanged > (_onAppUserChanged);
29
33
on < AppSettingsRefreshed > (_onAppSettingsRefreshed);
34
+ on < _AppConfigFetchRequested > (_onAppConfigFetchRequested); // Added
30
35
on < AppLogoutRequested > (_onLogoutRequested);
31
36
on < AppThemeModeChanged > (_onThemeModeChanged);
32
37
on < AppFlexSchemeChanged > (_onFlexSchemeChanged);
@@ -41,6 +46,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
41
46
42
47
final HtAuthRepository _authenticationRepository;
43
48
final HtDataRepository <UserAppSettings > _userAppSettingsRepository;
49
+ final HtDataRepository <AppConfig > _appConfigRepository; // Added
44
50
late final StreamSubscription <User ?> _userSubscription;
45
51
46
52
/// Handles user changes and loads initial settings once user is available.
@@ -67,6 +73,10 @@ class AppBloc extends Bloc<AppEvent, AppState> {
67
73
if (event.user != null ) {
68
74
add (const AppSettingsRefreshed ());
69
75
}
76
+ // Fetch AppConfig regardless of user, as it's global config
77
+ // Or fetch it once at BLoC initialization if it doesn't depend on user at all.
78
+ // For now, fetching after user ensures some app state is set.
79
+ add (const _AppConfigFetchRequested ());
70
80
}
71
81
72
82
/// Handles refreshing/loading app settings (theme, font).
@@ -285,4 +295,33 @@ class AppBloc extends Bloc<AppEvent, AppState> {
285
295
_userSubscription.cancel ();
286
296
return super .close ();
287
297
}
298
+
299
+ Future <void > _onAppConfigFetchRequested (
300
+ _AppConfigFetchRequested event,
301
+ Emitter <AppState > emit,
302
+ ) async {
303
+ // Avoid refetching if already loaded, unless a refresh mechanism is added
304
+ if (state.appConfig != null && state.status != AppStatus .initial) return ;
305
+
306
+ try {
307
+ final appConfig = await _appConfigRepository.read (id: 'app_config' );
308
+ emit (state.copyWith (appConfig: appConfig));
309
+ } on NotFoundException {
310
+ // If AppConfig is not found on the backend, use a local default.
311
+ // The AppConfig model has default values for its nested configurations.
312
+ emit (state.copyWith (appConfig: const AppConfig (id: 'app_config' )));
313
+ // Optionally, one might want to log this or attempt to create it on backend.
314
+ print (
315
+ '[AppBloc] AppConfig not found on backend, using local default.' ,
316
+ );
317
+ } on HtHttpException catch (e) {
318
+ // Failed to fetch AppConfig, log error. App might be partially functional.
319
+ print ('[AppBloc] Failed to fetch AppConfig: ${e .message }' );
320
+ // Emit state with null appConfig or keep existing if partially loaded before
321
+ emit (state.copyWith (appConfig: null , clearAppConfig: true ));
322
+ } catch (e) {
323
+ print ('[AppBloc] Unexpected error fetching AppConfig: $e ' );
324
+ emit (state.copyWith (appConfig: null , clearAppConfig: true ));
325
+ }
326
+ }
288
327
}
0 commit comments