From 2ce34919bfb18443c30db1baae285e4f3c4b7278 Mon Sep 17 00:00:00 2001 From: M Date: Fri, 29 Jan 2021 22:18:07 +0200 Subject: [PATCH] Fixes for backups for Android. Fixes for send almost all amount. --- android/app/build.gradle | 2 +- android/app/src/main/AndroidManifest.xml | 4 +++- ios/Podfile.lock | 6 ++++++ lib/bitcoin/bitcoin_wallet.dart | 21 ++++++++++++------- lib/src/screens/backup/backup_page.dart | 12 ++++++++--- .../exchange_trade/exchange_trade_page.dart | 12 +++++------ lib/view_model/backup_view_model.dart | 6 ++++-- lib/view_model/send/send_view_model.dart | 1 + pubspec.lock | 14 +++++++++++++ pubspec.yaml | 3 ++- 10 files changed, 60 insertions(+), 21 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 7f3990150..0c6a46dc4 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -31,7 +31,7 @@ if (keystorePropertiesFile.exists()) { } android { - compileSdkVersion 28 + compileSdkVersion 29 lintOptions { disable 'InvalidPackage' diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 437729891..abf27576f 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -4,12 +4,14 @@ + + android:icon="@mipmap/ic_launcher" + android:requestLegacyExternalStorage="true"> with Store { @override Future createTransaction( Object credentials) async { + const minAmount = 546; final transactionCredentials = credentials as BitcoinTransactionCredentials; final inputs = []; final allAmountFee = calculateEstimatedFee(transactionCredentials.priority, null); + final allAmount = balance.confirmed - allAmountFee; var fee = 0; - final amount = transactionCredentials.amount != null + final credentialsAmount = transactionCredentials.amount != null ? stringDoubleToBitcoinAmount(transactionCredentials.amount) - : balance.confirmed - allAmountFee; + : 0; + final amount = transactionCredentials.amount == null || + allAmount - credentialsAmount < minAmount + ? allAmount + : credentialsAmount; final txb = bitcoin.TransactionBuilder(network: bitcoin.bitcoin); final changeAddress = address; var leftAmount = amount; @@ -294,8 +300,8 @@ abstract class BitcoinWalletBase extends WalletBase with Store { final totalAmount = amount + fee; fee = transactionCredentials.amount != null - ? feeAmountForPriority( - transactionCredentials.priority, inputs.length, 2) + ? feeAmountForPriority(transactionCredentials.priority, inputs.length, + amount == allAmount ? 1 : 2) : allAmountFee; if (totalAmount > balance.confirmed) { @@ -329,7 +335,7 @@ abstract class BitcoinWalletBase extends WalletBase with Store { final feeAmount = transactionCredentials.priority.rate * estimatedSize; final changeValue = totalInputAmount - amount - feeAmount; - if (changeValue > 0) { + if (changeValue > minAmount) { txb.addOutput(changeAddress, changeValue); } @@ -375,8 +381,9 @@ abstract class BitcoinWalletBase extends WalletBase with Store { } else { inputsCount = _unspent.length; } - - return feeAmountForPriority(priority, inputsCount, 2); + // If send all, then we have no change value + return feeAmountForPriority( + priority, inputsCount, amount != null ? 2 : 1); } return 0; diff --git a/lib/src/screens/backup/backup_page.dart b/lib/src/screens/backup/backup_page.dart index 5a22f6bf9..cb9954241 100644 --- a/lib/src/screens/backup/backup_page.dart +++ b/lib/src/screens/backup/backup_page.dart @@ -14,6 +14,7 @@ import 'package:cake_wallet/utils/show_pop_up.dart'; import 'package:cake_wallet/view_model/backup_view_model.dart'; import 'package:cake_wallet/core/execution_state.dart'; import 'package:cake_wallet/src/screens/base_page.dart'; +import 'package:permission_handler/permission_handler.dart'; class BackupPage extends BasePage { BackupPage(this.backupViewModelBase); @@ -99,8 +100,6 @@ class BackupPage extends BasePage { actionRightButton: () async { Navigator.of(dialogContext).pop(); final backup = await backupViewModelBase.exportBackup(); - await backupViewModelBase.saveToDownload( - backup.name, backup.content); if (Platform.isAndroid) { onExportAndroid(context, backup); @@ -123,9 +122,16 @@ class BackupPage extends BasePage { rightButtonText: 'Save to Downloads', leftButtonText: 'Share', actionRightButton: () async { - Navigator.of(dialogContext).pop(); + final permission = await Permission.storage.request(); + + if (permission.isDenied) { + Navigator.of(dialogContext).pop(); + return; + } + await backupViewModelBase.saveToDownload( backup.name, backup.content); + Navigator.of(dialogContext).pop(); }, actionLeftButton: () { Navigator.of(dialogContext).pop(); diff --git a/lib/src/screens/exchange_trade/exchange_trade_page.dart b/lib/src/screens/exchange_trade/exchange_trade_page.dart index 36e84544d..dc3a4fb7b 100644 --- a/lib/src/screens/exchange_trade/exchange_trade_page.dart +++ b/lib/src/screens/exchange_trade/exchange_trade_page.dart @@ -255,11 +255,11 @@ class ExchangeTradeState extends State { .pendingTransaction.feeFormatted, rightButtonText: S.of(context).ok, leftButtonText: S.of(context).cancel, - actionRightButton: () { + actionRightButton: () async { Navigator.of(context).pop(); - widget.exchangeTradeViewModel.sendViewModel + await widget.exchangeTradeViewModel.sendViewModel .commitTransaction(); - showPopUp( + await showPopUp( context: context, builder: (BuildContext context) { return Observer(builder: (_) { @@ -359,10 +359,10 @@ class ExchangeTradeState extends State { }); }, actionLeftButton: () => Navigator.of(context).pop(), - feeFiatAmount: widget.exchangeTradeViewModel.sendViewModel - .pendingTransaction.feeFormatted, + feeFiatAmount: widget.exchangeTradeViewModel.sendViewModel.pendingTransactionFeeFiatAmount + + ' ' + widget.exchangeTradeViewModel.sendViewModel.fiat.title, fiatAmountValue: widget.exchangeTradeViewModel.sendViewModel - .pendingTransactionFeeFiatAmount + + .pendingTransactionFiatAmount + ' ' + widget.exchangeTradeViewModel.sendViewModel.fiat.title, recipientTitle: S.of(context).recipient_address, diff --git a/lib/view_model/backup_view_model.dart b/lib/view_model/backup_view_model.dart index 660c576c4..1a2e6da05 100644 --- a/lib/view_model/backup_view_model.dart +++ b/lib/view_model/backup_view_model.dart @@ -1,5 +1,4 @@ import 'dart:io'; - import 'package:cake_wallet/core/backup_service.dart'; import 'package:cake_wallet/core/execution_state.dart'; import 'package:cake_wallet/entities/secret_store_key.dart'; @@ -7,6 +6,7 @@ import 'package:cake_wallet/store/secret_store.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:mobx/mobx.dart'; +import 'package:intl/intl.dart'; part 'backup_view_model.g.dart'; @@ -56,9 +56,11 @@ abstract class BackupViewModelBase with Store { state = IsExecutingState(); final backupContent = await backupService.exportBackup(backupPassword); state = ExecutedSuccessfullyState(); + final now = DateTime.now(); + final formatter = DateFormat('yyyy-MM-dd_Hm'); return BackupExportFile(backupContent.toList(), - name: 'backup_${DateTime.now().toString()}.zip'); + name: 'cake_wallet_backup_${formatter.format(now)}'); } catch (e) { print(e.toString()); state = FailureState(e.toString()); diff --git a/lib/view_model/send/send_view_model.dart b/lib/view_model/send/send_view_model.dart index 419d57695..3afb57c15 100644 --- a/lib/view_model/send/send_view_model.dart +++ b/lib/view_model/send/send_view_model.dart @@ -167,6 +167,7 @@ abstract class SendViewModelBase with Store { Validator get templateValidator => TemplateValidator(); + @observable PendingTransaction pendingTransaction; @computed diff --git a/pubspec.lock b/pubspec.lock index 2dfa55586..383f2ef73 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -695,6 +695,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.9.2" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + url: "https://pub.dartlang.org" + source: hosted + version: "5.0.1+1" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" petitparser: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e70c37c69..75fa61b33 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,7 +11,7 @@ description: Cake Wallet. # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 4.1.0+37 +version: 4.1.0+38 environment: sdk: ">=2.7.0 <3.0.0" @@ -72,6 +72,7 @@ dependencies: cryptography: ^1.4.0 file_picker: ^2.1.4 unorm_dart: ^0.1.2 + permission_handler: ^5.0.1+1 dev_dependencies: flutter_test: