Skip to content

Commit 521d862

Browse files
authored
Merge pull request #104 from flutter-news-app-full-source-code/fix-account-bloc-unsynced-user-preferences
fix(account): synchronize UserContentPreferences with repository changes
2 parents 201edb1 + bec3857 commit 521d862

File tree

1 file changed

+16
-33
lines changed

1 file changed

+16
-33
lines changed

lib/account/bloc/account_bloc.dart

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
3131
add(AccountUserChanged(user));
3232
});
3333

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+
3447
// Register event handlers
3548
on<AccountUserChanged>(_onAccountUserChanged);
3649
on<AccountLoadUserPreferences>(_onAccountLoadUserPreferences);
@@ -47,6 +60,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
4760
final local_config.AppEnvironment _environment;
4861
final Logger _logger;
4962
late StreamSubscription<User?> _userSubscription;
63+
late StreamSubscription<Type> _userContentPreferencesSubscription;
5064

5165
Future<void> _onAccountUserChanged(
5266
AccountUserChanged event,
@@ -81,39 +95,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
8195
),
8296
);
8397
} 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.
11799
final defaultPreferences = UserContentPreferences(
118100
id: event.userId,
119101
followedCountries: const [],
@@ -509,6 +491,7 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
509491
@override
510492
Future<void> close() {
511493
_userSubscription.cancel();
494+
_userContentPreferencesSubscription.cancel();
512495
return super.close();
513496
}
514497
}

0 commit comments

Comments
 (0)