Skip to content

Commit

Permalink
always show positions/orders/fills in market view
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Jun 19, 2024
1 parent 0c9a9f8 commit 7dcc2c4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ class dydxMarketPositionViewPresenter: HostedViewPresenter<dydxMarketPositionVie
}
.store(in: &subscriptions)

Publishers
.CombineLatest(AbacusStateManager.shared.state.onboarded,
$position.removeDuplicates())
.sink { [weak self] (onboarded, position) in
if !onboarded {
self?.viewModel?.emptyText = DataLocalizer.localize(path: "APP.GENERAL.PLACEHOLDER_NO_POSITIONS_LOG_IN")
} else if position == nil {
self?.viewModel?.emptyText = DataLocalizer.localize(path: "APP.GENERAL.PLACEHOLDER_NO_POSITIONS")
} else {
self?.viewModel?.emptyText = nil
}
}
.store(in: &subscriptions)

Publishers
.CombineLatest4($position.compactMap { $0 }.removeDuplicates(),
AbacusStateManager.shared.state.selectedSubaccountTriggerOrders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,28 +171,20 @@ private class dydxMarketInfoViewPresenter: HostedViewPresenter<dydxMarketInfoVie
}

private func updatePositionSection(position: SubaccountPosition?, pendingPosition: SubaccountPendingPosition?) {
if let position, position.side.current != PositionSide.none, let viewModel = viewModel {
viewModel.showPositionSection = true
if let position, position.side.current != PositionSide.none {
fillsPresenter.filterByMarketId = position.id
fundingPresenter.filterByMarketId = position.id
ordersPresenter.filterByMarketId = position.id
positionPresenter.position = position
positionPresenter.pendingPosition = nil
resetPresentersForVisibilityChange()
} else if let pendingPosition, pendingPosition.orderCount > 0, let viewModel = viewModel {
viewModel.showPositionSection = true
} else if let pendingPosition, pendingPosition.orderCount > 0 {
fillsPresenter.filterByMarketId = pendingPosition.marketId
fundingPresenter.filterByMarketId = pendingPosition.marketId
ordersPresenter.filterByMarketId = pendingPosition.marketId
positionPresenter.position = nil
positionPresenter.pendingPosition = pendingPosition
resetPresentersForVisibilityChange()
} else {
viewModel?.showPositionSection = false
for (_, presenter) in selectionPresenters {
presenter.stop()
}
}
resetPresentersForVisibilityChange()
}

private func resetPresentersForVisibilityChange() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import PlatformUI
import Utilities

public class dydxMarketPositionViewModel: PlatformViewModel {
@Published public var emptyText: String?

@Published public var takeProfitStopLossAction: (() -> Void)?
@Published public var closeAction: (() -> Void)?
@Published public var unrealizedPNLAmount: SignedAmountViewModel?
Expand Down Expand Up @@ -69,20 +71,28 @@ public class dydxMarketPositionViewModel: PlatformViewModel {
guard let self = self else { return AnyView(PlatformView.nilView) }

return AnyView(
Group {
if let pendingPosition = self.pendingPosition {
pendingPosition.createView(parentStyle: style)
} else {
VStack {
VStack(spacing: 24) {
// check size to determine if there is current position data to display
VStack {
if let emptyText = self.emptyText {
PlaceholderViewModel(text: emptyText)
.createView()
} else {
self.createCollection(parentStyle: style)

self.createButtons(parentStyle: style)

self.createList(parentStyle: style)
}
.themeColor(background: .layer2)
}

if let pendingPosition = self.pendingPosition {
VStack(spacing: 16) {
self.createPendingPositionsHeader(parentStyle: style)
pendingPosition.createView(parentStyle: style)
}
.frame(width: UIScreen.main.bounds.width - 32)
}
}
.themeColor(background: .layer2)
.frame(width: UIScreen.main.bounds.width - 16)
)
}
Expand Down Expand Up @@ -310,6 +320,13 @@ public class dydxMarketPositionViewModel: PlatformViewModel {
}
.padding(.horizontal, 8)
}

private func createPendingPositionsHeader(parentStyle: ThemeStyle) -> some View {
Text(localizerPathKey: "APP.TRADE.UNOPENED_ISOLATED_POSITIONS")
.themeFont(fontSize: .larger)
.themeColor(foreground: .textSecondary)
.leftAligned()
}
}

#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class dydxMarketInfoViewModel: PlatformViewModel {
@Published public var resources = dydxMarketResourcesViewModel()
@Published public var configs: dydxMarketConfigsViewModel? = dydxMarketConfigsViewModel()

@Published public var showPositionSection: Bool = false
@Published public var sections = dydxPortfolioSectionsViewModel()
@Published public var fills = dydxPortfolioFillsViewModel()
@Published public var position = dydxMarketPositionViewModel()
Expand Down Expand Up @@ -48,7 +47,6 @@ public class dydxMarketInfoViewModel: PlatformViewModel {
vm.stats = .previewValue
vm.resources = .previewValue
vm.configs = .previewValue
vm.showPositionSection = true
vm.sections = .previewValue
vm.position = .previewValue
vm.orders = .previewValue
Expand All @@ -70,10 +68,8 @@ public class dydxMarketInfoViewModel: PlatformViewModel {
LazyVStack(pinnedViews: [.sectionHeaders]) {
self.createChartPagesSection(parentStyle: style)

if self.showPositionSection {
self.createPositionSection(parentStyle: style)
Spacer(minLength: 24)
}
self.createPositionSection(parentStyle: style)
Spacer(minLength: 24)

self.createStatsSection(parentStyle: style)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,27 @@ public class dydxPortfolioPendingPositionsItemViewModel: PlatformViewModel {
}()

private var topContent: some View {
VStack(spacing: 8) {
VStack(spacing: 12) {
HStack(spacing: 8) {
PlatformIconViewModel(type: .url(url: marketLogoUrl),
clip: .defaultCircle,
size: CGSize(width: 20, height: 20))
.createView()
Text(marketName)
.themeFont(fontSize: .small)
.themeFont(fontSize: .large)
.themeColor(foreground: .textSecondary)
Spacer()
}
HStack(spacing: 0) {
Text(localizerPathKey: "APP.GENERAL.MARGIN")
.themeFont(fontSize: .smaller)
.themeFont(fontSize: .medium)
.themeColor(foreground: .textTertiary)
Spacer()
Text(margin)
.themeFont(fontSize: .smaller)
.themeFont(fontSize: .medium)
.themeColor(foreground: .textSecondary)
}
}
.padding(.vertical, 10)
}

private var divider: some View {
Expand All @@ -85,13 +84,11 @@ public class dydxPortfolioPendingPositionsItemViewModel: PlatformViewModel {
}

let viewOrders = Text(localizerPathKey: viewOrdersStringKey, params: viewOrdersStringParams)
.themeFont(fontSize: .smaller)
.themeFont(fontSize: .medium)
.themeColor(foreground: .colorPurple)
.padding(.vertical, 8)
let cancel = Text(localizerPathKey: "APP.GENERAL.CANCEL")
.themeFont(fontSize: .smaller)
.themeFont(fontSize: .medium)
.themeColor(foreground: .colorRed)
.padding(.vertical, 8)
return HStack(spacing: 0) {
Button(action: viewOrdersAction, label: { viewOrders })
Spacer()
Expand All @@ -103,14 +100,16 @@ public class dydxPortfolioPendingPositionsItemViewModel: PlatformViewModel {
PlatformView(viewModel: self, parentStyle: parentStyle, styleKey: styleKey) { [weak self] _ in
guard let self = self else { return AnyView(PlatformView.nilView) }

let horizontalPadding: CGFloat = 12
return VStack(spacing: 0) {
let verticalPadding: CGFloat = 16
let horizontalPadding: CGFloat = 20
return VStack(spacing: 12) {
self.topContent
self.divider
.padding(.horizontal, -horizontalPadding)
self.bottomContent
}
.padding(.horizontal, horizontalPadding)
.padding(.vertical, verticalPadding)
.themeColor(background: .layer3)
.clipShape(.rect(cornerRadius: 10))
.wrappedInAnyView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,24 @@ public class dydxPortfolioPositionsViewModel: PlatformViewModel {
Spacer()
}
.padding(.horizontal, 16)
.frame(width: UIScreen.main.bounds.width - 32)
.themeFont(fontSize: .small)
.themeColor(foreground: .textTertiary)
.wrappedViewModel
}

private var openPositionsView: some View {
LazyVStack {
let items = self.positionItems.map { $0.createView() }
ForEach(items.indices, id: \.self) { index in
items[index]
if let emptyText = self.emptyText, positionItems.isEmpty {
AnyView(
PlaceholderViewModel(text: emptyText)
.createView()
)
} else {
let items = self.positionItems.map { $0.createView() }
ForEach(items.indices, id: \.self) { index in
items[index]
}
}
}
}
Expand All @@ -321,16 +329,9 @@ public class dydxPortfolioPositionsViewModel: PlatformViewModel {
}

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

if let emptyText = self.emptyText, positionItems.isEmpty, pendingPositionItems.isEmpty {
return AnyView(
PlaceholderViewModel(text: emptyText)
.createView(parentStyle: style)
)
}

return AnyView(
ScrollView {
VStack(spacing: 24) {
Expand Down

0 comments on commit 7dcc2c4

Please sign in to comment.