diff --git a/Trust.xcodeproj/project.pbxproj b/Trust.xcodeproj/project.pbxproj index 2e866979e..026dfc001 100644 --- a/Trust.xcodeproj/project.pbxproj +++ b/Trust.xcodeproj/project.pbxproj @@ -172,6 +172,7 @@ 299B5E2B1FCA9A640051361C /* ApproveERC20.swift in Sources */ = {isa = PBXBuildFile; fileRef = 299B5E2A1FCA9A640051361C /* ApproveERC20.swift */; }; 299B5E2D1FCBC0660051361C /* BalanceProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 299B5E2C1FCBC0660051361C /* BalanceProtocol.swift */; }; 299B5E2F1FCBC0790051361C /* TokenBalance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 299B5E2E1FCBC0790051361C /* TokenBalance.swift */; }; + 299B5E341FCBC5180051361C /* ConfirmPaymentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 299B5E331FCBC5180051361C /* ConfirmPaymentViewModel.swift */; }; 29A0E1851F706B8C00BAAAED /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A0E1841F706B8C00BAAAED /* String.swift */; }; 29A0E1871F706D0700BAAAED /* EthereumConverter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A0E1861F706D0700BAAAED /* EthereumConverter.swift */; }; 29A13E2C1F6A955700E432A2 /* CreateWalletViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A13E2B1F6A955700E432A2 /* CreateWalletViewModel.swift */; }; @@ -443,6 +444,7 @@ 299B5E2A1FCA9A640051361C /* ApproveERC20.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApproveERC20.swift; sourceTree = ""; }; 299B5E2C1FCBC0660051361C /* BalanceProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BalanceProtocol.swift; sourceTree = ""; }; 299B5E2E1FCBC0790051361C /* TokenBalance.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenBalance.swift; sourceTree = ""; }; + 299B5E331FCBC5180051361C /* ConfirmPaymentViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfirmPaymentViewModel.swift; sourceTree = ""; }; 29A0E1841F706B8C00BAAAED /* String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = ""; }; 29A0E1861F706D0700BAAAED /* EthereumConverter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EthereumConverter.swift; sourceTree = ""; }; 29A13E271F6A903500E432A2 /* WelcomeViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeViewModelTests.swift; sourceTree = ""; }; @@ -779,10 +781,11 @@ 291ED0901F6FA5C800E7E93A /* Transfer */ = { isa = PBXGroup; children = ( + 299B5E301FCBC2B90051361C /* Controllers */, 29B933F61F86073E009FCABB /* Coordinators */, 29B6AECE1F7C880700EC6DE3 /* Types */, 29B6AECD1F7C87F400EC6DE3 /* ViewModels */, - 29B6AECC1F7C87E700EC6DE3 /* Controllers */, + 29B6AECC1F7C87E700EC6DE3 /* ViewControllers */, ); path = Transfer; sourceTree = ""; @@ -1299,6 +1302,13 @@ path = Export; sourceTree = ""; }; + 299B5E301FCBC2B90051361C /* Controllers */ = { + isa = PBXGroup; + children = ( + ); + path = Controllers; + sourceTree = ""; + }; 29A13E261F6A901600E432A2 /* Welcome */ = { isa = PBXGroup; children = ( @@ -1326,7 +1336,7 @@ path = Foundation; sourceTree = ""; }; - 29B6AECC1F7C87E700EC6DE3 /* Controllers */ = { + 29B6AECC1F7C87E700EC6DE3 /* ViewControllers */ = { isa = PBXGroup; children = ( 291ED0911F6FA5D900E7E93A /* RequestViewController.swift */, @@ -1334,7 +1344,7 @@ 29B6AED31F7CA09600EC6DE3 /* TransactionConfigurationViewController.swift */, 291A1B661F98092F00ADEC80 /* ConfirmPaymentViewController.swift */, ); - path = Controllers; + path = ViewControllers; sourceTree = ""; }; 29B6AECD1F7C87F400EC6DE3 /* ViewModels */ = { @@ -1345,6 +1355,7 @@ 29C80D441FB51B1A0037B1E0 /* TransactionHeaderBaseViewModel.swift */, 2963B6AA1F9818C7003063C1 /* TransactionHeaderViewModel.swift */, 29C80D461FB51B680037B1E0 /* ConfirmTransactionHeaderViewModel.swift */, + 299B5E331FCBC5180051361C /* ConfirmPaymentViewModel.swift */, ); path = ViewModels; sourceTree = ""; @@ -2013,6 +2024,7 @@ 29BE3FD21F707DC300F6BFC2 /* TransactionDataCoordinator.swift in Sources */, 29C80D351FB2CD110037B1E0 /* Block.swift in Sources */, 291ED0921F6FA5D900E7E93A /* RequestViewController.swift in Sources */, + 299B5E341FCBC5180051361C /* ConfirmPaymentViewModel.swift in Sources */, 296106C21F76403A0006164B /* TokenViewCell.swift in Sources */, 29F114F01FA6D53700114A29 /* ImportSelectionType.swift in Sources */, 29850D251F6B27A800791A49 /* R.generated.swift in Sources */, diff --git a/Trust/Exchange/Coordinators/ExchangeCoordinator.swift b/Trust/Exchange/Coordinators/ExchangeCoordinator.swift index b00a96e1f..a5497ab92 100644 --- a/Trust/Exchange/Coordinators/ExchangeCoordinator.swift +++ b/Trust/Exchange/Coordinators/ExchangeCoordinator.swift @@ -49,7 +49,7 @@ extension ExchangeCoordinator: ExchangeViewControllerDelegate { let controller = ConfirmPaymentViewController( session: session, transaction: transaction, - viewModel: viewModel + headerViewModel: viewModel ) controller.delegate = self navigationController.pushViewController(controller, animated: true) diff --git a/Trust/Transfer/Coordinators/SendCoordinator.swift b/Trust/Transfer/Coordinators/SendCoordinator.swift index ae743c1e9..161bfd917 100644 --- a/Trust/Transfer/Coordinators/SendCoordinator.swift +++ b/Trust/Transfer/Coordinators/SendCoordinator.swift @@ -72,7 +72,7 @@ extension SendCoordinator: SendViewControllerDelegate { let controller = ConfirmPaymentViewController( session: session, transaction: transaction, - viewModel: viewModel + headerViewModel: viewModel ) controller.delegate = self navigationController.pushViewController(controller, animated: true) diff --git a/Trust/Transfer/Controllers/ConfirmPaymentViewController.swift b/Trust/Transfer/ViewControllers/ConfirmPaymentViewController.swift similarity index 90% rename from Trust/Transfer/Controllers/ConfirmPaymentViewController.swift rename to Trust/Transfer/ViewControllers/ConfirmPaymentViewController.swift index 29fa3fbb8..610499b67 100644 --- a/Trust/Transfer/Controllers/ConfirmPaymentViewController.swift +++ b/Trust/Transfer/ViewControllers/ConfirmPaymentViewController.swift @@ -48,10 +48,19 @@ class ConfirmPaymentViewController: UIViewController { } }() + var viewModel: ConfirmPaymentViewModel { + let currentBalance = Double(session.balance?.amountFull ?? "") + return ConfirmPaymentViewModel( + transaction: transaction, + currentBalance: currentBalance, + configuration: configuration + ) + } + init( session: WalletSession, transaction: UnconfirmedTransaction, - viewModel: TransactionHeaderBaseViewModel + headerViewModel: TransactionHeaderBaseViewModel ) { self.session = session self.transaction = transaction @@ -63,20 +72,16 @@ class ConfirmPaymentViewController: UIViewController { navigationItem.title = NSLocalizedString("confirmPayment.title", value: "Confirm", comment: "") - let totalFee = configuration.speed.gasPrice * configuration.speed.gasLimit - let gasLimit = configuration.speed.gasLimit - let fee = EthereumConverter.from(value: totalFee, to: .ether, minimumFractionDigits: 6) - let items: [UIView] = [ .spacer(), TransactionAppearance.header( - viewModel: viewModel + viewModel: headerViewModel ), TransactionAppearance.divider(color: Colors.lightGray, alpha: 0.3), TransactionAppearance.item(title: NSLocalizedString("confirmPayment.from", value: "From", comment: ""), subTitle: session.account.address.address), - TransactionAppearance.item(title: NSLocalizedString("confirmPayment.to", value: "To", comment: ""), subTitle: transaction.address.address), - TransactionAppearance.item(title: NSLocalizedString("confirmPayment.gasLimit", value: "Gas Limit", comment: ""), subTitle: gasLimit.description), - TransactionAppearance.item(title: NSLocalizedString("confirmPayment.gasFee", value: "Gas Fee", comment: ""), subTitle: fee + " ETH"), + TransactionAppearance.item(title: NSLocalizedString("confirmPayment.to", value: "To", comment: ""), subTitle: viewModel.addressText), + TransactionAppearance.item(title: NSLocalizedString("confirmPayment.gasLimit", value: "Gas Limit", comment: ""), subTitle: viewModel.gasLimiText), + TransactionAppearance.item(title: NSLocalizedString("confirmPayment.gasFee", value: "Gas Fee", comment: ""), subTitle: viewModel.feeText), ] for item in items { @@ -103,11 +108,13 @@ class ConfirmPaymentViewController: UIViewController { @objc func send() { self.displayLoading() + let amount = viewModel.amount + switch transaction.transferType { case .ether: self.sendTransactionCoordinator.send( address: transaction.address, - value: transaction.amount, + value: amount, configuration: self.configuration ) { [weak self] result in guard let `self` = self else { return } @@ -123,7 +130,7 @@ class ConfirmPaymentViewController: UIViewController { self.sendTransactionCoordinator.send( contract: token.address, to: transaction.address, - amount: transaction.amount, + amount: amount, decimals: token.decimals, configuration: self.configuration ) { [weak self] result in diff --git a/Trust/Transfer/Controllers/RequestViewController.swift b/Trust/Transfer/ViewControllers/RequestViewController.swift similarity index 100% rename from Trust/Transfer/Controllers/RequestViewController.swift rename to Trust/Transfer/ViewControllers/RequestViewController.swift diff --git a/Trust/Transfer/Controllers/SendViewController.swift b/Trust/Transfer/ViewControllers/SendViewController.swift similarity index 100% rename from Trust/Transfer/Controllers/SendViewController.swift rename to Trust/Transfer/ViewControllers/SendViewController.swift diff --git a/Trust/Transfer/Controllers/TransactionConfigurationViewController.swift b/Trust/Transfer/ViewControllers/TransactionConfigurationViewController.swift similarity index 100% rename from Trust/Transfer/Controllers/TransactionConfigurationViewController.swift rename to Trust/Transfer/ViewControllers/TransactionConfigurationViewController.swift diff --git a/Trust/Transfer/ViewModels/ConfirmPaymentViewModel.swift b/Trust/Transfer/ViewModels/ConfirmPaymentViewModel.swift new file mode 100644 index 000000000..646fe5128 --- /dev/null +++ b/Trust/Transfer/ViewModels/ConfirmPaymentViewModel.swift @@ -0,0 +1,52 @@ +// Copyright SIX DAY LLC. All rights reserved. + +import Foundation +import BigInt + +struct ConfirmPaymentViewModel { + + let transaction: UnconfirmedTransaction + let currentBalance: Double? + let configuration: TransactionConfiguration + let config: Config + + init( + transaction: UnconfirmedTransaction, + currentBalance: Double?, + configuration: TransactionConfiguration, + config: Config = Config() + ) { + self.transaction = transaction + self.currentBalance = currentBalance + self.configuration = configuration + self.config = config + } + + private var totalFee: BigInt { + return configuration.speed.gasPrice * configuration.speed.gasLimit + } + + private var gasLimit: BigInt { + return configuration.speed.gasLimit + } + + private var fee: String { + return EthereumConverter.from(value: totalFee, to: .ether, minimumFractionDigits: 6) + } + + var amount: Double { + return transaction.amount + } + + var addressText: String { + return transaction.address.address + } + + var feeText: String { + return fee.description + " \(config.server.symbol)" + } + + var gasLimiText: String { + return gasLimit.description + } +}