11import 'package:core/core.dart' ;
22import 'package:data_repository/data_repository.dart' ;
3- // Required for ThemeMode, AppBaseTheme, etc.
3+ import 'package:logging/logging.dart' ; // Import Logger
44
55/// {@template demo_data_initializer_service}
66/// A service responsible for ensuring that essential user-specific data
7- /// (like [UserAppSettings] , [UserContentPreferences] , and the [User] object
8- /// itself) exists in the data in-memory clients when a user is first encountered
9- /// in the demo environment.
7+ /// (like [UserAppSettings] and [UserContentPreferences] ) exists in the
8+ /// data in-memory clients when a user is first encountered in the demo environment.
109///
1110/// This service is specifically designed for the in-memory data clients
1211/// used in the demo environment. In production/development environments,
1312/// the backend API is responsible for initializing user data.
1413/// {@endtemplate}
1514class DemoDataInitializerService {
1615 /// {@macro demo_data_initializer_service}
17- const DemoDataInitializerService ({
16+ DemoDataInitializerService ({
1817 required DataRepository <UserAppSettings > userAppSettingsRepository,
1918 required DataRepository <UserContentPreferences >
2019 userContentPreferencesRepository,
21- required DataRepository <User > userRepository,
2220 }) : _userAppSettingsRepository = userAppSettingsRepository,
2321 _userContentPreferencesRepository = userContentPreferencesRepository,
24- _userRepository = userRepository;
22+ _logger = Logger ( 'DemoDataInitializerService' ); // Initialize logger
2523
2624 final DataRepository <UserAppSettings > _userAppSettingsRepository;
2725 final DataRepository <UserContentPreferences >
2826 _userContentPreferencesRepository;
29- final DataRepository < User > _userRepository;
27+ final Logger _logger; // Add logger instance
3028
3129 /// Initializes essential user-specific data in the in-memory clients
3230 /// for the given [user] .
3331 ///
34- /// This method checks if [UserAppSettings] , [UserContentPreferences] ,
35- /// and the [User] object itself exist for the provided user ID. If any
36- /// are missing, it creates them with default values.
32+ /// This method checks if [UserAppSettings] and [UserContentPreferences]
33+ /// exist for the provided user ID. If any are missing, it creates them
34+ /// with default values.
3735 ///
3836 /// This prevents "READ FAILED" errors when the application attempts to
3937 /// access these user-specific data points for a newly signed-in anonymous
4038 /// user in the demo environment.
4139 Future <void > initializeUserSpecificData (User user) async {
42- print (
43- '[DemoDataInitializerService] Initializing user-specific data for '
44- 'user ID: ${user .id }' ,
45- );
40+ _logger.info ('Initializing user-specific data for user ID: ${user .id }' );
4641
4742 await Future .wait ([
4843 _ensureUserAppSettingsExist (user.id),
4944 _ensureUserContentPreferencesExist (user.id),
50- _ensureUserClientUserExists (user),
5145 ]);
5246
53- print (
54- '[DemoDataInitializerService] User-specific data initialization '
55- 'completed for user ID: ${user .id }' ,
47+ _logger.info (
48+ 'User-specific data initialization completed for user ID: ${user .id }' ,
5649 );
5750 }
5851
@@ -61,12 +54,10 @@ class DemoDataInitializerService {
6154 Future <void > _ensureUserAppSettingsExist (String userId) async {
6255 try {
6356 await _userAppSettingsRepository.read (id: userId, userId: userId);
64- print (
65- '[DemoDataInitializerService] UserAppSettings found for user ID: $userId .' ,
66- );
57+ _logger.info ('UserAppSettings found for user ID: $userId .' );
6758 } on NotFoundException {
68- print (
69- '[DemoDataInitializerService] UserAppSettings not found for user ID: '
59+ _logger. info (
60+ 'UserAppSettings not found for user ID: '
7061 '$userId . Creating default settings.' ,
7162 );
7263 final defaultSettings = UserAppSettings (
@@ -95,14 +86,16 @@ class DemoDataInitializerService {
9586 item: defaultSettings,
9687 userId: userId,
9788 );
98- print (
99- '[DemoDataInitializerService] Default UserAppSettings created for '
89+ _logger. info (
90+ 'Default UserAppSettings created for '
10091 'user ID: $userId .' ,
10192 );
10293 } catch (e, s) {
103- print (
104- '[DemoDataInitializerService] Error ensuring UserAppSettings exist '
105- 'for user ID: $userId : $e \n $s ' ,
94+ _logger.severe (
95+ 'Error ensuring UserAppSettings exist '
96+ 'for user ID: $userId : $e ' ,
97+ e,
98+ s,
10699 );
107100 rethrow ;
108101 }
@@ -113,12 +106,10 @@ class DemoDataInitializerService {
113106 Future <void > _ensureUserContentPreferencesExist (String userId) async {
114107 try {
115108 await _userContentPreferencesRepository.read (id: userId, userId: userId);
116- print (
117- '[DemoDataInitializerService] UserContentPreferences found for user ID: $userId .' ,
118- );
109+ _logger.info ('UserContentPreferences found for user ID: $userId .' );
119110 } on NotFoundException {
120- print (
121- '[DemoDataInitializerService] UserContentPreferences not found for '
111+ _logger. info (
112+ 'UserContentPreferences not found for '
122113 'user ID: $userId . Creating default preferences.' ,
123114 );
124115 final defaultPreferences = UserContentPreferences (
@@ -132,48 +123,16 @@ class DemoDataInitializerService {
132123 item: defaultPreferences,
133124 userId: userId,
134125 );
135- print (
136- '[DemoDataInitializerService] Default UserContentPreferences created '
126+ _logger. info (
127+ 'Default UserContentPreferences created '
137128 'for user ID: $userId .' ,
138129 );
139130 } catch (e, s) {
140- print (
141- '[DemoDataInitializerService] Error ensuring UserContentPreferences '
142- 'exist for user ID: $userId : $e \n $s ' ,
143- );
144- rethrow ;
145- }
146- }
147-
148- /// Ensures that the [User] object for the given [user] exists in the
149- /// user client. If not found, creates it. If found, updates it.
150- ///
151- /// This is important because the `AuthInmemory` client might create a
152- /// basic user, but the `DataInMemory<User>` client might not have it
153- /// immediately.
154- Future <void > _ensureUserClientUserExists (User user) async {
155- try {
156- await _userRepository.read (id: user.id, userId: user.id);
157- // If user exists, ensure it's up-to-date (e.g., if roles changed)
158- await _userRepository.update (id: user.id, item: user, userId: user.id);
159- print (
160- '[DemoDataInitializerService] User object found and updated in '
161- 'user client for ID: ${user .id }.' ,
162- );
163- } on NotFoundException {
164- print (
165- '[DemoDataInitializerService] User object not found in user client '
166- 'for ID: ${user .id }. Creating it.' ,
167- );
168- await _userRepository.create (item: user, userId: user.id);
169- print (
170- '[DemoDataInitializerService] User object created in user client '
171- 'for ID: ${user .id }.' ,
172- );
173- } catch (e, s) {
174- print (
175- '[DemoDataInitializerService] Error ensuring User object exists in '
176- 'user client for ID: ${user .id }: $e \n $s ' ,
131+ _logger.severe (
132+ 'Error ensuring UserContentPreferences '
133+ 'exist for user ID: $userId : $e ' ,
134+ e,
135+ s,
177136 );
178137 rethrow ;
179138 }
0 commit comments