| 1 | import 'package:cake_wallet/bitcoin/bitcoin.dart'; |
| 2 | import 'package:cake_wallet/store/settings_store.dart'; |
| 3 | import 'package:cake_wallet/di.dart'; |
| 4 | import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart'; |
| 5 | import 'package:cw_core/utils/print_verbose.dart'; |
| 6 | import 'package:cw_core/wallet_type.dart'; |
| 7 | import 'package:mobx/mobx.dart'; |
| 8 | import 'package:cw_core/transaction_history.dart'; |
| 9 | import 'package:cw_core/wallet_base.dart'; |
| 10 | import 'package:cw_core/balance.dart'; |
| 11 | import 'package:cw_core/transaction_info.dart'; |
| 12 | import 'package:cw_core/sync_status.dart'; |
| 13 | import 'package:wakelock_plus/wakelock_plus.dart'; |
| 14 | |
| 15 | ReactionDisposer? _onWalletSyncStatusChangeReaction; |
| 16 | |
| 17 | void startWalletSyncStatusChangeReaction( |
| 18 | WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> wallet, |
| 19 | SettingsStore settingsStore) { |
| 20 | _onWalletSyncStatusChangeReaction?.reaction.dispose(); |
| 21 | _onWalletSyncStatusChangeReaction = reaction((_) => wallet.syncStatus, (SyncStatus status) async { |
| 22 | try { |
| 23 | if (status is ConnectedSyncStatus) { |
| 24 | SyncingSyncStatus.resetSyncStartTime(); |
| 25 | await wallet.startSync(); |
| 26 | } |
| 27 | |
| 28 | if (status is SyncingSyncStatus || status is ProcessingSyncStatus) { |
| 29 | await WakelockPlus.enable(); |
| 30 | } |
| 31 | |
| 32 | if (status is SyncedSyncStatus) { |
| 33 | await WakelockPlus.disable(); |
| 34 | SyncingSyncStatus.resetSyncStartTime(); |
| 35 | SyncingSyncStatus.blockHistory.clear(); |
| 36 | } |
| 37 | |
| 38 | if (status is FailedSyncStatus) { |
| 39 | await WakelockPlus.disable(); |
| 40 | SyncingSyncStatus.resetSyncStartTime(); |
| 41 | } |
| 42 | |
| 43 | if (status is SyncedSyncStatus && |
| 44 | wallet.type == WalletType.bitcoin && |
| 45 | settingsStore.usePayjoin) { |
| 46 | bitcoin!.resumePayjoinSessions(wallet); |
| 47 | } |
| 48 | } catch (e) { |
| 49 | printV(e.toString()); |
| 50 | } |
| 51 | }); |
| 52 | } |