@@ -7,6 +7,7 @@ import 'package:flutter/material.dart';
77import 'package:ht_auth_repository/ht_auth_repository.dart' ;
88import 'package:ht_data_repository/ht_data_repository.dart' ;
99import 'package:ht_main/app/config/config.dart' as local_config;
10+ import 'package:ht_main/shared/services/demo_data_migration_service.dart' ;
1011import 'package:ht_shared/ht_shared.dart' ; // Import shared models and exceptions
1112
1213part 'app_event.dart' ;
@@ -17,18 +18,20 @@ class AppBloc extends Bloc<AppEvent, AppState> {
1718 required HtAuthRepository authenticationRepository,
1819 required HtDataRepository <UserAppSettings > userAppSettingsRepository,
1920 required HtDataRepository <AppConfig > appConfigRepository,
20- required local_config.AppEnvironment environment, // Added
21- }) : _authenticationRepository = authenticationRepository,
22- _userAppSettingsRepository = userAppSettingsRepository,
23- _appConfigRepository = appConfigRepository,
24- super (
25- AppState (
26- settings: const UserAppSettings (id: 'default' ),
27- selectedBottomNavigationIndex: 0 ,
28- appConfig: null ,
29- environment: environment, // Pass environment to AppState
30- ),
31- ) {
21+ required local_config.AppEnvironment environment,
22+ 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+ ) {
3235 on < AppUserChanged > (_onAppUserChanged);
3336 on < AppSettingsRefreshed > (_onAppSettingsRefreshed);
3437 on < AppConfigFetchRequested > (_onAppConfigFetchRequested);
@@ -47,7 +50,9 @@ class AppBloc extends Bloc<AppEvent, AppState> {
4750
4851 final HtAuthRepository _authenticationRepository;
4952 final HtDataRepository <UserAppSettings > _userAppSettingsRepository;
50- final HtDataRepository <AppConfig > _appConfigRepository; // Added
53+ final HtDataRepository <AppConfig > _appConfigRepository;
54+ final local_config.AppEnvironment _environment;
55+ final DemoDataMigrationService ? demoDataMigrationService; // Added
5156 late final StreamSubscription <User ?> _userSubscription;
5257
5358 /// Handles user changes and loads initial settings once user is available.
@@ -57,13 +62,16 @@ class AppBloc extends Bloc<AppEvent, AppState> {
5762 ) async {
5863 // Determine the AppStatus based on the user object and its role
5964 final AppStatus status;
65+ final User ? oldUser = state.user; // Capture current user before state update
66+
6067 switch (event.user? .role) {
61- case null : // User is null (unauthenticated)
68+ case null :
6269 status = AppStatus .unauthenticated;
6370 case UserRole .standardUser:
6471 status = AppStatus .authenticated;
65- // ignore: no_default_cases
66- default :
72+ case UserRole .guestUser: // Explicitly map guestUser to anonymous
73+ status = AppStatus .anonymous;
74+ default : // Fallback for any other roles not explicitly handled
6775 status = AppStatus .anonymous;
6876 }
6977
@@ -74,11 +82,32 @@ class AppBloc extends Bloc<AppEvent, AppState> {
7482 // User is present (authenticated or anonymous)
7583 add (const AppSettingsRefreshed ()); // Load user-specific settings
7684 add (const AppConfigFetchRequested ()); // Now attempt to fetch AppConfig
85+
86+ // Check for anonymous to authenticated transition for data migration
87+ if (oldUser != null &&
88+ oldUser.role == UserRole .guestUser &&
89+ event.user! .role == UserRole .standardUser) {
90+ print (
91+ '[AppBloc] Anonymous user ${oldUser .id } transitioned to '
92+ 'authenticated user ${event .user !.id }. Attempting data migration.' ,
93+ );
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+ ),
101+ );
102+ } else {
103+ print (
104+ '[AppBloc] DemoDataMigrationService not available. '
105+ 'Skipping client-side data migration.' ,
106+ );
107+ }
108+ }
77109 } else {
78110 // User is null (unauthenticated or logged out)
79- // Clear appConfig if user is logged out, as it might be tied to auth context
80- // or simply to ensure fresh fetch on next login.
81- // Also ensure status is unauthenticated.
82111 emit (
83112 state.copyWith (
84113 appConfig: null ,
0 commit comments