Skip to content

Commit 46db2c9

Browse files
committed
refactor: inject fixture data for new anonymous users
Enhances the `DemoDataInitializerService` to accept and use fixture data for `UserAppSettings` and `UserContentPreferences`. Instead of creating empty default objects, the service now clones the first item from the provided fixture lists for any new user. This ensures that new anonymous users in the demo environment start with a rich, pre-populated state, including saved filters for the `SavedFiltersBar`. The service constructor is updated to require the fixture data, following dependency injection principles.
1 parent d385834 commit 46db2c9

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

lib/app/services/demo_data_initializer_service.dart

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import 'package:logging/logging.dart';
44

55
/// {@template demo_data_initializer_service}
66
/// A service responsible for ensuring that essential user-specific data
7-
/// (like [UserAppSettings] and [UserContentPreferences]) exists in the
8-
/// data in-memory clients when a user is first encountered in the demo environment.
7+
/// (like [UserAppSettings] and [UserContentPreferences]) exists for a new user
8+
/// in the demo environment.
9+
///
10+
/// Instead of creating default empty objects, this service now acts as a
11+
/// "fixture injector". It clones rich, pre-defined data from fixture lists,
12+
/// providing new anonymous users with a full-featured initial experience,
13+
/// including pre-populated saved filters.
914
///
1015
/// This service is specifically designed for the in-memory data clients
1116
/// used in the demo environment. In production/development environments,
@@ -17,6 +22,8 @@ class DemoDataInitializerService {
1722
required DataRepository<UserAppSettings> userAppSettingsRepository,
1823
required DataRepository<UserContentPreferences>
1924
userContentPreferencesRepository,
25+
required this.userAppSettingsFixturesData,
26+
required this.userContentPreferencesFixturesData,
2027
}) : _userAppSettingsRepository = userAppSettingsRepository,
2128
_userContentPreferencesRepository = userContentPreferencesRepository,
2229
_logger = Logger('DemoDataInitializerService');
@@ -26,6 +33,16 @@ class DemoDataInitializerService {
2633
_userContentPreferencesRepository;
2734
final Logger _logger;
2835

36+
/// A list of [UserAppSettings] fixture data to be used as a template.
37+
///
38+
/// The first item in this list will be cloned for new users.
39+
final List<UserAppSettings> userAppSettingsFixturesData;
40+
41+
/// A list of [UserContentPreferences] fixture data to be used as a template.
42+
///
43+
/// The first item in this list will be cloned for new users.
44+
final List<UserContentPreferences> userContentPreferencesFixturesData;
45+
2946
/// Initializes essential user-specific data in the in-memory clients
3047
/// for the given [user].
3148
///
@@ -58,36 +75,19 @@ class DemoDataInitializerService {
5875
} on NotFoundException {
5976
_logger.info(
6077
'UserAppSettings not found for user ID: '
61-
'$userId. Creating default settings.',
78+
'$userId. Creating settings from fixture.',
6279
);
63-
final defaultSettings = UserAppSettings(
80+
// Clone the first item from the fixture data, assigning the new user's ID.
81+
// This ensures every new demo user gets a rich, pre-populated set of settings.
82+
final fixtureSettings = userAppSettingsFixturesData.first.copyWith(
6483
id: userId,
65-
displaySettings: const DisplaySettings(
66-
baseTheme: AppBaseTheme.system,
67-
accentTheme: AppAccentTheme.defaultBlue,
68-
fontFamily: 'SystemDefault',
69-
textScaleFactor: AppTextScaleFactor.medium,
70-
fontWeight: AppFontWeight.regular,
71-
),
72-
language: languagesFixturesData.firstWhere(
73-
(l) => l.code == 'en',
74-
orElse: () => throw StateError(
75-
'Default language "en" not found in language fixtures.',
76-
),
77-
),
78-
feedPreferences: const FeedDisplayPreferences(
79-
headlineDensity: HeadlineDensity.standard,
80-
headlineImageStyle: HeadlineImageStyle.smallThumbnail,
81-
showSourceInHeadlineFeed: true,
82-
showPublishDateInHeadlineFeed: true,
83-
),
8484
);
8585
await _userAppSettingsRepository.create(
86-
item: defaultSettings,
86+
item: fixtureSettings,
8787
userId: userId,
8888
);
8989
_logger.info(
90-
'Default UserAppSettings created for '
90+
'UserAppSettings from fixture created for '
9191
'user ID: $userId.',
9292
);
9393
} catch (e, s) {
@@ -110,22 +110,18 @@ class DemoDataInitializerService {
110110
} on NotFoundException {
111111
_logger.info(
112112
'UserContentPreferences not found for '
113-
'user ID: $userId. Creating default preferences.',
114-
);
115-
final defaultPreferences = UserContentPreferences(
116-
id: userId,
117-
followedCountries: const [],
118-
followedSources: const [],
119-
followedTopics: const [],
120-
savedHeadlines: const [],
121-
savedFilters: const [],
113+
'user ID: $userId. Creating preferences from fixture.',
122114
);
115+
// Clone the first item from the fixture data, assigning the new user's ID.
116+
// This provides new demo users with pre-populated saved filters and other preferences.
117+
final fixturePreferences = userContentPreferencesFixturesData.first
118+
.copyWith(id: userId);
123119
await _userContentPreferencesRepository.create(
124-
item: defaultPreferences,
120+
item: fixturePreferences,
125121
userId: userId,
126122
);
127123
_logger.info(
128-
'Default UserContentPreferences created '
124+
'UserContentPreferences from fixture created '
129125
'for user ID: $userId.',
130126
);
131127
} catch (e, s) {

0 commit comments

Comments
 (0)