Skip to content

Commit 6febb45

Browse files
committed
feat(app): implement user-specific data initialization for demo mode
- Add DemoDataInitializerService import and instance to AppBloc - Initialize user-specific data when a user is present in demo mode - Log initialization status to console
1 parent 9cd15d2 commit 6febb45

File tree

1 file changed

+19
-34
lines changed

1 file changed

+19
-34
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flex_color_scheme/flex_color_scheme.dart';
99
import 'package:flutter/material.dart';
1010
import 'package:flutter_news_app_mobile_client_full_source_code/app/config/config.dart'
1111
as local_config;
12+
import 'package:flutter_news_app_mobile_client_full_source_code/app/services/demo_data_initializer_service.dart';
1213
import 'package:flutter_news_app_mobile_client_full_source_code/app/services/demo_data_migration_service.dart';
1314

1415
part 'app_event.dart';
@@ -22,6 +23,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
2223
required DataRepository<User> userRepository,
2324
required local_config.AppEnvironment environment,
2425
this.demoDataMigrationService,
26+
this.demoDataInitializerService,
2527
this.initialUser,
2628
}) : _authenticationRepository = authenticationRepository,
2729
_userAppSettingsRepository = userAppSettingsRepository,
@@ -87,6 +89,7 @@ class AppBloc extends Bloc<AppEvent, AppState> {
8789
final DataRepository<User> _userRepository;
8890
final local_config.AppEnvironment _environment;
8991
final DemoDataMigrationService? demoDataMigrationService;
92+
final DemoDataInitializerService? demoDataInitializerService;
9093
final User? initialUser;
9194
late final StreamSubscription<User?> _userSubscription;
9295

@@ -116,6 +119,22 @@ class AppBloc extends Bloc<AppEvent, AppState> {
116119

117120
if (event.user != null) {
118121
// User is present (authenticated or anonymous)
122+
// In demo mode, ensure user-specific data is initialized
123+
if (_environment == local_config.AppEnvironment.demo &&
124+
demoDataInitializerService != null) {
125+
print(
126+
'[AppBloc] Demo mode: Initializing user-specific data for '
127+
'user ${event.user!.id}.',
128+
);
129+
await demoDataInitializerService!.initializeUserSpecificData(
130+
event.user!,
131+
);
132+
print(
133+
'[AppBloc] Demo mode: User-specific data initialization completed '
134+
'for user ${event.user!.id}.',
135+
);
136+
}
137+
119138
add(const AppSettingsRefreshed());
120139
add(const AppConfigFetchRequested());
121140

@@ -220,40 +239,6 @@ class AppBloc extends Bloc<AppEvent, AppState> {
220239
locale: newLocale,
221240
),
222241
);
223-
} on NotFoundException {
224-
// User settings not found (e.g., first time user), use defaults
225-
print('User app settings not found, using defaults.');
226-
// Emit state with default settings
227-
emit(
228-
state.copyWith(
229-
themeMode: ThemeMode.system,
230-
flexScheme: FlexScheme.material,
231-
appTextScaleFactor: AppTextScaleFactor.medium,
232-
locale: const Locale('en'),
233-
settings: UserAppSettings(
234-
id: state.user!.id,
235-
displaySettings: const DisplaySettings(
236-
baseTheme: AppBaseTheme.system,
237-
accentTheme: AppAccentTheme.defaultBlue,
238-
fontFamily: 'SystemDefault',
239-
textScaleFactor: AppTextScaleFactor.medium,
240-
fontWeight: AppFontWeight.regular,
241-
),
242-
language: languagesFixturesData.firstWhere(
243-
(l) => l.code == 'en',
244-
orElse: () => throw StateError(
245-
'Default language "en" not found in language fixtures.',
246-
),
247-
),
248-
feedPreferences: const FeedDisplayPreferences(
249-
headlineDensity: HeadlineDensity.standard,
250-
headlineImageStyle: HeadlineImageStyle.largeThumbnail,
251-
showSourceInHeadlineFeed: true,
252-
showPublishDateInHeadlineFeed: true,
253-
),
254-
),
255-
),
256-
);
257242
} catch (e) {
258243
// Handle other potential errors during settings fetch
259244
// Optionally emit a failure state or log the error

0 commit comments

Comments
 (0)