diff --git a/Trust/AppCoordinator.swift b/Trust/AppCoordinator.swift index 5e05d73d1..849d0a650 100644 --- a/Trust/AppCoordinator.swift +++ b/Trust/AppCoordinator.swift @@ -16,6 +16,16 @@ class AppCoordinator: NSObject, Coordinator { private var keystore: Keystore private var appTracker = AppTracker() var coordinators: [Coordinator] = [] + var inCoordinator: InCoordinator? { + return coordinators.first { $0 is InCoordinator } as? InCoordinator + } + var ethPrice: Subscribable? { + if let inCoordinator = inCoordinator { + return inCoordinator.ethPrice + } else { + return nil + } + } init( window: UIWindow, keystore: Keystore, @@ -95,15 +105,6 @@ class AppCoordinator: NSObject, Coordinator { ) } - func getPriceOfEther() -> Double { - let incoordinator = coordinators.first { $0 is InCoordinator } - if let incoordinator = incoordinator as? InCoordinator { - return incoordinator.getPriceOfEther() - } else { - return 0 - } - } - func importPaidSignedOrder(signedOrder: SignedOrder, tokenObject: TokenObject, completion: @escaping (Bool) -> Void) { let inCoordinatorInstance = coordinators.first { $0 is InCoordinator diff --git a/Trust/AppDelegate.swift b/Trust/AppDelegate.swift index 1bdbed87f..14e2481c2 100644 --- a/Trust/AppDelegate.swift +++ b/Trust/AppDelegate.swift @@ -1,5 +1,4 @@ // Copyright SIX DAY LLC. All rights reserved. - import UIKit import Lokalise import Branch @@ -86,7 +85,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele let url = userActivity.webpageURL universalLinkCoordinator = UniversalLinkCoordinator() - universalLinkCoordinator.price = appCoordinator.getPriceOfEther() + universalLinkCoordinator.ethPrice = appCoordinator.ethPrice universalLinkCoordinator.delegate = self universalLinkCoordinator.start() let handled = universalLinkCoordinator.handleUniversalLink(url: url) diff --git a/Trust/EtherClient/CoinMarket/Types/CoinTicker.swift b/Trust/EtherClient/CoinMarket/Types/CoinTicker.swift index f94c30a8d..2bd6764b8 100644 --- a/Trust/EtherClient/CoinMarket/Types/CoinTicker.swift +++ b/Trust/EtherClient/CoinMarket/Types/CoinTicker.swift @@ -6,11 +6,7 @@ import RealmSwift struct CoinTicker: Codable { let id: String let symbol: String - var price: String { - didSet { - print("here") - } - } + var price: String let percent_change_24h: String let contract: String let image: String diff --git a/Trust/InCoordinator.swift b/Trust/InCoordinator.swift index 3da72264f..52ed53268 100644 --- a/Trust/InCoordinator.swift +++ b/Trust/InCoordinator.swift @@ -36,6 +36,11 @@ class InCoordinator: Coordinator { var keystore: Keystore var config: Config let appTracker: AppTracker + lazy var ethPrice: Subscribable = { + var value = Subscribable(nil) + fetchEthPrice() + return value + }() weak var delegate: InCoordinatorDelegate? var transactionCoordinator: TransactionCoordinator? { return self.coordinators.flatMap { @@ -82,19 +87,25 @@ class InCoordinator: Coordinator { addCoordinator(helpUsCoordinator) } - func getPriceOfEther() -> Double { + func fetchEthPrice() { let keystore = try! EtherKeystore() - let address = keystore.recentlyUsedWallet?.address let migration = MigrationInitializer(account: keystore.recentlyUsedWallet! , chainID: config.chainID) migration.perform() let web3 = self.web3(for: config.server) web3.start() let realm = self.realm(for: migration.config) let tokensStorage = TokensDataStore(realm: realm, account: keystore.recentlyUsedWallet!, config: config, web3: web3) - if let rates = tokensStorage.tickers, let ticker = rates.values.first(where: { $0.symbol == "ETH" }), let price = Double(ticker.price) { - return price - } else { - return 0 + tokensStorage.updatePrices() + + let etherToken = TokensDataStore.etherToken(for: config) + tokensStorage.tokensModel.subscribe {[weak self] tokensModel in + guard let tokens = tokensModel, let eth = tokens.first(where: { $0 == etherToken }) else { + return + } + var ticker = tokensStorage.coinTicker(for: eth) + if let ticker = ticker { + self?.ethPrice.value = Double(ticker.price) + } } } diff --git a/Trust/Market/Coordinators/UniversalLinkCoordinator.swift b/Trust/Market/Coordinators/UniversalLinkCoordinator.swift index bdfedfc40..a9e3e1e4a 100644 --- a/Trust/Market/Coordinators/UniversalLinkCoordinator.swift +++ b/Trust/Market/Coordinators/UniversalLinkCoordinator.swift @@ -16,7 +16,7 @@ class UniversalLinkCoordinator: Coordinator { var coordinators: [Coordinator] = [] weak var delegate: UniversalLinkCoordinatorDelegate? var importTicketViewController: ImportTicketViewController? - var price: Double? + var ethPrice: Subscribable? func start() { preparingToImportUniversalLink() @@ -68,11 +68,28 @@ class UniversalLinkCoordinator: Coordinator { //nil or "" implies free, if using payment server it is always free let etherprice = signedOrder.order.price / 1000000000000000000 let etherPriceDecimal = Decimal(string: etherprice.description)! - self.promptImportUniversalLink( - ticketHolder: ticketHolder, - ethCost: etherPriceDecimal.description, - dollarCost: self.price!.description - ) + if let price = ethPrice { + if let s = price.value { + self.promptImportUniversalLink( + ticketHolder: ticketHolder, + ethCost: etherPriceDecimal.description, + dollarCost: s.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 { + self.promptImportUniversalLink( + ticketHolder: ticketHolder, + ethCost: etherPriceDecimal.description, + dollarCost: s.description) + } + } + } + } else { + //No wallet and should be handled by client code, but we'll just be careful + //TODO pass in error message + showImportError(errorMessage: R.string.localizable.aClaimTicketFailedTitle()) + } } return true } @@ -98,7 +115,7 @@ class UniversalLinkCoordinator: Coordinator { self.promptImportUniversalLink( ticketHolder: ticketHolder, ethCost: "", - dollarCost: self.price!.description + dollarCost: "" ) } return true @@ -110,6 +127,7 @@ class UniversalLinkCoordinator: Coordinator { guard matchedPrefix else { return false } + let signedOrder = UniversalLinkHandler().parseUniversalLink(url: (url?.absoluteString)!) getTicketDetailsAndEcRecover(signedOrder: signedOrder) { result in @@ -303,7 +321,6 @@ extension UniversalLinkCoordinator: ImportTicketViewControllerDelegate { if let signedOrder = viewController.signedOrder, let tokenObj = viewController.tokenObject { importPaidSignedOrder(signedOrder: signedOrder, tokenObject: tokenObj) } - //kkk look for the call to showImportSuccessful(). Need to call this } } } diff --git a/TrustTests/Coordinators/AppCoordinatorTests.swift b/TrustTests/Coordinators/AppCoordinatorTests.swift index 6190d1bc4..f0559b64c 100644 --- a/TrustTests/Coordinators/AppCoordinatorTests.swift +++ b/TrustTests/Coordinators/AppCoordinatorTests.swift @@ -84,4 +84,28 @@ class AppCoordinatorTests: XCTestCase { XCTAssertEqual(1, coordinator.coordinators.count) XCTAssertTrue(coordinator.navigationController.viewControllers[0] is UITabBarController) } + + func testHasInCoordinatorWithWallet() { + let coordinator = AppCoordinator( + window: UIWindow(), + keystore: FakeKeystore( + wallets: [.make()] + ) + ) + + coordinator.start() + + XCTAssertNotNil(coordinator.inCoordinator) + } + + func testHasNoInCoordinatorWithoutWallets() { + let coordinator = AppCoordinator( + window: UIWindow(), + keystore: FakeKeystore() + ) + + coordinator.start() + + XCTAssertNil(coordinator.inCoordinator) + } }