@@ -325,18 +325,28 @@ class AppBloc extends Bloc<AppEvent, AppState> {
325
325
326
326
// If a new user is present, handle their data.
327
327
if (newUser != null ) {
328
- // In demo mode, ensure user-specific data is initialized BEFORE fetching.
328
+ // In demo mode, ensure essential user-specific data (settings,
329
+ // preferences, and the user object itself in the data client)
330
+ // are initialized if they don't already exist. This prevents
331
+ // NotFoundException during subsequent reads.
329
332
if (_environment == local_config.AppEnvironment .demo &&
330
333
demoDataInitializerService != null ) {
334
+ _logger.info (
335
+ '[AppBloc] Demo mode: Initializing user-specific data for '
336
+ 'user: ${newUser .id }' ,
337
+ );
331
338
try {
332
- _logger.info ('Demo mode: Initializing data for user ${newUser .id }.' );
333
339
await demoDataInitializerService! .initializeUserSpecificData (newUser);
334
340
_logger.info (
335
- 'Demo mode: Data initialization complete for ${newUser .id }.' ,
341
+ '[AppBloc] Demo mode: User-specific data initialized for '
342
+ 'user: ${newUser .id }.' ,
336
343
);
337
344
} catch (e, s) {
338
- _logger.severe ('ERROR: Failed to initialize demo user data.' , e, s);
339
- // If demo data initialization fails, it's a critical error for demo mode.
345
+ _logger.severe (
346
+ '[AppBloc] ERROR: Failed to initialize demo user data.' ,
347
+ e,
348
+ s,
349
+ );
340
350
emit (
341
351
state.copyWith (
342
352
status: AppLifeCycleStatus .criticalError,
@@ -345,33 +355,55 @@ class AppBloc extends Bloc<AppEvent, AppState> {
345
355
),
346
356
),
347
357
);
348
- return ; // Stop further processing if demo data init failed critically.
358
+ return ; // Stop further processing if initialization failed critically.
349
359
}
350
360
}
361
+
362
+ // Handle data migration if an anonymous user signs in.
363
+ if (oldUser != null &&
364
+ oldUser.appRole == AppUserRole .guestUser &&
365
+ newUser.appRole == AppUserRole .standardUser) {
366
+ _logger.info (
367
+ '[AppBloc] Anonymous user ${oldUser .id } transitioned to '
368
+ 'authenticated user ${newUser .id }. Attempting data migration.' ,
369
+ );
370
+ if (demoDataMigrationService != null &&
371
+ _environment == local_config.AppEnvironment .demo) {
372
+ try {
373
+ await demoDataMigrationService! .migrateAnonymousData (
374
+ oldUserId: oldUser.id,
375
+ newUserId: newUser.id,
376
+ );
377
+ _logger.info (
378
+ '[AppBloc] Demo mode: Data migration completed for ${newUser .id }.' ,
379
+ );
380
+ } catch (e, s) {
381
+ _logger.severe (
382
+ '[AppBloc] ERROR: Failed to migrate demo user data.' ,
383
+ e,
384
+ s,
385
+ );
386
+ // If demo data migration fails, it's a critical error for demo mode.
387
+ emit (
388
+ state.copyWith (
389
+ status: AppLifeCycleStatus .criticalError,
390
+ initialUserPreferencesError: UnknownException (
391
+ 'Failed to migrate demo user data: ${e .toString ()}' ,
392
+ ),
393
+ ),
394
+ );
395
+ return ; // Stop further processing if migration failed critically.
396
+ }
397
+ }
398
+ }
399
+
400
+ // After potential initialization and migration,
401
+ // ensure user-specific data (settings and preferences) are loaded.
351
402
await _fetchAndSetUserData (newUser, emit);
352
403
} else {
353
404
// If user logs out, clear user-specific data from state.
354
405
emit (state.copyWith (settings: null , userContentPreferences: null ));
355
406
}
356
-
357
- // Handle data migration if an anonymous user signs in.
358
- if (oldUser != null &&
359
- oldUser.appRole == AppUserRole .guestUser &&
360
- newUser != null &&
361
- newUser.appRole == AppUserRole .standardUser) {
362
- _logger.info (
363
- 'Anonymous user ${oldUser .id } transitioned to authenticated user '
364
- '${newUser .id }. Attempting data migration.' ,
365
- );
366
- if (demoDataMigrationService != null &&
367
- _environment == local_config.AppEnvironment .demo) {
368
- await demoDataMigrationService! .migrateAnonymousData (
369
- oldUserId: oldUser.id,
370
- newUserId: newUser.id,
371
- );
372
- _logger.info ('Demo mode: Data migration completed for ${newUser .id }.' );
373
- }
374
- }
375
407
}
376
408
377
409
/// Handles refreshing/loading app settings (theme, font).
0 commit comments