Skip to content

Commit

Permalink
prevent flashing of position icons
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Oct 22, 2024
1 parent 9c956e8 commit 3831b72
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private class dydxVaultViewBuilderPresenter: HostedViewPresenter<dydxVaultViewMo
viewModel?.vaultBalance = vault?.account?.balanceUsdc?.doubleValue
viewModel?.allTimeReturnUsdc = vault?.account?.allTimeReturnUsdc?.doubleValue.round(to: 2)

viewModel?.positions = vault?.positions?.positions?.map { (position) -> dydxVaultPositionViewModel? in
let newPositions = vault?.positions?.positions?.map { (position) -> dydxVaultPositionViewModel? in
guard let leverage = position.currentLeverageMultiple?.doubleValue,
let marketId = position.marketId,
// special case for fake USDC market to show unused margin
Expand All @@ -110,20 +110,40 @@ private class dydxVaultViewBuilderPresenter: HostedViewPresenter<dydxVaultViewMo
iconType = .init(url: URL(string: assetMap[assetId]?.resources?.imageUrl ?? ""), placeholderText: assetId.first?.uppercased())
tokenUnitPrecision = marketMap[marketId]?.configs?.displayStepSizeDecimals?.intValue ?? 2
}
return dydxVaultPositionViewModel(displayId: displayId,
iconType: iconType,
side: positionSize > 0 ? .long : .short,
leverage: leverage,
equity: equity,
notionalValue: notionalValue,
positionSize: positionSize.magnitude,
tokenUnitPrecision: tokenUnitPrecision,
pnlAmount: position.thirtyDayPnl?.absolute?.doubleValue,
pnlPercentage: position.thirtyDayPnl?.percent?.doubleValue,
sparklineValues: position.thirtyDayPnl?.sparklinePoints?.map({ $0.doubleValue }))

// only create new view model instance if it does not already exist
if let existing = viewModel?.positions?[marketId] {
return existing.updated(
marketId: marketId,
displayId: displayId,
iconType: iconType,
side: positionSize > 0 ? .long : .short,
leverage: leverage,
equity: equity,
notionalValue: notionalValue,
positionSize: positionSize.magnitude,
tokenUnitPrecision: tokenUnitPrecision,
pnlAmount: position.thirtyDayPnl?.absolute?.doubleValue,
pnlPercentage: position.thirtyDayPnl?.percent?.doubleValue,
sparklineValues: position.thirtyDayPnl?.sparklinePoints?.map({ $0.doubleValue }))
} else {
return dydxVaultPositionViewModel(
marketId: marketId,
displayId: displayId,
iconType: iconType,
side: positionSize > 0 ? .long : .short,
leverage: leverage,
equity: equity,
notionalValue: notionalValue,
positionSize: positionSize.magnitude,
tokenUnitPrecision: tokenUnitPrecision,
pnlAmount: position.thirtyDayPnl?.absolute?.doubleValue,
pnlPercentage: position.thirtyDayPnl?.percent?.doubleValue,
sparklineValues: position.thirtyDayPnl?.sparklinePoints?.map({ $0.doubleValue }))
}
}
.compactMap { $0 }
.sorted(by: { $0.equity > $1.equity })
viewModel?.positions = Dictionary(uniqueKeysWithValues: newPositions?.map({ ( $0.marketId, $0) }) ?? [])
}

private func updateChartState(vault: Abacus.Vault?, valueType: dydxVaultChartViewModel.ValueTypeOption, timeType: dydxVaultChartViewModel.ValueTimeOption) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import PlatformUI
import Utilities
import DGCharts
import dydxFormatter
import SDWebImage
import SDWebImageSwiftUI

public class dydxVaultPositionViewModel: PlatformViewModel {

@Published public var marketId: String
@Published public var displayId: String
@Published public var iconType: PlatformIconViewModel.IconType = .init(url: nil, placeholderText: nil)
@Published public var side: SideTextViewModel.Side
Expand Down Expand Up @@ -62,7 +65,35 @@ public class dydxVaultPositionViewModel: PlatformViewModel {
dydxFormatter.shared.percent(number: pnlPercentage, digits: 2) ?? "--"
}

public func updated(marketId: String,
displayId: String,
iconType: PlatformIconViewModel.IconType,
side: SideTextViewModel.Side,
leverage: Double,
equity: Double,
notionalValue: Double,
positionSize: Double,
tokenUnitPrecision: Int,
pnlAmount: Double?,
pnlPercentage: Double?,
sparklineValues: [Double]?) -> dydxVaultPositionViewModel {
self.marketId = marketId
self.displayId = displayId
self.iconType = iconType
self.side = side
self.leverage = leverage
self.equity = equity
self.notionalValue = notionalValue
self.positionSize = positionSize
self.tokenUnitPrecision = tokenUnitPrecision
self.pnlAmount = pnlAmount
self.pnlPercentage = pnlPercentage
self.sparklineValues = sparklineValues
return self
}

public init(
marketId: String,
displayId: String,
iconType: PlatformIconViewModel.IconType,
side: SideTextViewModel.Side,
Expand All @@ -74,6 +105,7 @@ public class dydxVaultPositionViewModel: PlatformViewModel {
pnlAmount: Double?,
pnlPercentage: Double?,
sparklineValues: [Double]?) {
self.marketId = marketId
self.displayId = displayId
self.iconType = iconType
self.side = side
Expand All @@ -87,7 +119,7 @@ public class dydxVaultPositionViewModel: PlatformViewModel {
self.sparklineValues = sparklineValues
}

public override func createView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> PlatformView {
public override func createView(parentStyle: ThemeStyle = ThemeStyle.defaultStyle, styleKey: String? = nil) -> PlatformUI.PlatformView {
PlatformView(viewModel: self, parentStyle: parentStyle, styleKey: styleKey) { [weak self] _ in
guard let self = self else { return AnyView(PlatformView.nilView) }
return VaultPositionView(viewModel: self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ public class dydxVaultViewModel: PlatformViewModel {
@Published public var thirtyDayReturnPercent: Double?
@Published public var totalValueLocked: Double?
@Published public var vaultChart: dydxVaultChartViewModel?
@Published public var positions: [dydxVaultPositionViewModel]?
@Published public var positions: [String: dydxVaultPositionViewModel]?
@Published public var cancelAction: (() -> Void)?
@Published public var learnMoreAction: (() -> Void)?
@Published public var withdrawAction: (() -> Void)?
@Published public var depositAction: (() -> Void)?

fileprivate var sortedPositions: [dydxVaultPositionViewModel] {
positions?.values.sorted(by: { $0.equity > $1.equity }) ?? []
}

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) }
Expand Down Expand Up @@ -256,7 +260,7 @@ private struct dydxVaultView: View {
}

private var positionsList: some View {
ForEach(viewModel.positions ?? [], id: \.id) { position in
ForEach(viewModel.sortedPositions, id: \.id) { position in
position.createView()
.centerAligned()
.frame(height: 53)
Expand Down

0 comments on commit 3831b72

Please sign in to comment.