This commit is contained in:
M
2020-11-10 01:14:47 +02:00
parent c2e7a854bb
commit 03f47e393c
8 changed files with 121 additions and 83 deletions
+27 -23
View File
@@ -391,32 +391,36 @@ Future<void> ios_migrate_trades_list(Box<Trade> tradeSource) async {
}
Future<void> ios_migrate_address_book(Box<Contact> contactSource) async {
final prefs = await SharedPreferences.getInstance();
try {
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('ios_migration_address_book_completed') ?? false) {
return;
}
if (prefs.getBool('ios_migration_address_book_completed') ?? false) {
return;
}
final appDocDir = await getApplicationDocumentsDirectory();
final addressBookJSON = File('${appDocDir.path}/address_book.json');
final appDocDir = await getApplicationDocumentsDirectory();
final addressBookJSON = File('${appDocDir.path}/address_book.json');
if (!addressBookJSON.existsSync()) {
if (!addressBookJSON.existsSync()) {
await prefs.setBool('ios_migration_address_book_completed', true);
return;
}
final List<dynamic> addresses =
json.decode(addressBookJSON.readAsStringSync()) as List<dynamic>;
final contacts = addresses.map((dynamic item) {
final _item = item as Map<String, dynamic>;
final type = _item["type"] as String;
final address = _item["address"] as String;
final name = _item["name"] as String;
return Contact(
address: address, name: name, type: CryptoCurrency.fromString(type));
});
await contactSource.addAll(contacts);
await prefs.setBool('ios_migration_address_book_completed', true);
return;
} catch(e) {
print(e.toString());
}
final List<dynamic> addresses =
json.decode(addressBookJSON.readAsStringSync()) as List<dynamic>;
final contacts = addresses.map((dynamic item) {
final _item = item as Map<String, dynamic>;
final type = _item["type"] as String;
final address = _item["address"] as String;
final name = _item["name"] as String;
return Contact(
address: address, name: name, type: CryptoCurrency.fromString(type));
});
await contactSource.addAll(contacts);
await prefs.setBool('ios_migration_address_book_completed', true);
}
+52 -11
View File
@@ -44,10 +44,12 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
subaddress = subaddressList.subaddresses.first;
address = subaddress.address;
_lastAutosaveTimestamp = 0;
_isSavingAfterSync = false;
_isSavingAfterNewTransaction = false;
});
}
static const int _autoAfterSyncSaceInterval = 60000;
static const int _autoAfterSyncSaveInterval = 60000;
@override
final MoneroTransactionHistory transactionHistory;
@@ -88,6 +90,8 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
SyncListener _listener;
ReactionDisposer _onAccountChangeReaction;
int _lastAutosaveTimestamp;
bool _isSavingAfterSync;
bool _isSavingAfterNewTransaction;
Future<void> init() async {
await accountList.update();
@@ -174,11 +178,14 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
@override
Future<PendingTransaction> createTransaction(Object credentials) async {
final _credentials = credentials as MoneroTransactionCreationCredentials;
final amount = moneroParseAmount(amount: _credentials.amount);
final amount = _credentials.amount != null
? moneroParseAmount(amount: _credentials.amount)
: null;
final unlockedBalance =
monero_wallet.getUnlockedBalance(accountIndex: account.id);
if (unlockedBalance < amount) {
if ((amount != null && unlockedBalance < amount) ||
(amount == null && unlockedBalance <= 0)) {
throw MoneroTransactionCreationException(
'Incorrect unlocked balance. Unlocked: $unlockedBalance. Transaction amount: ${_credentials.amount}.');
}
@@ -292,8 +299,8 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
}
void _askForUpdateBalance() {
final fullBalance = _getFullBalance();
final unlockedBalance = _getUnlockedBalance();
final fullBalance = _getFullBalance();
if (balance.fullBalance != fullBalance ||
balance.unlockedBalance != unlockedBalance) {
@@ -313,18 +320,47 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
monero_wallet.getUnlockedBalance(accountIndex: account.id);
Future<void> _afterSyncSave() async {
final nowTimestamp = DateTime.now().millisecondsSinceEpoch;
final sum = _lastAutosaveTimestamp + _autoAfterSyncSaceInterval;
if (_lastAutosaveTimestamp != 0 && sum < nowTimestamp) {
if (_isSavingAfterSync) {
return;
}
_lastAutosaveTimestamp = nowTimestamp + _autoAfterSyncSaceInterval;
await save();
_isSavingAfterSync = true;
try {
final nowTimestamp = DateTime.now().millisecondsSinceEpoch;
final sum = _lastAutosaveTimestamp + _autoAfterSyncSaveInterval;
if (_lastAutosaveTimestamp > 0 && sum < nowTimestamp) {
return;
}
await save();
_lastAutosaveTimestamp = nowTimestamp + _autoAfterSyncSaveInterval;
} catch (e) {
print(e.toString());
}
_isSavingAfterSync = false;
}
Future<void> _afterNewTransactionSave() async {
if (_isSavingAfterNewTransaction) {
return;
}
_isSavingAfterNewTransaction = true;
try {
await save();
} catch (e) {
print(e.toString());
}
_isSavingAfterNewTransaction = false;
}
void _onNewBlock(int height, int blocksLeft, double ptc) async {
print('_onNewBlock called');
if (walletInfo.isRecovery) {
_askForUpdateTransactionHistory();
}
@@ -334,14 +370,19 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
if (blocksLeft < 100) {
syncStatus = SyncedSyncStatus();
await _afterSyncSave();
if (walletInfo.isRecovery) {
setAsRecovered();
}
} else {
syncStatus = SyncingSyncStatus(blocksLeft, ptc);
}
}
void _onNewTransaction() {
print('_onNewTransaction called');
_askForUpdateTransactionHistory();
_askForUpdateBalance();
_afterSyncSave();
Timer(Duration(seconds: 1), () => _afterNewTransactionSave());
}
}
+2 -2
View File
@@ -220,9 +220,9 @@ class AddressTextField extends StatelessWidget {
Future<void> _pasteAddress(BuildContext context) async {
String address;
await Clipboard.getData('text/plain').then((value) => address = value.text);
await Clipboard.getData('text/plain').then((value) => address = value?.text);
if (address.isNotEmpty) {
if (address?.isNotEmpty ?? false) {
controller.text = address;
}
}
+6 -3
View File
@@ -160,9 +160,12 @@ abstract class SettingsStoreBase with Store {
actionListDisplayMode.addAll(deserializeActionlistDisplayModes(
sharedPreferences.getInt(PreferencesKey.displayActionListModeKey) ??
defaultActionsMode));
final pinLength =
sharedPreferences.getInt(PreferencesKey.currentPinLength) ??
defaultPinLength;
var pinLength = sharedPreferences.getInt(PreferencesKey.currentPinLength);
// If no value
if (pinLength == null || pinLength == 0) {
pinLength = defaultPinLength;
}
final savedLanguageCode =
sharedPreferences.getString(PreferencesKey.currentLanguageCode) ??
await LanguageService.localeDetection();