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

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

@ -5,6 +5,7 @@ import Foundation
open class Subscribable<T> {
private var _value: T?
private var _subscribers: [(T?) -> Void] = []
private var _oneTimeSubscribers: [(T) -> Void] = []
open var value: T? {
get {
return _value
@ -14,6 +15,13 @@ open class Subscribable<T> {
for f in _subscribers {
f(value)
}
if let value = value {
for f in _oneTimeSubscribers {
f(value)
}
_oneTimeSubscribers = []
}
}
}
@ -27,4 +35,11 @@ open class Subscribable<T> {
}
_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()
return value
}()
var ethBalance = Subscribable<BigInt>(nil)
weak var delegate: InCoordinatorDelegate?
var transactionCoordinator: TransactionCoordinator? {
return self.coordinators.flatMap {
@ -212,8 +213,11 @@ class InCoordinator: Coordinator {
guard let tokens = tokensModel, let eth = tokens.first(where: { $0 == etherToken }) else {
return
}
guard let balance = BigInt(eth.value), !(balance.isZero) else { return }
self?.promptBackupWallet()
if let balance = BigInt(eth.value) {
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.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.notEnoughEth.title" = "You do not have enough ETH to import this ticket";
"a.claim.ticket.validating.title" = "Processing...";
"a.claim.ticket.promptImport.title" = "Import?";
"a.claim.ticket.inProgress.title" = "Importing tickets...";

@ -17,6 +17,7 @@ class UniversalLinkCoordinator: Coordinator {
weak var delegate: UniversalLinkCoordinatorDelegate?
var importTicketViewController: ImportTicketViewController?
var ethPrice: Subscribable<Double>?
var ethBalance: Subscribable<BigInt>?
func start()
{
@ -139,7 +140,15 @@ class UniversalLinkCoordinator: Coordinator {
if let goodResult = result {
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
{
@ -150,6 +159,7 @@ class UniversalLinkCoordinator: Coordinator {
}
}
else {
//TODO localize
self.showImportError(errorMessage: "Invalid Link, please try again")
}

@ -149,13 +149,17 @@ struct ImportTicketViewControllerViewModel {
return R.string.localizable.aClaimTicketInProgressTitle()
case .succeeded:
return R.string.localizable.aClaimTicketSuccessTitle()
case .failed:
return R.string.localizable.aClaimTicketFailedTitle()
case .failed(let errorMessage):
return errorMessage
}
}
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 {

Loading…
Cancel
Save