From 74ee7217c13bd514ac5b74388bdbaad634121a39 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Tue, 9 Aug 2022 21:28:15 +0300 Subject: [PATCH] WalletConnectRouter --- .../DApp/SelectChain/SelectChainView.swift | 19 +++++++++++++- .../SelectChainViewController.swift | 6 +++++ Example/ExampleApp.xcodeproj/project.pbxproj | 7 +++++ .../Wallet/WalletViewController.swift | 7 +++++ Package.swift | 8 +++++- Sources/WalletConnectRouter/Router.m | 26 +++++++++++++++++++ Sources/WalletConnectRouter/include/Router.h | 5 ++++ .../include/module.modulemap | 3 +++ 8 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 Sources/WalletConnectRouter/Router.m create mode 100644 Sources/WalletConnectRouter/include/Router.h create mode 100644 Sources/WalletConnectRouter/include/module.modulemap diff --git a/Example/DApp/SelectChain/SelectChainView.swift b/Example/DApp/SelectChain/SelectChainView.swift index 1e54ad3480..934bfe45c5 100644 --- a/Example/DApp/SelectChain/SelectChainView.swift +++ b/Example/DApp/SelectChain/SelectChainView.swift @@ -2,12 +2,14 @@ import Foundation import UIKit class SelectChainView: UIView { + let tableView: UITableView = { let tableView = UITableView(frame: .zero, style: .insetGrouped) tableView.backgroundColor = .tertiarySystemBackground tableView.register(UITableViewCell.self, forCellReuseIdentifier: "chain") return tableView }() + let connectButton: UIButton = { let button = UIButton(type: .system) button.setTitle("Connect", for: .normal) @@ -17,12 +19,22 @@ class SelectChainView: UIView { return button }() + let openWallet: UIButton = { + let button = UIButton(type: .system) + button.setTitle("Open Wallet", for: .normal) + button.backgroundColor = .systemFill + button.tintColor = .white + button.layer.cornerRadius = 8 + return button + }() + override init(frame: CGRect) { super.init(frame: frame) tableView.register(UITableViewCell.self, forCellReuseIdentifier: "chain_cell") backgroundColor = .systemBackground addSubview(tableView) addSubview(connectButton) + addSubview(openWallet) subviews.forEach { $0.translatesAutoresizingMaskIntoConstraints = false } @@ -35,7 +47,12 @@ class SelectChainView: UIView { connectButton.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor, constant: -16), connectButton.centerXAnchor.constraint(equalTo: safeAreaLayoutGuide.centerXAnchor), connectButton.heightAnchor.constraint(equalToConstant: 44), - connectButton.widthAnchor.constraint(equalToConstant: 120) + connectButton.widthAnchor.constraint(equalToConstant: 120), + + openWallet.bottomAnchor.constraint(equalTo: connectButton.topAnchor, constant: -16), + openWallet.centerXAnchor.constraint(equalTo: safeAreaLayoutGuide.centerXAnchor), + openWallet.heightAnchor.constraint(equalToConstant: 44), + openWallet.widthAnchor.constraint(equalToConstant: 120) ]) } diff --git a/Example/DApp/SelectChain/SelectChainViewController.swift b/Example/DApp/SelectChain/SelectChainViewController.swift index bfe28634bb..4ca4b6c650 100644 --- a/Example/DApp/SelectChain/SelectChainViewController.swift +++ b/Example/DApp/SelectChain/SelectChainViewController.swift @@ -21,6 +21,7 @@ class SelectChainViewController: UIViewController, UITableViewDataSource { navigationItem.title = "Available Chains" selectChainView.tableView.dataSource = self selectChainView.connectButton.addTarget(self, action: #selector(connect), for: .touchUpInside) + selectChainView.openWallet.addTarget(self, action: #selector(openWallet), for: .touchUpInside) Sign.instance.sessionSettlePublisher.sink {[unowned self] session in onSessionSettled?(session) }.store(in: &publishers) @@ -42,6 +43,11 @@ class SelectChainViewController: UIViewController, UITableViewDataSource { } } + @objc + private func openWallet() { + UIApplication.shared.open(URL(string: "walletconnectwallet://")!) + } + private func showConnectScreen(uriString: String) { DispatchQueue.main.async { [unowned self] in let vc = UINavigationController(rootViewController: ConnectViewController(uri: uriString)) diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index 6018e9231a..9628992c24 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -122,6 +122,7 @@ A5C20229287EB34C007E3188 /* AccountStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5C20228287EB34C007E3188 /* AccountStorage.swift */; }; A5C2022B287EB89A007E3188 /* WelcomeInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5C2022A287EB89A007E3188 /* WelcomeInteractor.swift */; }; A5C2022D287EC3F0007E3188 /* RegisterService.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5C2022C287EC3F0007E3188 /* RegisterService.swift */; }; + A5C4DD8728A2DE88006A626D /* WalletConnectRouter in Frameworks */ = {isa = PBXBuildFile; productRef = A5C4DD8628A2DE88006A626D /* WalletConnectRouter */; }; A5D85226286333D500DAF5C3 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = A5D85225286333D500DAF5C3 /* Starscream */; }; A5D85228286333E300DAF5C3 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = A5D85227286333E300DAF5C3 /* Starscream */; }; A5E03DF52864651200888481 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = A5E03DF42864651200888481 /* Starscream */; }; @@ -306,6 +307,7 @@ 764E1D5826F8DBAB00A1FB15 /* WalletConnect in Frameworks */, 844943A1278EC49700CC26BB /* Web3 in Frameworks */, A5D85226286333D500DAF5C3 /* Starscream in Frameworks */, + A5C4DD8728A2DE88006A626D /* WalletConnectRouter in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -933,6 +935,7 @@ 764E1D5726F8DBAB00A1FB15 /* WalletConnect */, 844943A0278EC49700CC26BB /* Web3 */, A5D85225286333D500DAF5C3 /* Starscream */, + A5C4DD8628A2DE88006A626D /* WalletConnectRouter */, ); productName = ExampleApp; productReference = 764E1D3C26F8D3FC00A1FB15 /* WalletConnect Wallet.app */; @@ -1786,6 +1789,10 @@ package = A5D85224286333D500DAF5C3 /* XCRemoteSwiftPackageReference "Starscream" */; productName = Starscream; }; + A5C4DD8628A2DE88006A626D /* WalletConnectRouter */ = { + isa = XCSwiftPackageProductDependency; + productName = WalletConnectRouter; + }; A5D85225286333D500DAF5C3 /* Starscream */ = { isa = XCSwiftPackageProductDependency; package = A5D85224286333D500DAF5C3 /* XCRemoteSwiftPackageReference "Starscream" */; diff --git a/Example/ExampleApp/Wallet/WalletViewController.swift b/Example/ExampleApp/Wallet/WalletViewController.swift index cc9b001e69..c2f01b08c2 100644 --- a/Example/ExampleApp/Wallet/WalletViewController.swift +++ b/Example/ExampleApp/Wallet/WalletViewController.swift @@ -1,6 +1,7 @@ import UIKit import WalletConnectSign import WalletConnectUtils +import WalletConnectRouter import Web3 import CryptoSwift import Combine @@ -23,6 +24,7 @@ final class WalletViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() navigationItem.title = "Wallet" + navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(goBack)) walletView.scanButton.addTarget(self, action: #selector(showScanner), for: .touchUpInside) walletView.pasteButton.addTarget(self, action: #selector(showTextInput), for: .touchUpInside) @@ -48,6 +50,11 @@ final class WalletViewController: UIViewController { present(alert, animated: true) } + @objc + private func goBack() { + Router.goBack() + } + private func showSessionProposal(_ proposal: Proposal) { let proposalViewController = ProposalViewController(proposal: proposal) proposalViewController.delegate = self diff --git a/Package.swift b/Package.swift index a94e15017c..80a3871bec 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,10 @@ let package = Package( targets: ["Chat"]), .library( name: "WalletConnectPairing", - targets: ["WalletConnectPairing"]) + targets: ["WalletConnectPairing"]), + .library( + name: "WalletConnectRouter", + targets: ["WalletConnectRouter"]), ], dependencies: [], targets: [ @@ -54,6 +57,9 @@ let package = Package( .target( name: "Commons", dependencies: []), + .target( + name: "WalletConnectRouter", + dependencies: []), .testTarget( name: "WalletConnectSignTests", dependencies: ["WalletConnectSign", "TestingUtils"]), diff --git a/Sources/WalletConnectRouter/Router.m b/Sources/WalletConnectRouter/Router.m new file mode 100644 index 0000000000..9010aa7e67 --- /dev/null +++ b/Sources/WalletConnectRouter/Router.m @@ -0,0 +1,26 @@ +#import +#import "Router.h" + +@import UIKit; +@import ObjectiveC.runtime; + +@interface UISystemNavigationAction : NSObject +@property(nonatomic, readonly, nonnull) NSArray* destinations; +-(BOOL)sendResponseForDestination:(NSUInteger)destination; +@end + +@implementation Router + ++ (void)goBack { + Ivar sysNavIvar = class_getInstanceVariable(UIApplication.class, "_systemNavigationAction"); + UIApplication* app = UIApplication.sharedApplication; + UISystemNavigationAction* action = object_getIvar(app, sysNavIvar); + if (!action) { + return; + } + NSUInteger destination = action.destinations.firstObject.unsignedIntegerValue; + [action sendResponseForDestination:destination]; +} + +@end + diff --git a/Sources/WalletConnectRouter/include/Router.h b/Sources/WalletConnectRouter/include/Router.h new file mode 100644 index 0000000000..222fbe4cc6 --- /dev/null +++ b/Sources/WalletConnectRouter/include/Router.h @@ -0,0 +1,5 @@ +#import + +@interface Router: NSObject ++ (void)goBack; +@end diff --git a/Sources/WalletConnectRouter/include/module.modulemap b/Sources/WalletConnectRouter/include/module.modulemap new file mode 100644 index 0000000000..036065d038 --- /dev/null +++ b/Sources/WalletConnectRouter/include/module.modulemap @@ -0,0 +1,3 @@ +module WalletConnectRouter { + header "Router.h" +}