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 {
setUserPlan();
await apiService.connect(force: true);
apiService.listenToNetworkChanges();
// call this function so invalid media files are get purged
retryMediaUpload(true);
}

View file

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