Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixhuang committed Jan 16, 2025
1 parent aef52dc commit 687f0f8
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// dydxTradeRestrictedViewPresenter.swift
// dydxUI
//
// Created by Michael Maguire on 9/25/23.
// Copyright © 2023 dYdX Trading Inc. All rights reserved.
//

import SwiftUI
import PlatformUI
import Utilities

import dydxViews
import PlatformParticles
import RoutingKit
import ParticlesKit
import PanModal

public class dydxTradeRestrictedViewBuilder: NSObject, ObjectBuilderProtocol {
public func build<T>() -> T? {
let presenter = dydxTradeRestrictedPresenter()
let view = presenter.viewModel?.createView() ?? PlatformViewModel().createView()
return dydxTradeRestrictedController(presenter: presenter, view: view, configuration: .default) as? T
}
}

private class dydxTradeRestrictedController: HostingViewController<PlatformView, dydxTradeRestrictedViewModel> {
override var modalPresentationStyle: UIModalPresentationStyle { get { .overFullScreen } set {} }

override public func arrive(to request: RoutingRequest?, animated: Bool) -> Bool {
if request?.path == "/error/geo" {
return true
}
return false
}
}

private protocol dydxTradeRestrictedPresenterProtocol: HostedViewPresenterProtocol {
var viewModel: dydxTradeRestrictedViewModel? { get }
}

private class dydxTradeRestrictedPresenter: HostedViewPresenter<dydxTradeRestrictedViewModel>, dydxTradeRestrictedPresenterProtocol {
override init() {
super.init()

viewModel = dydxTradeRestrictedViewModel()
}
}

public class dydxTradeRestrictedViewModel: PlatformViewModel {

public init() { }

public static var previewValue: dydxTradeRestrictedViewModel {
let vm = dydxTradeRestrictedViewModel()
return vm
}

public override func createView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> PlatformView {
PlatformView(viewModel: self, parentStyle: parentStyle, styleKey: styleKey) { [weak self] _ in
guard let self = self else { return AnyView(PlatformView.nilView) }

return AnyView(
ZStack(alignment: .bottom) {
ThemeColor.SemanticColor.layer1.color
.opacity(0.8)
VStack(alignment: .leading, spacing: 16) {
HStack(spacing: 0) {
Text(DataLocalizer.shared?.localize(path: "ERRORS.ONBOARDING.REGION_NOT_PERMITTED_TITLE", params: nil) ?? "")
.themeFont(fontType: .plus, fontSize: .larger)
.themeColor(foreground: .textPrimary)
Spacer()
}
Text(DataLocalizer.shared?.localize(path: "ERRORS.ONBOARDING.REGION_NOT_PERMITTED_SUBTITLE", params: nil) ?? "")
.themeFont(fontType: .base, fontSize: .medium)
.themeColor(foreground: .textSecondary)
}
.padding([.top, .leading, .trailing], 36)
.padding(.bottom, self.safeAreaInsets?.bottom)
.themeColor(background: .layer3)
.cornerRadius(36, corners: [.topLeft, .topRight])
.frame(width: UIScreen.main.bounds.width)
}
.edgesIgnoringSafeArea(.vertical)
)
}
}
}

#if DEBUG
struct dydxTradeRestricted_Previews_Dark: PreviewProvider {
@StateObject static var themeSettings = ThemeSettings.shared

static var previews: some View {
ThemeSettings.applyDarkTheme()
ThemeSettings.applyStyles()
return dydxTradeRestrictedViewModel.previewValue
.createView()
// .edgesIgnoringSafeArea(.bottom)
.previewLayout(.sizeThatFits)
}
}

struct dydxTradeRestricted_Previews_Light: PreviewProvider {
@StateObject static var themeSettings = ThemeSettings.shared

static var previews: some View {
ThemeSettings.applyLightTheme()
ThemeSettings.applyStyles()
return dydxTradeRestrictedViewModel.previewValue
.createView()
// .edgesIgnoringSafeArea(.bottom)
.previewLayout(.sizeThatFits)
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ protocol dydxSimpleUIMarketDetailsViewPresenterProtocol: HostedViewPresenterProt
class dydxSimpleUIMarketDetailsViewPresenter: HostedViewPresenter<dydxSimpleUIMarketDetailsViewModel>, dydxSimpleUIMarketDetailsViewPresenterProtocol {
@Published var marketId: String?

var onContentChanged: ((SharedMarketViewModel?) -> Void)?

private let marketPresenter = SharedMarketPresenter()
private lazy var childPresenters: [HostedViewPresenterProtocol] = [
marketPresenter
Expand All @@ -48,21 +46,9 @@ class dydxSimpleUIMarketDetailsViewPresenter: HostedViewPresenter<dydxSimpleUIMa

marketPresenter.$viewModel
.sink { [weak self] viewModel in
self?.viewModel = dydxSimpleUIMarketDetailsViewModel()
self?.viewModel?.sharedMarketViewModel = viewModel
self?.onContentChanged?(viewModel)
}
.store(in: &subscriptions)

// Publishers
// .CombineLatest3($marketId,
// AbacusStateManager.shared.state.marketMap,
// AbacusStateManager.shared.state.assetMap)
// .sink { [weak self] marketId, marketMap, _ in
// guard let marketId = marketId, let market = marketMap[marketId] else { return }
// let tickSizeNumDecimals = market.configs?.displayTickSizeDecimals?.intValue ?? 0
// let stepSizeNumDecimals = market.configs?.displayStepSizeDecimals?.intValue ?? 0
//// self?.updateStats(market: market, asset: assetMap[market.assetId], stepSizeNumDecimals: stepSizeNumDecimals, tickSizeNumDecimals: tickSizeNumDecimals)
// }
// .store(in: &subscriptions)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,23 @@ class dydxSimpleUIMarketPositionViewPresenter: HostedViewPresenter<dydxSimpleUIM
}

private func updatePositionSection(position: SubaccountPosition?, marketMap: [String: PerpetualMarket], assetMap: [String: Asset]) {
let newViewModel = dydxSimpleUIMarketPositionViewModel()
newViewModel.tpSlGroupViewModel = viewModel?.tpSlGroupViewModel
viewModel = newViewModel
guard let position, let sharedOrderViewModel = dydxPortfolioPositionsViewPresenter.createPositionViewModelItem(position: position,
marketMap: marketMap,
assetMap: assetMap)
else {
viewModel?.side = nil // hide the view
viewModel?.hasPosition = false
viewModel?.side = SideTextViewModel(side: .none, coloringOption: .none)
return
}

viewModel?.hasPosition = true
viewModel?.symbol = sharedOrderViewModel.token?.symbol
viewModel?.unrealizedPNLAmount = sharedOrderViewModel.unrealizedPnl
viewModel?.size = sharedOrderViewModel.size
viewModel?.side = SideTextViewModel(side: sharedOrderViewModel.sideText.side, coloringOption: .withBackground)
viewModel?.side = SideTextViewModel(side: sharedOrderViewModel.sideText.side, coloringOption: .colored)
viewModel?.liquidationPrice = sharedOrderViewModel.liquidationPrice
viewModel?.entryPrice = sharedOrderViewModel.entryPrice

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ private class dydxSimpleUIMarketInfoViewPresenter: HostedViewPresenter<dydxSimpl
$marketId.assign(to: &detailsPresenter.$marketId)
$marketId.assign(to: &positionPresenter.$marketId)

// To force the list header to update (might be a bug in SwiftUI)
detailsPresenter.onContentChanged = { [weak self] market in
self?.viewModel?.details = dydxSimpleUIMarketDetailsViewModel()
self?.viewModel?.details?.sharedMarketViewModel = market
}

attachChildren(workers: childPresenters)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ struct CollectionItemUtil {
VStack(spacing: 0) {
VStack(alignment: .leading, spacing: 8) {
titleViewModel?.createView(parentStyle: parentStyle)
valueViewModel?.createView(parentStyle: parentStyle, styleKey: nil)
if let valueViewModel {
valueViewModel.createView(parentStyle: parentStyle, styleKey: nil)
} else {
Text( "-")
.themeFont(fontSize: .large)
.themeColor(foreground: .textSecondary)
.lineLimit(1)
.leftAligned()
}
}
Spacer()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class dydxSimpleUIMarketDetailsViewModel: PlatformViewModel {
}
.themeFont(fontSize: .medium)
.themeColor(foreground: .textSecondary)
.padding(.vertical, 8)
.padding(.horizontal, 16)

HStack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class dydxSimpleUIMarketPositionViewModel: PlatformViewModel {
@Published public var symbol: String?

@Published public var tpSlGroupViewModel: dydxMarketTpSlGroupViewModel?
@Published public var hasPosition: Bool = true

public init() { }

Expand All @@ -44,7 +45,7 @@ public class dydxSimpleUIMarketPositionViewModel: PlatformViewModel {

public override func createView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> PlatformView {
PlatformView(viewModel: self, parentStyle: parentStyle, styleKey: styleKey) { [weak self] style in
guard let self = self, self.side != nil else { return AnyView(PlatformView.nilView) }
guard let self = self else { return AnyView(PlatformView.nilView) }

return AnyView(
self.createContent(style: style)
Expand Down Expand Up @@ -97,28 +98,30 @@ public class dydxSimpleUIMarketPositionViewModel: PlatformViewModel {
.frame(maxWidth: .infinity)
.padding(.horizontal, 16)

HStack {
CollectionItemUtil.createCollectionItem(parentStyle: style,
title: DataLocalizer.localize(path: "APP.GENERAL.FUNDING_RATE_CHART_SHORT"),
valueViewModel: funding)
.frame(minWidth: 0, maxWidth: .infinity)

CollectionItemUtil.createCollectionItem(parentStyle: style,
title: DataLocalizer.localize(path: "APP.GENERAL.AVG_ENTRY"),
value: entryPrice)
.frame(minWidth: 0, maxWidth: .infinity)

CollectionItemUtil.createCollectionItem(parentStyle: style,
title: DataLocalizer.localize(path: "APP.TRADE.LIQUIDATION_PRICE_SHORT"),
value: liquidationPrice)
.frame(minWidth: 0, maxWidth: .infinity)
}
.frame(maxWidth: .infinity)
.padding(.horizontal, 16)

self.tpSlGroupViewModel?.createView(parentStyle: style)
if self.hasPosition {
HStack {
CollectionItemUtil.createCollectionItem(parentStyle: style,
title: DataLocalizer.localize(path: "APP.GENERAL.FUNDING_RATE_CHART_SHORT"),
valueViewModel: funding)
.frame(minWidth: 0, maxWidth: .infinity)

CollectionItemUtil.createCollectionItem(parentStyle: style,
title: DataLocalizer.localize(path: "APP.GENERAL.AVG_ENTRY"),
value: entryPrice)
.frame(minWidth: 0, maxWidth: .infinity)

CollectionItemUtil.createCollectionItem(parentStyle: style,
title: DataLocalizer.localize(path: "APP.TRADE.LIQUIDATION_PRICE_SHORT"),
value: liquidationPrice)
.frame(minWidth: 0, maxWidth: .infinity)
}
.frame(maxWidth: .infinity)
.padding(.horizontal, 16)

self.tpSlGroupViewModel?.createView(parentStyle: style)
.frame(maxWidth: .infinity)
.padding(.horizontal, 16)
}
}
.frame(minWidth: 0, maxWidth: .infinity)
}
Expand All @@ -137,15 +140,23 @@ public class dydxSimpleUIMarketPositionViewModel: PlatformViewModel {

Spacer()

let content = Text(DataLocalizer.localize(path: "APP.TRADE.CLOSE_POSITION"))
.themeColor(foreground: .colorRed)
.themeFont(fontSize: .small)
.wrappedViewModel
if hasPosition {
let content = Text(DataLocalizer.localize(path: "APP.TRADE.CLOSE_POSITION"))
.themeColor(foreground: .colorRed)
.themeFont(fontSize: .small)
.wrappedViewModel

PlatformButtonViewModel(content: content, type: .pill, state: .secondary, action: { [weak self] in
self?.closeAction?()
})
.createView(parentStyle: style)
Button(action: { [weak self] in
self?.closeAction?()
}) {
content.createView(parentStyle: style)
}
.buttonStyle(BorderlessButtonStyle())
.padding([.bottom, .top], 8)
.padding([.leading, .trailing], 12)
.themeColor(background: .layer3)
.clipShape(Capsule())
}
}
.padding(.trailing, 16)

Expand Down

0 comments on commit 687f0f8

Please sign in to comment.