feat: enhance connectivity management and status handling
- Integrated connectivity status monitoring into the RouterNotifier to manage navigation based on network availability. - Refactored ConnectivityService to streamline connectivity checks and improve state management, ensuring a more reliable online/offline status. - Updated the connection issue page to directly utilize the connectivity status provider, simplifying the connectivity state handling. - Improved offline indicator behavior to provide clearer feedback on connectivity changes. - Enhanced the persistent streaming service to react to connectivity status changes more effectively.
This commit is contained in:
@@ -31,6 +31,10 @@ class RouterNotifier extends ChangeNotifier {
|
|||||||
authNavigationStateProvider,
|
authNavigationStateProvider,
|
||||||
_onStateChanged,
|
_onStateChanged,
|
||||||
),
|
),
|
||||||
|
ref.listen<ConnectivityStatus>(
|
||||||
|
connectivityStatusProvider,
|
||||||
|
_onStateChanged,
|
||||||
|
),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,13 +91,18 @@ class RouterNotifier extends ChangeNotifier {
|
|||||||
? Routes.chat
|
? Routes.chat
|
||||||
: Routes.authentication;
|
: Routes.authentication;
|
||||||
}
|
}
|
||||||
final connectivityAsync = ref.read(connectivityStatusProvider);
|
|
||||||
final connectivity = connectivityAsync.asData?.value;
|
|
||||||
|
|
||||||
|
// Check connectivity status to determine if we should show connection issue
|
||||||
|
final connectivity = ref.read(connectivityStatusProvider);
|
||||||
|
|
||||||
|
// Only show connection issue page if:
|
||||||
|
// 1. Not in reviewer mode
|
||||||
|
// 2. Connectivity is explicitly offline
|
||||||
|
// 3. Auth is authenticated (don't interrupt auth flow)
|
||||||
final shouldShowConnectionIssue =
|
final shouldShowConnectionIssue =
|
||||||
!reviewerMode &&
|
!reviewerMode &&
|
||||||
connectivity == ConnectivityStatus.offline &&
|
connectivity == ConnectivityStatus.offline &&
|
||||||
authState != AuthNavigationState.needsLogin;
|
authState == AuthNavigationState.authenticated;
|
||||||
|
|
||||||
if (shouldShowConnectionIssue) {
|
if (shouldShowConnectionIssue) {
|
||||||
return location == Routes.connectionIssue ? null : Routes.connectionIssue;
|
return location == Routes.connectionIssue ? null : Routes.connectionIssue;
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:dio/io.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
|
|
||||||
@@ -9,329 +12,284 @@ import '../providers/app_providers.dart';
|
|||||||
|
|
||||||
part 'connectivity_service.g.dart';
|
part 'connectivity_service.g.dart';
|
||||||
|
|
||||||
enum ConnectivityStatus { online, offline, checking }
|
/// Connectivity status for the app.
|
||||||
|
/// - [online]: Server is reachable
|
||||||
|
/// - [offline]: No network or server unreachable
|
||||||
|
enum ConnectivityStatus { online, offline }
|
||||||
|
|
||||||
|
/// Simplified connectivity service that monitors network and server health.
|
||||||
|
///
|
||||||
|
/// Key improvements:
|
||||||
|
/// - No "checking" state to prevent UI flashing
|
||||||
|
/// - Assumes online by default (optimistic)
|
||||||
|
/// - Only shows offline when explicitly confirmed
|
||||||
|
/// - Minimal state changes during startup
|
||||||
class ConnectivityService {
|
class ConnectivityService {
|
||||||
ConnectivityService(this._dio, this._ref, [Connectivity? connectivity])
|
ConnectivityService(this._dio, this._ref, [Connectivity? connectivity])
|
||||||
: _connectivity = connectivity ?? Connectivity() {
|
: _connectivity = connectivity ?? Connectivity() {
|
||||||
_startConnectivityMonitoring();
|
_initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
final Dio _dio;
|
final Dio _dio;
|
||||||
final Ref _ref;
|
final Ref _ref;
|
||||||
final Connectivity _connectivity;
|
final Connectivity _connectivity;
|
||||||
|
|
||||||
final _connectivityController =
|
final _statusController = StreamController<ConnectivityStatus>.broadcast();
|
||||||
StreamController<ConnectivityStatus>.broadcast();
|
|
||||||
|
|
||||||
Timer? _initialCheckTimer;
|
|
||||||
Timer? _pollTimer;
|
|
||||||
StreamSubscription<List<ConnectivityResult>>? _connectivitySubscription;
|
StreamSubscription<List<ConnectivityResult>>? _connectivitySubscription;
|
||||||
Completer<void>? _activeCheck;
|
Timer? _pollTimer;
|
||||||
List<ConnectivityResult>? _lastConnectivityResults;
|
Timer? _noNetworkGraceTimer;
|
||||||
|
|
||||||
ConnectivityStatus _lastStatus = ConnectivityStatus.checking;
|
// Start optimistically as online to prevent flash
|
||||||
Duration _interval = const Duration(seconds: 10);
|
ConnectivityStatus _currentStatus = ConnectivityStatus.online;
|
||||||
int _recentFailures = 0;
|
bool _hasNetworkInterface = false;
|
||||||
|
bool _hasConfirmedNetwork = false;
|
||||||
|
bool _hasSuccessfulProbe = false;
|
||||||
|
int _consecutiveFailures = 0;
|
||||||
int _lastLatencyMs = -1;
|
int _lastLatencyMs = -1;
|
||||||
bool _hasNetwork = true;
|
|
||||||
bool _queuedImmediateCheck = false;
|
|
||||||
|
|
||||||
Stream<ConnectivityStatus> get connectivityStream =>
|
Stream<ConnectivityStatus> get statusStream => _statusController.stream;
|
||||||
_connectivityController.stream;
|
ConnectivityStatus get currentStatus => _currentStatus;
|
||||||
ConnectivityStatus get currentStatus => _lastStatus;
|
|
||||||
int get lastLatencyMs => _lastLatencyMs;
|
int get lastLatencyMs => _lastLatencyMs;
|
||||||
|
bool get isOnline => _currentStatus == ConnectivityStatus.online;
|
||||||
|
|
||||||
Stream<bool> get isConnected =>
|
void _initialize() {
|
||||||
connectivityStream.map((status) => status == ConnectivityStatus.online);
|
// Listen to network interface changes
|
||||||
|
_connectivitySubscription = _connectivity.onConnectivityChanged.listen(
|
||||||
bool get isCurrentlyConnected => _lastStatus == ConnectivityStatus.online;
|
_handleNetworkChange,
|
||||||
|
onError: (_) {}, // Ignore connectivity errors
|
||||||
void _startConnectivityMonitoring() {
|
|
||||||
_initialCheckTimer = Timer(const Duration(milliseconds: 800), () {
|
|
||||||
unawaited(_runConnectivityCheck(force: true));
|
|
||||||
});
|
|
||||||
|
|
||||||
_connectivitySubscription = _connectivity.onConnectivityChanged.listen((
|
|
||||||
results,
|
|
||||||
) {
|
|
||||||
unawaited(_handleConnectivityChange(results));
|
|
||||||
});
|
|
||||||
|
|
||||||
unawaited(
|
|
||||||
_connectivity.checkConnectivity().then(
|
|
||||||
(results) => _handleConnectivityChange(results),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Check initial network state immediately
|
||||||
|
_connectivity.checkConnectivity().then(_handleNetworkChange);
|
||||||
|
|
||||||
|
// Start periodic health checks
|
||||||
|
_scheduleNextCheck();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _runConnectivityCheck({bool force = false}) async {
|
void _handleNetworkChange(List<ConnectivityResult> results) {
|
||||||
if (_connectivityController.isClosed) return;
|
final hadNetwork = _hasNetworkInterface;
|
||||||
|
final hasNetwork = results.any((r) => r != ConnectivityResult.none);
|
||||||
|
_hasNetworkInterface = hasNetwork;
|
||||||
|
|
||||||
if (!_hasNetwork) {
|
if (!hasNetwork) {
|
||||||
_lastLatencyMs = -1;
|
if (hadNetwork || _hasConfirmedNetwork) {
|
||||||
|
// Lost network after previously confirming it
|
||||||
|
_cancelNoNetworkGrace();
|
||||||
_updateStatus(ConnectivityStatus.offline);
|
_updateStatus(ConnectivityStatus.offline);
|
||||||
return;
|
_stopPolling();
|
||||||
|
} else {
|
||||||
|
// During startup we often get a transient "none" result.
|
||||||
|
// Defer emitting offline until it persists beyond the grace window.
|
||||||
|
_noNetworkGraceTimer ??= Timer(const Duration(seconds: 2), () {
|
||||||
|
if (!_hasNetworkInterface) {
|
||||||
|
_updateStatus(ConnectivityStatus.offline);
|
||||||
|
_stopPolling();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
_initialCheckTimer?.cancel();
|
|
||||||
_initialCheckTimer = null;
|
|
||||||
_cancelScheduledPoll();
|
|
||||||
|
|
||||||
final existingCheck = _activeCheck;
|
|
||||||
if (existingCheck != null) {
|
|
||||||
if (force) {
|
|
||||||
_queuedImmediateCheck = true;
|
|
||||||
}
|
|
||||||
await existingCheck.future;
|
|
||||||
if (force && _queuedImmediateCheck) {
|
|
||||||
_queuedImmediateCheck = false;
|
|
||||||
await _runConnectivityCheck(force: false);
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final completer = Completer<void>();
|
// Network available
|
||||||
_activeCheck = completer;
|
_cancelNoNetworkGrace();
|
||||||
|
if (!_hasConfirmedNetwork) {
|
||||||
if (_lastStatus != ConnectivityStatus.checking) {
|
_hasConfirmedNetwork = true;
|
||||||
_updateStatus(ConnectivityStatus.checking);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if (!hadNetwork) {
|
||||||
await _checkConnectivity();
|
// Network just came back, check server immediately
|
||||||
} finally {
|
_checkServerHealth();
|
||||||
completer.complete();
|
}
|
||||||
_activeCheck = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_queuedImmediateCheck) {
|
void _scheduleNextCheck({Duration? delay}) {
|
||||||
_queuedImmediateCheck = false;
|
_stopPolling();
|
||||||
await _runConnectivityCheck(force: false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_scheduleNextPoll();
|
// Adaptive polling based on failure count
|
||||||
}
|
final interval =
|
||||||
|
delay ??
|
||||||
|
(_consecutiveFailures >= 3
|
||||||
|
? const Duration(seconds: 30)
|
||||||
|
: _consecutiveFailures >= 1
|
||||||
|
? const Duration(seconds: 20)
|
||||||
|
: const Duration(seconds: 15));
|
||||||
|
|
||||||
void _scheduleNextPoll() {
|
_pollTimer = Timer(interval, () {
|
||||||
if (_connectivityController.isClosed || !_hasNetwork) {
|
if (_hasNetworkInterface) {
|
||||||
return;
|
_checkServerHealth();
|
||||||
}
|
}
|
||||||
|
|
||||||
_pollTimer = Timer(_interval, () {
|
|
||||||
_pollTimer = null;
|
|
||||||
unawaited(_runConnectivityCheck());
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void _cancelScheduledPoll() {
|
void _stopPolling() {
|
||||||
_pollTimer?.cancel();
|
_pollTimer?.cancel();
|
||||||
_pollTimer = null;
|
_pollTimer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _haveSameConnectivity(
|
Future<void> _checkServerHealth() async {
|
||||||
List<ConnectivityResult> previous,
|
if (_statusController.isClosed || !_hasNetworkInterface) {
|
||||||
List<ConnectivityResult> current,
|
return;
|
||||||
) {
|
|
||||||
if (identical(previous, current)) return true;
|
|
||||||
if (previous.length != current.length) return false;
|
|
||||||
final previousSet = previous.toSet();
|
|
||||||
final currentSet = current.toSet();
|
|
||||||
if (previousSet.length != currentSet.length) return false;
|
|
||||||
for (final value in previousSet) {
|
|
||||||
if (!currentSet.contains(value)) return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final isReachable = await _probeServer();
|
||||||
|
|
||||||
|
Duration? overrideDelay;
|
||||||
|
|
||||||
|
if (isReachable) {
|
||||||
|
_consecutiveFailures = 0;
|
||||||
|
_hasSuccessfulProbe = true;
|
||||||
|
_updateStatus(ConnectivityStatus.online);
|
||||||
|
} else {
|
||||||
|
_consecutiveFailures++;
|
||||||
|
// Only surface offline after we've confirmed the server once or we have
|
||||||
|
// multiple consecutive failures. This avoids startup flicker where
|
||||||
|
// authorization or DNS is still settling.
|
||||||
|
if (_hasSuccessfulProbe || _consecutiveFailures >= 2) {
|
||||||
|
_updateStatus(ConnectivityStatus.offline);
|
||||||
|
} else {
|
||||||
|
overrideDelay = const Duration(seconds: 3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_scheduleNextCheck(delay: overrideDelay);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> _probeServer() async {
|
||||||
|
final baseUri = _getServerUri();
|
||||||
|
if (baseUri == null) {
|
||||||
|
// No server configured yet, assume online
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _handleConnectivityChange(
|
|
||||||
List<ConnectivityResult> results,
|
|
||||||
) async {
|
|
||||||
if (_connectivityController.isClosed) return;
|
|
||||||
|
|
||||||
final previousResults = _lastConnectivityResults;
|
|
||||||
_lastConnectivityResults = results;
|
|
||||||
final hadNetwork = _hasNetwork;
|
|
||||||
_hasNetwork = results.any((result) => result != ConnectivityResult.none);
|
|
||||||
|
|
||||||
if (!_hasNetwork) {
|
|
||||||
_lastLatencyMs = -1;
|
|
||||||
_queuedImmediateCheck = false;
|
|
||||||
_cancelScheduledPoll();
|
|
||||||
_initialCheckTimer?.cancel();
|
|
||||||
_initialCheckTimer = null;
|
|
||||||
_updateStatus(ConnectivityStatus.offline);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final networkTypeChanged = previousResults == null
|
|
||||||
? true
|
|
||||||
: !_haveSameConnectivity(previousResults, results);
|
|
||||||
|
|
||||||
if (!hadNetwork ||
|
|
||||||
_lastStatus == ConnectivityStatus.offline ||
|
|
||||||
networkTypeChanged) {
|
|
||||||
unawaited(_runConnectivityCheck(force: true));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _checkConnectivity() async {
|
|
||||||
if (_connectivityController.isClosed) return;
|
|
||||||
|
|
||||||
final serverReachability = await _probeActiveServer();
|
|
||||||
if (serverReachability != null) {
|
|
||||||
if (serverReachability) {
|
|
||||||
_updateStatus(ConnectivityStatus.online);
|
|
||||||
} else {
|
|
||||||
_lastLatencyMs = -1;
|
|
||||||
_updateStatus(ConnectivityStatus.offline);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final fallbackReachability = await _probeAnyKnownServer();
|
|
||||||
if (fallbackReachability != null) {
|
|
||||||
if (fallbackReachability) {
|
|
||||||
_updateStatus(ConnectivityStatus.online);
|
|
||||||
} else {
|
|
||||||
_lastLatencyMs = -1;
|
|
||||||
_updateStatus(ConnectivityStatus.offline);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_lastLatencyMs = -1;
|
|
||||||
_updateStatus(ConnectivityStatus.online);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _updateStatus(ConnectivityStatus status) {
|
|
||||||
if (_lastStatus != status) {
|
|
||||||
_lastStatus = status;
|
|
||||||
if (!_connectivityController.isClosed) {
|
|
||||||
_connectivityController.add(status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (status == ConnectivityStatus.offline) {
|
|
||||||
_recentFailures = (_recentFailures + 1).clamp(0, 10);
|
|
||||||
} else if (status == ConnectivityStatus.online) {
|
|
||||||
_recentFailures = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
final newInterval = _recentFailures >= 3
|
|
||||||
? const Duration(seconds: 20)
|
|
||||||
: _recentFailures == 2
|
|
||||||
? const Duration(seconds: 15)
|
|
||||||
: const Duration(seconds: 10);
|
|
||||||
|
|
||||||
if (newInterval != _interval) {
|
|
||||||
_interval = newInterval;
|
|
||||||
_cancelScheduledPoll();
|
|
||||||
if (_lastStatus != ConnectivityStatus.offline && _hasNetwork) {
|
|
||||||
_scheduleNextPoll();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool> checkConnectivity() async {
|
|
||||||
await _runConnectivityCheck(force: true);
|
|
||||||
return _lastStatus == ConnectivityStatus.online;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dispose() {
|
|
||||||
_initialCheckTimer?.cancel();
|
|
||||||
_initialCheckTimer = null;
|
|
||||||
_cancelScheduledPoll();
|
|
||||||
_connectivitySubscription?.cancel();
|
|
||||||
_connectivitySubscription = null;
|
|
||||||
_activeCheck = null;
|
|
||||||
if (!_connectivityController.isClosed) {
|
|
||||||
_connectivityController.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool?> _probeActiveServer() async {
|
|
||||||
final baseUri = _resolveBaseUri();
|
|
||||||
if (baseUri == null) return null;
|
|
||||||
|
|
||||||
return _probeBaseEndpoint(baseUri, updateLatency: true);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool?> _probeAnyKnownServer() async {
|
|
||||||
try {
|
|
||||||
final configs = await _ref.read(serverConfigsProvider.future);
|
|
||||||
for (final config in configs) {
|
|
||||||
final uri = _buildBaseUri(config.url);
|
|
||||||
if (uri == null) continue;
|
|
||||||
final result = await _probeBaseEndpoint(uri);
|
|
||||||
if (result != null) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (_) {}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool?> _probeBaseEndpoint(
|
|
||||||
Uri baseUri, {
|
|
||||||
bool updateLatency = false,
|
|
||||||
}) async {
|
|
||||||
try {
|
try {
|
||||||
final start = DateTime.now();
|
final start = DateTime.now();
|
||||||
final healthUri = baseUri.resolve('/health');
|
final healthUri = baseUri.resolve('/health');
|
||||||
|
|
||||||
final response = await _dio
|
final response = await _dio
|
||||||
.getUri(
|
.getUri(
|
||||||
healthUri,
|
healthUri,
|
||||||
options: Options(
|
options: Options(
|
||||||
method: 'GET',
|
sendTimeout: const Duration(seconds: 2),
|
||||||
sendTimeout: const Duration(seconds: 3),
|
receiveTimeout: const Duration(seconds: 2),
|
||||||
receiveTimeout: const Duration(seconds: 3),
|
|
||||||
followRedirects: false,
|
followRedirects: false,
|
||||||
validateStatus: (status) => status != null && status < 500,
|
validateStatus: (status) => status != null && status < 500,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.timeout(const Duration(seconds: 4));
|
.timeout(const Duration(seconds: 3));
|
||||||
|
|
||||||
final isHealthy = response.statusCode == 200;
|
final isHealthy = response.statusCode == 200;
|
||||||
if (isHealthy && updateLatency) {
|
|
||||||
|
if (isHealthy) {
|
||||||
_lastLatencyMs = DateTime.now().difference(start).inMilliseconds;
|
_lastLatencyMs = DateTime.now().difference(start).inMilliseconds;
|
||||||
|
} else {
|
||||||
|
_lastLatencyMs = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return isHealthy;
|
return isHealthy;
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
|
_lastLatencyMs = -1;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Uri? _resolveBaseUri() {
|
Uri? _getServerUri() {
|
||||||
final api = _ref.read(apiServiceProvider);
|
final api = _ref.read(apiServiceProvider);
|
||||||
if (api != null) {
|
if (api != null) {
|
||||||
return _buildBaseUri(api.baseUrl);
|
return _parseUri(api.baseUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
final activeServer = _ref.read(activeServerProvider);
|
final activeServer = _ref.read(activeServerProvider);
|
||||||
return activeServer.maybeWhen(
|
return activeServer.maybeWhen(
|
||||||
data: (server) => server != null ? _buildBaseUri(server.url) : null,
|
data: (server) => server != null ? _parseUri(server.url) : null,
|
||||||
orElse: () => null,
|
orElse: () => null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Uri? _buildBaseUri(String baseUrl) {
|
Uri? _parseUri(String url) {
|
||||||
if (baseUrl.isEmpty) return null;
|
if (url.isEmpty) return null;
|
||||||
|
|
||||||
Uri? parsed = Uri.tryParse(baseUrl.trim());
|
Uri? parsed = Uri.tryParse(url.trim());
|
||||||
|
if (parsed == null) return null;
|
||||||
|
|
||||||
|
if (!parsed.hasScheme) {
|
||||||
|
parsed = Uri.tryParse('https://$url') ?? Uri.tryParse('http://$url');
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _updateStatus(ConnectivityStatus newStatus) {
|
||||||
|
if (_currentStatus != newStatus && !_statusController.isClosed) {
|
||||||
|
_currentStatus = newStatus;
|
||||||
|
_statusController.add(newStatus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _cancelNoNetworkGrace() {
|
||||||
|
_noNetworkGraceTimer?.cancel();
|
||||||
|
_noNetworkGraceTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Manually trigger a connectivity check.
|
||||||
|
Future<bool> checkNow() async {
|
||||||
|
await _checkServerHealth();
|
||||||
|
return _currentStatus == ConnectivityStatus.online;
|
||||||
|
}
|
||||||
|
|
||||||
|
void dispose() {
|
||||||
|
_stopPolling();
|
||||||
|
_connectivitySubscription?.cancel();
|
||||||
|
_connectivitySubscription = null;
|
||||||
|
_cancelNoNetworkGrace();
|
||||||
|
|
||||||
|
if (!_statusController.isClosed) {
|
||||||
|
_statusController.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configures the Dio instance to accept self-signed certificates.
|
||||||
|
static void configureSelfSignedCerts(Dio dio, String serverUrl) {
|
||||||
|
if (kIsWeb) return;
|
||||||
|
|
||||||
|
final uri = _parseStaticUri(serverUrl);
|
||||||
|
if (uri == null) return;
|
||||||
|
|
||||||
|
final adapter = dio.httpClientAdapter;
|
||||||
|
if (adapter is! IOHttpClientAdapter) return;
|
||||||
|
|
||||||
|
adapter.createHttpClient = () {
|
||||||
|
final client = HttpClient();
|
||||||
|
final host = uri.host.toLowerCase();
|
||||||
|
final port = uri.hasPort ? uri.port : null;
|
||||||
|
|
||||||
|
client.badCertificateCallback =
|
||||||
|
(X509Certificate cert, String requestHost, int requestPort) {
|
||||||
|
if (requestHost.toLowerCase() != host) return false;
|
||||||
|
if (port == null) return true;
|
||||||
|
return requestPort == port;
|
||||||
|
};
|
||||||
|
|
||||||
|
return client;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static Uri? _parseStaticUri(String url) {
|
||||||
|
if (url.trim().isEmpty) return null;
|
||||||
|
|
||||||
|
Uri? parsed = Uri.tryParse(url.trim());
|
||||||
if (parsed == null) return null;
|
if (parsed == null) return null;
|
||||||
|
|
||||||
if (!parsed.hasScheme) {
|
if (!parsed.hasScheme) {
|
||||||
parsed =
|
parsed =
|
||||||
Uri.tryParse('https://$baseUrl') ?? Uri.tryParse('http://$baseUrl');
|
Uri.tryParse('https://${url.trim()}') ??
|
||||||
|
Uri.tryParse('http://${url.trim()}');
|
||||||
}
|
}
|
||||||
if (parsed == null) return null;
|
|
||||||
|
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provider for the connectivity service
|
||||||
final connectivityServiceProvider = Provider<ConnectivityService>((ref) {
|
final connectivityServiceProvider = Provider<ConnectivityService>((ref) {
|
||||||
final activeServer = ref.watch(activeServerProvider);
|
final activeServer = ref.watch(activeServerProvider);
|
||||||
|
|
||||||
@@ -340,7 +298,7 @@ final connectivityServiceProvider = Provider<ConnectivityService>((ref) {
|
|||||||
if (server == null) {
|
if (server == null) {
|
||||||
final dio = Dio();
|
final dio = Dio();
|
||||||
final service = ConnectivityService(dio, ref);
|
final service = ConnectivityService(dio, ref);
|
||||||
ref.onDispose(() => service.dispose());
|
ref.onDispose(service.dispose);
|
||||||
return service;
|
return service;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -358,32 +316,36 @@ final connectivityServiceProvider = Provider<ConnectivityService>((ref) {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (server.allowSelfSignedCertificates) {
|
||||||
|
ConnectivityService.configureSelfSignedCerts(dio, server.url);
|
||||||
|
}
|
||||||
|
|
||||||
final service = ConnectivityService(dio, ref);
|
final service = ConnectivityService(dio, ref);
|
||||||
ref.onDispose(() => service.dispose());
|
ref.onDispose(service.dispose);
|
||||||
return service;
|
return service;
|
||||||
},
|
},
|
||||||
orElse: () {
|
orElse: () {
|
||||||
final dio = Dio();
|
final dio = Dio();
|
||||||
final service = ConnectivityService(dio, ref);
|
final service = ConnectivityService(dio, ref);
|
||||||
ref.onDispose(() => service.dispose());
|
ref.onDispose(service.dispose);
|
||||||
return service;
|
return service;
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Riverpod notifier for connectivity status
|
||||||
@Riverpod(keepAlive: true)
|
@Riverpod(keepAlive: true)
|
||||||
class ConnectivityStatusNotifier extends _$ConnectivityStatusNotifier {
|
class ConnectivityStatusNotifier extends _$ConnectivityStatusNotifier {
|
||||||
StreamSubscription<ConnectivityStatus>? _subscription;
|
StreamSubscription<ConnectivityStatus>? _subscription;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
FutureOr<ConnectivityStatus> build() {
|
ConnectivityStatus build() {
|
||||||
final service = ref.watch(connectivityServiceProvider);
|
final service = ref.watch(connectivityServiceProvider);
|
||||||
|
|
||||||
_subscription?.cancel();
|
_subscription?.cancel();
|
||||||
_subscription = service.connectivityStream.listen(
|
_subscription = service.statusStream.listen(
|
||||||
(status) => state = AsyncValue.data(status),
|
(status) => state = status,
|
||||||
onError: (error, stackTrace) =>
|
onError: (_, _) {}, // Ignore errors, keep current state
|
||||||
state = AsyncValue.error(error, stackTrace),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
ref.onDispose(() {
|
ref.onDispose(() {
|
||||||
@@ -391,17 +353,16 @@ class ConnectivityStatusNotifier extends _$ConnectivityStatusNotifier {
|
|||||||
_subscription = null;
|
_subscription = null;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Return current status immediately (starts as online)
|
||||||
return service.currentStatus;
|
return service.currentStatus;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Simple provider for checking if online
|
||||||
final isOnlineProvider = Provider<bool>((ref) {
|
final isOnlineProvider = Provider<bool>((ref) {
|
||||||
final reviewerMode = ref.watch(reviewerModeProvider);
|
final reviewerMode = ref.watch(reviewerModeProvider);
|
||||||
if (reviewerMode) return true;
|
if (reviewerMode) return true;
|
||||||
|
|
||||||
final status = ref.watch(connectivityStatusProvider);
|
final status = ref.watch(connectivityStatusProvider);
|
||||||
return status.when(
|
return status == ConnectivityStatus.online;
|
||||||
data: (status) => status != ConnectivityStatus.offline,
|
|
||||||
loading: () => true,
|
|
||||||
error: (error, _) => true,
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -74,9 +74,9 @@ class PersistentStreamingService with WidgetsBindingObserver {
|
|||||||
|
|
||||||
_connectivitySubscription?.cancel();
|
_connectivitySubscription?.cancel();
|
||||||
_connectivityService = service;
|
_connectivityService = service;
|
||||||
_connectivitySubscription = service.isConnected.listen(
|
_connectivitySubscription = service.statusStream
|
||||||
_handleConnectivityChange,
|
.map((status) => status == ConnectivityStatus.online)
|
||||||
);
|
.listen(_handleConnectivityChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _handleConnectivityChange(bool connected) {
|
void _handleConnectivityChange(bool connected) {
|
||||||
|
|||||||
@@ -30,8 +30,7 @@ class _ConnectionIssuePageState extends ConsumerState<ConnectionIssuePage> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final l10n = AppLocalizations.of(context)!;
|
final l10n = AppLocalizations.of(context)!;
|
||||||
final connectivityAsync = ref.watch(connectivityStatusProvider);
|
final connectivity = ref.watch(connectivityStatusProvider);
|
||||||
final connectivity = connectivityAsync.asData?.value;
|
|
||||||
final activeServerAsync = ref.watch(activeServerProvider);
|
final activeServerAsync = ref.watch(activeServerProvider);
|
||||||
final activeServer = activeServerAsync.asData?.value;
|
final activeServer = activeServerAsync.asData?.value;
|
||||||
|
|
||||||
@@ -253,14 +252,12 @@ class _ConnectionIssuePageState extends ConsumerState<ConnectionIssuePage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String? _statusLabel(ConnectivityStatus? status, AppLocalizations l10n) {
|
String? _statusLabel(ConnectivityStatus? status, AppLocalizations l10n) {
|
||||||
|
if (status == null) return null;
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case ConnectivityStatus.online:
|
case ConnectivityStatus.online:
|
||||||
return l10n.connectedToServer;
|
return l10n.connectedToServer;
|
||||||
case ConnectivityStatus.offline:
|
case ConnectivityStatus.offline:
|
||||||
return l10n.pleaseCheckConnection;
|
return l10n.pleaseCheckConnection;
|
||||||
case ConnectivityStatus.checking:
|
|
||||||
case null:
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,9 +28,8 @@ class OfflineIndicator extends ConsumerWidget {
|
|||||||
orElse: () => false,
|
orElse: () => false,
|
||||||
);
|
);
|
||||||
|
|
||||||
final overlay = connectivityStatus.when(
|
final overlay = () {
|
||||||
data: (status) {
|
if ((connectivityStatus == ConnectivityStatus.offline || socketOffline) &&
|
||||||
if ((status == ConnectivityStatus.offline || socketOffline) &&
|
|
||||||
!wasOffline) {
|
!wasOffline) {
|
||||||
return const SizedBox.shrink();
|
return const SizedBox.shrink();
|
||||||
}
|
}
|
||||||
@@ -38,10 +37,7 @@ class OfflineIndicator extends ConsumerWidget {
|
|||||||
return const _BackOnlineToast();
|
return const _BackOnlineToast();
|
||||||
}
|
}
|
||||||
return const SizedBox.shrink();
|
return const SizedBox.shrink();
|
||||||
},
|
}();
|
||||||
loading: () => const SizedBox.shrink(),
|
|
||||||
error: (unusedError, unusedStackTrace) => const SizedBox.shrink(),
|
|
||||||
);
|
|
||||||
|
|
||||||
return Stack(children: [child, overlay]);
|
return Stack(children: [child, overlay]);
|
||||||
}
|
}
|
||||||
@@ -53,22 +49,16 @@ class _WasOffline extends _$WasOffline {
|
|||||||
@override
|
@override
|
||||||
bool build() {
|
bool build() {
|
||||||
// Initialize based on current connectivity (assume online until proven otherwise)
|
// Initialize based on current connectivity (assume online until proven otherwise)
|
||||||
ref.listen<AsyncValue<ConnectivityStatus>>(connectivityStatusProvider, (
|
ref.listen<ConnectivityStatus>(connectivityStatusProvider, (
|
||||||
prev,
|
prev,
|
||||||
next,
|
next,
|
||||||
) {
|
) {
|
||||||
next.when(
|
if (next == ConnectivityStatus.offline) {
|
||||||
data: (status) {
|
|
||||||
if (status == ConnectivityStatus.offline) {
|
|
||||||
state = true; // mark that we have been offline
|
state = true; // mark that we have been offline
|
||||||
} else if (status == ConnectivityStatus.online && state) {
|
} else if (next == ConnectivityStatus.online && state) {
|
||||||
// After we emit the toast once, clear flag shortly after
|
// After we emit the toast once, clear flag shortly after
|
||||||
Future.microtask(() => state = false);
|
Future.microtask(() => state = false);
|
||||||
}
|
}
|
||||||
},
|
|
||||||
loading: () {},
|
|
||||||
error: (error, stackTrace) {},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user