From d44352bd5d3a04805703dfa9e05d3f4b1e4b1d97 Mon Sep 17 00:00:00 2001 From: Mike Date: Thu, 15 Aug 2024 17:11:27 -0400 Subject: [PATCH] keep chart vm in memory --- .../_v4/Vault/dydxVaultViewBuilder.swift | 51 ++++++++++++++----- .../Landing/dydxVaultChartViewModel.swift | 46 ++++------------- .../Vault/Landing/dydxVaultViewModel.swift | 3 +- 3 files changed, 48 insertions(+), 52 deletions(-) diff --git a/dydx/dydxPresenters/dydxPresenters/_v4/Vault/dydxVaultViewBuilder.swift b/dydx/dydxPresenters/dydxPresenters/_v4/Vault/dydxVaultViewBuilder.swift index bc305c98b..13a16344b 100644 --- a/dydx/dydxPresenters/dydxPresenters/_v4/Vault/dydxVaultViewBuilder.swift +++ b/dydx/dydxPresenters/dydxPresenters/_v4/Vault/dydxVaultViewBuilder.swift @@ -13,6 +13,7 @@ import PlatformParticles import RoutingKit import ParticlesKit import PlatformUI +import Charts public class dydxVaultViewBuilder: NSObject, ObjectBuilderProtocol { public func build() -> T? { @@ -41,23 +42,45 @@ private class dydxVaultViewBuilderPresenter: HostedViewPresenter [ChartDataEntry] { + let selectedValueTime = viewModel?.vaultChart?.selectedValueTime ?? .oneDay + let now = Date().timeIntervalSince1970 + let finalTimeSecondsAway = selectedValueTime == .oneDay ? 3600.0*24.0 : selectedValueTime == .sevenDays ? 3600.0*24.0*7.0 : 3600.0*24.0*30.0 + let numEntries = Int.random(in: 0..<100) + let entries = (0.. [dydxVaultPositionViewModel] { + return [ + dydxVaultPositionViewModel(assetName: "logo_bitcoin", market: "BTC", side: .long, leverage: 10.80, notionalValue: 100000, positionSize: 10000, token: "BTC", tokenUnitPrecision: 6, pnlAmount: 1000, pnlPercentage: 10, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_ethereum", market: "ETH", side: .short, leverage: 88.88, notionalValue: 50000, positionSize: 10000, token: "ETH", tokenUnitPrecision: -1, pnlAmount: -500, pnlPercentage: -1, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_bitcoin", market: "BTC", side: .long, leverage: 10.80, notionalValue: 100000, positionSize: 10000, token: "BTC", tokenUnitPrecision: 6, pnlAmount: 1000, pnlPercentage: 10, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_ethereum", market: "ETH", side: .short, leverage: 88.88, notionalValue: 50000, positionSize: 10000, token: "ETH", tokenUnitPrecision: -1, pnlAmount: -500, pnlPercentage: -1, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_bitcoin", market: "BTC", side: .long, leverage: 10.80, notionalValue: 100000, positionSize: 10000, token: "BTC", tokenUnitPrecision: 6, pnlAmount: 1000, pnlPercentage: 10, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_ethereum", market: "ETH", side: .short, leverage: 88.88, notionalValue: 50000, positionSize: 10000, token: "ETH", tokenUnitPrecision: -1, pnlAmount: -500, pnlPercentage: -1, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_bitcoin", market: "BTC", side: .long, leverage: 10.80, notionalValue: 100000, positionSize: 10000, token: "BTC", tokenUnitPrecision: 6, pnlAmount: 1000, pnlPercentage: 10, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_ethereum", market: "ETH", side: .short, leverage: 88.88, notionalValue: 50000, positionSize: 10000, token: "ETH", tokenUnitPrecision: -1, pnlAmount: -500, pnlPercentage: -1, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_bitcoin", market: "BTC", side: .long, leverage: 10.80, notionalValue: 100000, positionSize: 10000, token: "BTC", tokenUnitPrecision: 6, pnlAmount: 1000, pnlPercentage: 10, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_ethereum", market: "ETH", side: .short, leverage: 88.88, notionalValue: 50000, positionSize: 10000, token: "ETH", tokenUnitPrecision: -1, pnlAmount: -500, pnlPercentage: -1, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_bitcoin", market: "BTC", side: .long, leverage: 10.80, notionalValue: 100000, positionSize: 10000, token: "BTC", tokenUnitPrecision: 6, pnlAmount: 1000, pnlPercentage: 10, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + dydxVaultPositionViewModel(assetName: "logo_ethereum", market: "ETH", side: .short, leverage: 88.88, notionalValue: 50000, positionSize: 10000, token: "ETH", tokenUnitPrecision: -1, pnlAmount: -500, pnlPercentage: -1, sparklineValues: (0..<10).map { _ in Double.random(in: 0.0...1.0) }), + ] + + } } diff --git a/dydx/dydxViews/dydxViews/_v4/Vault/Landing/dydxVaultChartViewModel.swift b/dydx/dydxViews/dydxViews/_v4/Vault/Landing/dydxVaultChartViewModel.swift index 906ad2fd4..96b4bf07f 100644 --- a/dydx/dydxViews/dydxViews/_v4/Vault/Landing/dydxVaultChartViewModel.swift +++ b/dydx/dydxViews/dydxViews/_v4/Vault/Landing/dydxVaultChartViewModel.swift @@ -15,18 +15,12 @@ import dydxChart public class dydxVaultChartViewModel: PlatformViewModel { - @Published var selectedValueType: ValueTypeOption = .pnl - @Published var selectedValueTime: ValueTimeOption = .oneDay { - didSet { - //TODO: remove, just for testing - guard oldValue != selectedValueTime else { return } - setEntries(selectedValueTime: selectedValueTime, selectedValueType: selectedValueType) - } - } + @Published public var selectedValueType: ValueTypeOption = .pnl + @Published public var selectedValueTime: ValueTimeOption = .oneDay fileprivate let valueTypeOptions = ValueTypeOption.allCases fileprivate let valueTimeOptions = ValueTimeOption.allCases - + fileprivate let lineChart = { let lineChart = LineChartView() lineChart.data = LineChartData() @@ -55,23 +49,10 @@ public class dydxVaultChartViewModel: PlatformViewModel { return lineChart }() - // TODO: replace with actual data, delete cancellables - public func setEntries(entries: [ChartDataEntry] = [], selectedValueTime newSelectedValueTime: ValueTimeOption? = nil, selectedValueType newSelectedValueType: ValueTypeOption? = nil) { - if let newSelectedValueType { - selectedValueType = newSelectedValueType - } - if let newSelectedValueTime { - selectedValueTime = newSelectedValueTime - } - //TODO: remove - // this is just for testing - let now = Date().timeIntervalSince1970 - let finalTimeSecondsAway = selectedValueTime == .oneDay ? 3600.0*24.0 : selectedValueTime == .sevenDays ? 3600.0*24.0*7.0 : 3600.0*24.0*30.0 - let numEntries = Int.random(in: 0..<100) - let entries = (0..= (entries.first?.y ?? -Double.infinity) let color = isPositive ? ThemeSettings.positiveColor.uiColor : ThemeSettings.negativeColor.uiColor @@ -101,17 +82,8 @@ public class dydxVaultChartViewModel: PlatformViewModel { lineChart.xAxis.valueFormatter = selectedValueTime.valueFormatter lineChart.data = LineChartData(dataSet: dataSet) - } - - // TODO: delete and replace with real data - private var cancellables = Set() - init() { - super.init() - Timer.publish(every: 1, triggerNow: true) - .sink { [weak self] _ in - self?.setEntries() - } - .store(in: &cancellables) + lineChart.notifyDataSetChanged() + objectWillChange.send() } public enum ValueTypeOption: CaseIterable, RadioButtonContentDisplayable { diff --git a/dydx/dydxViews/dydxViews/_v4/Vault/Landing/dydxVaultViewModel.swift b/dydx/dydxViews/dydxViews/_v4/Vault/Landing/dydxVaultViewModel.swift index 0bfab4ded..4a2fa90b0 100644 --- a/dydx/dydxViews/dydxViews/_v4/Vault/Landing/dydxVaultViewModel.swift +++ b/dydx/dydxViews/dydxViews/_v4/Vault/Landing/dydxVaultViewModel.swift @@ -15,6 +15,7 @@ public class dydxVaultViewModel: PlatformViewModel { @Published public var vaultBalance: Double? @Published public var profitDollars: Double? @Published public var profitPercentage: Double? + @Published public var vaultChart: dydxVaultChartViewModel? @Published public var positions: [dydxVaultPositionViewModel]? @Published public var cancelAction: (() -> Void)? @Published public var learnMoreAction: (() -> Void)? @@ -182,7 +183,7 @@ private struct dydxVaultView: View { // MARK: - Section 3 - graph var chart: some View { - dydxVaultChartViewModel() + viewModel.vaultChart? .createView() .frame(height: 174) }