2023-05-05 12:58:41 +00:00
|
|
|
import 'package:cw_core/monero_amount_format.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
2022-03-30 15:57:04 +00:00
|
|
|
import 'package:cw_core/account.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_monero/api/account_list.dart' as account_list;
|
2023-05-05 12:58:41 +00:00
|
|
|
import 'package:cw_monero/api/wallet.dart' as monero_wallet;
|
2021-12-24 12:52:08 +00: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:58:41 +00:00
|
|
|
List<Account> getAll() => account_list.getAllAccount().map((accountRow) {
|
|
|
|
final accountIndex = accountRow.getId();
|
|
|
|
final balance = monero_wallet.getFullBalance(accountIndex: accountIndex);
|
|
|
|
|
|
|
|
return Account(
|
|
|
|
id: accountRow.getId(),
|
|
|
|
label: accountRow.getLabel(),
|
|
|
|
balance: moneroAmountToString(amount: balance),
|
|
|
|
);
|
|
|
|
}).toList();
|
2021-12-24 12:52:08 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> addAccount({required String label}) async {
|
2021-12-24 12:52:08 +00:00
|
|
|
await account_list.addAccount(label: label);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> setLabelAccount({required int accountIndex, required String label}) async {
|
2023-05-05 12:58:41 +00:00
|
|
|
await account_list.setLabelForAccount(accountIndex: accountIndex, label: label);
|
2021-12-24 12:52:08 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void refresh() {
|
|
|
|
if (_isRefreshing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isRefreshing = true;
|
|
|
|
account_list.refreshAccounts();
|
|
|
|
_isRefreshing = false;
|
|
|
|
} catch (e) {
|
|
|
|
_isRefreshing = false;
|
|
|
|
print(e);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|