| 1 | import "package:cake_wallet/core/amount_parsing_proxy.dart"; |
| 2 | import "package:cake_wallet/di.dart"; |
| 3 | import "package:cake_wallet/entities/preferences_key.dart"; |
| 4 | import "package:cake_wallet/reactions/wallet_connect.dart"; |
| 5 | import "package:cake_wallet/src/screens/wallet_connect/services/walletkit_service.dart"; |
| 6 | import "package:cake_wallet/store/authentication_store.dart"; |
| 7 | import "package:cake_wallet/store/settings_store.dart"; |
| 8 | import "package:cake_wallet/store/wallet_list_store.dart"; |
| 9 | import "package:cake_wallet/themes/core/theme_store.dart"; |
| 10 | import "package:cake_wallet/utils/exception_handler.dart"; |
| 11 | import "package:cw_core/balance.dart"; |
| 12 | import "package:cw_core/transaction_history.dart"; |
| 13 | import "package:cw_core/transaction_info.dart"; |
| 14 | import "package:cw_core/utils/print_verbose.dart"; |
| 15 | import "package:cw_core/wallet_base.dart"; |
| 16 | import "package:cw_core/wallet_type.dart"; |
| 17 | import "package:mobx/mobx.dart"; |
| 18 | import "package:shared_preferences/shared_preferences.dart"; |
| 19 | |
| 20 | part "app_store.g.dart"; |
| 21 | |
| 22 | class AppStore = AppStoreBase with _$AppStore; |
| 23 | |
| 24 | abstract class AppStoreBase with Store { |
| 25 | AppStoreBase({ |
| 26 | required this.authenticationStore, |
| 27 | required this.walletList, |
| 28 | required this.settingsStore, |
| 29 | required this.themeStore, |
| 30 | }) : _amountParsingProxy = AmountParsingProxy(settingsStore.displayAmountsInSatoshi) { |
| 31 | reaction( |
| 32 | (_) => settingsStore.displayAmountsInSatoshi, |
| 33 | (value) => _amountParsingProxy = AmountParsingProxy(value), |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | AuthenticationStore authenticationStore; |
| 38 | |
| 39 | @observable |
| 40 | WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>? wallet; |
| 41 | |
| 42 | @observable |
| 43 | String? currentRouteName; |
| 44 | |
| 45 | WalletListStore walletList; |
| 46 | |
| 47 | SettingsStore settingsStore; |
| 48 | |
| 49 | ThemeStore themeStore; |
| 50 | |
| 51 | @observable |
| 52 | AmountParsingProxy _amountParsingProxy; |
| 53 | |
| 54 | @computed |
| 55 | AmountParsingProxy get amountParsingProxy => _amountParsingProxy; |
| 56 | |
| 57 | @action |
| 58 | Future<void> changeCurrentWallet( |
| 59 | WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> wallet) async { |
| 60 | final changingToSameWalletType = this.wallet?.type == wallet.type; |
| 61 | final previousWalletType = this.wallet?.type; |
| 62 | |
| 63 | await this.wallet?.close(shouldCleanup: !changingToSameWalletType); |
| 64 | this.wallet = wallet; |
| 65 | this.wallet!.setExceptionHandler(ExceptionHandler.onError); |
| 66 | |
| 67 | if (isWalletConnectCompatibleChain(wallet.type)) { |
| 68 | getIt.get<WalletKitService>().resetConnectionsState(); |
| 69 | _queueWalletConnectAction(_setupWalletConnect); |
| 70 | } else if (previousWalletType != null && isWalletConnectCompatibleChain(previousWalletType)) { |
| 71 | _queueWalletConnectAction(_disposeWalletConnect); |
| 72 | } |
| 73 | await getIt.get<SharedPreferences>().setString(PreferencesKey.currentWalletName, wallet.name); |
| 74 | await getIt |
| 75 | .get<SharedPreferences>() |
| 76 | .setInt(PreferencesKey.currentWalletType, serializeToInt(wallet.type)); |
| 77 | } |
| 78 | |
| 79 | Future<void> _lastWalletConnectAction = Future.value(); |
| 80 | |
| 81 | void _queueWalletConnectAction(Future<void> Function() action) { |
| 82 | _lastWalletConnectAction = _lastWalletConnectAction.then((_) => action()); |
| 83 | } |
| 84 | |
| 85 | Future<void> _setupWalletConnect() async { |
| 86 | try { |
| 87 | final wcService = getIt.get<WalletKitService>(); |
| 88 | await wcService.onDispose(); |
| 89 | wcService.create(); |
| 90 | await wcService.init(); |
| 91 | } catch (e, s) { |
| 92 | printV("WalletConnect setup failed: $e\n$s"); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | Future<void> _disposeWalletConnect() async { |
| 97 | try { |
| 98 | await getIt.get<WalletKitService>().onDispose(); |
| 99 | } catch (e) { |
| 100 | printV("WalletConnect teardown failed: $e"); |
| 101 | } |
| 102 | } |
| 103 | } |