detect network changes

This commit is contained in:
otsmr 2025-06-13 23:50:50 +02:00
parent 27cd92f17f
commit 88649c0dc0
2 changed files with 26 additions and 2 deletions

View file

@ -73,6 +73,7 @@ class _AppState extends State<App> with WidgetsBindingObserver {
Future initAsync() async { Future initAsync() async {
setUserPlan(); setUserPlan();
await apiService.connect(force: true); await apiService.connect(force: true);
apiService.listenToNetworkChanges();
// call this function so invalid media files are get purged // call this function so invalid media files are get purged
retryMediaUpload(true); retryMediaUpload(true);
} }

View file

@ -3,6 +3,7 @@ import 'dart:collection';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:math'; import 'dart:math';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:drift/drift.dart'; import 'package:drift/drift.dart';
import 'package:fixnum/fixnum.dart'; import 'package:fixnum/fixnum.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
@ -56,6 +57,7 @@ class ApiService {
final HashMap<Int64, server.ServerToClient?> messagesV0 = HashMap(); final HashMap<Int64, server.ServerToClient?> messagesV0 = HashMap();
IOWebSocketChannel? _channel; IOWebSocketChannel? _channel;
StreamSubscription<List<ConnectivityResult>>? connectivitySubscription;
Future<bool> _connectTo(String apiUrl) async { Future<bool> _connectTo(String apiUrl) async {
try { try {
@ -68,7 +70,11 @@ class ApiService {
Log.info("websocket connected to $apiUrl"); Log.info("websocket connected to $apiUrl");
return true; return true;
} on WebSocketChannelException catch (e) { } on WebSocketChannelException catch (e) {
if (!e.message
.toString()
.contains("No address associated with hostname")) {
Log.error("could not connect to api got: $e"); Log.error("could not connect to api got: $e");
}
return false; return false;
} }
} }
@ -102,8 +108,11 @@ class ApiService {
isAuthenticated = false; isAuthenticated = false;
globalCallbackConnectionState(false); globalCallbackConnectionState(false);
await twonlyDB.messagesDao.resetPendingDownloadState(); await twonlyDB.messagesDao.resetPendingDownloadState();
}
Future startReconnectionTimer() async {
reconnectionTimer?.cancel();
reconnectionTimer ??= Timer(Duration(seconds: _reconnectionDelay), () { reconnectionTimer ??= Timer(Duration(seconds: _reconnectionDelay), () {
Log.info("starting with reconnection.");
reconnectionTimer = null; reconnectionTimer = null;
connect(force: true); connect(force: true);
}); });
@ -121,6 +130,20 @@ class ApiService {
callback(); callback();
} }
Future listenToNetworkChanges() async {
if (connectivitySubscription != null) {
return;
}
connectivitySubscription = Connectivity()
.onConnectivityChanged
.listen((List<ConnectivityResult> result) {
if (!result.contains(ConnectivityResult.none)) {
connect(force: true);
}
// Received changes in available connectivity types!
});
}
Future<bool> connect({bool force = false}) async { Future<bool> connect({bool force = false}) async {
if (reconnectionTimer != null && !force) { if (reconnectionTimer != null && !force) {
return false; return false;