@@ -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,25 @@ 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+ if (userAppSettingsFixturesData.isEmpty) {
83+ throw StateError (
84+ 'Cannot create settings from fixture: userAppSettingsFixturesData is empty.' ,
85+ );
86+ }
87+ final fixtureSettings = userAppSettingsFixturesData.first.copyWith (
6488 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- ),
8489 );
90+
8591 await _userAppSettingsRepository.create (
86- item: defaultSettings ,
92+ item: fixtureSettings ,
8793 userId: userId,
8894 );
8995 _logger.info (
90- 'Default UserAppSettings created for '
96+ 'UserAppSettings from fixture created for '
9197 'user ID: $userId .' ,
9298 );
9399 } catch (e, s) {
@@ -110,22 +116,24 @@ class DemoDataInitializerService {
110116 } on NotFoundException {
111117 _logger.info (
112118 '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 [],
119+ 'user ID: $userId . Creating preferences from fixture.' ,
122120 );
121+ // Clone the first item from the fixture data, assigning the new user's ID.
122+ // This provides new demo users with pre-populated saved filters and other preferences.
123+ if (userContentPreferencesFixturesData.isEmpty) {
124+ throw StateError (
125+ 'Cannot create preferences from fixture: userContentPreferencesFixturesData is empty.' ,
126+ );
127+ }
128+ final fixturePreferences = userContentPreferencesFixturesData.first
129+ .copyWith (id: userId);
130+
123131 await _userContentPreferencesRepository.create (
124- item: defaultPreferences ,
132+ item: fixturePreferences ,
125133 userId: userId,
126134 );
127135 _logger.info (
128- 'Default UserContentPreferences created '
136+ 'UserContentPreferences from fixture created '
129137 'for user ID: $userId .' ,
130138 );
131139 } catch (e, s) {
0 commit comments