dev
dart 73 lines 2.03 KB
Raw
1 import 'package:cw_wownero/api/wallet.dart';
2 import 'package:monero/wownero.dart' as wownero;
3
4 wownero.wallet? wptr = null;
5
6 int _wlptrForW = 0;
7 wownero.WalletListener? _wlptr = null;
8
9 wownero.WalletListener getWlptr() {
10 if (wptr!.address == _wlptrForW) return _wlptr!;
11 _wlptrForW = wptr!.address;
12 _wlptr = wownero.WOWNERO_cw_getWalletListener(wptr!);
13 return _wlptr!;
14 }
15
16 wownero.SubaddressAccount? subaddressAccount;
17
18 bool isUpdating = false;
19
20 void refreshAccounts() {
21 if (wptr == null) return;
22
23 try {
24 isUpdating = true;
25 subaddressAccount = wownero.Wallet_subaddressAccount(wptr!);
26 wownero.SubaddressAccount_refresh(subaddressAccount!);
27 isUpdating = false;
28 } catch (e) {
29 isUpdating = false;
30 rethrow;
31 }
32 }
33
34 List<wownero.SubaddressAccountRow> getAllAccount() {
35 if (wptr == null) return [];
36 // final size = wownero.Wallet_numSubaddressAccounts(wptr!);
37 refreshAccounts();
38 int size = wownero.SubaddressAccount_getAll_size(subaddressAccount!);
39 if (size == 0) {
40 wownero.Wallet_addSubaddressAccount(wptr!);
41 return getAllAccount();
42 }
43 return List.generate(size, (index) {
44 return wownero.SubaddressAccount_getAll_byIndex(subaddressAccount!, index: index);
45 });
46 }
47
48 void addAccountSync({required String label}) {
49 wownero.Wallet_addSubaddressAccount(wptr!, label: label);
50 }
51
52 void setLabelForAccountSync({required int accountIndex, required String label}) {
53 wownero.SubaddressAccount_setLabel(subaddressAccount!, accountIndex: accountIndex, label: label);
54 }
55
56 void _addAccount(String label) => addAccountSync(label: label);
57
58 void _setLabelForAccount(Map<String, dynamic> args) {
59 final label = args['label'] as String;
60 final accountIndex = args['accountIndex'] as int;
61
62 setLabelForAccountSync(label: label, accountIndex: accountIndex);
63 }
64
65 Future<void> addAccount({required String label}) async {
66 _addAccount(label);
67 await store();
68 }
69
70 Future<void> setLabelForAccount({required int accountIndex, required String label}) async {
71 _setLabelForAccount({'accountIndex': accountIndex, 'label': label});
72 await store();
73 }