Skip to content

Commit c26fa14

Browse files
committed
refactor: pass environment to AccountBloc
- Pass environment to AccountBloc - Add delay in demo mode
1 parent 2081096 commit c26fa14

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

lib/account/bloc/account_bloc.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ import 'package:bloc/bloc.dart';
44
import 'package:equatable/equatable.dart';
55
import 'package:ht_auth_repository/ht_auth_repository.dart';
66
import 'package:ht_data_repository/ht_data_repository.dart';
7+
import 'package:ht_main/app/config/config.dart' as local_config;
78
import 'package:ht_shared/ht_shared.dart';
89

910
part 'account_event.dart';
1011
part 'account_state.dart';
1112

13+
1214
class 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 {

lib/app/view/app.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,14 @@ class App extends StatelessWidget {
101101
htUserContentPreferencesRepository:
102102
_htUserContentPreferencesRepository,
103103
htAppConfigRepository: _htAppConfigRepository,
104-
environment: _environment,
104+
environment: _environment, // Pass environment
105105
),
106106
),
107107
);
108108
}
109109
}
110110

111+
111112
class _AppView extends StatefulWidget {
112113
const _AppView({
113114
required this.htAuthenticationRepository,
@@ -159,6 +160,7 @@ class _AppViewState extends State<_AppView> {
159160
htUserContentPreferencesRepository:
160161
widget.htUserContentPreferencesRepository,
161162
htAppConfigRepository: widget.htAppConfigRepository,
163+
environment: widget.environment, // Pass environment
162164
);
163165

164166
// Removed Dynamic Link Initialization

lib/router/router.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:ht_main/account/view/manage_followed_items/sources/add_source_to
1414
import 'package:ht_main/account/view/manage_followed_items/sources/followed_sources_list_page.dart'; // New
1515
import 'package:ht_main/account/view/saved_headlines_page.dart'; // Import SavedHeadlinesPage
1616
import 'package:ht_main/app/bloc/app_bloc.dart';
17+
import 'package:ht_main/app/config/config.dart' as local_config;
1718
import 'package:ht_main/app/view/app_shell.dart';
1819
import 'package:ht_main/authentication/bloc/authentication_bloc.dart';
1920
import 'package:ht_main/authentication/view/authentication_page.dart';
@@ -62,11 +63,13 @@ GoRouter createRouter({
6263
required HtDataRepository<UserContentPreferences>
6364
htUserContentPreferencesRepository,
6465
required HtDataRepository<AppConfig> htAppConfigRepository,
66+
required local_config.AppEnvironment environment, // Added environment
6567
}) {
6668
// Instantiate AccountBloc once to be shared
6769
final accountBloc = AccountBloc(
6870
authenticationRepository: htAuthenticationRepository,
6971
userContentPreferencesRepository: htUserContentPreferencesRepository,
72+
environment: environment, // Pass environment to AccountBloc
7073
);
7174

7275
return GoRouter(

0 commit comments

Comments
 (0)