Skip to content

Commit

Permalink
move export secret and etherscan lookup to wallet connection screen
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Nov 27, 2023
1 parent 2a6db34 commit b66db4c
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 57 deletions.
54 changes: 50 additions & 4 deletions PlatformUI/PlatformUI/DesignSystem/Theme/ThemeViewModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public extension View {
}

func themeColor(background: ThemeColor.SemanticColor) -> some View {
modifier(SemanticColorModifier(layerColor: background))
modifier(BackgroundColorModifier(layerColor: background))
}

func themeGradient(background: ThemeColor.SemanticColor, gradientColor: Color) -> some View {
Expand All @@ -31,7 +31,7 @@ public extension Text {
}
}

struct TextColorModifier: ViewModifier {
private struct TextColorModifier: ViewModifier {
@EnvironmentObject var themeSettings: ThemeSettings

let textColor: ThemeColor.SemanticColor
Expand All @@ -42,7 +42,7 @@ struct TextColorModifier: ViewModifier {
}
}

struct SemanticColorModifier: ViewModifier {
private struct BackgroundColorModifier: ViewModifier {
@EnvironmentObject var themeSettings: ThemeSettings

let layerColor: ThemeColor.SemanticColor
Expand All @@ -53,7 +53,18 @@ struct SemanticColorModifier: ViewModifier {
}
}

struct GradientColorModifier: ViewModifier {
private struct BorderColorModifier: ViewModifier {
@EnvironmentObject var themeSettings: ThemeSettings

let layerColor: ThemeColor.SemanticColor

func body(content: Content) -> some View {
content
.background(themeSettings.themeConfig.themeColor.color(of: layerColor))
}
}

private struct GradientColorModifier: ViewModifier {
@EnvironmentObject var themeSettings: ThemeSettings

let layerColor: ThemeColor.SemanticColor
Expand Down Expand Up @@ -268,8 +279,43 @@ public extension View {
func border(borderWidth: CGFloat = 1, cornerRadius: CGFloat = 0, borderColor: Color? = ThemeColor.SemanticColor.layer5.color) -> some View {
modifier(BorderModifier(cornerRadius: cornerRadius, borderWidth: borderWidth, borderColor: borderColor))
}

func borderAndClip(style: BorderAndClipStyle, borderColor: ThemeColor.SemanticColor, lineWidth: CGFloat) -> some View {
modifier(BorderAndClipModifier(style: style, borderColor: borderColor, lineWidth: lineWidth))
}
}

/// The clip shape/style
public enum BorderAndClipStyle {
/// A rectangular shape with rounded corners with specified corner radius, aligned inside the frame of the view containing it.
case cornerRadius(CGFloat)
/// A capsule shape is equivalent to a rounded rectangle where the corner radius is chosen as half the length of the rectangle’s smallest edge.
case capsule
}

private struct BorderAndClipModifier: ViewModifier {
let style: BorderAndClipStyle
let borderColor: ThemeColor.SemanticColor
let lineWidth: CGFloat

func body(content: Content) -> some View {
switch style {
case .cornerRadius(let cornerRadius):
content
.clipShape(RoundedRectangle(cornerSize: .init(width: cornerRadius, height: cornerRadius)))
.overlay(RoundedRectangle(cornerRadius: cornerRadius)
.strokeBorder(borderColor.color, lineWidth: lineWidth))

case .capsule:
content
.clipShape(Capsule())
.overlay(Capsule()
.strokeBorder(borderColor.color, lineWidth: lineWidth))
}
}
}


private struct BorderModifier: ViewModifier {
var cornerRadius: CGFloat = .infinity
var borderWidth: CGFloat = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,6 @@ private class dydxAddressDetailsViewPresenter: HostedViewPresenter<dydxAddressDe
type: .success,
error: nil, time: 3)
}

self?.viewModel?.etherscanAction = {
guard let ethereumAddress = walletState.currentWallet?.ethereumAddress else {
return
}

let urlString = "https://etherscan.io/address/\(ethereumAddress)"
if let url = URL(string: urlString), URLHandler.shared?.canOpenURL(url) ?? false {
URLHandler.shared?.open(url, completionHandler: nil)
}
}

self?.viewModel?.keyExportAction = {
Router.shared?.navigate(to: RoutingRequest(url: "/my-profile/keyexport"), animated: true, completion: nil)
}
}
.store(in: &subscriptions)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class dydxProfileHeaderViewPresenter: HostedViewPresenter<dydxProfileHeaderViewM
if let chainLogo = AbacusStateManager.shared.environment?.chainLogo {
self.viewModel?.dydxChainLogoUrl = URL(string: chainLogo)
}
self.viewModel?.switchWalletAction = { [weak self] in
self.viewModel?.manageWalletAction = { [weak self] in
guard let self = self else {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ private class Wallets2ViewPresenter: HostedViewPresenter<Wallets2ViewModel> {
}
}

viewModel.openInEtherscanTapped = {
let ethereumAddress = wallet.ethereumAddress
let urlString = "https://etherscan.io/address/\(ethereumAddress)"
if let url = URL(string: urlString), URLHandler.shared?.canOpenURL(url) ?? false {
URLHandler.shared?.open(url, completionHandler: nil)
}
}

viewModel.exportSecretPhraseTapped = {
Router.shared?.navigate(to: RoutingRequest(url: "/my-profile/keyexport"), animated: true, completion: nil)
}

viewModel.walletImageUrl = wallet.imageUrl

// TODO:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import dydxFormatter
public final class AbacusStateManager: NSObject {
public static let shared = AbacusStateManager()

public let deploymentUri = dydxStringFeatureFlag.deployment_url.string ?? (CredientialConfig.shared.key(for: "webAppUrl"))!
public let deploymentUri = {
let url = dydxStringFeatureFlag.deployment_url.string ?? (CredientialConfig.shared.key(for: "webAppUrl"))!
return url.last == "/" ? url : url + "/"
}()

public var isMainNet: Bool {
asyncStateManager.environment?.isMainNet ?? false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class dydxProfileHeaderViewModel: PlatformViewModel {
@Published public var dydxChainLogoUrl: URL?
@Published public var dydxAddress: String?
@Published public var seeMoreInfoAction: (() -> Void)?
@Published public var switchWalletAction: (() -> Void)?
@Published public var manageWalletAction: (() -> Void)?

public init() { }

Expand All @@ -34,14 +34,14 @@ public class dydxProfileHeaderViewModel: PlatformViewModel {
clip: .noClip,
size: .init(width: 14, height: 8),
templateColor: .textTertiary)
let switchWalletButton = HStack(spacing: 9) {
Text(DataLocalizer.localize(path: "APP.GENERAL.SWITCH_WALLET"))
let manageWalletButton = HStack(spacing: 9) {
Text(DataLocalizer.localize(path: "APP.GENERAL.MANAGE_WALLET"))
.themeFont(fontSize: .small)
.themeColor(foreground: .textTertiary)
dropDownIcon.createView(parentStyle: parentStyle)
}
.onTapGesture { [weak self] in
self?.switchWalletAction?()
self?.manageWalletAction?()
}

let addressInfoView = VStack(alignment: .leading, spacing: 4) {
Expand All @@ -63,7 +63,7 @@ public class dydxProfileHeaderViewModel: PlatformViewModel {
.createView(parentStyle: parentStyle)
Spacer()
if self.dydxAddress?.isEmpty == false {
switchWalletButton
manageWalletButton
}
}
HStack {
Expand Down
93 changes: 63 additions & 30 deletions dydx/dydxViews/dydxViews/_v4/Wallet/WalletConnectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class WalletConnectionViewModel: PlatformViewModel {
@Published public var walletImageUrl: URL?
@Published public var pnl24hPercent: SignedAmountViewModel?
@Published public var onTap: (() -> Void)?
@Published public var openInEtherscanTapped: (() -> Void)?
@Published public var exportSecretPhraseTapped: (() -> Void)?

public init() {}

Expand All @@ -35,45 +37,76 @@ public class WalletConnectionViewModel: PlatformViewModel {
PlatformView(viewModel: self, parentStyle: parentStyle, styleKey: styleKey) { [weak self] style in
AnyView(
Group {
let icon = PlatformIconViewModel(type: .url(url: self?.walletImageUrl), clip: .defaultCircle)
let buttonBorderWidth = 1.5
let buttonContentVerticalPadding: CGFloat = 8
let buttonContentHorizontalPadding: CGFloat = 12

let icon = self?.walletImageUrl == nil ? nil : PlatformIconViewModel(type: .url(url: self?.walletImageUrl), clip: .defaultCircle)
.createView(parentStyle: style)
let status = PlatformIconViewModel(type: .asset(name: "status_filled", bundle: Bundle.dydxView),
size: CGSize(width: 8, height: 8),
templateColor: self?.templateColor ?? .textTertiary)
size: CGSize(width: 8, height: 8),
templateColor: self?.templateColor ?? .textTertiary)
.createView(parentStyle: style)

let main =
Text(self?.walletAddress ?? "")
let addressText = Text(self?.walletAddress ?? "")
.lineLimit(1)
.truncationMode(.middle)
.themeFont(fontSize: .medium)
.themeColor(foreground: .textPrimary)

let blockExplorerButton = Button(action: {
self?.openInEtherscanTapped?()
}, label: {
HStack(spacing: 4) {
Text(DataLocalizer.shared?.localize(path: "APP.GENERAL.BLOCK_EXPLORER", params: nil) ?? "")
.lineLimit(1)
.themeFont(fontSize: .medium)
.themeColor(foreground: .textTertiary)
PlatformIconViewModel(type: .asset(name: "icon_external_link",
bundle: .dydxView),
clip: .noClip,
size: .init(width: 20, height: 20),
templateColor: .textTertiary)
.createView(parentStyle: style)
}
.fixedSize()
.padding(.horizontal, buttonContentHorizontalPadding)
.padding(.vertical, buttonContentVerticalPadding)
.themeColor(background: .layer3)
})
.borderAndClip(style: .capsule, borderColor: .layer6, lineWidth: buttonBorderWidth)

let exportPhraseButton = Button(action: {
self?.exportSecretPhraseTapped?()
}, label: {
Text(DataLocalizer.shared?.localize(path: "APP.MNEMONIC_EXPORT.EXPORT_PHRASE", params: nil) ?? "")
.lineLimit(1)
.truncationMode(.middle)
.themeFont(fontSize: .medium)
.themeColor(foreground: .textPrimary)

let trailing =
VStack(alignment: .trailing, spacing: 2) {
Text(self?.equity ?? "")
.themeFont(fontType: .number, fontSize: .small)
if let pnl24hPercent = self?.pnl24hPercent {
HStack {
Text(DataLocalizer.localize(path: "APP.GENERAL.TIME_STRINGS.24H", params: nil))
.themeFont(fontSize: .smaller)
pnl24hPercent.createView(parentStyle: style
.themeColor(foreground: .textTertiary)
.themeFont(fontType: .number, fontSize: .smaller))
}
.themeColor(foreground: .colorRed)
.padding(.horizontal, buttonContentHorizontalPadding)
.padding(.vertical, buttonContentVerticalPadding)
})
.themeColor(background: .colorFadedRed)
.borderAndClip(style: .capsule, borderColor: .borderDestructive, lineWidth: buttonBorderWidth)

let main = VStack(alignment: .leading) {
addressText
if self?.selected == true {
HStack(spacing: 10) {
blockExplorerButton
exportPhraseButton
}
}

Group {
PlatformTableViewCellViewModel(leading: status.wrappedViewModel,
logo: icon.wrappedViewModel,
main: main.wrappedViewModel,
trailing: trailing.wrappedViewModel)
.createView(parentStyle: style)
}
.frame(width: UIScreen.main.bounds.width - 32, height: 64)
.themeColor(background: .layer5)
.cornerRadius(16)
.padding(.vertical, 16)

PlatformTableViewCellViewModel(leading: status.wrappedViewModel,
logo: icon?.wrappedViewModel,
main: main.wrappedViewModel)
.createView(parentStyle: style)
.frame(width: UIScreen.main.bounds.width - 32)
.themeColor(background: self?.selected == true ? .layer1 : .layer3)
.borderAndClip(style: .cornerRadius(10), borderColor: .borderDefault, lineWidth: 1)
.onTapGesture {
self?.onTap?()
}
Expand Down
2 changes: 1 addition & 1 deletion dydx/dydxViews/dydxViews/_v4/Wallet/Wallets2View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Wallets2ViewModel: PlatformViewModel {
guard let self = self else { return AnyView(PlatformView.nilView) }

let view = VStack(alignment: .leading, spacing: 16) {
Text(DataLocalizer.localize(path: "APP.GENERAL.SWITCH_WALLET", params: nil))
Text(DataLocalizer.localize(path: "APP.GENERAL.MANAGE_WALLET", params: nil))
.themeFont(fontType: .bold, fontSize: .largest)
.padding(.top, 40)

Expand Down

0 comments on commit b66db4c

Please sign in to comment.