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

Example Apps #48

Merged
merged 21 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 19 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
52 changes: 52 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/IntegrationTests.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1320"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "IntegrationTests"
BuildableName = "IntegrationTests"
BlueprintName = "IntegrationTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
29 changes: 29 additions & 0 deletions Example/DApp/ Accounts/AccountsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import UIKit

final class AccountsView: UIView {
let tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .plain)
tableView.backgroundColor = .tertiarySystemBackground
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "accountCell")
return tableView
}()

override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .systemBackground
addSubview(tableView)

subviews.forEach { $0.translatesAutoresizingMaskIntoConstraints = false }

NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 16),
tableView.leadingAnchor.constraint(equalTo: leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor)
])
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
87 changes: 87 additions & 0 deletions Example/DApp/ Accounts/AccountsViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import UIKit
import WalletConnect

struct AccountDetails {
let chain: String
let methods: [String]
let account: String
}

final class AccountsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let client = ClientDelegate.shared.client
let session: Session
var accountsDetails: [AccountDetails] = []
var onDisconnect: (()->())?

private let accountsView: AccountsView = {
AccountsView()
}()

init(session: Session) {
self.session = session
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func loadView() {
view = accountsView
}

override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Accounts"

navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "Disconnect",
style: .plain,
target: self,
action: #selector(disconnect)
)
accountsView.tableView.dataSource = self
accountsView.tableView.delegate = self
client.logger.setLogging(level: .debug)
session.accounts.forEach { account in
let splits = account.split(separator: ":", omittingEmptySubsequences: false)
guard splits.count == 3 else { return }
let chain = String(splits[0] + ":" + splits[1])
accountsDetails.append(AccountDetails(chain: chain, methods: Array(session.permissions.methods), account: account))
}
}

@objc
private func disconnect() {
client.disconnect(topic: session.topic, reason: Reason(code: 0, message: "disconnect"))
onDisconnect?()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
accountsDetails.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "accountCell", for: indexPath)
let details = accountsDetails[indexPath.row]
cell.textLabel?.text = details.account
cell.imageView?.image = UIImage(named: details.chain)
cell.textLabel?.numberOfLines = 0
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
showAccountRequestScreen(accountsDetails[indexPath.row])
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}

func showAccountRequestScreen(_ details: AccountDetails) {
let vc = AccountRequestViewController(session: session, accountDetails: details)
navigationController?.pushViewController(vc, animated: true)
}

}
69 changes: 69 additions & 0 deletions Example/DApp/AccountRequest/AccountRequestView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

import UIKit
import Foundation

class AccountRequestView: UIView {
let iconView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = .systemFill
imageView.layer.cornerRadius = 32
return imageView
}()

let chainLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 17.0, weight: .heavy)
return label
}()
let accountLabel: UILabel = {
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .subheadline)
label.textColor = .secondaryLabel
label.numberOfLines = 0
label.textAlignment = .center
return label
}()

let headerStackView: UIStackView = {
let stackView = UIStackView()
stackView.axis = .vertical
stackView.spacing = 16
stackView.alignment = .center
return stackView
}()

let tableView = UITableView()

override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .systemBackground
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "method_cell")
addSubview(iconView)
addSubview(headerStackView)
addSubview(tableView)

headerStackView.addArrangedSubview(chainLabel)
headerStackView.addArrangedSubview(accountLabel)

subviews.forEach { $0.translatesAutoresizingMaskIntoConstraints = false }

NSLayoutConstraint.activate([
iconView.topAnchor.constraint(equalTo: topAnchor, constant: 64),
iconView.centerXAnchor.constraint(equalTo: centerXAnchor),

headerStackView.topAnchor.constraint(equalTo: iconView.bottomAnchor, constant: 32),
headerStackView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 32),
headerStackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -32),

tableView.topAnchor.constraint(equalTo: headerStackView.bottomAnchor, constant: 0),
tableView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0),
tableView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: 0),
tableView.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor),
])
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Loading