From 07308318fea4a40ba5e73139fffa8e31bdf2997b Mon Sep 17 00:00:00 2001 From: Hwee-Boon Yar Date: Sat, 28 Apr 2018 16:52:36 +0800 Subject: [PATCH 1/3] Support one-time subscriptions (in code) --- Trust/Foundation/Subscribable.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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) + } + } } From 730abcbbb9f858c34c31daa3c6f5006880711b35 Mon Sep 17 00:00:00 2001 From: Hwee-Boon Yar Date: Sat, 28 Apr 2018 16:57:19 +0800 Subject: [PATCH 2/3] Check balance against sell link price --- Trust/AppCoordinator.swift | 8 ++++++++ Trust/AppDelegate.swift | 1 + Trust/InCoordinator.swift | 8 ++++++-- Trust/Localization/en.lproj/Localizable.strings | 1 + .../Coordinators/UniversalLinkCoordinator.swift | 12 +++++++++++- .../ImportTicketViewControllerViewModel.swift | 4 ++-- 6 files changed, 29 insertions(+), 5 deletions(-) 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/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 571b1c86b..7b8a68563 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..bd9570ba2 100644 --- a/Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift +++ b/Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift @@ -149,8 +149,8 @@ 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 } } From 63642a2aded73c5fccbb2ba527f88fa639579f16 Mon Sep 17 00:00:00 2001 From: Hwee-Boon Yar Date: Sat, 28 Apr 2018 17:00:34 +0800 Subject: [PATCH 3/3] Show import ticket link errors in red --- .../ViewModels/ImportTicketViewControllerViewModel.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift b/Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift index bd9570ba2..ccd0300c7 100644 --- a/Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift +++ b/Trust/Market/ViewModels/ImportTicketViewControllerViewModel.swift @@ -155,7 +155,11 @@ struct ImportTicketViewControllerViewModel { } 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 {