Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] WalletConnectRouter #424

Merged
merged 1 commit into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Example/DApp/SelectChain/SelectChainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 }

Expand All @@ -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)
])
}

Expand Down
6 changes: 6 additions & 0 deletions Example/DApp/SelectChain/SelectChainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,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 */; };
Expand Down Expand Up @@ -305,6 +306,7 @@
A5AE354728A1A2AC0059AE8A /* Web3 in Frameworks */,
764E1D5826F8DBAB00A1FB15 /* WalletConnect in Frameworks */,
A5D85226286333D500DAF5C3 /* Starscream in Frameworks */,
A5C4DD8728A2DE88006A626D /* WalletConnectRouter in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -931,6 +933,7 @@
764E1D5726F8DBAB00A1FB15 /* WalletConnect */,
A5D85225286333D500DAF5C3 /* Starscream */,
A5AE354628A1A2AC0059AE8A /* Web3 */,
A5C4DD8628A2DE88006A626D /* WalletConnectRouter */,
);
productName = ExampleApp;
productReference = 764E1D3C26F8D3FC00A1FB15 /* WalletConnect Wallet.app */;
Expand Down Expand Up @@ -1778,6 +1781,10 @@
package = A5AE354528A1A2AC0059AE8A /* XCRemoteSwiftPackageReference "Web3" */;
productName = Web3;
};
A5C4DD8628A2DE88006A626D /* WalletConnectRouter */ = {
isa = XCSwiftPackageProductDependency;
productName = WalletConnectRouter;
};
A5D85225286333D500DAF5C3 /* Starscream */ = {
isa = XCSwiftPackageProductDependency;
package = A5D85224286333D500DAF5C3 /* XCRemoteSwiftPackageReference "Starscream" */;
Expand Down
7 changes: 7 additions & 0 deletions Example/ExampleApp/Wallet/WalletViewController.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import UIKit
import WalletConnectSign
import WalletConnectUtils
import WalletConnectRouter
import Web3
import CryptoSwift
import Combine
Expand All @@ -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)

Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ let package = Package(
name: "WalletConnectChat",
targets: ["Chat"]),
.library(
name: "WalletConnectAuth",
targets: ["Auth"])
name: "WalletConnectRouter",
targets: ["WalletConnectRouter"]),
],
dependencies: [
.package(url: "https://github.com/flypaper0/Web3.swift", .branch("master"))
Expand Down Expand Up @@ -62,6 +62,9 @@ let package = Package(
.target(
name: "Commons",
dependencies: []),
.target(
name: "WalletConnectRouter",
dependencies: []),
.testTarget(
name: "WalletConnectSignTests",
dependencies: ["WalletConnectSign", "TestingUtils"]),
Expand Down
26 changes: 26 additions & 0 deletions Sources/WalletConnectRouter/Router.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#import <Foundation/Foundation.h>
#import "Router.h"

@import UIKit;
@import ObjectiveC.runtime;

@interface UISystemNavigationAction : NSObject
@property(nonatomic, readonly, nonnull) NSArray<NSNumber*>* 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

5 changes: 5 additions & 0 deletions Sources/WalletConnectRouter/include/Router.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <Foundation/Foundation.h>

@interface Router: NSObject
+ (void)goBack;
@end
3 changes: 3 additions & 0 deletions Sources/WalletConnectRouter/include/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module WalletConnectRouter {
header "Router.h"
}