From 55c9a84dc90d1a2032e7385fc14214011a18e0cd Mon Sep 17 00:00:00 2001 From: Elizaveta Semenova Date: Fri, 21 Jul 2023 14:38:40 +0300 Subject: [PATCH] [PWN-9256] Fixes for validation logic and error handling (stop timer if error came) --- .../WithdrawCalculatorView.swift | 2 +- .../WithdrawCalculatorViewModel.swift | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/p2p_wallet/Scenes/Main/BankTransfer/WithdrawCalculator/WithdrawCalculatorView.swift b/p2p_wallet/Scenes/Main/BankTransfer/WithdrawCalculator/WithdrawCalculatorView.swift index bb05eab8e4..d6e12e4182 100644 --- a/p2p_wallet/Scenes/Main/BankTransfer/WithdrawCalculator/WithdrawCalculatorView.swift +++ b/p2p_wallet/Scenes/Main/BankTransfer/WithdrawCalculator/WithdrawCalculatorView.swift @@ -22,7 +22,7 @@ struct WithdrawCalculatorView: View { action: viewModel.actionPressed.send ) .padding(.top, 12) - .padding(.bottom, 36) + .padding(.bottom, 16) .background(Color(Asset.Colors.smoke.color).edgesIgnoringSafeArea(.bottom)) }) .scrollDismissesKeyboard() diff --git a/p2p_wallet/Scenes/Main/BankTransfer/WithdrawCalculator/WithdrawCalculatorViewModel.swift b/p2p_wallet/Scenes/Main/BankTransfer/WithdrawCalculator/WithdrawCalculatorViewModel.swift index 4211e684c0..c4e3a3112e 100644 --- a/p2p_wallet/Scenes/Main/BankTransfer/WithdrawCalculator/WithdrawCalculatorViewModel.swift +++ b/p2p_wallet/Scenes/Main/BankTransfer/WithdrawCalculator/WithdrawCalculatorViewModel.swift @@ -101,27 +101,28 @@ private extension WithdrawCalculatorViewModel { $fromAmount.eraseToAnyPublisher(), $toAmount.eraseToAnyPublisher() ) + .filter { $0.0 != nil } // Only if $exchangeRates is not failed. Otherwise it has own state .sink { [weak self] _, fromAmount, toAmount in guard let self else { return } switch (fromAmount, toAmount) { case (nil, _), (Double.zero, _): self.actionData = .zero self.fromAmountTextColor = Asset.Colors.night.color - case (fromAmount, toAmount) where fromAmount > self.fromBalance: - self.actionData = WithdrawCalculatorAction(isEnabled: false, title: L10n.notEnoughMoney) - self.fromAmountTextColor = Asset.Colors.rose.color - case (fromAmount, toAmount) where toAmount < Constants.EUR.min: + case (fromAmount, toAmount) where toAmount > Constants.EUR.max: actionData = WithdrawCalculatorAction( isEnabled: false, - title: L10n.asMinimalAmountForTransfer(Constants.EUR.min.formattedFiat(currency: .eur)) + title: L10n.onlyPerOneTransfer(Constants.EUR.max.formattedFiat(currency: .eur)) ) self.fromAmountTextColor = Asset.Colors.rose.color - case (fromAmount, toAmount) where toAmount > Constants.EUR.max: + case (fromAmount, toAmount) where toAmount < Constants.EUR.min: actionData = WithdrawCalculatorAction( isEnabled: false, - title: L10n.onlyPerOneTransfer(Constants.EUR.max.formattedFiat(currency: .eur)) + title: L10n.asMinimalAmountForTransfer(Constants.EUR.min.formattedFiat(currency: .eur)) ) self.fromAmountTextColor = Asset.Colors.rose.color + case (fromAmount, toAmount) where fromAmount > self.fromBalance: + self.actionData = WithdrawCalculatorAction(isEnabled: false, title: L10n.notEnoughMoney) + self.fromAmountTextColor = Asset.Colors.rose.color default: actionData = WithdrawCalculatorAction(isEnabled: true, title: L10n.next.uppercaseFirst) self.fromAmountTextColor = Asset.Colors.night.color @@ -216,8 +217,8 @@ private extension WithdrawCalculatorViewModel { reachability .isDisconnected .sink { [weak self] in - self?.cancelUpdate() self?.notificationService.showConnectionErrorNotification() + self?.commonErrorHandling() } .store(in: &subscriptions) } @@ -236,6 +237,7 @@ private extension WithdrawCalculatorViewModel { exchangeRatesFailCount = 0 scheduleRatesUpdate() changeEditing(isEnabled: true) + isFromFirstResponder = true } catch let error as NSError where error.isNetworkConnectionError { notificationService.showConnectionErrorNotification() commonErrorHandling() @@ -252,6 +254,7 @@ private extension WithdrawCalculatorViewModel { } func commonErrorHandling() { + cancelUpdate() arePricesLoading = false actionData = WithdrawCalculatorAction.failure exchangeRates = nil