2025-08-10 01:20:45 +05:30
|
|
|
import 'dart:async';
|
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import '../providers/app_providers.dart';
|
|
|
|
|
|
|
|
|
|
enum ConnectivityStatus { online, offline, checking }
|
|
|
|
|
|
|
|
|
|
class ConnectivityService {
|
|
|
|
|
final Dio _dio;
|
2025-09-24 10:52:15 +05:30
|
|
|
final Ref _ref;
|
2025-08-10 01:20:45 +05:30
|
|
|
Timer? _connectivityTimer;
|
|
|
|
|
final _connectivityController =
|
|
|
|
|
StreamController<ConnectivityStatus>.broadcast();
|
|
|
|
|
ConnectivityStatus _lastStatus = ConnectivityStatus.checking;
|
2025-09-23 13:43:01 +05:30
|
|
|
int _recentFailures = 0;
|
|
|
|
|
Duration _interval = const Duration(seconds: 10);
|
|
|
|
|
int _lastLatencyMs = -1;
|
2025-08-10 01:20:45 +05:30
|
|
|
|
2025-09-24 10:52:15 +05:30
|
|
|
ConnectivityService(this._dio, this._ref) {
|
2025-08-10 01:20:45 +05:30
|
|
|
_startConnectivityMonitoring();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<ConnectivityStatus> get connectivityStream =>
|
|
|
|
|
_connectivityController.stream;
|
|
|
|
|
ConnectivityStatus get currentStatus => _lastStatus;
|
2025-09-23 13:43:01 +05:30
|
|
|
int get lastLatencyMs => _lastLatencyMs;
|
2025-09-13 10:16:58 +05:30
|
|
|
|
2025-08-16 20:27:44 +05:30
|
|
|
/// Stream that emits true when connected, false when offline
|
2025-09-13 10:16:58 +05:30
|
|
|
Stream<bool> get isConnected =>
|
|
|
|
|
connectivityStream.map((status) => status == ConnectivityStatus.online);
|
|
|
|
|
|
2025-08-16 20:27:44 +05:30
|
|
|
/// Check if currently connected
|
|
|
|
|
bool get isCurrentlyConnected => _lastStatus == ConnectivityStatus.online;
|
2025-08-10 01:20:45 +05:30
|
|
|
|
|
|
|
|
void _startConnectivityMonitoring() {
|
|
|
|
|
// Initial check after a brief delay to avoid showing offline during startup
|
2025-09-23 13:43:01 +05:30
|
|
|
Timer(const Duration(milliseconds: 800), () {
|
2025-08-10 01:20:45 +05:30
|
|
|
_checkConnectivity();
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-23 13:43:01 +05:30
|
|
|
// Check periodically; interval adapts to recent failures
|
|
|
|
|
_connectivityTimer = Timer.periodic(_interval, (_) {
|
2025-08-10 01:20:45 +05:30
|
|
|
_checkConnectivity();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _checkConnectivity() async {
|
2025-09-24 10:52:15 +05:30
|
|
|
final serverReachability = await _probeActiveServer();
|
|
|
|
|
if (serverReachability != null) {
|
|
|
|
|
if (serverReachability) {
|
2025-08-10 01:20:45 +05:30
|
|
|
_updateStatus(ConnectivityStatus.online);
|
2025-09-24 10:52:15 +05:30
|
|
|
} else {
|
|
|
|
|
_lastLatencyMs = -1;
|
|
|
|
|
_updateStatus(ConnectivityStatus.offline);
|
2025-08-10 01:20:45 +05:30
|
|
|
}
|
2025-09-24 10:52:15 +05:30
|
|
|
return;
|
2025-08-10 01:20:45 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-24 10:52:15 +05:30
|
|
|
final fallbackReachability = await _probeAnyKnownServer();
|
|
|
|
|
if (fallbackReachability != null) {
|
|
|
|
|
if (fallbackReachability) {
|
|
|
|
|
_updateStatus(ConnectivityStatus.online);
|
|
|
|
|
} else {
|
|
|
|
|
_lastLatencyMs = -1;
|
|
|
|
|
_updateStatus(ConnectivityStatus.offline);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2025-08-10 01:20:45 +05:30
|
|
|
}
|
2025-09-24 10:52:15 +05:30
|
|
|
|
|
|
|
|
// No configured server to probe; assume usable connectivity so setup flows continue.
|
|
|
|
|
_lastLatencyMs = -1;
|
|
|
|
|
_updateStatus(ConnectivityStatus.online);
|
2025-08-10 01:20:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _updateStatus(ConnectivityStatus status) {
|
|
|
|
|
if (_lastStatus != status) {
|
|
|
|
|
_lastStatus = status;
|
|
|
|
|
_connectivityController.add(status);
|
|
|
|
|
}
|
2025-09-23 13:43:01 +05:30
|
|
|
|
|
|
|
|
// Adapt polling interval based on recent failures to reduce battery/CPU
|
|
|
|
|
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;
|
|
|
|
|
_connectivityTimer?.cancel();
|
|
|
|
|
_connectivityTimer = Timer.periodic(
|
|
|
|
|
_interval,
|
|
|
|
|
(_) => _checkConnectivity(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-08-10 01:20:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<bool> checkConnectivity() async {
|
|
|
|
|
await _checkConnectivity();
|
|
|
|
|
return _lastStatus == ConnectivityStatus.online;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dispose() {
|
|
|
|
|
_connectivityTimer?.cancel();
|
|
|
|
|
_connectivityController.close();
|
|
|
|
|
}
|
2025-09-24 10:52:15 +05:30
|
|
|
|
|
|
|
|
Future<bool?> _probeActiveServer() async {
|
|
|
|
|
final healthUri = _resolveHealthUri();
|
|
|
|
|
if (healthUri == null) return null;
|
|
|
|
|
|
|
|
|
|
return _probeHealthEndpoint(healthUri, updateLatency: true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<bool?> _probeAnyKnownServer() async {
|
|
|
|
|
try {
|
|
|
|
|
final configs = await _ref.read(serverConfigsProvider.future);
|
|
|
|
|
for (final config in configs) {
|
|
|
|
|
final uri = _buildHealthUri(config.url);
|
|
|
|
|
if (uri == null) continue;
|
|
|
|
|
final result = await _probeHealthEndpoint(uri);
|
|
|
|
|
if (result != null) {
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<bool?> _probeHealthEndpoint(
|
|
|
|
|
Uri uri, {
|
|
|
|
|
bool updateLatency = false,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
final start = DateTime.now();
|
|
|
|
|
final response = await _dio
|
|
|
|
|
.getUri(
|
|
|
|
|
uri,
|
|
|
|
|
options: Options(
|
|
|
|
|
method: 'GET',
|
|
|
|
|
sendTimeout: const Duration(seconds: 3),
|
|
|
|
|
receiveTimeout: const Duration(seconds: 3),
|
|
|
|
|
followRedirects: false,
|
|
|
|
|
validateStatus: (status) => status != null && status < 500,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.timeout(const Duration(seconds: 4));
|
|
|
|
|
|
|
|
|
|
final isHealthy =
|
|
|
|
|
response.statusCode == 200 && _responseIndicatesHealth(response.data);
|
|
|
|
|
if (isHealthy && updateLatency) {
|
|
|
|
|
_lastLatencyMs = DateTime.now().difference(start).inMilliseconds;
|
|
|
|
|
}
|
|
|
|
|
return isHealthy;
|
|
|
|
|
} catch (_) {
|
|
|
|
|
// Treat as unreachable.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uri? _resolveHealthUri() {
|
|
|
|
|
final api = _ref.read(apiServiceProvider);
|
|
|
|
|
if (api != null) {
|
|
|
|
|
return _buildHealthUri(api.baseUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final activeServer = _ref.read(activeServerProvider);
|
|
|
|
|
return activeServer.maybeWhen(
|
|
|
|
|
data: (server) => server != null ? _buildHealthUri(server.url) : null,
|
|
|
|
|
orElse: () => null,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uri? _buildHealthUri(String baseUrl) {
|
|
|
|
|
if (baseUrl.isEmpty) return null;
|
|
|
|
|
|
|
|
|
|
Uri? parsed = Uri.tryParse(baseUrl.trim());
|
|
|
|
|
if (parsed == null) return null;
|
|
|
|
|
|
|
|
|
|
if (!parsed.hasScheme) {
|
|
|
|
|
parsed =
|
|
|
|
|
Uri.tryParse('https://$baseUrl') ?? Uri.tryParse('http://$baseUrl');
|
|
|
|
|
}
|
|
|
|
|
if (parsed == null) return null;
|
|
|
|
|
|
|
|
|
|
return parsed.resolve('health');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool _responseIndicatesHealth(dynamic data) {
|
|
|
|
|
if (data is Map) {
|
|
|
|
|
final dynamic status = data['status'];
|
|
|
|
|
if (status is bool) return status;
|
|
|
|
|
if (status is num) return status != 0;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-08-10 01:20:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Providers
|
|
|
|
|
final connectivityServiceProvider = Provider<ConnectivityService>((ref) {
|
2025-09-13 10:16:58 +05:30
|
|
|
// Use a lightweight Dio instance only for connectivity checks
|
|
|
|
|
final dio = Dio();
|
2025-09-24 10:52:15 +05:30
|
|
|
final service = ConnectivityService(dio, ref);
|
2025-08-10 01:20:45 +05:30
|
|
|
ref.onDispose(() => service.dispose());
|
|
|
|
|
return service;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final connectivityStatusProvider = StreamProvider<ConnectivityStatus>((ref) {
|
|
|
|
|
final service = ref.watch(connectivityServiceProvider);
|
|
|
|
|
return service.connectivityStream;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final isOnlineProvider = Provider<bool>((ref) {
|
|
|
|
|
// In reviewer mode, treat app as online to enable flows
|
|
|
|
|
final reviewerMode = ref.watch(reviewerModeProvider);
|
|
|
|
|
if (reviewerMode) return true;
|
|
|
|
|
final status = ref.watch(connectivityStatusProvider);
|
|
|
|
|
return status.when(
|
|
|
|
|
data: (status) => status == ConnectivityStatus.online,
|
|
|
|
|
loading: () => true, // Assume online while checking
|
|
|
|
|
error: (_, _) =>
|
|
|
|
|
true, // Assume online on error to avoid false offline states
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Dio provider (if not already defined elsewhere)
|
2025-09-13 10:16:58 +05:30
|
|
|
// Removed unused Dio provider to avoid confusion. Use ApiService instead.
|