@@ -4,18 +4,22 @@ import 'package:bloc/bloc.dart';
44import 'package:equatable/equatable.dart' ;
55import 'package:ht_auth_repository/ht_auth_repository.dart' ;
66import 'package:ht_data_repository/ht_data_repository.dart' ;
7+ import 'package:ht_main/app/config/config.dart' as local_config;
78import 'package:ht_shared/ht_shared.dart' ;
89
910part 'account_event.dart' ;
1011part 'account_state.dart' ;
1112
13+
1214class AccountBloc extends Bloc <AccountEvent , AccountState > {
1315 AccountBloc ({
1416 required HtAuthRepository authenticationRepository,
1517 required HtDataRepository <UserContentPreferences >
1618 userContentPreferencesRepository,
19+ required local_config.AppEnvironment environment, // Added environment
1720 }) : _authenticationRepository = authenticationRepository,
1821 _userContentPreferencesRepository = userContentPreferencesRepository,
22+ _environment = environment, // Initialize environment
1923 super (const AccountState ()) {
2024 // Listen to user changes from HtAuthRepository
2125 _userSubscription = _authenticationRepository.authStateChanges.listen ((
@@ -37,6 +41,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
3741 final HtAuthRepository _authenticationRepository;
3842 final HtDataRepository <UserContentPreferences >
3943 _userContentPreferencesRepository;
44+ final local_config.AppEnvironment _environment; // New field for environment
4045 late StreamSubscription <User ?> _userSubscription;
4146
4247 Future <void > _onAccountUserChanged (
@@ -72,6 +77,17 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
7277 ),
7378 );
7479 } on NotFoundException {
80+ // In demo mode, a short delay is introduced here to mitigate a race
81+ // condition during anonymous to authenticated data migration.
82+ // This ensures that the DemoDataMigrationService has a chance to
83+ // complete its migration of UserContentPreferences before AccountBloc
84+ // attempts to create a new default preference for the authenticated user.
85+ // This is a temporary stub for the demo environment only and is not
86+ // needed in production/development where backend handles migration.
87+ if (_environment == local_config.AppEnvironment .demo) {
88+ // ignore: inference_failure_on_instance_creation
89+ await Future .delayed (const Duration (seconds: 1 ));
90+ }
7591 // If preferences not found, create a default one for the user
7692 final defaultPreferences = UserContentPreferences (id: event.userId);
7793 try {
0 commit comments