2020-10-01 10:52:49 +00:00
|
|
|
import 'package:cw_monero/structs/subaddress_row.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:mobx/mobx.dart';
|
|
|
|
import 'package:cw_monero/subaddress_list.dart' as subaddress_list;
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/monero/subaddress.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
part 'monero_subaddress_list.g.dart';
|
|
|
|
|
|
|
|
class MoneroSubaddressList = MoneroSubaddressListBase
|
|
|
|
with _$MoneroSubaddressList;
|
|
|
|
|
|
|
|
abstract class MoneroSubaddressListBase with Store {
|
|
|
|
MoneroSubaddressListBase() {
|
|
|
|
_isRefreshing = false;
|
|
|
|
_isUpdating = false;
|
|
|
|
subaddresses = ObservableList<Subaddress>();
|
|
|
|
}
|
|
|
|
|
|
|
|
@observable
|
|
|
|
ObservableList<Subaddress> subaddresses;
|
|
|
|
|
|
|
|
bool _isRefreshing;
|
|
|
|
bool _isUpdating;
|
|
|
|
|
|
|
|
void update({int accountIndex}) {
|
|
|
|
if (_isUpdating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isUpdating = true;
|
|
|
|
refresh(accountIndex: accountIndex);
|
|
|
|
subaddresses.clear();
|
|
|
|
subaddresses.addAll(getAll());
|
|
|
|
_isUpdating = false;
|
|
|
|
} catch (e) {
|
|
|
|
_isUpdating = false;
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Subaddress> getAll() {
|
2020-10-01 10:52:49 +00:00
|
|
|
var subaddresses = subaddress_list.getAllSubaddresses();
|
2020-09-30 18:23:15 +00:00
|
|
|
|
2020-10-01 10:52:49 +00:00
|
|
|
if (subaddresses.length > 2) {
|
|
|
|
final primary = subaddresses.first;
|
|
|
|
final rest = subaddresses.sublist(1).reversed;
|
|
|
|
subaddresses = [primary] + rest.toList();
|
2020-09-30 18:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return subaddresses
|
2020-06-20 07:10:00 +00:00
|
|
|
.map((subaddressRow) => Subaddress.fromRow(subaddressRow))
|
|
|
|
.toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future addSubaddress({int accountIndex, String label}) async {
|
|
|
|
await subaddress_list.addSubaddress(
|
|
|
|
accountIndex: accountIndex, label: label);
|
|
|
|
update(accountIndex: accountIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future setLabelSubaddress(
|
|
|
|
{int accountIndex, int addressIndex, String label}) async {
|
|
|
|
await subaddress_list.setLabelForSubaddress(
|
|
|
|
accountIndex: accountIndex, addressIndex: addressIndex, label: label);
|
|
|
|
update(accountIndex: accountIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void refresh({int accountIndex}) {
|
|
|
|
if (_isRefreshing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_isRefreshing = true;
|
|
|
|
subaddress_list.refreshSubaddresses(accountIndex: accountIndex);
|
|
|
|
_isRefreshing = false;
|
|
|
|
} on PlatformException catch (e) {
|
|
|
|
_isRefreshing = false;
|
|
|
|
print(e);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|