refactor: riverpod 3
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
// Types are used through app_providers.dart
|
||||
import '../providers/app_providers.dart';
|
||||
import '../models/user.dart';
|
||||
@@ -7,6 +9,8 @@ import 'token_validator.dart';
|
||||
import 'auth_cache_manager.dart';
|
||||
import '../utils/debug_logger.dart';
|
||||
|
||||
part 'auth_state_manager.g.dart';
|
||||
|
||||
/// Comprehensive auth state representation
|
||||
@immutable
|
||||
class AuthState {
|
||||
@@ -78,25 +82,41 @@ enum AuthStatus {
|
||||
}
|
||||
|
||||
/// Unified auth state manager - single source of truth for all auth operations
|
||||
class AuthStateManager extends Notifier<AuthState> {
|
||||
@Riverpod(keepAlive: true)
|
||||
class AuthStateManager extends _$AuthStateManager {
|
||||
final AuthCacheManager _cacheManager = AuthCacheManager();
|
||||
// Prevent overlapping silent-login attempts from multiple triggers
|
||||
Future<bool>? _silentLoginFuture;
|
||||
bool _initialized = false;
|
||||
|
||||
AuthState get _current =>
|
||||
state.asData?.value ?? const AuthState(status: AuthStatus.initial);
|
||||
|
||||
void _set(AuthState next, {bool cache = false}) {
|
||||
state = AsyncValue.data(next);
|
||||
if (cache) {
|
||||
_cacheManager.cacheAuthState(next);
|
||||
}
|
||||
}
|
||||
|
||||
void _update(
|
||||
AuthState Function(AuthState current) transform, {
|
||||
bool cache = false,
|
||||
}) {
|
||||
final next = transform(_current);
|
||||
_set(next, cache: cache);
|
||||
}
|
||||
|
||||
@override
|
||||
AuthState build() {
|
||||
if (!_initialized) {
|
||||
_initialized = true;
|
||||
Future.microtask(_initialize);
|
||||
}
|
||||
|
||||
return const AuthState(status: AuthStatus.initial);
|
||||
Future<AuthState> build() async {
|
||||
await _initialize();
|
||||
return _current;
|
||||
}
|
||||
|
||||
/// Initialize auth state from storage
|
||||
Future<void> _initialize() async {
|
||||
state = state.copyWith(status: AuthStatus.loading, isLoading: true);
|
||||
_update(
|
||||
(current) =>
|
||||
current.copyWith(status: AuthStatus.loading, isLoading: true),
|
||||
);
|
||||
|
||||
try {
|
||||
final storage = ref.read(optimizedStorageServiceProvider);
|
||||
@@ -107,11 +127,14 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
// Fast path: trust token format to avoid blocking startup on network
|
||||
final formatOk = _isValidTokenFormat(token);
|
||||
if (formatOk) {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.authenticated,
|
||||
token: token,
|
||||
isLoading: false,
|
||||
clearError: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.authenticated,
|
||||
token: token,
|
||||
isLoading: false,
|
||||
clearError: true,
|
||||
),
|
||||
cache: true,
|
||||
);
|
||||
|
||||
// Update API service with token and load user data in background
|
||||
@@ -132,27 +155,33 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
// Token format invalid; clear and require login
|
||||
DebugLogger.auth('Token format invalid, deleting token');
|
||||
await storage.deleteAuthToken();
|
||||
state = state.copyWith(
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
clearError: true,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
DebugLogger.error('auth-init-failed', scope: 'auth/state', error: e);
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error: 'Failed to initialize auth: $e',
|
||||
isLoading: false,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error: 'Failed to initialize auth: $e',
|
||||
isLoading: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -162,10 +191,12 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
String apiKey, {
|
||||
bool rememberCredentials = false,
|
||||
}) async {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.loading,
|
||||
isLoading: true,
|
||||
clearError: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.loading,
|
||||
isLoading: true,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
@@ -216,19 +247,19 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
}
|
||||
|
||||
// Update state (without user data initially)
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.authenticated,
|
||||
token: tokenStr,
|
||||
isLoading: false,
|
||||
clearError: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.authenticated,
|
||||
token: tokenStr,
|
||||
isLoading: false,
|
||||
clearError: true,
|
||||
),
|
||||
cache: true,
|
||||
);
|
||||
|
||||
// Update API service with token
|
||||
_updateApiServiceToken(tokenStr);
|
||||
|
||||
// Cache the successful auth state
|
||||
_cacheManager.cacheAuthState(state);
|
||||
|
||||
// Load user data in background (consistent with credentials method)
|
||||
_loadUserData();
|
||||
|
||||
@@ -240,11 +271,13 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
}
|
||||
} catch (e) {
|
||||
DebugLogger.error('api-key-login-failed', scope: 'auth/state', error: e);
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error: e.toString(),
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error: e.toString(),
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -256,10 +289,12 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
String password, {
|
||||
bool rememberCredentials = false,
|
||||
}) async {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.loading,
|
||||
isLoading: true,
|
||||
clearError: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.loading,
|
||||
isLoading: true,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
@@ -302,18 +337,18 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
}
|
||||
|
||||
// Update state and API service
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.authenticated,
|
||||
token: tokenStr,
|
||||
isLoading: false,
|
||||
clearError: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.authenticated,
|
||||
token: tokenStr,
|
||||
isLoading: false,
|
||||
clearError: true,
|
||||
),
|
||||
cache: true,
|
||||
);
|
||||
|
||||
_updateApiServiceToken(tokenStr);
|
||||
|
||||
// Cache the successful auth state
|
||||
_cacheManager.cacheAuthState(state);
|
||||
|
||||
// Load user data in background
|
||||
_loadUserData();
|
||||
|
||||
@@ -321,11 +356,13 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
return true;
|
||||
} catch (e) {
|
||||
DebugLogger.error('login-failed', scope: 'auth/state', error: e);
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error: e.toString(),
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error: e.toString(),
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -361,10 +398,12 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
}
|
||||
|
||||
Future<bool> _performSilentLogin() async {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.loading,
|
||||
isLoading: true,
|
||||
clearError: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.loading,
|
||||
isLoading: true,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
@@ -372,10 +411,12 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
final savedCredentials = await storage.getSavedCredentials();
|
||||
|
||||
if (savedCredentials == null) {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
isLoading: false,
|
||||
clearError: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
isLoading: false,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -394,11 +435,13 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
ref.invalidate(serverConfigsProvider);
|
||||
ref.invalidate(activeServerProvider);
|
||||
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error:
|
||||
'Saved server configuration is no longer available. Please reconnect.',
|
||||
isLoading: false,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error:
|
||||
'Saved server configuration is no longer available. Please reconnect.',
|
||||
isLoading: false,
|
||||
),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -411,10 +454,12 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
final activeServer = await ref.read(activeServerProvider.future);
|
||||
if (activeServer == null) {
|
||||
await storage.setActiveServerId(null);
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error: 'Server configuration not found',
|
||||
isLoading: false,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error: 'Server configuration not found',
|
||||
isLoading: false,
|
||||
),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -439,11 +484,13 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
await storage.deleteSavedCredentials();
|
||||
}
|
||||
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
error: e.toString(),
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
error: e.toString(),
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
),
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -463,11 +510,13 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
_updateApiServiceToken(null);
|
||||
|
||||
// Update state
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.tokenExpired,
|
||||
clearToken: true,
|
||||
clearUser: true,
|
||||
clearError: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.tokenExpired,
|
||||
clearToken: true,
|
||||
clearUser: true,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
|
||||
// Attempt silent re-login if credentials are available
|
||||
@@ -482,7 +531,10 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
|
||||
/// Logout user
|
||||
Future<void> logout() async {
|
||||
state = state.copyWith(status: AuthStatus.loading, isLoading: true);
|
||||
_update(
|
||||
(current) =>
|
||||
current.copyWith(status: AuthStatus.loading, isLoading: true),
|
||||
);
|
||||
|
||||
try {
|
||||
// Call server logout if possible
|
||||
@@ -505,24 +557,28 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
_updateApiServiceToken(null);
|
||||
|
||||
// Update state
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
clearUser: true,
|
||||
clearError: true,
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
clearUser: true,
|
||||
clearError: true,
|
||||
),
|
||||
);
|
||||
|
||||
DebugLogger.auth('Logout complete');
|
||||
} catch (e) {
|
||||
DebugLogger.error('logout-failed', scope: 'auth/state', error: e);
|
||||
// Even if logout fails, clear local state
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
clearUser: true,
|
||||
error: 'Logout error: $e',
|
||||
_update(
|
||||
(current) => current.copyWith(
|
||||
status: AuthStatus.unauthenticated,
|
||||
isLoading: false,
|
||||
clearToken: true,
|
||||
clearUser: true,
|
||||
error: 'Logout error: $e',
|
||||
),
|
||||
);
|
||||
_updateApiServiceToken(null);
|
||||
}
|
||||
@@ -532,13 +588,14 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
Future<void> _loadUserData() async {
|
||||
try {
|
||||
// First try to extract user info from JWT token if available
|
||||
if (state.token != null) {
|
||||
final jwtUserInfo = TokenValidator.extractUserInfo(state.token!);
|
||||
final current = _current;
|
||||
if (current.token != null) {
|
||||
final jwtUserInfo = TokenValidator.extractUserInfo(current.token!);
|
||||
if (jwtUserInfo != null) {
|
||||
final userFromJwt = _userFromJwtClaims(jwtUserInfo);
|
||||
if (userFromJwt != null) {
|
||||
DebugLogger.auth('Extracted user info from JWT token');
|
||||
state = state.copyWith(user: userFromJwt);
|
||||
_update((current) => current.copyWith(user: userFromJwt));
|
||||
}
|
||||
|
||||
// Still try to load from server in background for complete data
|
||||
@@ -563,15 +620,16 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
Future<void> _loadServerUserData() async {
|
||||
try {
|
||||
final api = ref.read(apiServiceProvider);
|
||||
if (api != null && state.isAuthenticated) {
|
||||
final current = _current;
|
||||
if (api != null && current.isAuthenticated) {
|
||||
// Check if we already have user data from token validation
|
||||
if (state.user != null) {
|
||||
if (current.user != null) {
|
||||
DebugLogger.auth('user-data-present-from-token', scope: 'auth/state');
|
||||
return;
|
||||
}
|
||||
|
||||
final user = await api.getCurrentUser();
|
||||
state = state.copyWith(user: user);
|
||||
_update((current) => current.copyWith(user: user));
|
||||
DebugLogger.auth('Loaded complete user data from server');
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -647,8 +705,8 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
// Store the user data if validation was successful
|
||||
if (serverResult.isValid &&
|
||||
validationUser != null &&
|
||||
state.isAuthenticated) {
|
||||
state = state.copyWith(user: validationUser);
|
||||
_current.isAuthenticated) {
|
||||
_update((current) => current.copyWith(user: validationUser));
|
||||
DebugLogger.auth('Cached user data from token validation');
|
||||
}
|
||||
|
||||
@@ -754,31 +812,3 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
extension _StringFallbackExtension on String {
|
||||
String ifEmptyReturn(String fallback) => isEmpty ? fallback : this;
|
||||
}
|
||||
|
||||
/// Provider for the unified auth state manager
|
||||
final authStateManagerProvider = NotifierProvider<AuthStateManager, AuthState>(
|
||||
AuthStateManager.new,
|
||||
);
|
||||
|
||||
/// Computed providers for common auth state queries
|
||||
final isAuthenticatedProvider = Provider<bool>((ref) {
|
||||
return ref.watch(
|
||||
authStateManagerProvider.select((state) => state.isAuthenticated),
|
||||
);
|
||||
});
|
||||
|
||||
final authTokenProvider2 = Provider<String?>((ref) {
|
||||
return ref.watch(authStateManagerProvider.select((state) => state.token));
|
||||
});
|
||||
|
||||
final authUserProvider = Provider<User?>((ref) {
|
||||
return ref.watch(authStateManagerProvider.select((state) => state.user));
|
||||
});
|
||||
|
||||
final authErrorProvider2 = Provider<String?>((ref) {
|
||||
return ref.watch(authStateManagerProvider.select((state) => state.error));
|
||||
});
|
||||
|
||||
final isAuthLoadingProvider = Provider<bool>((ref) {
|
||||
return ref.watch(authStateManagerProvider.select((state) => state.isLoading));
|
||||
});
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../services/storage_service.dart';
|
||||
// (removed duplicate) import '../services/optimized_storage_service.dart';
|
||||
@@ -22,6 +25,8 @@ import '../services/optimized_storage_service.dart';
|
||||
import '../services/socket_service.dart';
|
||||
import '../utils/debug_logger.dart';
|
||||
|
||||
part 'app_providers.g.dart';
|
||||
|
||||
// Storage providers
|
||||
final sharedPreferencesProvider = Provider<SharedPreferences>((ref) {
|
||||
throw UnimplementedError();
|
||||
@@ -189,45 +194,171 @@ final apiServiceProvider = Provider<ApiService?>((ref) {
|
||||
});
|
||||
|
||||
// Socket.IO service provider
|
||||
final socketServiceProvider = Provider<SocketService?>((ref) {
|
||||
final reviewerMode = ref.watch(reviewerModeProvider);
|
||||
if (reviewerMode) return null;
|
||||
@Riverpod(keepAlive: true)
|
||||
class SocketServiceManager extends _$SocketServiceManager {
|
||||
SocketService? _service;
|
||||
ProviderSubscription<String?>? _tokenSubscription;
|
||||
|
||||
final activeServer = ref.watch(activeServerProvider);
|
||||
final token = ref.watch(authTokenProvider3.select((t) => t));
|
||||
final transportMode = ref.watch(
|
||||
appSettingsProvider.select((s) => s.socketTransportMode),
|
||||
);
|
||||
@override
|
||||
FutureOr<SocketService?> build() async {
|
||||
final reviewerMode = ref.watch(reviewerModeProvider);
|
||||
if (reviewerMode) {
|
||||
_disposeService();
|
||||
return null;
|
||||
}
|
||||
|
||||
return activeServer.maybeWhen(
|
||||
data: (server) {
|
||||
if (server == null) return null;
|
||||
final s = SocketService(
|
||||
final server = await ref.watch(activeServerProvider.future);
|
||||
if (server == null) {
|
||||
_disposeService();
|
||||
return null;
|
||||
}
|
||||
|
||||
final transportMode = ref.watch(
|
||||
appSettingsProvider.select((settings) => settings.socketTransportMode),
|
||||
);
|
||||
final websocketOnly = transportMode == 'ws';
|
||||
final token = ref.watch(authTokenProvider3);
|
||||
|
||||
final requiresNewService =
|
||||
_service == null ||
|
||||
_service!.serverConfig.id != server.id ||
|
||||
_service!.websocketOnly != websocketOnly;
|
||||
if (requiresNewService) {
|
||||
_disposeService();
|
||||
_service = SocketService(
|
||||
serverConfig: server,
|
||||
authToken: token,
|
||||
websocketOnly: transportMode == 'ws',
|
||||
websocketOnly: websocketOnly,
|
||||
);
|
||||
// best-effort connect, but defer to post-frame with a small delay
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
await Future.delayed(const Duration(milliseconds: 150));
|
||||
// ignore: discarded_futures
|
||||
s.connect();
|
||||
});
|
||||
// Keep socket token up-to-date without reconstructing the service
|
||||
ref.listen<String?>(authTokenProvider3, (prev, next) {
|
||||
s.updateAuthToken(next);
|
||||
});
|
||||
ref.onDispose(() {
|
||||
try {
|
||||
s.dispose();
|
||||
} catch (_) {}
|
||||
});
|
||||
return s;
|
||||
},
|
||||
orElse: () => null,
|
||||
);
|
||||
_scheduleConnect(_service!);
|
||||
} else {
|
||||
_service!.updateAuthToken(token);
|
||||
}
|
||||
|
||||
_tokenSubscription ??= ref.listen<String?>(authTokenProvider3, (
|
||||
previous,
|
||||
next,
|
||||
) {
|
||||
_service?.updateAuthToken(next);
|
||||
});
|
||||
|
||||
ref.onDispose(() {
|
||||
_tokenSubscription?.close();
|
||||
_tokenSubscription = null;
|
||||
_disposeService();
|
||||
});
|
||||
|
||||
return _service;
|
||||
}
|
||||
|
||||
void _scheduleConnect(SocketService service) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
await Future.delayed(const Duration(milliseconds: 150));
|
||||
if (!ref.mounted) return;
|
||||
try {
|
||||
unawaited(service.connect());
|
||||
} catch (_) {}
|
||||
});
|
||||
}
|
||||
|
||||
void _disposeService() {
|
||||
if (_service == null) return;
|
||||
try {
|
||||
_service!.dispose();
|
||||
} catch (_) {}
|
||||
_service = null;
|
||||
}
|
||||
}
|
||||
|
||||
final socketServiceProvider = Provider<SocketService?>((ref) {
|
||||
final asyncService = ref.watch(socketServiceManagerProvider);
|
||||
return asyncService.maybeWhen(data: (service) => service, orElse: () => null);
|
||||
});
|
||||
|
||||
enum SocketConnectionState { disconnected, connecting, connected }
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class SocketConnectionStream extends _$SocketConnectionStream {
|
||||
StreamController<SocketConnectionState>? _controller;
|
||||
ProviderSubscription<AsyncValue<SocketService?>>? _serviceSubscription;
|
||||
void Function()? _cancelConnectListener;
|
||||
void Function()? _cancelDisconnectListener;
|
||||
|
||||
@override
|
||||
Stream<SocketConnectionState> build() {
|
||||
final controller = StreamController<SocketConnectionState>.broadcast();
|
||||
_controller = controller;
|
||||
|
||||
void emitState(SocketService? service) {
|
||||
if (service == null) {
|
||||
controller.add(SocketConnectionState.disconnected);
|
||||
_unbindSocket();
|
||||
return;
|
||||
}
|
||||
controller.add(
|
||||
service.isConnected
|
||||
? SocketConnectionState.connected
|
||||
: SocketConnectionState.connecting,
|
||||
);
|
||||
_bindSocket(service);
|
||||
}
|
||||
|
||||
emitState(
|
||||
ref
|
||||
.watch(socketServiceManagerProvider)
|
||||
.maybeWhen(data: (service) => service, orElse: () => null),
|
||||
);
|
||||
|
||||
_serviceSubscription = ref.listen<AsyncValue<SocketService?>>(
|
||||
socketServiceManagerProvider,
|
||||
(previous, next) {
|
||||
emitState(
|
||||
next.maybeWhen(data: (service) => service, orElse: () => null),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
ref.onDispose(() {
|
||||
_serviceSubscription?.close();
|
||||
_serviceSubscription = null;
|
||||
_unbindSocket();
|
||||
_controller?.close();
|
||||
_controller = null;
|
||||
});
|
||||
|
||||
return controller.stream;
|
||||
}
|
||||
|
||||
void _bindSocket(SocketService service) {
|
||||
_unbindSocket();
|
||||
|
||||
void handleConnect(dynamic _) {
|
||||
_controller?.add(SocketConnectionState.connected);
|
||||
}
|
||||
|
||||
void handleDisconnect(dynamic _) {
|
||||
_controller?.add(SocketConnectionState.disconnected);
|
||||
}
|
||||
|
||||
service.socket?.on('connect', handleConnect);
|
||||
service.socket?.on('disconnect', handleDisconnect);
|
||||
|
||||
_cancelConnectListener = () {
|
||||
service.socket?.off('connect', handleConnect);
|
||||
};
|
||||
_cancelDisconnectListener = () {
|
||||
service.socket?.off('disconnect', handleDisconnect);
|
||||
};
|
||||
}
|
||||
|
||||
void _unbindSocket() {
|
||||
_cancelConnectListener?.call();
|
||||
_cancelDisconnectListener?.call();
|
||||
_cancelConnectListener = null;
|
||||
_cancelDisconnectListener = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Attachment upload queue provider
|
||||
final attachmentUploadQueueProvider = Provider<AttachmentUploadQueue?>((ref) {
|
||||
final api = ref.watch(apiServiceProvider);
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import '../providers/app_providers.dart';
|
||||
import '../../features/auth/providers/unified_auth_providers.dart';
|
||||
@@ -16,6 +17,8 @@ import '../services/connectivity_service.dart';
|
||||
import '../utils/debug_logger.dart';
|
||||
import '../models/server_config.dart';
|
||||
|
||||
part 'app_startup_providers.g.dart';
|
||||
|
||||
enum _ConversationWarmupStatus { idle, warming, complete }
|
||||
|
||||
final _conversationWarmupStatusProvider =
|
||||
@@ -116,149 +119,174 @@ void _scheduleConversationWarmup(Ref ref, {bool force = false}) {
|
||||
|
||||
/// App-level startup/background task flow orchestrator.
|
||||
///
|
||||
/// Moves background initialization out of widgets and into a Riverpod provider,
|
||||
/// keeping UI lean and business logic centralized.
|
||||
final appStartupFlowProvider = Provider<void>((ref) {
|
||||
// Ensure token integration listeners are active
|
||||
ref.watch(authApiIntegrationProvider);
|
||||
ref.watch(apiTokenUpdaterProvider);
|
||||
ref.watch(silentLoginCoordinatorProvider);
|
||||
/// Moves background initialization out of widgets and into a Riverpod controller,
|
||||
/// keeping UI lean and business logic centralized while avoiding side effects
|
||||
/// during provider build.
|
||||
@Riverpod(keepAlive: true)
|
||||
class AppStartupFlow extends _$AppStartupFlow {
|
||||
bool _started = false;
|
||||
|
||||
// Kick background model loading flow (non-blocking)
|
||||
ref.watch(backgroundModelLoadProvider);
|
||||
@override
|
||||
FutureOr<void> build() {}
|
||||
|
||||
// If authenticated, keep socket service alive and connected
|
||||
final navState = ref.watch(authNavigationStateProvider);
|
||||
if (navState == AuthNavigationState.authenticated) {
|
||||
ref.watch(socketServiceProvider);
|
||||
void start() {
|
||||
if (_started) return;
|
||||
_started = true;
|
||||
state = const AsyncValue<void>.data(null);
|
||||
_activate();
|
||||
}
|
||||
|
||||
// Ensure resume-triggered foreground refresh is active
|
||||
ref.watch(foregroundRefreshProvider);
|
||||
void _activate() {
|
||||
final ref = this.ref;
|
||||
|
||||
// Keep Socket.IO connection alive in background within platform limits
|
||||
ref.watch(socketPersistenceProvider);
|
||||
// Ensure token integration listeners are active
|
||||
ref.watch(authApiIntegrationProvider);
|
||||
ref.watch(apiTokenUpdaterProvider);
|
||||
ref.watch(silentLoginCoordinatorProvider);
|
||||
|
||||
// Ensure persistent streaming uses the shared connectivity service
|
||||
final connectivityService = ref.watch(connectivityServiceProvider);
|
||||
PersistentStreamingService().attachConnectivityService(connectivityService);
|
||||
// Kick background model loading flow (non-blocking)
|
||||
ref.watch(backgroundModelLoadProvider);
|
||||
|
||||
// Warm the conversations list in the background as soon as possible,
|
||||
// but avoid doing so on poor connectivity to reduce startup load.
|
||||
// Apply a small randomized delay to smooth load spikes across app wakes.
|
||||
Future.microtask(() async {
|
||||
final online = ref.read(isOnlineProvider);
|
||||
if (!online) return;
|
||||
// Slightly increase jitter to reduce contention on startup
|
||||
final jitter = Duration(
|
||||
milliseconds: 150 + (DateTime.now().millisecond % 200),
|
||||
);
|
||||
// Defer until after first frame to keep first paint smooth
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
await Future.delayed(jitter);
|
||||
_scheduleConversationWarmup(ref);
|
||||
});
|
||||
});
|
||||
// If authenticated, keep socket service alive and connected
|
||||
final navState = ref.watch(authNavigationStateProvider);
|
||||
if (navState == AuthNavigationState.authenticated) {
|
||||
ref.watch(socketServiceProvider);
|
||||
}
|
||||
|
||||
// One-time, post-frame system UI polish: set status bar icon brightness to
|
||||
// match theme after the first frame. Avoids flicker at startup.
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
try {
|
||||
final context = NavigationService.context;
|
||||
final view = context != null ? View.maybeOf(context) : null;
|
||||
final dispatcher = WidgetsBinding.instance.platformDispatcher;
|
||||
final platformBrightness =
|
||||
view?.platformDispatcher.platformBrightness ??
|
||||
dispatcher.platformBrightness;
|
||||
final isDark = platformBrightness == Brightness.dark;
|
||||
SystemChrome.setSystemUIOverlayStyle(
|
||||
SystemUiOverlayStyle(
|
||||
statusBarIconBrightness: isDark ? Brightness.light : Brightness.dark,
|
||||
systemNavigationBarIconBrightness: isDark
|
||||
? Brightness.light
|
||||
: Brightness.dark,
|
||||
),
|
||||
// Ensure resume-triggered foreground refresh is active
|
||||
ref.watch(foregroundRefreshProvider);
|
||||
|
||||
// Keep Socket.IO connection alive in background within platform limits
|
||||
ref.watch(socketPersistenceProvider);
|
||||
ref.watch(socketConnectionStreamProvider);
|
||||
|
||||
// Ensure persistent streaming uses the shared connectivity service
|
||||
final connectivityService = ref.watch(connectivityServiceProvider);
|
||||
PersistentStreamingService().attachConnectivityService(connectivityService);
|
||||
|
||||
// Warm the conversations list in the background as soon as possible,
|
||||
// but avoid doing so on poor connectivity to reduce startup load.
|
||||
// Apply a small randomized delay to smooth load spikes across app wakes.
|
||||
Future.microtask(() async {
|
||||
final online = ref.read(isOnlineProvider);
|
||||
if (!online) return;
|
||||
// Slightly increase jitter to reduce contention on startup
|
||||
final jitter = Duration(
|
||||
milliseconds: 150 + (DateTime.now().millisecond % 200),
|
||||
);
|
||||
} catch (_) {}
|
||||
});
|
||||
|
||||
// Watch for auth transitions to trigger warmup and other background work
|
||||
ref.listen<AuthNavigationState>(authNavigationStateProvider, (prev, next) {
|
||||
if (next == AuthNavigationState.authenticated) {
|
||||
// Schedule microtask so we don't perform side-effects inside build
|
||||
Future.microtask(() async {
|
||||
try {
|
||||
final api = ref.read(apiServiceProvider);
|
||||
if (api == null) {
|
||||
DebugLogger.warning('API service not available for startup flow');
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure API has the latest token immediately
|
||||
final authToken = ref.read(authTokenProvider3);
|
||||
if (authToken != null && authToken.isNotEmpty) {
|
||||
api.updateAuthToken(authToken);
|
||||
DebugLogger.auth('StartupFlow: Applied auth token to API');
|
||||
}
|
||||
|
||||
// Preload default model in background (best-effort) with an adaptive
|
||||
// delay based on network latency to avoid hammering poor networks.
|
||||
final latency = ref.read(connectivityServiceProvider).lastLatencyMs;
|
||||
final delayMs = latency < 0
|
||||
? 300
|
||||
: latency > 800
|
||||
? 600
|
||||
: 200 + (latency ~/ 2);
|
||||
Future.delayed(Duration(milliseconds: delayMs), () async {
|
||||
try {
|
||||
await ref.read(defaultModelProvider.future);
|
||||
} catch (e) {
|
||||
DebugLogger.warning(
|
||||
'model-preload-failed',
|
||||
scope: 'startup',
|
||||
data: {'error': e},
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// Kick background chat warmup now that we're authenticated
|
||||
_scheduleConversationWarmup(ref, force: true);
|
||||
|
||||
// Show onboarding once when user reaches chat and hasn't seen it yet
|
||||
await _maybeShowOnboarding(ref);
|
||||
} catch (e) {
|
||||
DebugLogger.error('startup-flow-failed', scope: 'startup', error: e);
|
||||
}
|
||||
// Defer until after first frame to keep first paint smooth
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||||
await Future.delayed(jitter);
|
||||
_scheduleConversationWarmup(ref);
|
||||
});
|
||||
} else {
|
||||
// Reset warmup state when leaving authenticated flow
|
||||
ref
|
||||
.read(_conversationWarmupStatusProvider.notifier)
|
||||
.set(_ConversationWarmupStatus.idle);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Retry warmup when connectivity is restored
|
||||
ref.listen<bool>(isOnlineProvider, (prev, next) {
|
||||
if (next == true) {
|
||||
_scheduleConversationWarmup(ref);
|
||||
}
|
||||
});
|
||||
// One-time, post-frame system UI polish: set status bar icon brightness to
|
||||
// match theme after the first frame. Avoids flicker at startup.
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
try {
|
||||
final context = NavigationService.context;
|
||||
final view = context != null ? View.maybeOf(context) : null;
|
||||
final dispatcher = WidgetsBinding.instance.platformDispatcher;
|
||||
final platformBrightness =
|
||||
view?.platformDispatcher.platformBrightness ??
|
||||
dispatcher.platformBrightness;
|
||||
final isDark = platformBrightness == Brightness.dark;
|
||||
SystemChrome.setSystemUIOverlayStyle(
|
||||
SystemUiOverlayStyle(
|
||||
statusBarIconBrightness: isDark
|
||||
? Brightness.light
|
||||
: Brightness.dark,
|
||||
systemNavigationBarIconBrightness: isDark
|
||||
? Brightness.light
|
||||
: Brightness.dark,
|
||||
),
|
||||
);
|
||||
} catch (_) {}
|
||||
});
|
||||
|
||||
// When conversations reload (e.g., manual refresh), ensure warmup runs again
|
||||
ref.listen<AsyncValue<List<Conversation>>>(conversationsProvider, (
|
||||
previous,
|
||||
next,
|
||||
) {
|
||||
final wasReady = previous?.hasValue == true || previous?.hasError == true;
|
||||
if (wasReady && next.isLoading) {
|
||||
ref
|
||||
.read(_conversationWarmupStatusProvider.notifier)
|
||||
.set(_ConversationWarmupStatus.idle);
|
||||
Future.microtask(() => _scheduleConversationWarmup(ref, force: true));
|
||||
}
|
||||
});
|
||||
});
|
||||
// Watch for auth transitions to trigger warmup and other background work
|
||||
ref.listen<AuthNavigationState>(authNavigationStateProvider, (prev, next) {
|
||||
if (next == AuthNavigationState.authenticated) {
|
||||
// Schedule microtask so we don't perform side-effects inside build
|
||||
Future.microtask(() async {
|
||||
try {
|
||||
final api = ref.read(apiServiceProvider);
|
||||
if (api == null) {
|
||||
DebugLogger.warning('API service not available for startup flow');
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure API has the latest token immediately
|
||||
final authToken = ref.read(authTokenProvider3);
|
||||
if (authToken != null && authToken.isNotEmpty) {
|
||||
api.updateAuthToken(authToken);
|
||||
DebugLogger.auth('StartupFlow: Applied auth token to API');
|
||||
}
|
||||
|
||||
// Preload default model in background (best-effort) with an adaptive
|
||||
// delay based on network latency to avoid hammering poor networks.
|
||||
final latency = ref.read(connectivityServiceProvider).lastLatencyMs;
|
||||
final delayMs = latency < 0
|
||||
? 300
|
||||
: latency > 800
|
||||
? 600
|
||||
: 200 + (latency ~/ 2);
|
||||
Future.delayed(Duration(milliseconds: delayMs), () async {
|
||||
try {
|
||||
await ref.read(defaultModelProvider.future);
|
||||
} catch (e) {
|
||||
DebugLogger.warning(
|
||||
'model-preload-failed',
|
||||
scope: 'startup',
|
||||
data: {'error': e},
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// Kick background chat warmup now that we're authenticated
|
||||
_scheduleConversationWarmup(ref, force: true);
|
||||
|
||||
// Show onboarding once when user reaches chat and hasn't seen it yet
|
||||
await _maybeShowOnboarding(ref);
|
||||
} catch (e) {
|
||||
DebugLogger.error(
|
||||
'startup-flow-failed',
|
||||
scope: 'startup',
|
||||
error: e,
|
||||
);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Reset warmup state when leaving authenticated flow
|
||||
ref
|
||||
.read(_conversationWarmupStatusProvider.notifier)
|
||||
.set(_ConversationWarmupStatus.idle);
|
||||
}
|
||||
});
|
||||
|
||||
// Retry warmup when connectivity is restored
|
||||
ref.listen<bool>(isOnlineProvider, (prev, next) {
|
||||
if (next == true) {
|
||||
_scheduleConversationWarmup(ref);
|
||||
}
|
||||
});
|
||||
|
||||
// When conversations reload (e.g., manual refresh), ensure warmup runs again
|
||||
ref.listen<AsyncValue<List<Conversation>>>(conversationsProvider, (
|
||||
previous,
|
||||
next,
|
||||
) {
|
||||
final wasReady = previous?.hasValue == true || previous?.hasError == true;
|
||||
if (wasReady && next.isLoading) {
|
||||
ref
|
||||
.read(_conversationWarmupStatusProvider.notifier)
|
||||
.set(_ConversationWarmupStatus.idle);
|
||||
Future.microtask(() => _scheduleConversationWarmup(ref, force: true));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Tracks whether we've already attempted a silent login for the current app session.
|
||||
final _silentLoginAttemptedProvider =
|
||||
|
||||
Reference in New Issue
Block a user