feat: enhance authentication state management with model preloading

- Introduced a new private method `_preloadDefaultModel` to preload the default model upon successful authentication.
- Updated the token handling logic to kick off dependent background work, ensuring a smoother user experience.
- Enhanced error handling during model preloading to log failures appropriately.
- This change improves the efficiency of user data loading and prepares the application for subsequent operations.
This commit is contained in:
cogwheel0
2025-10-01 23:44:22 +05:30
parent ebe6cec17c
commit 2d35bf4e07

View File

@@ -137,8 +137,9 @@ class AuthStateManager extends _$AuthStateManager {
cache: true,
);
// Update API service with token and load user data in background
// Update API service with token and kick off dependent background work
_updateApiServiceToken(token);
_preloadDefaultModel();
_loadUserData();
// Background server validation; if it fails, invalidate token gracefully
@@ -257,8 +258,9 @@ class AuthStateManager extends _$AuthStateManager {
cache: true,
);
// Update API service with token
// Update API service with token and kick off dependent background work
_updateApiServiceToken(tokenStr);
_preloadDefaultModel();
// Load user data in background (consistent with credentials method)
_loadUserData();
@@ -348,6 +350,7 @@ class AuthStateManager extends _$AuthStateManager {
);
_updateApiServiceToken(tokenStr);
_preloadDefaultModel();
// Load user data in background
_loadUserData();
@@ -584,6 +587,24 @@ class AuthStateManager extends _$AuthStateManager {
}
}
/// Preload the default model as soon as authentication succeeds.
void _preloadDefaultModel() {
Future.microtask(() async {
if (!ref.mounted) return;
try {
await ref.read(defaultModelProvider.future);
DebugLogger.auth('Default model preload requested');
} catch (e) {
if (!ref.mounted) return;
DebugLogger.warning(
'default-model-preload-failed',
scope: 'auth/state',
data: {'error': e.toString()},
);
}
});
}
/// Load user data in background with JWT extraction fallback
Future<void> _loadUserData() async {
try {