From 5b450a5ccbe66f160b263f006f06b374802e73df Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 19 Mar 2021 10:30:43 -0300 Subject: [PATCH] Show available balance, not actual balance. --- .../SendPaymentCompletionActionSheet.swift | 6 ++++- .../Payments/SendPaymentHelper.swift | 25 ++++++++++++++++--- .../Payments/SendPaymentViewController.swift | 6 ++++- .../translations/en.lproj/Localizable.strings | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Signal/src/ViewControllers/Payments/SendPaymentCompletionActionSheet.swift b/Signal/src/ViewControllers/Payments/SendPaymentCompletionActionSheet.swift index c37dfe03c1..bd44908ae6 100644 --- a/Signal/src/ViewControllers/Payments/SendPaymentCompletionActionSheet.swift +++ b/Signal/src/ViewControllers/Payments/SendPaymentCompletionActionSheet.swift @@ -514,7 +514,11 @@ public class SendPaymentCompletionActionSheet: ActionSheetController { @objc public func updateBalanceLabel() { - SendPaymentHelper.updateBalanceLabel(balanceLabel) + guard let helper = helper else { + Logger.verbose("Missing helper.") + return + } + helper.updateBalanceLabel(balanceLabel) } private func tryToSendPayment(paymentInfo: PaymentInfo) { diff --git a/Signal/src/ViewControllers/Payments/SendPaymentHelper.swift b/Signal/src/ViewControllers/Payments/SendPaymentHelper.swift index d1e99d6b15..1ffe491337 100644 --- a/Signal/src/ViewControllers/Payments/SendPaymentHelper.swift +++ b/Signal/src/ViewControllers/Payments/SendPaymentHelper.swift @@ -65,12 +65,15 @@ class SendPaymentHelper { } } + private var maximumPaymentAmount: TSPaymentAmount? + required init(delegate: SendPaymentHelperDelegate) { self.delegate = delegate addObservers() updateCurrentCurrencyConversion() + updateMaximumPaymentAmount() } private func addObservers() { @@ -144,9 +147,9 @@ class SendPaymentHelper { return label } - public static func updateBalanceLabel(_ balanceLabel: UILabel) { + public func updateBalanceLabel(_ balanceLabel: UILabel) { - guard let paymentBalance = Self.paymentsSwift.currentPaymentBalance else { + guard let maximumPaymentAmount = self.maximumPaymentAmount else { // Use whitespace to ensure that the height of the label // is constant, avoiding layout jitter. balanceLabel.text = " " @@ -156,12 +159,28 @@ class SendPaymentHelper { let format = NSLocalizedString("PAYMENTS_NEW_PAYMENT_BALANCE_FORMAT", comment: "Format for the 'balance' indicator. Embeds {{ the current payments balance }}.") balanceLabel.text = String(format: format, - Self.formatMobileCoinAmount(paymentBalance.amount)) + Self.formatMobileCoinAmount(maximumPaymentAmount)) + } + + private func updateMaximumPaymentAmount() { + firstly { + Self.paymentsSwift.maximumPaymentAmount() + }.done(on: .main) { [weak self] maximumPaymentAmount in + guard let self = self else { return } + self.maximumPaymentAmount = maximumPaymentAmount + self.delegate?.balanceDidChange() + }.catch(on: .global()) { error in + owsFailDebugUnlessNetworkFailure(error) + } + + delegate?.balanceDidChange() } @objc private func currentPaymentBalanceDidChange() { delegate?.balanceDidChange() + + updateMaximumPaymentAmount() } @objc diff --git a/Signal/src/ViewControllers/Payments/SendPaymentViewController.swift b/Signal/src/ViewControllers/Payments/SendPaymentViewController.swift index e47092db5f..13bc2c6a5a 100644 --- a/Signal/src/ViewControllers/Payments/SendPaymentViewController.swift +++ b/Signal/src/ViewControllers/Payments/SendPaymentViewController.swift @@ -673,7 +673,11 @@ public class SendPaymentViewController: OWSViewController { } private func updateBalanceLabel() { - SendPaymentHelper.updateBalanceLabel(balanceLabel) + guard let helper = helper else { + Logger.verbose("Missing helper.") + return + } + helper.updateBalanceLabel(balanceLabel) } private func showInvalidAmountAlert() { diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index 9f6d205abd..3962b7a674 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -3392,7 +3392,7 @@ "PAYMENTS_NEW_PAYMENT_ADD_MEMO" = "Add Note"; /* Format for the 'balance' indicator. Embeds {{ the current payments balance }}. */ -"PAYMENTS_NEW_PAYMENT_BALANCE_FORMAT" = "Balance: %@"; +"PAYMENTS_NEW_PAYMENT_BALANCE_FORMAT" = "Available Balance: %@"; /* Label for the 'confirm payment' button. */ "PAYMENTS_NEW_PAYMENT_CONFIRM_PAYMENT_BUTTON" = "Confirm Payment";