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

Network detail screen #29

Merged
merged 7 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/WalletConnect/WalletConnectSwiftV2",
"state" : {
"branch" : "remove-wcm",
"revision" : "0085250fd993f40a638f8d3e300f4af8cbf9e7a8"
"revision" : "4aa4c8229077c133730e361d0d7a17f9e56bdffd",
"version" : "1.9.6"
}
}
],
Expand Down
4 changes: 2 additions & 2 deletions Sample/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = Example/Example.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 7;
CURRENT_PROJECT_VERSION = 9;
DEVELOPMENT_ASSET_PATHS = "\"Example/Preview Content\"";
DEVELOPMENT_TEAM = W5R8AG9K22;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down Expand Up @@ -364,7 +364,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = Example/Example.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 7;
CURRENT_PROJECT_VERSION = 9;
DEVELOPMENT_ASSET_PATHS = "\"Example/Preview Content\"";
DEVELOPMENT_TEAM = W5R8AG9K22;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down
6 changes: 6 additions & 0 deletions Sample/Example/ComponentLibraryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ struct ComponentLibraryView: View {
Group {
#if DEBUG
List {
NavigationLink(destination: AccountButtonPreviewView()) {
Text("AccountButton")
}
NavigationLink(destination: NetworkButtonPreviewView()) {
Text("NetworkButton")
}
NavigationLink(destination: W3MButtonStylePreviewView()) {
Text("W3MButton")
}
Expand Down
4 changes: 2 additions & 2 deletions Sample/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ struct ContentView: View {
VStack {
Spacer()

Web3Button()
Web3ModalButton()

NetworkButton()
Web3ModalNetworkButton()

Spacer()

Expand Down
1 change: 0 additions & 1 deletion Sample/Example/ExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ struct ExampleApp: App {

Web3Modal.configure(
projectId: projectId,
chainId: Blockchain("eip155:1")!,
metadata: metadata
)
}
Expand Down
67 changes: 39 additions & 28 deletions Sources/Web3Modal/Components/AccountButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct AccountButtonStyle: ButtonStyle {
}

var selectedChain: Chain {
return store.selectedChain ?? ChainsPresets.ethChains.first!
return store.selectedChain ?? ChainPresets.ethChains.first!
}

func makeBody(configuration: Configuration) -> some View {
Expand Down Expand Up @@ -158,11 +158,11 @@ public struct AccountButton: View {
Button(action: {
Web3Modal.present()
}, label: {})
.buttonStyle(AccountButtonStyle(store: store))
.onAppear {
fetchIdentity()
fetchBalance()
}
.buttonStyle(AccountButtonStyle(store: store))
.onAppear {
fetchIdentity()
fetchBalance()
}
}

func fetchIdentity() {
Expand All @@ -186,45 +186,56 @@ public struct AccountButton: View {
}
}

struct AccountButton_Preview: PreviewProvider {
#if DEBUG

public struct AccountButtonPreviewView: View {
public init() {}

static let store = { (balance: Double?) -> Store in
let store = Store()
store.balance = balance
store.session = .stub

Web3Modal.configure(projectId: "", metadata: .init(name: "", description: "", url: "", icons: []))

return store
}

static var previews: some View {
public var body: some View {
VStack {
AccountButton(store: AccountButton_Preview.store(1.23))
AccountButton(store: AccountButtonPreviewView.store(1.23))

AccountButton(store: AccountButton_Preview.store(nil))
AccountButton(store: AccountButtonPreviewView.store(nil))

AccountButton(store: AccountButton_Preview.store(1.23))
AccountButton(store: AccountButtonPreviewView.store(1.23))
.disabled(true)

AccountButton(store: AccountButton_Preview.store(nil))
AccountButton(store: AccountButtonPreviewView.store(nil))
.disabled(true)

Button(action: {}, label: {
// Text("Foo")
})
.buttonStyle(
AccountButtonStyle(
store: AccountButton_Preview.store(1.23),
isPressedOverride: true
Button(action: {}, label: {})
.buttonStyle(
AccountButtonStyle(
store: AccountButtonPreviewView.store(1.23),
isPressedOverride: true
)
)
)

Button(action: {}, label: {
// Text("Foo")
})
.buttonStyle(
AccountButtonStyle(
store: AccountButton_Preview.store(nil),
isPressedOverride: true
Button(action: {}, label: {})
.buttonStyle(
AccountButtonStyle(
store: AccountButtonPreviewView.store(nil),
isPressedOverride: true
)
)
)
}
}
}

struct AccountButton_Preview: PreviewProvider {
static var previews: some View {
AccountButtonPreviewView()
}
}

#endif
8 changes: 7 additions & 1 deletion Sources/Web3Modal/Components/ConnectButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public struct ConnectButton: View {
Web3Modal.present()
} label: {
if store.connecting {
CircleProgressView(color: .white, lineWidth: 2, isAnimating: .constant(true))
DrawingProgressView(
shape: .circle,
color: .white,
lineWidth: 2,
duration: 1,
isAnimating: .constant(true)
)
.frame(width: 20, height: 20)
} else {
Text("Connect wallet")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import SwiftUI

public struct Web3Button: View {

public struct Web3ModalButton: View {
@ObservedObject var store: Store

public init() {
Expand All @@ -24,7 +23,6 @@ public struct Web3Button: View {
}

struct Web3Button_Preview: PreviewProvider {

static let store = { () -> Store in
let store = Store()
store.balance = 1.23
Expand All @@ -34,9 +32,9 @@ struct Web3Button_Preview: PreviewProvider {

static var previews: some View {
VStack {
Web3Button(store: Web3Button_Preview.store)
Web3ModalButton(store: Web3Button_Preview.store)

Web3Button(store: Web3Button_Preview.store)
Web3ModalButton(store: Web3Button_Preview.store)
.disabled(true)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwiftUI
import Web3ModalUI

public struct NetworkButton: View {
public struct Web3ModalNetworkButton: View {
@ObservedObject var store: Store

public init() {
Expand All @@ -14,7 +14,6 @@ public struct NetworkButton: View {

public var body: some View {
if let selectedChain = store.selectedChain {

let storedImage = store.chainImages[selectedChain.imageId]
let chainImage = Image(
uiImage: storedImage ?? UIImage()
Expand Down Expand Up @@ -45,7 +44,11 @@ public struct NetworkButton: View {
}
}

struct NetworkButton_Preview: PreviewProvider {
#if DEBUG

public struct NetworkButtonPreviewView: View {
public init() {}

static let store = { (chain: Chain?) -> Store in
let store = Store()
store.balance = 1.23
Expand All @@ -54,21 +57,27 @@ struct NetworkButton_Preview: PreviewProvider {
return store
}

static var previews: some View {
public var body: some View {
VStack {
NetworkButton(store: NetworkButton_Preview.store(nil))

ConnectButton()
Web3ModalNetworkButton(store: NetworkButtonPreviewView.store(nil))

NetworkButton(store: NetworkButton_Preview.store(ChainsPresets.ethChains[0]))
Web3ModalNetworkButton(store: NetworkButtonPreviewView.store(ChainPresets.ethChains[0]))

NetworkButton(store: NetworkButton_Preview.store(ChainsPresets.ethChains[1]))
Web3ModalNetworkButton(store: NetworkButtonPreviewView.store(ChainPresets.ethChains[1]))

NetworkButton(store: NetworkButton_Preview.store(ChainsPresets.ethChains[0]))
Web3ModalNetworkButton(store: NetworkButtonPreviewView.store(ChainPresets.ethChains[0]))
.disabled(true)

NetworkButton(store: NetworkButton_Preview.store(nil))
Web3ModalNetworkButton(store: NetworkButtonPreviewView.store(nil))
.disabled(true)
}
}
}

struct NetworkButton_Preview: PreviewProvider {
static var previews: some View {
NetworkButtonPreviewView()
}
}

#endif
3 changes: 2 additions & 1 deletion Sources/Web3Modal/Core/W3MAPIInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ final class W3MAPIInteractor: ObservableObject {
}

self.isLoading = false
self.objectWillChange.send()
}
}

Expand Down Expand Up @@ -174,7 +175,7 @@ final class W3MAPIInteractor: ObservableObject {
func prefetchChainImages() async throws {
var chainImages: [String: UIImage] = [:]

try await ChainsPresets.ethChains.concurrentMap { chain in
try await ChainPresets.ethChains.concurrentMap { chain in

let url = URL(string: "https://api.web3modal.com/public/getAssetImage/\(chain.imageId)")!
var request = URLRequest(url: url)
Expand Down
24 changes: 10 additions & 14 deletions Sources/Web3Modal/Core/Web3Modal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Web3Modal {


if let blockchain = session.accounts.first?.blockchain {
let matchingChain = ChainsPresets.ethChains.first(where: {
let matchingChain = ChainPresets.ethChains.first(where: {
$0.chainNamespace == blockchain.namespace && $0.chainReference == blockchain.reference
})

Expand All @@ -53,7 +53,6 @@ public class Web3Modal {

struct Config {
let projectId: String
var chainId: Blockchain
var metadata: AppMetadata
var sessionParams: SessionParams

Expand All @@ -71,7 +70,6 @@ public class Web3Modal {
/// - metadata: App metadata
public static func configure(
projectId: String,
chainId: Blockchain,
metadata: AppMetadata,
sessionParams: SessionParams = .default,
recommendedWalletIds: [String] = [],
Expand All @@ -81,22 +79,13 @@ public class Web3Modal {
Pair.configure(metadata: metadata)
Web3Modal.config = Web3Modal.Config(
projectId: projectId,
chainId: chainId,
metadata: metadata,
sessionParams: sessionParams,
includeWebWallets: includeWebWallets,
recommendedWalletIds: recommendedWalletIds,
excludedWalletIds: excludedWalletIds
)


let matchingChain = ChainsPresets.ethChains.first(where: {
$0.chainNamespace == chainId.namespace && $0.chainReference == chainId.reference
})

Store.shared.selectedChain = matchingChain


Task {
let interactor = W3MAPIInteractor()

Expand All @@ -111,7 +100,6 @@ public class Web3Modal {
Web3Modal.config.sessionParams = sessionParams
}


public static func getSelectedChain() -> Chain? {
guard let chain = Store.shared.selectedChain else {
return nil
Expand All @@ -125,6 +113,14 @@ public class Web3Modal {

extension Web3Modal {

public static func addChainPreset(_ chain: Chain) {
ChainPresets.ethChains.append(chain)
}

public static func selectChain(_ chain: Chain) {
Store.shared.selectedChain = chain
}

public static func selectChain(from presentingViewController: UIViewController? = nil) {
guard let vc = presentingViewController ?? topViewController() else {
assertionFailure("No controller found for presenting modal")
Expand Down Expand Up @@ -208,7 +204,7 @@ public struct SessionParams {
public static let `default`: Self = {
let methods: Set<String> = Set(EthUtils.ethMethods)
let events: Set<String> = ["chainChanged", "accountsChanged"]
let blockchains: Set<Blockchain> = Set(ChainsPresets.ethChains.map(\.id).compactMap(Blockchain.init))
let blockchains: Set<Blockchain> = Set(ChainPresets.ethChains.map(\.id).compactMap(Blockchain.init))

let namespaces: [String: ProposalNamespace] = [
"eip155": ProposalNamespace(
Expand Down
3 changes: 3 additions & 0 deletions Sources/Web3Modal/Helpers/Session+Stub.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import WalletConnectSign
import Foundation

#if DEBUG
extension Session {
static let stubJson: Data = """
{
Expand Down Expand Up @@ -46,3 +47,5 @@ extension Session {
try! JSONDecoder().decode(Session.self, from: Session.stubJson)
}()
}

#endif
Loading