@@ -20,18 +20,18 @@ class AppBloc extends Bloc<AppEvent, AppState> {
2020 required HtDataRepository <AppConfig > appConfigRepository,
2121 required local_config.AppEnvironment environment,
2222 this .demoDataMigrationService, // Added
23- }) : _authenticationRepository = authenticationRepository,
24- _userAppSettingsRepository = userAppSettingsRepository,
25- _appConfigRepository = appConfigRepository,
26- _environment = environment,
27- super (
28- AppState (
29- settings: const UserAppSettings (id: 'default' ),
30- selectedBottomNavigationIndex: 0 ,
31- appConfig: null ,
32- environment: environment,
33- ),
34- ) {
23+ }) : _authenticationRepository = authenticationRepository,
24+ _userAppSettingsRepository = userAppSettingsRepository,
25+ _appConfigRepository = appConfigRepository,
26+ _environment = environment,
27+ super (
28+ AppState (
29+ settings: const UserAppSettings (id: 'default' ),
30+ selectedBottomNavigationIndex: 0 ,
31+ appConfig: null ,
32+ environment: environment,
33+ ),
34+ ) {
3535 on < AppUserChanged > (_onAppUserChanged);
3636 on < AppSettingsRefreshed > (_onAppSettingsRefreshed);
3737 on < AppConfigFetchRequested > (_onAppConfigFetchRequested);
@@ -62,7 +62,8 @@ class AppBloc extends Bloc<AppEvent, AppState> {
6262 ) async {
6363 // Determine the AppStatus based on the user object and its role
6464 final AppStatus status;
65- final User ? oldUser = state.user; // Capture current user before state update
65+ final User ? oldUser =
66+ state.user; // Capture current user before state update
6667
6768 switch (event.user? .role) {
6869 case null :
@@ -91,18 +92,30 @@ class AppBloc extends Bloc<AppEvent, AppState> {
9192 '[AppBloc] Anonymous user ${oldUser .id } transitioned to '
9293 'authenticated user ${event .user !.id }. Attempting data migration.' ,
9394 );
94- // Trigger data migration if service is available (i.e., in demo mode)
95- if (demoDataMigrationService != null ) {
96- unawaited (
97- demoDataMigrationService! .migrateAnonymousData (
98- oldUserId: oldUser.id,
99- newUserId: event.user! .id,
100- ),
95+ // This block handles data migration specifically for the demo environment.
96+ // In production/development, this logic is typically handled by the backend.
97+ if (demoDataMigrationService != null &&
98+ _environment == local_config.AppEnvironment .demo) {
99+ print (
100+ '[AppBloc] Demo mode: Awaiting data migration from anonymous '
101+ 'user ${oldUser .id } to authenticated user ${event .user !.id }.' ,
102+ );
103+ // Await the migration to ensure it completes before refreshing settings.
104+ await demoDataMigrationService! .migrateAnonymousData (
105+ oldUserId: oldUser.id,
106+ newUserId: event.user! .id,
107+ );
108+ // After successful migration, explicitly refresh app settings
109+ // to load the newly migrated data into the AppBloc's state.
110+ add (const AppSettingsRefreshed ());
111+ print (
112+ '[AppBloc] Demo mode: Data migration completed and settings '
113+ 'refresh triggered for user ${event .user !.id }.' ,
101114 );
102115 } else {
103116 print (
104- '[AppBloc] DemoDataMigrationService not available. '
105- 'Skipping client-side data migration.' ,
117+ '[AppBloc] DemoDataMigrationService not available or not in demo '
118+ 'environment. Skipping client-side data migration.' ,
106119 );
107120 }
108121 }
0 commit comments