2023-05-05 15:58:41 +03:00
|
|
|
import 'package:cw_core/monero_amount_format.dart';
|
2024-12-09 12:23:59 -06:00
|
|
|
import 'package:cw_core/utils/print_verbose.dart';
|
2025-04-03 03:31:25 +02:00
|
|
|
import 'package:cw_monero/api/wallet_manager.dart';
|
2021-12-24 14:52:08 +02:00
|
|
|
import 'package:mobx/mobx.dart';
|
2022-03-30 17:57:04 +02:00
|
|
|
import 'package:cw_core/account.dart';
|
2021-12-24 14:52:08 +02:00
|
|
|
import 'package:cw_monero/api/account_list.dart' as account_list;
|
2025-05-02 14:30:39 +02:00
|
|
|
import 'package:monero/src/monero.dart';
|
2021-12-24 14:52:08 +02:00
|
|
|
|
|
|
|
part 'monero_account_list.g.dart';
|
|
|
|
|
|
|
|
class MoneroAccountList = MoneroAccountListBase with _$MoneroAccountList;
|
|
|
|
|
|
|
|
abstract class MoneroAccountListBase with Store {
|
|
|
|
MoneroAccountListBase()
|
|
|
|
: accounts = ObservableList<Account>(),
|
|
|
|
_isRefreshing = false,
|
|
|
|
_isUpdating = false {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@observable
|
|
|
|
ObservableList<Account> accounts;
|
|
|
|
bool _isRefreshing;
|
|
|
|
bool _isUpdating;
|
|
|
|
|
|
|
|
void update() async {
|
|
|
|
if (_isUpdating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isUpdating = true;
|
|
|
|
refresh();
|
|
|
|
final accounts = getAll();
|
|
|
|
|
|
|
|
if (accounts.isNotEmpty) {
|
|
|
|
this.accounts.clear();
|
|
|
|
this.accounts.addAll(accounts);
|
|
|
|
}
|
|
|
|
|
|
|
|
_isUpdating = false;
|
|
|
|
} catch (e) {
|
|
|
|
_isUpdating = false;
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-10 23:17:52 +02:00
|
|
|
static Map<int, List<Account>> cachedAccounts = {};
|
2025-04-03 03:31:25 +02:00
|
|
|
|
|
|
|
List<Account> getAll() {
|
|
|
|
final allAccounts = account_list.getAllAccount();
|
|
|
|
final currentCount = allAccounts.length;
|
2025-05-02 14:30:39 +02:00
|
|
|
cachedAccounts[account_list.currentWallet!.ffiAddress()] ??= [];
|
2025-04-03 03:31:25 +02:00
|
|
|
|
2025-05-02 14:30:39 +02:00
|
|
|
if (cachedAccounts[account_list.currentWallet!.ffiAddress()]!.length == currentCount) {
|
|
|
|
return cachedAccounts[account_list.currentWallet!.ffiAddress()]!;
|
2025-04-03 03:31:25 +02:00
|
|
|
}
|
|
|
|
|
2025-05-02 14:30:39 +02:00
|
|
|
cachedAccounts[account_list.currentWallet!.ffiAddress()] = allAccounts.map((accountRow) {
|
|
|
|
final balance = accountRow.getUnlockedBalance();
|
2023-05-05 15:58:41 +03:00
|
|
|
|
|
|
|
return Account(
|
2025-05-02 14:30:39 +02:00
|
|
|
id: accountRow.getRowId(),
|
|
|
|
label: accountRow.getLabel(),
|
|
|
|
balance: moneroAmountToString(amount: account_list.currentWallet!.amountFromString(balance)),
|
2023-05-05 15:58:41 +03:00
|
|
|
);
|
|
|
|
}).toList();
|
2025-04-03 03:31:25 +02:00
|
|
|
|
2025-05-02 14:30:39 +02:00
|
|
|
return cachedAccounts[account_list.currentWallet!.ffiAddress()]!;
|
2025-04-03 03:31:25 +02:00
|
|
|
}
|
2021-12-24 14:52:08 +02:00
|
|
|
|
2025-05-02 14:30:39 +02:00
|
|
|
void addAccount({required String label}) {
|
|
|
|
account_list.addAccount(label: label);
|
2021-12-24 14:52:08 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2025-05-02 14:30:39 +02:00
|
|
|
void setLabelAccount({required int accountIndex, required String label}) {
|
|
|
|
account_list.setLabelForAccount(accountIndex: accountIndex, label: label);
|
2021-12-24 14:52:08 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void refresh() {
|
|
|
|
if (_isRefreshing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isRefreshing = true;
|
|
|
|
account_list.refreshAccounts();
|
|
|
|
_isRefreshing = false;
|
|
|
|
} catch (e) {
|
|
|
|
_isRefreshing = false;
|
2024-12-09 12:23:59 -06:00
|
|
|
printV(e);
|
2021-12-24 14:52:08 +02:00
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|