Skip to content

Commit 05fca73

Browse files
committed
feat: fetch and use app config
- Fetches AppConfig on user change - Uses default if not found on backend - Handles fetch errors gracefully
1 parent 1ec0145 commit 05fca73

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@ class AppBloc extends Bloc<AppEvent, AppState> {
1515
AppBloc({
1616
required HtAuthRepository authenticationRepository,
1717
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+
) {
2832
on<AppUserChanged>(_onAppUserChanged);
2933
on<AppSettingsRefreshed>(_onAppSettingsRefreshed);
34+
on<_AppConfigFetchRequested>(_onAppConfigFetchRequested); // Added
3035
on<AppLogoutRequested>(_onLogoutRequested);
3136
on<AppThemeModeChanged>(_onThemeModeChanged);
3237
on<AppFlexSchemeChanged>(_onFlexSchemeChanged);
@@ -41,6 +46,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
4146

4247
final HtAuthRepository _authenticationRepository;
4348
final HtDataRepository<UserAppSettings> _userAppSettingsRepository;
49+
final HtDataRepository<AppConfig> _appConfigRepository; // Added
4450
late final StreamSubscription<User?> _userSubscription;
4551

4652
/// Handles user changes and loads initial settings once user is available.
@@ -67,6 +73,10 @@ class AppBloc extends Bloc<AppEvent, AppState> {
6773
if (event.user != null) {
6874
add(const AppSettingsRefreshed());
6975
}
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());
7080
}
7181

7282
/// Handles refreshing/loading app settings (theme, font).
@@ -285,4 +295,33 @@ class AppBloc extends Bloc<AppEvent, AppState> {
285295
_userSubscription.cancel();
286296
return super.close();
287297
}
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+
}
288327
}

0 commit comments

Comments
 (0)