Skip to content

Commit

Permalink
Merge pull request #1036 from WalletConnect/feat/wcm/web-wallet-detail
Browse files Browse the repository at this point in the history
[WCM] Support web wallets + accentColor theming
  • Loading branch information
radeknovis authored Aug 16, 2023
2 parents 44c5332 + 003aa4b commit d571b04
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 106 deletions.
5 changes: 3 additions & 2 deletions Example/DApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {

WalletConnectModal.configure(
projectId: InputConfig.projectId,
metadata: metadata
metadata: metadata,
accentColor: .green
)

setupWindow(scene: scene)
}

Expand Down
2 changes: 1 addition & 1 deletion Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down
60 changes: 57 additions & 3 deletions Sources/WalletConnectModal/Mocks/Listing+Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,63 @@ import Foundation

extension Listing {
static let stubList: [Listing] = [
Listing(id: UUID().uuidString, name: "Sample Wallet", homepage: "https://example.com", order: 1, imageId: UUID().uuidString, app: Listing.App(ios: "https://example.com/download-ios", mac: "https://example.com/download-mac", safari: "https://example.com/download-safari"), mobile: Listing.Mobile(native: "sampleapp://deeplink", universal: "https://example.com/universal")),
Listing(id: UUID().uuidString, name: "Awesome Wallet", homepage: "https://example.com/awesome", order: 2, imageId: UUID().uuidString, app: Listing.App(ios: "https://example.com/download-ios", mac: "https://example.com/download-mac", safari: "https://example.com/download-safari"), mobile: Listing.Mobile(native: "awesomeapp://deeplink", universal: "https://example.com/awesome/universal")),
Listing(id: UUID().uuidString, name: "Cool Wallet", homepage: "https://example.com/cool", order: 3, imageId: UUID().uuidString, app: Listing.App(ios: "https://example.com/download-ios", mac: "https://example.com/download-mac", safari: "https://example.com/download-safari"), mobile: Listing.Mobile(native: "coolapp://deeplink", universal: "https://example.com/cool/universal"))
Listing(
id: UUID().uuidString,
name: "Sample Wallet",
homepage: "https://example.com",
order: 1,
imageId: UUID().uuidString,
app: Listing.App(
ios: "https://example.com/download-ios",
browser: "https://example.com/download-safari"
),
mobile: .init(
native: "sampleapp://deeplink",
universal: "https://example.com/universal"
),
desktop: .init(
native: nil,
universal: "https://example.com/universal"
)
),
Listing(
id: UUID().uuidString,
name: "Awesome Wallet",
homepage: "https://example.com/awesome",
order: 2,
imageId: UUID().uuidString,
app: Listing.App(
ios: "https://example.com/download-ios",
browser: "https://example.com/download-safari"
),
mobile: .init(
native: "awesomeapp://deeplink",
universal: "https://example.com/awesome/universal"
),
desktop: .init(
native: nil,
universal: "https://example.com/awesome/universal"
)
),
Listing(
id: UUID().uuidString,
name: "Cool Wallet",
homepage: "https://example.com/cool",
order: 3,
imageId: UUID().uuidString,
app: Listing.App(
ios: "https://example.com/download-ios",
browser: "https://example.com/download-safari"
),
mobile: .init(
native: "coolapp://deeplink",
universal: "https://example.com/cool/universal"
),
desktop: .init(
native: nil,
universal: "https://example.com/cool/universal"
)
)
]
}

Expand Down
17 changes: 7 additions & 10 deletions Sources/WalletConnectModal/Modal/ModalSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public struct ModalSheet: View {
VStack(spacing: 0) {
contentHeader()
content()

}
.frame(maxWidth: .infinity)
.background(Color.background1)
Expand Down Expand Up @@ -75,13 +74,12 @@ public struct ModalSheet: View {
EmptyView()
}
}
.animation(.default)
.animation(.default, value: viewModel.destination)
.foregroundColor(.accent)
.frame(height: 60)
.overlay(
VStack {
if viewModel.destination.hasSearch {

HStack {
Image(systemName: "magnifyingglass")
TextField("Search", text: $viewModel.searchTerm, onEditingChanged: { editing in
Expand Down Expand Up @@ -128,7 +126,7 @@ public struct ModalSheet: View {
viewModel.destination
}, set: { _ in }),
navigateTo: viewModel.navigateTo(_:),
onListingTap: { viewModel.onListingTap($0, preferUniversal: false) }
onListingTap: { viewModel.onListingTap($0) }
)
}

Expand All @@ -144,7 +142,6 @@ public struct ModalSheet: View {

@ViewBuilder
private func content() -> some View {

switch viewModel.destination {
case .welcome,
.viewAll:
Expand All @@ -155,18 +152,18 @@ public struct ModalSheet: View {
case .getWallet:
GetAWalletView(
wallets: Array(viewModel.wallets.prefix(6)),
onWalletTap: viewModel.onGetWalletTap(_:),
onWalletTap: viewModel.openAppstore(wallet:),
navigateToExternalLink: viewModel.navigateToExternalLink(_:)
)
.frame(minHeight: verticalSizeClass == .compact ? 200 : 550)
.padding(.bottom, 20)

case let .walletDetail(wallet):
WalletDetail(
wallet: wallet,
deeplink: { viewModel.onListingTap($0, preferUniversal: false) },
deeplinkUniversal: { viewModel.onListingTap($0, preferUniversal: true) },
openAppStore: viewModel.onGetWalletTap(_:)
viewModel: .init(
wallet: wallet,
deeplinkHandler: viewModel
)
)
}
}
Expand Down
83 changes: 44 additions & 39 deletions Sources/WalletConnectModal/Modal/ModalViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,8 @@ final class ModalViewModel: ObservableObject {
uiApplicationWrapper.openURL(url, nil)
}

func onListingTap(_ listing: Listing, preferUniversal: Bool) {
func onListingTap(_ listing: Listing) {
setLastTimeUsed(listing.id)

navigateToDeepLink(
universalLink: listing.mobile.universal ?? "",
nativeLink: listing.mobile.native ?? "",
preferUniversal: preferUniversal
)
}

func onGetWalletTap(_ listing: Listing) {
guard
let storeLinkString = listing.app.ios,
let storeLink = URL(string: storeLinkString)
else { return }

uiApplicationWrapper.openURL(storeLink, nil)
}

func onBackButton() {
Expand Down Expand Up @@ -257,28 +242,29 @@ private extension ModalViewModel {

// MARK: - Deeplinking

private extension ModalViewModel {
enum DeeplinkErrors: LocalizedError {
case noWalletLinkFound
case uriNotCreated
case failedToOpen
protocol WalletDeeplinkHandler {
func openAppstore(wallet: Listing)
func navigateToDeepLink(wallet: Listing, preferUniversal: Bool, preferBrowser: Bool)
}

extension ModalViewModel: WalletDeeplinkHandler {

func openAppstore(wallet: Listing) {
guard
let storeLinkString = wallet.app.ios,
let storeLink = URL(string: storeLinkString)
else { return }

var errorDescription: String? {
switch self {
case .noWalletLinkFound:
return NSLocalizedString("No valid link for opening given wallet found", comment: "")
case .uriNotCreated:
return NSLocalizedString("Couldn't generate link due to missing connection URI", comment: "")
case .failedToOpen:
return NSLocalizedString("Given link couldn't be opened", comment: "")
}
}
uiApplicationWrapper.openURL(storeLink, nil)
}

func navigateToDeepLink(universalLink: String, nativeLink: String, preferUniversal: Bool) {
func navigateToDeepLink(wallet: Listing, preferUniversal: Bool, preferBrowser: Bool) {
do {
let nativeUrlString = try formatNativeUrlString(nativeLink)
let universalUrlString = try formatUniversalUrlString(universalLink)
let nativeScheme = preferBrowser ? nil : wallet.mobile.native
let universalScheme = preferBrowser ? wallet.desktop.universal : wallet.mobile.universal

let nativeUrlString = try formatNativeUrlString(nativeScheme)
let universalUrlString = try formatUniversalUrlString(universalScheme)

if let nativeUrl = nativeUrlString?.toURL(), !preferUniversal {
uiApplicationWrapper.openURL(nativeUrl) { success in
Expand All @@ -299,13 +285,32 @@ private extension ModalViewModel {
toast = Toast(style: .error, message: error.localizedDescription)
}
}
}

private extension ModalViewModel {
enum DeeplinkErrors: LocalizedError {
case noWalletLinkFound
case uriNotCreated
case failedToOpen

var errorDescription: String? {
switch self {
case .noWalletLinkFound:
return NSLocalizedString("No valid link for opening given wallet found", comment: "")
case .uriNotCreated:
return NSLocalizedString("Couldn't generate link due to missing connection URI", comment: "")
case .failedToOpen:
return NSLocalizedString("Given link couldn't be opened", comment: "")
}
}
}

func isHttpUrl(url: String) -> Bool {
return url.hasPrefix("http://") || url.hasPrefix("https://")
}

func formatNativeUrlString(_ string: String) throws -> String? {
if string.isEmpty { return nil }
func formatNativeUrlString(_ string: String?) throws -> String? {
guard let string = string, !string.isEmpty else { return nil }

if isHttpUrl(url: string) {
return try formatUniversalUrlString(string)
Expand All @@ -324,8 +329,8 @@ private extension ModalViewModel {
return "\(safeAppUrl)wc?uri=\(deeplinkUri)"
}

func formatUniversalUrlString(_ string: String) throws -> String? {
if string.isEmpty { return nil }
func formatUniversalUrlString(_ string: String?) throws -> String? {
guard let string = string, !string.isEmpty else { return nil }

if !isHttpUrl(url: string) {
return try formatNativeUrlString(string)
Expand Down
Loading

0 comments on commit d571b04

Please sign in to comment.