@@ -59,7 +59,6 @@ class AppBloc extends Bloc<AppEvent, AppState> {
5959 on < AppFontFamilyChanged > (_onFontFamilyChanged);
6060 on < AppTextScaleFactorChanged > (_onAppTextScaleFactorChanged);
6161 on < AppFontWeightChanged > (_onAppFontWeightChanged);
62- on < AppOpened > (_onAppOpened);
6362
6463 // Listen directly to the auth state changes stream
6564 _userSubscription = _authenticationRepository.authStateChanges.listen (
@@ -438,18 +437,41 @@ class AppBloc extends Bloc<AppEvent, AppState> {
438437 '[AppBloc] Remote Config fetched successfully. ID: ${remoteConfig .id } for user: ${state .user !.id }' ,
439438 );
440439
441- // Determine the correct status based on the existing user's role.
442- // This ensures that successfully fetching config doesn't revert auth status to 'initial'.
443- final newStatusBasedOnUser =
444- state.user! .appRole == AppUserRole .standardUser
440+ // --- CRITICAL STARTUP SEQUENCE EVALUATION ---
441+ // After successfully fetching the remote configuration, we must
442+ // evaluate the app's status in a specific order before allowing
443+ // the main UI to be built.
444+
445+ // 1. Check for Maintenance Mode. This has the highest priority.
446+ if (remoteConfig.appStatus.isUnderMaintenance) {
447+ emit (
448+ state.copyWith (
449+ status: AppStatus .underMaintenance,
450+ remoteConfig: remoteConfig,
451+ ),
452+ );
453+ return ;
454+ }
455+
456+ // 2. Check for a Required Update.
457+ // TODO(fulleni): Compare with actual app version from package_info_plus.
458+ if (remoteConfig.appStatus.isLatestVersionOnly) {
459+ emit (
460+ state.copyWith (
461+ status: AppStatus .updateRequired,
462+ remoteConfig: remoteConfig,
463+ ),
464+ );
465+ return ;
466+ }
467+
468+ // 3. If no critical status is active, proceed to the normal app state.
469+ // The status is determined by the user's role (authenticated/anonymous).
470+ final finalStatus = state.user! .appRole == AppUserRole .standardUser
445471 ? AppStatus .authenticated
446472 : AppStatus .anonymous;
447- emit (
448- state.copyWith (
449- remoteConfig: remoteConfig,
450- status: newStatusBasedOnUser,
451- ),
452- );
473+
474+ emit (state.copyWith (remoteConfig: remoteConfig, status: finalStatus));
453475 } on HttpException catch (e) {
454476 print (
455477 '[AppBloc] Failed to fetch AppConfig (HttpException) for user ${state .user ?.id }: ${e .runtimeType } - ${e .message }' ,
@@ -524,24 +546,4 @@ class AppBloc extends Bloc<AppEvent, AppState> {
524546 );
525547 }
526548 }
527-
528- Future <void > _onAppOpened (AppOpened event, Emitter <AppState > emit) async {
529- if (state.remoteConfig == null ) {
530- return ;
531- }
532-
533- final appStatus = state.remoteConfig! .appStatus;
534-
535- if (appStatus.isUnderMaintenance) {
536- emit (state.copyWith (status: AppStatus .underMaintenance));
537- return ;
538- }
539-
540- // TODO(fulleni): Get the current app version from a package like
541- // package_info_plus and compare it with appStatus.latestAppVersion.
542- if (appStatus.isLatestVersionOnly) {
543- emit (state.copyWith (status: AppStatus .updateRequired));
544- return ;
545- }
546- }
547549}
0 commit comments