dev
dart 63 lines 2.15 KB
Raw
1 import 'dart:async';
2
3 import 'package:cake_wallet/reactions/wallet_connect.dart';
4 import 'package:cake_wallet/utils/tor.dart';
5 import 'package:connectivity_plus/connectivity_plus.dart';
6 import 'package:cw_core/utils/print_verbose.dart';
7 import 'package:cw_core/wallet_base.dart';
8 import 'package:cw_core/sync_status.dart';
9 import 'package:cw_core/wallet_type.dart';
10 import 'package:cake_wallet/store/settings_store.dart';
11 import 'package:cake_wallet/evm/evm.dart';
12
13 Timer? _checkConnectionTimer;
14
15 void startCheckConnectionReaction(WalletBase wallet, SettingsStore settingsStore,
16 {int timeInterval = 5}) {
17 _checkConnectionTimer?.cancel();
18 // TODO: check the validity of this code, and if it's working fine, then no need for
19 // having the connect function in electrum.dart when the syncstatus is lost or failed and add the not connected state
20 _checkConnectionTimer = Timer.periodic(Duration(seconds: timeInterval), (_) async {
21 if (wallet.type == WalletType.bitcoin && wallet.syncStatus is SyncingSyncStatus) {
22 return;
23 }
24 if (wallet.type == WalletType.decred && wallet.syncStatus is ProcessingSyncStatus) {
25 return;
26 }
27
28 try {
29 final connectivityResult = await (Connectivity().checkConnectivity());
30
31 int? chainId;
32 if (isEVMCompatibleChain(wallet.type)) {
33 chainId = evm!.getSelectedChainId(wallet);
34 }
35
36 final node = settingsStore.getCurrentNode(wallet.type, chainId: chainId);
37
38 if (connectivityResult.contains(ConnectivityResult.none)) {
39 // Still try node even when OS says no network - it might be a LAN node.
40 if (!await node.requestNode()) {
41 wallet.syncStatus = FailedSyncStatus();
42 return;
43 }
44 }
45
46 if (wallet.type != WalletType.bitcoin &&
47 (wallet.syncStatus is LostConnectionSyncStatus ||
48 wallet.syncStatus is FailedSyncStatus)) {
49 final alive = await node.requestNode();
50
51 if (alive) {
52 if (settingsStore.currentBuiltinTor) {
53 await ensureTorStarted(context: null);
54 }
55
56 await wallet.connectToNode(node: node);
57 }
58 }
59 } catch (e) {
60 printV(e.toString());
61 }
62 });
63 }