refactor: optimize codebase
This commit is contained in:
@@ -384,7 +384,25 @@ class AuthStateManager extends Notifier<AuthState> {
|
||||
final username = savedCredentials['username']!;
|
||||
final password = savedCredentials['password']!;
|
||||
|
||||
// Set active server if needed
|
||||
// Ensure the saved server still exists before switching
|
||||
final serverConfigs = await ref.read(serverConfigsProvider.future);
|
||||
final hasServer = serverConfigs.any((config) => config.id == serverId);
|
||||
|
||||
if (!hasServer) {
|
||||
await storage.deleteSavedCredentials();
|
||||
await storage.setActiveServerId(null);
|
||||
ref.invalidate(serverConfigsProvider);
|
||||
ref.invalidate(activeServerProvider);
|
||||
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
error: 'Saved server configuration is no longer available. Please reconnect.',
|
||||
isLoading: false,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set active server once we know it exists
|
||||
await storage.setActiveServerId(serverId);
|
||||
ref.invalidate(activeServerProvider);
|
||||
|
||||
|
||||
@@ -126,10 +126,13 @@ final activeServerProvider = FutureProvider<ServerConfig?>((ref) async {
|
||||
|
||||
if (activeId == null || configs.isEmpty) return null;
|
||||
|
||||
return configs.firstWhere(
|
||||
(config) => config.id == activeId,
|
||||
orElse: () => configs.first,
|
||||
);
|
||||
for (final config in configs) {
|
||||
if (config.id == activeId) {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
final serverConnectionStateProvider = Provider<bool>((ref) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import '../../features/auth/providers/unified_auth_providers.dart';
|
||||
import '../services/navigation_service.dart';
|
||||
import '../models/conversation.dart';
|
||||
import '../services/background_streaming_handler.dart';
|
||||
import '../services/persistent_streaming_service.dart';
|
||||
import '../../features/onboarding/views/onboarding_sheet.dart';
|
||||
import '../../shared/theme/theme_extensions.dart';
|
||||
import '../services/connectivity_service.dart';
|
||||
@@ -124,6 +125,10 @@ final appStartupFlowProvider = Provider<void>((ref) {
|
||||
// Keep Socket.IO connection alive in background within platform limits
|
||||
ref.watch(socketPersistenceProvider);
|
||||
|
||||
// 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
|
||||
Future.microtask(() => _scheduleConversationWarmup(ref));
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import '../../features/auth/views/authentication_page.dart';
|
||||
import '../../features/auth/views/connect_signin_page.dart';
|
||||
import '../../features/auth/views/server_connection_page.dart';
|
||||
import '../../features/chat/views/chat_page.dart';
|
||||
import '../../features/files/views/workspace_page.dart';
|
||||
import '../../features/navigation/views/splash_launcher_page.dart';
|
||||
import '../../features/profile/views/app_customization_page.dart';
|
||||
import '../../features/profile/views/profile_page.dart';
|
||||
@@ -147,11 +146,6 @@ final goRouterProvider = Provider<GoRouter>((ref) {
|
||||
name: RouteNames.appCustomization,
|
||||
builder: (context, state) => const AppCustomizationPage(),
|
||||
),
|
||||
GoRoute(
|
||||
path: Routes.workspace,
|
||||
name: RouteNames.workspace,
|
||||
builder: (context, state) => const WorkspacePage(),
|
||||
),
|
||||
];
|
||||
|
||||
final router = GoRouter(
|
||||
|
||||
@@ -98,7 +98,6 @@ class Routes {
|
||||
static const String authentication = '/authentication';
|
||||
static const String profile = '/profile';
|
||||
static const String appCustomization = '/profile/customization';
|
||||
static const String workspace = '/workspace';
|
||||
}
|
||||
|
||||
/// Friendly names for GoRouter routes to support context.pushNamed.
|
||||
@@ -110,5 +109,4 @@ class RouteNames {
|
||||
static const String authentication = 'authentication';
|
||||
static const String profile = 'profile';
|
||||
static const String appCustomization = 'app-customization';
|
||||
static const String workspace = 'workspace';
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wakelock_plus/wakelock_plus.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'background_streaming_handler.dart';
|
||||
import 'connectivity_service.dart';
|
||||
import '../utils/debug_logger.dart';
|
||||
@@ -31,6 +30,7 @@ class PersistentStreamingService with WidgetsBindingObserver {
|
||||
|
||||
// Connectivity monitoring
|
||||
StreamSubscription<bool>? _connectivitySubscription;
|
||||
ConnectivityService? _connectivityService;
|
||||
bool _hasConnectivity = true;
|
||||
|
||||
// Recovery state
|
||||
@@ -42,7 +42,6 @@ class PersistentStreamingService with WidgetsBindingObserver {
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_backgroundHandler = BackgroundStreamingHandler.instance;
|
||||
_setupBackgroundHandlerCallbacks();
|
||||
_setupConnectivityMonitoring();
|
||||
_startHeartbeat();
|
||||
}
|
||||
|
||||
@@ -68,31 +67,31 @@ class PersistentStreamingService with WidgetsBindingObserver {
|
||||
};
|
||||
}
|
||||
|
||||
void _setupConnectivityMonitoring() {
|
||||
// Create a connectivity service instance - this would normally be injected
|
||||
// For now, create a temporary instance just for monitoring
|
||||
final connectivityService = ConnectivityService(Dio());
|
||||
void attachConnectivityService(ConnectivityService service) {
|
||||
if (identical(_connectivityService, service)) {
|
||||
return;
|
||||
}
|
||||
|
||||
_connectivitySubscription = connectivityService.isConnected.listen((
|
||||
connected,
|
||||
) {
|
||||
final wasConnected = _hasConnectivity;
|
||||
_hasConnectivity = connected;
|
||||
_connectivitySubscription?.cancel();
|
||||
_connectivityService = service;
|
||||
_connectivitySubscription = service.isConnected.listen(_handleConnectivityChange);
|
||||
}
|
||||
|
||||
if (!wasConnected && connected) {
|
||||
// Connectivity restored - try to recover streams
|
||||
DebugLogger.stream(
|
||||
'PersistentStreaming: Connectivity restored, recovering streams',
|
||||
);
|
||||
_recoverActiveStreams();
|
||||
} else if (wasConnected && !connected) {
|
||||
// Connectivity lost - mark streams as suspended
|
||||
DebugLogger.stream(
|
||||
'PersistentStreaming: Connectivity lost, suspending streams',
|
||||
);
|
||||
_suspendAllStreams();
|
||||
}
|
||||
});
|
||||
void _handleConnectivityChange(bool connected) {
|
||||
final wasConnected = _hasConnectivity;
|
||||
_hasConnectivity = connected;
|
||||
|
||||
if (!wasConnected && connected) {
|
||||
DebugLogger.stream(
|
||||
'PersistentStreaming: Connectivity restored, recovering streams',
|
||||
);
|
||||
_recoverActiveStreams();
|
||||
} else if (wasConnected && !connected) {
|
||||
DebugLogger.stream(
|
||||
'PersistentStreaming: Connectivity lost, suspending streams',
|
||||
);
|
||||
_suspendAllStreams();
|
||||
}
|
||||
}
|
||||
|
||||
void _startHeartbeat() {
|
||||
|
||||
Reference in New Issue
Block a user