Skip to content

Commit

Permalink
Create and use UI package
Browse files Browse the repository at this point in the history
  • Loading branch information
radeknovis committed Oct 3, 2023
1 parent 26af0f2 commit 5455532
Show file tree
Hide file tree
Showing 291 changed files with 203 additions and 121 deletions.
1 change: 1 addition & 0 deletions Example/ComponentLibraryView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SwiftUI
import Web3Modal
import Web3ModalUI

struct ComponentLibraryView: View {
var body: some View {
Expand Down
1 change: 1 addition & 0 deletions Example/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SwiftUI
import Web3Modal
import Web3ModalUI

struct ContentView: View {
var body: some View {
Expand Down
21 changes: 19 additions & 2 deletions Web3Modal/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ let package = Package(
.library(
name: "Web3Modal",
targets: ["Web3Modal"]
),
.library(
name: "Web3ModalUI",
targets: ["Web3ModalUI"]
)
],
dependencies: [
Expand All @@ -34,7 +38,14 @@ let package = Package(
.product(
name: "WalletConnect",
package: "WalletConnectSwiftV2"
)
),
"Web3ModalUI"
]
),
.target(
name: "Web3ModalUI",
resources: [
.process("Resources/Assets.xcassets")
]
),

Expand All @@ -44,7 +55,13 @@ let package = Package(
name: "Web3ModalTests",
dependencies: [
"Web3Modal",
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
.product(name: "SnapshotTesting", package: "swift-snapshot-testing")
]
),
.testTarget(
name: "Web3ModalUITests",
dependencies: [
"Web3ModalUI"
]
)
]
Expand Down
2 changes: 1 addition & 1 deletion Web3Modal/Sources/Web3Modal/Core/Web3Modal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class Web3Modal {
includeWebWallets: includeWebWallets,
recommendedWalletIds: recommendedWalletIds,
excludedWalletIds: excludedWalletIds
)
)
}

public static func set(sessionParams: SessionParams) {
Expand Down
6 changes: 0 additions & 6 deletions Web3Modal/Sources/Web3Modal/Extensions/Collection.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
import Foundation

extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript(safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}
19 changes: 0 additions & 19 deletions Web3Modal/Sources/Web3Modal/Models/WalletImage.swift

This file was deleted.

25 changes: 0 additions & 25 deletions Web3Modal/Sources/Web3Modal/Resources/Radius.swift

This file was deleted.

26 changes: 0 additions & 26 deletions Web3Modal/Sources/Web3Modal/Resources/Spacing.swift

This file was deleted.

1 change: 1 addition & 0 deletions Web3Modal/Sources/Web3Modal/Screens/AllWalletsView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Combine
import SwiftUI
import Web3ModalUI

struct AllWalletsView: View {
@EnvironmentObject var router: Router
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import Web3ModalUI

struct ConnectWithQRCode: View {

Expand Down
9 changes: 5 additions & 4 deletions Web3Modal/Sources/Web3Modal/Screens/GetAWalletView.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import Web3ModalUI

struct GetAWalletView: View {
let wallets: [Wallet]
Expand Down Expand Up @@ -33,10 +34,10 @@ struct GetAWalletView: View {
.buttonStyle(W3MListSelectStyle(
imageContent: {
W3MAllWalletsImage(images: [
.init(image: Image("MockWalletImage", bundle: .module), walletName: "Metamask"),
.init(image: Image("MockWalletImage", bundle: .module), walletName: "Trust"),
.init(image: Image("MockWalletImage", bundle: .module), walletName: "Safe"),
.init(image: Image("MockWalletImage", bundle: .module), walletName: "Rainbow"),
.init(image: Image("MockWalletImage", bundle: .UIModule), walletName: "Metamask"),
.init(image: Image("MockWalletImage", bundle: .UIModule), walletName: "Trust"),
.init(image: Image("MockWalletImage", bundle: .UIModule), walletName: "Safe"),
.init(image: Image("MockWalletImage", bundle: .UIModule), walletName: "Rainbow"),
])
}
))
Expand Down
5 changes: 3 additions & 2 deletions Web3Modal/Sources/Web3Modal/Screens/QRCodeView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import QRCode
import SwiftUI
import Web3ModalUI

struct QRCodeView: View {
let uri: String
Expand Down Expand Up @@ -47,7 +48,7 @@ struct QRCodeView: View {
let uiImage = imageData != nil ?
UIImage(data: imageData!) :
UIImage(named: "imageLogo",
in: .module,
in: .UIModule,
compatibleWith: nil)?.withColor(UIColor(.Blue100))

if let uiImage = uiImage {
Expand Down Expand Up @@ -109,7 +110,7 @@ public struct QRCodeViewPreviewView: View {
VStack {
QRCodeView(
uri: QRCodeViewPreviewView.stubUri,
imageData: UIImage(named: "MockWalletImage", in: .module, compatibleWith: nil)?.pngData()
imageData: UIImage(named: "MockWalletImage", in: .UIModule, compatibleWith: nil)?.pngData()
)
.previewLayout(.sizeThatFits)

Expand Down
1 change: 1 addition & 0 deletions Web3Modal/Sources/Web3Modal/Screens/WhatIsWalletView.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import Web3ModalUI

struct WhatIsWalletView: View {

Expand Down
17 changes: 12 additions & 5 deletions Web3Modal/Sources/Web3Modal/Sheets/Web3ModalView.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import Web3ModalUI

struct Web3ModalView: View {
@StateObject var router: Router
Expand All @@ -12,6 +13,12 @@ struct Web3ModalView: View {
_interactor = StateObject(
wrappedValue: W3MAPIInteractor(store: store)
)

ImageLoader.headers = [
"x-sdk-type": "w3m",
"x-sdk-version": "ios-3.0.0-alpha.0",
"x-project-id": Web3Modal.config?.projectId ?? ""
]
}

var body: some View {
Expand Down Expand Up @@ -98,7 +105,7 @@ struct Web3ModalView: View {
Text("Rainbow")
})
.buttonStyle(W3MListSelectStyle(
imageContent: { Image("MockWalletImage", bundle: .module).resizable() }
imageContent: { Image("MockWalletImage", bundle: .UIModule).resizable() }
))

Button(action: {
Expand All @@ -109,10 +116,10 @@ struct Web3ModalView: View {
.buttonStyle(W3MListSelectStyle(
imageContent: {
W3MAllWalletsImage(images: [
.init(image: Image("MockWalletImage", bundle: .module), walletName: "Metamask"),
.init(image: Image("MockWalletImage", bundle: .module), walletName: "Trust"),
.init(image: Image("MockWalletImage", bundle: .module), walletName: "Safe"),
.init(image: Image("MockWalletImage", bundle: .module), walletName: "Rainbow"),
.init(image: Image("MockWalletImage", bundle: .UIModule), walletName: "Metamask"),
.init(image: Image("MockWalletImage", bundle: .UIModule), walletName: "Trust"),
.init(image: Image("MockWalletImage", bundle: .UIModule), walletName: "Safe"),
.init(image: Image("MockWalletImage", bundle: .UIModule), walletName: "Rainbow"),
])
}
))
Expand Down
41 changes: 41 additions & 0 deletions Web3Modal/Sources/Web3ModalUI/Bundle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Foundation

public extension Foundation.Bundle {
private class CurrentBundle {}

/// We override `resource_bundle_accessor.swift` in order to provide correct
/// path for SwiftUI Preview.
static var UIModule: Bundle = {
let moduleBundleNameInPackage = "swift-web3modal_Web3ModalUI"
let candidates = [
Bundle.main.resourceURL,
Bundle(for: CurrentBundle.self).resourceURL,
Bundle.main.bundleURL,

/// Provide extra paths to search for resource.
/// Bundle should be present here when running previews from a different
/// package (this is the path to `__/Debug-iphonesimulator/__`).
/// This avoids following error message
/// `resource_bundle_accessor.swift:27: Fatal error: unable to find bundle named swift-web3modal_Web3ModalUI`
Bundle(for: CurrentBundle.self).resourceURL?
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent(),

Bundle(for: CurrentBundle.self).resourceURL?
.deletingLastPathComponent()
.deletingLastPathComponent()
]

for candidate in candidates {
let bundleName = candidate?.appendingPathComponent(moduleBundleNameInPackage + ".bundle")

if let bundle = bundleName.flatMap(Bundle.init(url:)) {
print("\(bundle.bundlePath.description)")
return bundle
}
}

fatalError("unable to find bundle \(moduleBundleNameInPackage.description)")
}()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftUI

struct W3MActionEntryStyle: ButtonStyle {
public struct W3MActionEntryStyle: ButtonStyle {
@Environment(\.isEnabled) var isEnabled
@Environment(\.dynamicTypeSize) var dynamicTypeSize

Expand All @@ -11,7 +11,7 @@ struct W3MActionEntryStyle: ButtonStyle {

var isPressedOverride: Bool?

init(
public init(
leftIcon: Image? = nil,
rightIcon: Image? = nil
) {
Expand All @@ -31,7 +31,7 @@ struct W3MActionEntryStyle: ButtonStyle {
}
#endif

func makeBody(configuration: Configuration) -> some View {
public func makeBody(configuration: Configuration) -> some View {
HStack(spacing: Spacing.xs) {
if let leftIcon {
leftIcon
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import SwiftUI

struct W3MAllWalletsImage: View {
public struct W3MAllWalletsImage: View {

@ScaledMetric var scale: CGFloat = 1

var images: [WalletImage]

public init(images: [WalletImage]) {
self.images = images
}

var body: some View {
public var body: some View {
VStack(spacing: 2 * scale) {
HStack(spacing: 2 * scale) {
walletImage(images[safe: 0])
Expand Down Expand Up @@ -59,6 +63,31 @@ struct W3MAllWalletsImage: View {
}
}

public struct WalletImage {
let image: Image?
let url: String?
let walletName: String?

public init(url: String, walletName: String?) {
self.image = nil
self.url = url
self.walletName = walletName
}

public init(image: Image, walletName: String?) {
self.image = image
self.url = nil
self.walletName = walletName
}
}

private extension Collection {
/// Returns the element at the specified index if it is within bounds, otherwise nil.
subscript(safe index: Index) -> Element? {
return indices.contains(index) ? self[index] : nil
}
}

#if DEBUG
public struct W3MAllWalletsImageView: View {
public init() {}
Expand Down
Loading

0 comments on commit 5455532

Please sign in to comment.