@@ -31,6 +31,19 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
31
31
add (AccountUserChanged (user));
32
32
});
33
33
34
+ // Listen to changes in UserContentPreferences from the repository.
35
+ // This ensures the AccountBloc's state is updated whenever preferences
36
+ // are created, updated, or deleted, resolving any synchronization issue.
37
+ _userContentPreferencesSubscription = _userContentPreferencesRepository
38
+ .entityUpdated
39
+ .where ((type) => type == UserContentPreferences )
40
+ .listen ((_) {
41
+ // If there's a current user, reload their preferences.
42
+ if (state.user? .id != null ) {
43
+ add (AccountLoadUserPreferences (userId: state.user! .id));
44
+ }
45
+ });
46
+
34
47
// Register event handlers
35
48
on < AccountUserChanged > (_onAccountUserChanged);
36
49
on < AccountLoadUserPreferences > (_onAccountLoadUserPreferences);
@@ -47,6 +60,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
47
60
final local_config.AppEnvironment _environment;
48
61
final Logger _logger;
49
62
late StreamSubscription <User ?> _userSubscription;
63
+ late StreamSubscription <Type > _userContentPreferencesSubscription;
50
64
51
65
Future <void > _onAccountUserChanged (
52
66
AccountUserChanged event,
@@ -81,39 +95,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
81
95
),
82
96
);
83
97
} on NotFoundException {
84
- // In demo mode, a short delay is introduced here to mitigate a race
85
- // condition during anonymous to authenticated data migration.
86
- // This ensures that the DemoDataMigrationService has a chance to
87
- // complete its migration of UserContentPreferences before AccountBloc
88
- // attempts to create a new default preference for the authenticated user.
89
- // This is a temporary stub for the demo environment only and is not
90
- // needed in production/development where backend handles migration.
91
- if (_environment == local_config.AppEnvironment .demo) {
92
- // ignore: inference_failure_on_instance_creation
93
- await Future .delayed (const Duration (milliseconds: 50 ));
94
- // After delay, re-attempt to read the preferences. This is crucial
95
- // because migration might have completed during the delay.
96
- try {
97
- final migratedPreferences = await _userContentPreferencesRepository
98
- .read (id: event.userId, userId: event.userId);
99
- emit (
100
- state.copyWith (
101
- status: AccountStatus .success,
102
- preferences: _sortPreferences (migratedPreferences),
103
- clearError: true ,
104
- ),
105
- );
106
- return ; // Exit if successfully read after migration
107
- } on NotFoundException {
108
- // Still not found after delay, proceed to create default.
109
- _logger.info (
110
- '[AccountBloc] UserContentPreferences still not found after '
111
- 'migration delay. Creating default preferences.' ,
112
- );
113
- }
114
- }
115
- // If preferences not found (either initially or after re-attempt), create
116
- // a default one for the user.
98
+ // If preferences not found, create a default one for the user.
117
99
final defaultPreferences = UserContentPreferences (
118
100
id: event.userId,
119
101
followedCountries: const [],
@@ -509,6 +491,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
509
491
@override
510
492
Future <void > close () {
511
493
_userSubscription.cancel ();
494
+ _userContentPreferencesSubscription.cancel ();
512
495
return super .close ();
513
496
}
514
497
}
0 commit comments