Merge pull request #240 from James-Sangalli/check-balance-before-importing-paid-link

Check balance before importing paid link
pull/244/head
James Sangalli 7 years ago committed by GitHub
commit ae39eb1511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      Trust/AppCoordinator.swift
  2. 1
      Trust/AppDelegate.swift
  3. 15
      Trust/Foundation/Subscribable.swift
  4. 8
      Trust/InCoordinator.swift
  5. 1
      Trust/Localization/en.lproj/Localizable.strings
  6. 12
      Trust/Market/Coordinators/UniversalLinkCoordinator.swift
  7. 10
      Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift

@ -3,6 +3,7 @@
import Foundation import Foundation
import TrustKeystore import TrustKeystore
import UIKit import UIKit
import BigInt
class AppCoordinator: NSObject, Coordinator { class AppCoordinator: NSObject, Coordinator {
let navigationController: UINavigationController let navigationController: UINavigationController
@ -26,6 +27,13 @@ class AppCoordinator: NSObject, Coordinator {
return nil return nil
} }
} }
var ethBalance: Subscribable<BigInt>? {
if let inCoordinator = inCoordinator {
return inCoordinator.ethBalance
} else {
return nil
}
}
init( init(
window: UIWindow, window: UIWindow,
keystore: Keystore, keystore: Keystore,

@ -86,6 +86,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
let url = userActivity.webpageURL let url = userActivity.webpageURL
universalLinkCoordinator = UniversalLinkCoordinator() universalLinkCoordinator = UniversalLinkCoordinator()
universalLinkCoordinator.ethPrice = appCoordinator.ethPrice universalLinkCoordinator.ethPrice = appCoordinator.ethPrice
universalLinkCoordinator.ethBalance = appCoordinator.ethBalance
universalLinkCoordinator.delegate = self universalLinkCoordinator.delegate = self
universalLinkCoordinator.start() universalLinkCoordinator.start()
let handled = universalLinkCoordinator.handleUniversalLink(url: url) let handled = universalLinkCoordinator.handleUniversalLink(url: url)

@ -5,6 +5,7 @@ import Foundation
open class Subscribable<T> { open class Subscribable<T> {
private var _value: T? private var _value: T?
private var _subscribers: [(T?) -> Void] = [] private var _subscribers: [(T?) -> Void] = []
private var _oneTimeSubscribers: [(T) -> Void] = []
open var value: T? { open var value: T? {
get { get {
return _value return _value
@ -14,6 +15,13 @@ open class Subscribable<T> {
for f in _subscribers { for f in _subscribers {
f(value) f(value)
} }
if let value = value {
for f in _oneTimeSubscribers {
f(value)
}
_oneTimeSubscribers = []
}
} }
} }
@ -27,4 +35,11 @@ open class Subscribable<T> {
} }
_subscribers.append(subscribe) _subscribers.append(subscribe)
} }
open func subscribeOnce(_ subscribe: @escaping (T) -> Void) {
if let value = _value {
subscribe(value)
} else {
_oneTimeSubscribers.append(subscribe)
}
}
} }

@ -41,6 +41,7 @@ class InCoordinator: Coordinator {
fetchEthPrice() fetchEthPrice()
return value return value
}() }()
var ethBalance = Subscribable<BigInt>(nil)
weak var delegate: InCoordinatorDelegate? weak var delegate: InCoordinatorDelegate?
var transactionCoordinator: TransactionCoordinator? { var transactionCoordinator: TransactionCoordinator? {
return self.coordinators.flatMap { return self.coordinators.flatMap {
@ -212,8 +213,11 @@ class InCoordinator: Coordinator {
guard let tokens = tokensModel, let eth = tokens.first(where: { $0 == etherToken }) else { guard let tokens = tokensModel, let eth = tokens.first(where: { $0 == etherToken }) else {
return return
} }
guard let balance = BigInt(eth.value), !(balance.isZero) else { return } if let balance = BigInt(eth.value) {
self?.promptBackupWallet() self?.ethBalance.value = BigInt(eth.value)
guard !(balance.isZero) else { return }
self?.promptBackupWallet()
}
} }
} }

@ -298,6 +298,7 @@
"a.claim.ticket.title" = "Importing tickets"; "a.claim.ticket.title" = "Importing tickets";
"a.claim.ticket.success.title" = "Your ticket has been transferred and the balance will be updated shortly"; "a.claim.ticket.success.title" = "Your ticket has been transferred and the balance will be updated shortly";
"a.claim.ticket.failed.title" = "Invalid ticket link"; "a.claim.ticket.failed.title" = "Invalid ticket link";
"a.claim.ticket.failed.notEnoughEth.title" = "You do not have enough ETH to import this ticket";
"a.claim.ticket.validating.title" = "Processing..."; "a.claim.ticket.validating.title" = "Processing...";
"a.claim.ticket.promptImport.title" = "Import?"; "a.claim.ticket.promptImport.title" = "Import?";
"a.claim.ticket.inProgress.title" = "Importing tickets..."; "a.claim.ticket.inProgress.title" = "Importing tickets...";

@ -17,6 +17,7 @@ class UniversalLinkCoordinator: Coordinator {
weak var delegate: UniversalLinkCoordinatorDelegate? weak var delegate: UniversalLinkCoordinatorDelegate?
var importTicketViewController: ImportTicketViewController? var importTicketViewController: ImportTicketViewController?
var ethPrice: Subscribable<Double>? var ethPrice: Subscribable<Double>?
var ethBalance: Subscribable<BigInt>?
func start() func start()
{ {
@ -139,7 +140,15 @@ class UniversalLinkCoordinator: Coordinator {
if let goodResult = result { if let goodResult = result {
if signedOrder.order.price > 0 if signedOrder.order.price > 0
{ {
let success = self.handlePaidUniversalLink(signedOrder: signedOrder, ticketHolder: goodResult) if let balance = self.ethBalance {
balance.subscribeOnce { value in
if value > signedOrder.order.price {
let success = self.handlePaidUniversalLink(signedOrder: signedOrder, ticketHolder: goodResult)
} else {
self.showImportError(errorMessage: R.string.localizable.aClaimTicketFailedNotEnoughEthTitle())
}
}
}
} }
else else
{ {
@ -150,6 +159,7 @@ class UniversalLinkCoordinator: Coordinator {
} }
} }
else { else {
//TODO localize
self.showImportError(errorMessage: "Invalid Link, please try again") self.showImportError(errorMessage: "Invalid Link, please try again")
} }

@ -149,13 +149,17 @@ struct ImportTicketViewControllerViewModel {
return R.string.localizable.aClaimTicketInProgressTitle() return R.string.localizable.aClaimTicketInProgressTitle()
case .succeeded: case .succeeded:
return R.string.localizable.aClaimTicketSuccessTitle() return R.string.localizable.aClaimTicketSuccessTitle()
case .failed: case .failed(let errorMessage):
return R.string.localizable.aClaimTicketFailedTitle() return errorMessage
} }
} }
var statusColor: UIColor { var statusColor: UIColor {
return UIColor(red: 20, green: 20, blue: 20) if case let .failed = state {
return Colors.appRed
} else {
return UIColor(red: 20, green: 20, blue: 20)
}
} }
var statusFont: UIFont { var statusFont: UIFont {

Loading…
Cancel
Save