dev
dart 67 lines 1.91 KB
Raw
1 import 'dart:async';
2
3 import 'package:cake_wallet/reactions/wallet_connect.dart';
4 import 'package:cake_wallet/store/app_store.dart';
5 import 'package:cake_wallet/core/wallet_loading_service.dart';
6 import 'package:cw_core/wallet_base.dart';
7 import 'package:cw_core/wallet_info.dart';
8 import 'package:cw_core/wallet_type.dart';
9 import 'package:cw_core/utils/print_verbose.dart';
10 import 'package:mobx/mobx.dart';
11
12 part 'wallet_switcher_view_model.g.dart';
13
14 class WalletSwitcherViewModel = WalletSwitcherViewModelBase with _$WalletSwitcherViewModel;
15
16 abstract class WalletSwitcherViewModelBase with Store {
17 WalletSwitcherViewModelBase({
18 required this.appStore,
19 required this.walletLoadingService,
20 });
21
22 final AppStore appStore;
23 final WalletLoadingService walletLoadingService;
24
25 @observable
26 WalletInfo? selectedWallet;
27
28 @observable
29 bool isProcessing = false;
30
31 @action
32 Future<List<WalletInfo>> getWallets(WalletType? walletType) async {
33 final wiList = await WalletInfo.getAll();
34 if (walletType == null) return wiList;
35
36 // For EVM-compatible wallet types, show all EVM-compatible wallets
37 // This allows users to switch between any EVM wallet regardless of the specific chain
38 if (isEVMCompatibleChain(walletType)) {
39 return wiList.where((wallet) => isEVMCompatibleChain(wallet.type)).toList();
40 }
41
42 return wiList.where((wallet) => wallet.type == walletType).toList();
43 }
44
45 @action
46 void selectWallet(WalletInfo walletInfo) => selectedWallet = walletInfo;
47
48 @action
49 Future<bool> switchToSelectedWallet() async {
50 if (selectedWallet == null) return false;
51
52 try {
53 isProcessing = true;
54
55 final wallet = await walletLoadingService.load(selectedWallet!.type, selectedWallet!.name);
56
57 await appStore.changeCurrentWallet(wallet);
58
59 return true;
60 } catch (e) {
61 printV('Failed to switch wallet: $e');
62 return false;
63 } finally {
64 isProcessing = false;
65 }
66 }
67 }