diff --git a/Trust/AppCoordinator.swift b/Trust/AppCoordinator.swift index 849d0a650..a32633a26 100644 --- a/Trust/AppCoordinator.swift +++ b/Trust/AppCoordinator.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? { + if let inCoordinator = inCoordinator { + return inCoordinator.ethBalance + } else { + return nil + } + } init( window: UIWindow, keystore: Keystore, diff --git a/Trust/AppDelegate.swift b/Trust/AppDelegate.swift index 14e2481c2..7c8228281 100644 --- a/Trust/AppDelegate.swift +++ b/Trust/AppDelegate.swift @@ -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) diff --git a/Trust/Foundation/Subscribable.swift b/Trust/Foundation/Subscribable.swift index 0a45b7a24..dabd186ab 100644 --- a/Trust/Foundation/Subscribable.swift +++ b/Trust/Foundation/Subscribable.swift @@ -5,6 +5,7 @@ import Foundation open class Subscribable { 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 { for f in _subscribers { f(value) } + + if let value = value { + for f in _oneTimeSubscribers { + f(value) + } + _oneTimeSubscribers = [] + } } } @@ -27,4 +35,11 @@ open class Subscribable { } _subscribers.append(subscribe) } + open func subscribeOnce(_ subscribe: @escaping (T) -> Void) { + if let value = _value { + subscribe(value) + } else { + _oneTimeSubscribers.append(subscribe) + } + } } diff --git a/Trust/InCoordinator.swift b/Trust/InCoordinator.swift index 59d07d5c7..c3d1ebabb 100644 --- a/Trust/InCoordinator.swift +++ b/Trust/InCoordinator.swift @@ -41,6 +41,7 @@ class InCoordinator: Coordinator { fetchEthPrice() return value }() + var ethBalance = Subscribable(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() + } } } diff --git a/Trust/Localization/en.lproj/Localizable.strings b/Trust/Localization/en.lproj/Localizable.strings index 195a9ca76..0ceebb94c 100644 --- a/Trust/Localization/en.lproj/Localizable.strings +++ b/Trust/Localization/en.lproj/Localizable.strings @@ -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..."; diff --git a/Trust/Market/Coordinators/UniversalLinkCoordinator.swift b/Trust/Market/Coordinators/UniversalLinkCoordinator.swift index e92a8efba..754a58c6d 100644 --- a/Trust/Market/Coordinators/UniversalLinkCoordinator.swift +++ b/Trust/Market/Coordinators/UniversalLinkCoordinator.swift @@ -17,6 +17,7 @@ class UniversalLinkCoordinator: Coordinator { weak var delegate: UniversalLinkCoordinatorDelegate? var importTicketViewController: ImportTicketViewController? var ethPrice: Subscribable? + var ethBalance: Subscribable? 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") } diff --git a/Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift b/Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift index a4d07f35f..ccd0300c7 100644 --- a/Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift +++ b/Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift @@ -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 {