dev
dart 63 lines 1.57 KB
Raw
1 import 'package:mobx/mobx.dart';
2 import 'package:cake_wallet/core/execution_state.dart';
3 import 'package:cake_wallet/core/wallet_loading_service.dart';
4 import 'package:cake_wallet/store/app_store.dart';
5 import 'package:cw_core/wallet_type.dart';
6 import 'package:cake_wallet/view_model/wallet_unlock_view_model.dart';
7
8 part 'wallet_unlock_loadable_view_model.g.dart';
9
10 class WalletUnlockLoadableViewModel = WalletUnlockLoadableViewModelBase
11 with _$WalletUnlockLoadableViewModel;
12
13 abstract class WalletUnlockLoadableViewModelBase extends WalletUnlockViewModel with Store {
14 WalletUnlockLoadableViewModelBase(this._appStore, this._walletLoadingService,
15 {required this.walletName, required this.walletType})
16 : password = '',
17 state = InitialExecutionState();
18
19 final String walletName;
20
21 final WalletType walletType;
22
23 @override
24 @observable
25 String password;
26
27 @override
28 @observable
29 ExecutionState state;
30
31 final WalletLoadingService _walletLoadingService;
32
33 final AppStore _appStore;
34
35 @override
36 @action
37 void setPassword(String password) => this.password = password;
38
39 @override
40 @action
41 Future<void> unlock() async {
42 try {
43 state = InitialExecutionState();
44 final wallet = await _walletLoadingService.load(walletType, walletName, password: password);
45 _appStore.changeCurrentWallet(wallet);
46 success();
47 } catch (e) {
48 failure(e.toString());
49 }
50 }
51
52 @override
53 @action
54 void success() {
55 state = ExecutedSuccessfullyState();
56 }
57
58 @override
59 @action
60 void failure(e) {
61 state = FailureState(e.toString());
62 }
63 }