| 1 | import 'package:cw_core/monero_amount_format.dart'; |
| 2 | import 'package:cw_core/utils/print_verbose.dart'; |
| 3 | import 'package:cw_monero/api/wallet_manager.dart'; |
| 4 | import 'package:mobx/mobx.dart'; |
| 5 | import 'package:cw_core/account.dart'; |
| 6 | import 'package:cw_monero/api/account_list.dart' as account_list; |
| 7 | import 'package:monero/src/monero.dart'; |
| 8 | |
| 9 | part 'monero_account_list.g.dart'; |
| 10 | |
| 11 | class MoneroAccountList = MoneroAccountListBase with _$MoneroAccountList; |
| 12 | |
| 13 | abstract class MoneroAccountListBase with Store { |
| 14 | MoneroAccountListBase() |
| 15 | : accounts = ObservableList<Account>(), |
| 16 | _isRefreshing = false, |
| 17 | _isUpdating = false { |
| 18 | refresh(); |
| 19 | } |
| 20 | |
| 21 | @observable |
| 22 | ObservableList<Account> accounts; |
| 23 | bool _isRefreshing; |
| 24 | bool _isUpdating; |
| 25 | |
| 26 | void update() async { |
| 27 | if (_isUpdating) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | try { |
| 32 | _isUpdating = true; |
| 33 | refresh(); |
| 34 | final accounts = getAll(); |
| 35 | |
| 36 | if (accounts.isNotEmpty) { |
| 37 | this.accounts.clear(); |
| 38 | this.accounts.addAll(accounts); |
| 39 | } |
| 40 | |
| 41 | _isUpdating = false; |
| 42 | } catch (e) { |
| 43 | _isUpdating = false; |
| 44 | rethrow; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static Map<int, List<Account>> cachedAccounts = {}; |
| 49 | |
| 50 | List<Account> getAll() { |
| 51 | final allAccounts = account_list.getAllAccount(); |
| 52 | final currentCount = allAccounts.length; |
| 53 | cachedAccounts[account_list.currentWallet!.ffiAddress()] ??= []; |
| 54 | |
| 55 | if (cachedAccounts[account_list.currentWallet!.ffiAddress()]!.length == currentCount) { |
| 56 | return cachedAccounts[account_list.currentWallet!.ffiAddress()]!; |
| 57 | } |
| 58 | |
| 59 | cachedAccounts[account_list.currentWallet!.ffiAddress()] = allAccounts.map((accountRow) { |
| 60 | final balance = accountRow.getUnlockedBalance(); |
| 61 | |
| 62 | return Account( |
| 63 | id: accountRow.getRowId(), |
| 64 | label: accountRow.getLabel(), |
| 65 | balance: |
| 66 | moneroAmountToString(amount: account_list.currentWallet!.amountFromString(balance)), |
| 67 | ); |
| 68 | }).toList(); |
| 69 | |
| 70 | return cachedAccounts[account_list.currentWallet!.ffiAddress()]!; |
| 71 | } |
| 72 | |
| 73 | void addAccount({required String label}) { |
| 74 | account_list.addAccount(label: label); |
| 75 | update(); |
| 76 | } |
| 77 | |
| 78 | void setLabelAccount({required int accountIndex, required String label}) { |
| 79 | account_list.setLabelForAccount(accountIndex: accountIndex, label: label); |
| 80 | update(); |
| 81 | } |
| 82 | |
| 83 | void refresh() { |
| 84 | if (_isRefreshing) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | try { |
| 89 | _isRefreshing = true; |
| 90 | account_list.refreshAccounts(); |
| 91 | _isRefreshing = false; |
| 92 | } catch (e) { |
| 93 | _isRefreshing = false; |
| 94 | printV(e); |
| 95 | rethrow; |
| 96 | } |
| 97 | } |
| 98 | } |