diff --git a/ios/BlueWalletWatch Extension/ExtensionDelegate.swift b/ios/BlueWalletWatch Extension/ExtensionDelegate.swift index b477b2281..412b89dff 100644 --- a/ios/BlueWalletWatch Extension/ExtensionDelegate.swift +++ b/ios/BlueWalletWatch Extension/ExtensionDelegate.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/6/19. -// Copyright © 2019 Facebook. All rights reserved. + // import WatchKit diff --git a/ios/BlueWalletWatch Extension/InterfaceController.swift b/ios/BlueWalletWatch Extension/InterfaceController.swift index 7c03fee44..7b6d093b3 100644 --- a/ios/BlueWalletWatch Extension/InterfaceController.swift +++ b/ios/BlueWalletWatch Extension/InterfaceController.swift @@ -3,79 +3,72 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/6/19. -// Copyright © 2019 Facebook. All rights reserved. // import WatchKit import WatchConnectivity import Foundation -class InterfaceController: WKInterfaceController { - +class InterfaceController: WKInterfaceController, WCSessionDelegate { + @IBOutlet weak var walletsTable: WKInterfaceTable! @IBOutlet weak var noWalletsAvailableLabel: WKInterfaceLabel! override func awake(withContext context: Any?) { super.awake(withContext: context) - if let contextUnwrapped = context as? [String: Any] { - WatchDataSource.shared.processData(data: contextUnwrapped) - } - if WCSession.isSupported() { - print("Activating watch session") - WCSession.default.delegate = self - WCSession.default.activate() - } + setupSession() + processContextData(context) } override func willActivate() { - // This method is called when watch view controller is about to be visible to user super.willActivate() - - if (WatchDataSource.shared.wallets.isEmpty) { - noWalletsAvailableLabel.setHidden(false) - } else { - processWalletsTable() - } - NotificationCenter.default.addObserver(self, selector: #selector(processWalletsTable), name: WatchDataSource.NotificationName.dataUpdated, object: nil) + updateUI() + NotificationCenter.default.addObserver(self, selector: #selector(updateUI), name: WatchDataSource.NotificationName.dataUpdated, object: nil) } - @objc private func processWalletsTable() { - walletsTable.setNumberOfRows(WatchDataSource.shared.wallets.count, withRowType: WalletInformation.identifier) + private func setupSession() { + guard WCSession.isSupported() else { return } + WCSession.default.delegate = self + WCSession.default.activate() + } + + private func processContextData(_ context: Any?) { + guard let contextUnwrapped = context as? [String: Any] else { return } + WatchDataSource.shared.processData(data: contextUnwrapped) + } + + @objc private func updateUI() { + let wallets = WatchDataSource.shared.wallets + let isEmpty = wallets.isEmpty + noWalletsAvailableLabel.setHidden(!isEmpty) + walletsTable.setHidden(isEmpty) - for index in 0.. Any? { - return rowIndex; + return rowIndex } -} - -extension InterfaceController: WCSessionDelegate { - func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) { WatchDataSource.shared.processData(data: applicationContext) } - func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) { WatchDataSource.shared.processData(data: userInfo) } func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) { - WatchDataSource.shared.companionWalletsInitialized = activationState == .activated if activationState == .activated { WatchDataSource.shared.processWalletsData(walletsInfo: WCSession.default.applicationContext) } @@ -90,3 +83,13 @@ extension InterfaceController: WCSessionDelegate { } } + +// WalletInformation extension for configuration +extension WalletInformation { + func configure(with wallet: Wallet) { + walletBalanceLabel.setHidden(wallet.hideBalance) + name = wallet.label + balance = wallet.hideBalance ? "" : wallet.balance + type = WalletGradient(rawValue: wallet.type) ?? .SegwitHD + } +} diff --git a/ios/BlueWalletWatch Extension/NotificationController.swift b/ios/BlueWalletWatch Extension/NotificationController.swift index c9b649e1c..772616947 100644 --- a/ios/BlueWalletWatch Extension/NotificationController.swift +++ b/ios/BlueWalletWatch Extension/NotificationController.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/6/19. -// Copyright © 2019 Facebook. All rights reserved. + // import WatchKit diff --git a/ios/BlueWalletWatch Extension/NumericKeypadInterfaceController.swift b/ios/BlueWalletWatch Extension/NumericKeypadInterfaceController.swift index 1a78fddf3..55d57a280 100644 --- a/ios/BlueWalletWatch Extension/NumericKeypadInterfaceController.swift +++ b/ios/BlueWalletWatch Extension/NumericKeypadInterfaceController.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/23/19. -// Copyright © 2019 Facebook. All rights reserved. + // import WatchKit diff --git a/ios/BlueWalletWatch Extension/Objects/Transaction.swift b/ios/BlueWalletWatch Extension/Objects/Transaction.swift index d7fca13af..6db26b74e 100644 --- a/ios/BlueWalletWatch Extension/Objects/Transaction.swift +++ b/ios/BlueWalletWatch Extension/Objects/Transaction.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/13/19. -// Copyright © 2019 Facebook. All rights reserved. + // import Foundation diff --git a/ios/BlueWalletWatch Extension/Objects/TransactionTableRow.swift b/ios/BlueWalletWatch Extension/Objects/TransactionTableRow.swift index ca7989016..c41b69732 100644 --- a/ios/BlueWalletWatch Extension/Objects/TransactionTableRow.swift +++ b/ios/BlueWalletWatch Extension/Objects/TransactionTableRow.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/10/19. -// Copyright © 2019 Facebook. All rights reserved. + // import WatchKit @@ -50,3 +50,13 @@ class TransactionTableRow: NSObject { } } + +// TransactionTableRow extension for configuration + extension TransactionTableRow { + func configure(with transaction: Transaction) { + amount = transaction.amount + type = transaction.type + memo = transaction.memo + time = transaction.time + } + } diff --git a/ios/BlueWalletWatch Extension/Objects/Wallet.swift b/ios/BlueWalletWatch Extension/Objects/Wallet.swift index 6061fc89d..aaa5a252d 100644 --- a/ios/BlueWalletWatch Extension/Objects/Wallet.swift +++ b/ios/BlueWalletWatch Extension/Objects/Wallet.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/13/19. -// Copyright © 2019 Facebook. All rights reserved. + // import Foundation diff --git a/ios/BlueWalletWatch Extension/Objects/WalletGradient.swift b/ios/BlueWalletWatch Extension/Objects/WalletGradient.swift index 63b74ecc8..d863c7000 100644 --- a/ios/BlueWalletWatch Extension/Objects/WalletGradient.swift +++ b/ios/BlueWalletWatch Extension/Objects/WalletGradient.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/23/19. -// Copyright © 2019 Facebook. All rights reserved. + // import Foundation diff --git a/ios/BlueWalletWatch Extension/Objects/WalletInformation.swift b/ios/BlueWalletWatch Extension/Objects/WalletInformation.swift index 47c369533..8b6bac70b 100644 --- a/ios/BlueWalletWatch Extension/Objects/WalletInformation.swift +++ b/ios/BlueWalletWatch Extension/Objects/WalletInformation.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/10/19. -// Copyright © 2019 Facebook. All rights reserved. + // import WatchKit diff --git a/ios/BlueWalletWatch Extension/Objects/WatchDataSource.swift b/ios/BlueWalletWatch Extension/Objects/WatchDataSource.swift index 172717de6..a44e94e21 100644 --- a/ios/BlueWalletWatch Extension/Objects/WatchDataSource.swift +++ b/ios/BlueWalletWatch Extension/Objects/WatchDataSource.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/20/19. -// Copyright © 2019 Facebook. All rights reserved. + // diff --git a/ios/BlueWalletWatch Extension/ReceiveInterfaceController.swift b/ios/BlueWalletWatch Extension/ReceiveInterfaceController.swift index d36a5916f..d4de4d0a9 100644 --- a/ios/BlueWalletWatch Extension/ReceiveInterfaceController.swift +++ b/ios/BlueWalletWatch Extension/ReceiveInterfaceController.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/12/19. -// Copyright © 2019 Facebook. All rights reserved. + // import WatchKit diff --git a/ios/BlueWalletWatch Extension/SpecifyInterfaceController.swift b/ios/BlueWalletWatch Extension/SpecifyInterfaceController.swift index cf13fbea9..b6038e855 100644 --- a/ios/BlueWalletWatch Extension/SpecifyInterfaceController.swift +++ b/ios/BlueWalletWatch Extension/SpecifyInterfaceController.swift @@ -3,7 +3,7 @@ // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/23/19. -// Copyright © 2019 Facebook. All rights reserved. + // import WatchKit diff --git a/ios/BlueWalletWatch Extension/ViewQRCodefaceController.swift b/ios/BlueWalletWatch Extension/ViewQRCodefaceController.swift index 44f634af0..f25878477 100644 --- a/ios/BlueWalletWatch Extension/ViewQRCodefaceController.swift +++ b/ios/BlueWalletWatch Extension/ViewQRCodefaceController.swift @@ -1,10 +1,6 @@ -// -// ReceiveInterfaceController.swift // BlueWalletWatch Extension // -// Created by Marcos Rodriguez on 3/12/19. -// Copyright © 2019 Facebook. All rights reserved. -// +// Created by Marcos Rodriguez on 3/11/19. import WatchKit import Foundation @@ -15,91 +11,85 @@ class ViewQRCodefaceController: WKInterfaceController { static let identifier = "ViewQRCodefaceController" @IBOutlet weak var imageInterface: WKInterfaceImage! @IBOutlet weak var addressLabel: WKInterfaceLabel! + var address: String? { didSet { - if let address = address, !address.isEmpty{ - userActivity.userInfo = [HandOffUserInfoKey.Xpub.rawValue: address] - userActivity.becomeCurrent() - } + updateQRCode() + updateUserActivity() } } + private var interfaceMode = InterfaceMode.Address private let userActivity: NSUserActivity = NSUserActivity(activityType: HandoffIdentifier.Xpub.rawValue) override func awake(withContext context: Any?) { super.awake(withContext: context) - userActivity.title = HandOffTitle.Xpub.rawValue - userActivity.requiredUserInfoKeys = [HandOffUserInfoKey.Xpub.rawValue] - userActivity.isEligibleForHandoff = true + configureUserActivity() guard let passedContext = context as? String else { pop() return } address = passedContext addressLabel.setText(passedContext) + toggleViewButtonPressed() + } - DispatchQueue.main.async { - guard let cgImage = EFQRCode.generate( - for: passedContext) else { - return - } - let image = UIImage(cgImage: cgImage) - self.imageInterface.setImage(nil) - self.imageInterface.setImage(image) - } - if #available(watchOSApplicationExtension 6.0, *) { - if let image = UIImage(systemName: "textformat.subscript") { - addMenuItem(with: image, title: "Address", action:#selector(toggleViewButtonPressed)) - } else { - addMenuItem(with: .shuffle, title: "Address", action: #selector(toggleViewButtonPressed)) - } + private func configureUserActivity() { + userActivity.title = HandOffTitle.Xpub.rawValue + userActivity.requiredUserInfoKeys = [HandOffUserInfoKey.Xpub.rawValue] + userActivity.isEligibleForHandoff = true + } + + private func updateUserActivity() { + if let address = address, !address.isEmpty { + userActivity.userInfo = [HandOffUserInfoKey.Xpub.rawValue: address] + userActivity.becomeCurrent() } else { - addMenuItem(with: .shuffle, title: "Address", action: #selector(toggleViewButtonPressed)) + userActivity.invalidate() } } - @IBAction @objc func toggleViewButtonPressed() { - clearAllMenuItems() - switch interfaceMode { - case .Address: - addressLabel.setHidden(false) - imageInterface.setHidden(true) - if #available(watchOSApplicationExtension 6.0, *) { - if let image = UIImage(systemName: "qrcode") { - addMenuItem(with: image, title: "QR Code", action:#selector(toggleViewButtonPressed)) - } else { - addMenuItem(with: .shuffle, title: "QR Code", action: #selector(toggleViewButtonPressed)) - } - } else { - addMenuItem(with: .shuffle, title: "QR Code", action: #selector(toggleViewButtonPressed)) - + + private func updateQRCode() { + guard let address = address, !address.isEmpty else { + imageInterface.setImage(nil) + return + } + DispatchQueue.global(qos: .userInteractive).async { + guard let cgImage = EFQRCode.generate(for: address) else { + return } - case .QRCode: - addressLabel.setHidden(true) - imageInterface.setHidden(false) - if #available(watchOSApplicationExtension 6.0, *) { - if let image = UIImage(systemName: "textformat.subscript") { - addMenuItem(with: image, title: "Address", action:#selector(toggleViewButtonPressed)) - } else { - addMenuItem(with: .shuffle, title: "Address", action: #selector(toggleViewButtonPressed)) - } - } else { - addMenuItem(with: .shuffle, title: "Address", action: #selector(toggleViewButtonPressed)) + DispatchQueue.main.async { + let image = UIImage(cgImage: cgImage) + self.imageInterface.setImage(image) } } + } + + @IBAction @objc func toggleViewButtonPressed() { + clearAllMenuItems() interfaceMode = interfaceMode == .QRCode ? .Address : .QRCode + + let menuItemTitle = interfaceMode == .QRCode ? "QR Code" : "Address" + let systemImageName = interfaceMode == .QRCode ? "textformat.subscript" : "qrcode" + let defaultMenuItemIcon = interfaceMode == .QRCode ? WKMenuItemIcon.shuffle : WKMenuItemIcon.shuffle + + addressLabel.setHidden(interfaceMode != .Address) + imageInterface.setHidden(interfaceMode != .QRCode) + + if #available(watchOSApplicationExtension 6.0, *), let image = UIImage(systemName: systemImageName) { + addMenuItem(with: image, title: menuItemTitle, action: #selector(toggleViewButtonPressed)) + } else { + addMenuItem(with: defaultMenuItemIcon, title: menuItemTitle, action: #selector(toggleViewButtonPressed)) + } } override func willActivate() { super.willActivate() - update(userActivity) + updateUserActivity() } - override func didDeactivate() { super.didDeactivate() userActivity.invalidate() - invalidateUserActivity() } - - } diff --git a/ios/BlueWalletWatch Extension/WalletDetailsInterfaceController.swift b/ios/BlueWalletWatch Extension/WalletDetailsInterfaceController.swift index 711caeeb1..1263d0085 100644 --- a/ios/BlueWalletWatch Extension/WalletDetailsInterfaceController.swift +++ b/ios/BlueWalletWatch Extension/WalletDetailsInterfaceController.swift @@ -1,10 +1,6 @@ -// -// WalletDetailsInterfaceController.swift // BlueWalletWatch Extension // // Created by Marcos Rodriguez on 3/11/19. -// Copyright © 2019 Facebook. All rights reserved. -// import WatchKit import Foundation @@ -23,49 +19,69 @@ class WalletDetailsInterfaceController: WKInterfaceController { @IBOutlet weak var noTransactionsLabel: WKInterfaceLabel! @IBOutlet weak var transactionsTable: WKInterfaceTable! - override func awake(withContext context: Any?) { super.awake(withContext: context) guard let identifier = context as? Int else { pop() return } - processInterface(identifier: identifier) + loadWalletDetails(identifier: identifier) } - func processInterface(identifier: Int) { - let wallet = WatchDataSource.shared.wallets[identifier] - self.wallet = wallet - walletBalanceLabel.setHidden(wallet.hideBalance) - walletBalanceLabel.setText(wallet.hideBalance ? "" : wallet.balance) - walletNameLabel.setText(wallet.label) - walletBasicsGroup.setBackgroundImageNamed(WalletGradient(rawValue: wallet.type)?.imageString) - createInvoiceButton.setHidden(!(wallet.type == WalletGradient.LightningCustodial.rawValue || wallet.type == WalletGradient.LightningLDK.rawValue)) - receiveButton.setHidden(wallet.receiveAddress.isEmpty) - viewXPubButton.setHidden(!((wallet.type != WalletGradient.LightningCustodial.rawValue || wallet.type != WalletGradient.LightningLDK.rawValue) && !(wallet.xpub ?? "").isEmpty)) - processWalletsTable() + private func loadWalletDetails(identifier: Int) { + let wallet = WatchDataSource.shared.wallets[identifier] + self.wallet = wallet + updateWalletUI(wallet: wallet) + updateTransactionsTable(forWallet: wallet) } + private func updateWalletUI(wallet: Wallet) { + walletBalanceLabel.setHidden(wallet.hideBalance) + walletBalanceLabel.setText(wallet.hideBalance ? "" : wallet.balance) + walletNameLabel.setText(wallet.label) + walletBasicsGroup.setBackgroundImageNamed(WalletGradient(rawValue: wallet.type)?.imageString) + + let isLightningWallet = wallet.type == WalletGradient.LightningCustodial.rawValue || wallet.type == WalletGradient.LightningLDK.rawValue + createInvoiceButton.setHidden(!isLightningWallet) + receiveButton.setHidden(wallet.receiveAddress.isEmpty) + viewXPubButton.setHidden(!isXPubAvailable(wallet: wallet)) + } + + private func isXPubAvailable(wallet: Wallet) -> Bool { + return (wallet.type != WalletGradient.LightningCustodial.rawValue && wallet.type != WalletGradient.LightningLDK.rawValue) && !(wallet.xpub ?? "").isEmpty + } + + private func updateTransactionsTable(forWallet wallet: Wallet) { + let transactions = wallet.transactions + transactionsTable.setNumberOfRows(transactions.count, withRowType: TransactionTableRow.identifier) + + for index in 0.. Any? { - return (wallet?.identifier, "receive") + guard let wallet = wallet else { return nil } + return (wallet.identifier, "receive") } } diff --git a/ios/Bridge.swift b/ios/Bridge.swift index 87c7573dc..ae3944ae5 100644 --- a/ios/Bridge.swift +++ b/ios/Bridge.swift @@ -3,7 +3,6 @@ // BlueWallet // // Created by Marcos Rodriguez on 9/19/19. -// Copyright © 2019 Facebook. All rights reserved. // import Foundation diff --git a/ios/Widgets/Shared/WidgetAPI.swift b/ios/Widgets/Shared/WidgetAPI.swift index 4fe2f69ab..65de410ba 100644 --- a/ios/Widgets/Shared/WidgetAPI.swift +++ b/ios/Widgets/Shared/WidgetAPI.swift @@ -3,7 +3,7 @@ // TodayExtension // // Created by Marcos Rodriguez on 11/2/19. -// Copyright © 2019 Facebook. All rights reserved. + // import Foundation