Merge pull request #265 from James-Sangalli/show-ticket-and-price-details-in-ticket-import-even-if-low-balance

Show ticket details and price when prompting user to import ticket, even if the wallet's ETH balance is too low
pull/267/head
James Sangalli 7 years ago committed by GitHub
commit 1f9a7a6cf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 78
      Trust/Market/Coordinators/UniversalLinkCoordinator.swift
  2. 3
      Trust/Market/ViewControllers/ImportTicketViewController.swift
  3. 12
      Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift

@ -64,28 +64,19 @@ class UniversalLinkCoordinator: Coordinator {
isStormBird: true
)
}
//nil or "" implies free, if using payment server it is always free
let etherprice = signedOrder.order.price /// 1000000000000000000
let divideAmount = Decimal(string: "1000000000000000000")!
let etherPriceDecimal = Decimal(string: etherprice.description)! / divideAmount
if let price = ethPrice {
if let s = price.value {
let dollarCost = Decimal(s) * etherPriceDecimal
self.promptImportUniversalLink(
ticketHolder: ticketHolder,
ethCost: etherPriceDecimal.description,
dollarCost: dollarCost.description
)
}
else
{
price.subscribe { value in
//TODO good to test if there's a leak here if user has already cancelled before this
if let s = price.value {
let dollarCost = Decimal(s) * etherPriceDecimal
self.promptImportUniversalLink(
let ethCost = self.convert(ethCost: signedOrder.order.price)
self.promptImportUniversalLink(
ticketHolder: ticketHolder,
ethCost: ethCost.description
)
price.subscribe { [weak self] value in
if let price = price.value {
if let celf = self {
let (ethCost, dollarCost) = celf.convert(ethCost: signedOrder.order.price, rate: price)
celf.promptImportUniversalLink(
ticketHolder: ticketHolder,
ethCost: etherPriceDecimal.description,
ethCost: ethCost.description,
dollarCost: dollarCost.description
)
}
@ -145,7 +136,20 @@ class UniversalLinkCoordinator: Coordinator {
if value > signedOrder.order.price {
let success = self.handlePaidUniversalLink(signedOrder: signedOrder, ticketHolder: goodResult)
} else {
self.showImportError(errorMessage: R.string.localizable.aClaimTicketFailedNotEnoughEthTitle())
if let price = self.ethPrice {
if price.value == nil {
let ethCost = self.convert(ethCost: signedOrder.order.price)
self.showImportError(errorMessage: R.string.localizable.aClaimTicketFailedNotEnoughEthTitle(), ticketHolder: goodResult, ethCost: ethCost.description)
}
price.subscribe { [weak self] value in
if let celf = self {
if let price = price.value {
let (ethCost, dollarCost) = celf.convert(ethCost: signedOrder.order.price, rate: price)
celf.showImportError(errorMessage: R.string.localizable.aClaimTicketFailedNotEnoughEthTitle(), ticketHolder: goodResult, ethCost: ethCost.description, dollarCost: dollarCost.description)
}
}
}
}
}
}
}
@ -252,16 +256,20 @@ class UniversalLinkCoordinator: Coordinator {
private func updateImportTicketController(with state: ImportTicketViewControllerViewModel.State, ticketHolder: TicketHolder? = nil, ethCost: String? = nil, dollarCost: String? = nil) {
if let vc = importTicketViewController, var viewModel = vc.viewModel {
viewModel.state = state
if let ticketHolder = ticketHolder, let ethCost = ethCost, let dollarCost = dollarCost {
viewModel.ticketHolder = ticketHolder
viewModel.ethCost = ethCost
viewModel.dollarCost = dollarCost
}
if let ticketHolder = ticketHolder {
viewModel.ticketHolder = ticketHolder
}
if let ethCost = ethCost {
viewModel.ethCost = ethCost
}
if let dollarCost = dollarCost {
viewModel.dollarCost = dollarCost
}
vc.configure(viewModel: viewModel)
}
}
private func promptImportUniversalLink(ticketHolder: TicketHolder, ethCost: String, dollarCost: String) {
private func promptImportUniversalLink(ticketHolder: TicketHolder, ethCost: String, dollarCost: String? = nil) {
updateImportTicketController(with: .promptImport, ticketHolder: ticketHolder, ethCost: ethCost, dollarCost: dollarCost)
}
@ -277,8 +285,8 @@ class UniversalLinkCoordinator: Coordinator {
coordinator.start()
}
private func showImportError(errorMessage: String) {
updateImportTicketController(with: .failed(errorMessage: errorMessage))
private func showImportError(errorMessage: String, ticketHolder: TicketHolder? = nil, ethCost: String? = nil, dollarCost: String? = nil) {
updateImportTicketController(with: .failed(errorMessage: errorMessage), ticketHolder: ticketHolder, ethCost: ethCost, dollarCost: dollarCost)
}
//handling free transfers, sell links cannot be handled here
@ -313,6 +321,18 @@ class UniversalLinkCoordinator: Coordinator {
}
}
}
private func convert(ethCost: BigUInt, rate: Double) -> (ethCost: Decimal, dollarCost: Decimal) {
let etherCostDecimal = convert(ethCost: ethCost)
let dollarCost = Decimal(rate) * etherCostDecimal
return (etherCostDecimal, dollarCost)
}
private func convert(ethCost: BigUInt) -> Decimal {
let divideAmount = Decimal(string: "1000000000000000000")!
let etherCostDecimal = Decimal(string: ethCost.description)! / divideAmount
return etherCostDecimal
}
}
extension UniversalLinkCoordinator: ImportTicketViewControllerDelegate {

@ -193,6 +193,7 @@ class ImportTicketViewController: UIViewController {
dollarCostLabelLabel.font = viewModel.dollarCostLabelLabelFont
dollarCostLabelLabel.textAlignment = .center
dollarCostLabelLabel.text = viewModel.dollarCostLabelLabelText
dollarCostLabelLabel.isHidden = viewModel.hideDollarCost
dollarCostLabel.textColor = viewModel.dollarCostLabelColor
dollarCostLabel.font = viewModel.dollarCostLabelFont
@ -200,7 +201,7 @@ class ImportTicketViewController: UIViewController {
dollarCostLabel.text = viewModel.dollarCostLabelText
dollarCostLabel.backgroundColor = viewModel.dollarCostLabelBackgroundColor
dollarCostLabel.layer.masksToBounds = true
dollarCostLabel.isHidden = !viewModel.showDollarCostLabel
dollarCostLabel.isHidden = viewModel.hideDollarCost
activityIndicator.color = viewModel.activityIndicatorColor

@ -220,10 +220,6 @@ struct ImportTicketViewControllerViewModel {
}
}
var showDollarCostLabel: Bool {
return !transactionIsFree
}
var dollarCostLabelBackgroundColor: UIColor {
return UIColor(red: 236, green: 236, blue: 236)
}
@ -290,6 +286,14 @@ struct ImportTicketViewControllerViewModel {
return ethCost.isEmpty
}
var hideDollarCost: Bool {
if transactionIsFree {
return true
}
guard let dollarCost = dollarCost else { return true }
return dollarCost.trimmed.isEmpty
}
init(state: State) {
self.state = state
}

Loading…
Cancel
Save