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