2021-12-24 12:52:08 +00:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
2023-08-24 13:54:05 +00:00
|
|
|
import 'package:cw_monero/api/coins_info.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_monero/api/subaddress_list.dart' as subaddress_list;
|
2022-03-30 15:57:04 +00:00
|
|
|
import 'package:cw_core/subaddress.dart';
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
part 'monero_subaddress_list.g.dart';
|
|
|
|
|
|
|
|
class MoneroSubaddressList = MoneroSubaddressListBase
|
|
|
|
with _$MoneroSubaddressList;
|
|
|
|
|
|
|
|
abstract class MoneroSubaddressListBase with Store {
|
2022-10-12 17:09:57 +00:00
|
|
|
MoneroSubaddressListBase()
|
|
|
|
: _isRefreshing = false,
|
|
|
|
_isUpdating = false,
|
|
|
|
subaddresses = ObservableList<Subaddress>();
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
@observable
|
|
|
|
ObservableList<Subaddress> subaddresses;
|
|
|
|
|
|
|
|
bool _isRefreshing;
|
|
|
|
bool _isUpdating;
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
void update({required int accountIndex}) {
|
2023-08-24 13:54:05 +00:00
|
|
|
refreshCoins(accountIndex);
|
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
if (_isUpdating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isUpdating = true;
|
|
|
|
refresh(accountIndex: accountIndex);
|
|
|
|
subaddresses.clear();
|
|
|
|
subaddresses.addAll(getAll());
|
|
|
|
_isUpdating = false;
|
|
|
|
} catch (e) {
|
|
|
|
_isUpdating = false;
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Subaddress> getAll() {
|
|
|
|
var subaddresses = subaddress_list.getAllSubaddresses();
|
|
|
|
|
|
|
|
if (subaddresses.length > 2) {
|
|
|
|
final primary = subaddresses.first;
|
|
|
|
final rest = subaddresses.sublist(1).reversed;
|
|
|
|
subaddresses = [primary] + rest.toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
return subaddresses
|
2022-03-30 15:57:04 +00:00
|
|
|
.map((subaddressRow) => Subaddress(
|
|
|
|
id: subaddressRow.getId(),
|
|
|
|
address: subaddressRow.getAddress(),
|
|
|
|
label: subaddressRow.getId() == 0 &&
|
|
|
|
subaddressRow.getLabel().toLowerCase() == 'Primary account'.toLowerCase()
|
|
|
|
? 'Primary address'
|
|
|
|
: subaddressRow.getLabel()))
|
2021-12-24 12:52:08 +00:00
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> addSubaddress({required int accountIndex, required String label}) async {
|
2021-12-24 12:52:08 +00:00
|
|
|
await subaddress_list.addSubaddress(
|
|
|
|
accountIndex: accountIndex, label: label);
|
|
|
|
update(accountIndex: accountIndex);
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
Future<void> setLabelSubaddress(
|
|
|
|
{required int accountIndex, required int addressIndex, required String label}) async {
|
2021-12-24 12:52:08 +00:00
|
|
|
await subaddress_list.setLabelForSubaddress(
|
|
|
|
accountIndex: accountIndex, addressIndex: addressIndex, label: label);
|
|
|
|
update(accountIndex: accountIndex);
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
void refresh({required int accountIndex}) {
|
2021-12-24 12:52:08 +00:00
|
|
|
if (_isRefreshing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isRefreshing = true;
|
|
|
|
subaddress_list.refreshSubaddresses(accountIndex: accountIndex);
|
|
|
|
_isRefreshing = false;
|
|
|
|
} on PlatformException catch (e) {
|
|
|
|
_isRefreshing = false;
|
|
|
|
print(e);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|