Skip to content

Commit

Permalink
use displayid for market mode selector
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Aug 19, 2024
1 parent 6707502 commit 892af07
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ParticlesKit
import PlatformUI
import dydxStateManager
import Abacus
import Combine

public class dydxMarginModeViewBuilder: NSObject, ObjectBuilderProtocol {
public func build<T>() -> T? {
Expand Down Expand Up @@ -62,16 +63,31 @@ private class dydxMarginModeViewPresenter: HostedViewPresenter<dydxMarginModeVie
override func start() {
super.start()

Publishers
.CombineLatest(
AbacusStateManager.shared.state.tradeInput,
AbacusStateManager.shared.state.marketMap
)
.sink { [weak self] tradeInput, marketMap in
self?.updateMarketDisplayId(tradeInput: tradeInput, marketMap: marketMap)
}
.store(in: &subscriptions)

AbacusStateManager.shared.state.tradeInput
.compactMap { $0 }
.sink {[weak self] input in
// call this in separate publisher so that market maps do not overwrite trade input
self?.update(tradeInput: input)
}
.store(in: &subscriptions)
}

private func updateMarketDisplayId(tradeInput: Abacus.TradeInput?, marketMap: [String: PerpetualMarket]?) {
guard let marketDisplayId = marketMap?[tradeInput?.marketId ?? ""]?.displayId else { return }
viewModel?.marketDisplayId = marketDisplayId
}

private func update(tradeInput: Abacus.TradeInput) {
viewModel?.market = tradeInput.marketId
let isSelectionDisabled = tradeInput.options?.marginModeOptions == nil
viewModel?.isDisabled = isSelectionDisabled
switch tradeInput.marginMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ public class dydxMarginModeItemViewModel: PlatformViewModel {
}

public class dydxMarginModeViewModel: PlatformViewModel {
@Published public var market: String?
@Published public var marketDisplayId: String?
@Published public var items: [dydxMarginModeItemViewModel] = []
@Published public var isDisabled: Bool = false

public init() { }

public static var previewValue: dydxMarginModeViewModel {
let vm = dydxMarginModeViewModel()
vm.market = "ETH-USD"
vm.marketDisplayId = "ETH-USD"
return vm
}

Expand All @@ -112,7 +112,7 @@ public class dydxMarginModeViewModel: PlatformViewModel {
Text(DataLocalizer.localize(path: "APP.GENERAL.MARGIN_MODE"))
.themeColor(foreground: .textPrimary)

Text(self.market ?? "")
Text(self.marketDisplayId ?? "")
.themeColor(foreground: .textSecondary)

Spacer()
Expand All @@ -127,7 +127,7 @@ public class dydxMarginModeViewModel: PlatformViewModel {
}

if self.isDisabled {
InlineAlertViewModel(.init(title: nil, body: DataLocalizer.localize(path: "WARNINGS.TRADE_BOX.UNABLE_TO_CHANGE_MARGIN_MODE", params: ["MARKET": self.market ?? "--"]), level: .warning))
InlineAlertViewModel(.init(title: nil, body: DataLocalizer.localize(path: "WARNINGS.TRADE_BOX.UNABLE_TO_CHANGE_MARGIN_MODE", params: ["MARKET": self.marketDisplayId ?? "--"]), level: .warning))
.createView()
}
Spacer()
Expand Down

0 comments on commit 892af07

Please sign in to comment.