CakeWallet/lib/reactions/check_connection.dart

39 lines
1.1 KiB
Dart
Raw Normal View History

2020-09-21 14:50:26 +03:00
import 'dart:async';
2021-12-24 14:37:24 +02:00
import 'package:cw_core/wallet_base.dart';
import 'package:cw_core/sync_status.dart';
2020-09-21 14:50:26 +03:00
import 'package:cake_wallet/store/settings_store.dart';
import 'package:connectivity/connectivity.dart';
2022-10-12 13:09:57 -04:00
Timer? _checkConnectionTimer;
2020-09-21 14:50:26 +03:00
void startCheckConnectionReaction(
WalletBase wallet, SettingsStore settingsStore,
{int timeInterval = 5}) {
2020-09-21 14:50:26 +03:00
_checkConnectionTimer?.cancel();
_checkConnectionTimer =
Timer.periodic(Duration(seconds: timeInterval), (_) async {
try {
final connectivityResult = await (Connectivity().checkConnectivity());
2020-09-21 14:50:26 +03:00
if (connectivityResult == ConnectivityResult.none) {
wallet.syncStatus = FailedSyncStatus();
return;
}
2020-09-21 14:50:26 +03:00
if (wallet.syncStatus is LostConnectionSyncStatus ||
wallet.syncStatus is FailedSyncStatus) {
2020-09-21 14:50:26 +03:00
final alive =
await settingsStore.getCurrentNode(wallet.type).requestNode();
if (alive) {
await wallet.connectToNode(
node: settingsStore.getCurrentNode(wallet.type));
}
}
} catch (e) {
print(e.toString());
2020-09-21 14:50:26 +03:00
}
});
}