Skip to content

Commit

Permalink
keep chart vm in memory
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-dydx committed Aug 15, 2024
1 parent a1b418d commit d44352b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import PlatformParticles
import RoutingKit
import ParticlesKit
import PlatformUI
import Charts

public class dydxVaultViewBuilder: NSObject, ObjectBuilderProtocol {
public func build<T>() -> T? {
Expand Down Expand Up @@ -41,23 +42,45 @@ private class dydxVaultViewBuilderPresenter: HostedViewPresenter<dydxVaultViewMo
super.init()

viewModel = dydxVaultViewModel()
viewModel?.vaultChart = dydxVaultChartViewModel()

//TODO: remove & replace, test only
Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { [weak self] _ in
self?.viewModel?.positions = [
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) }),
]
guard let self = self else { return }
self.viewModel?.vaultChart?.setEntries(entries: self.generateEntries())
self.viewModel?.positions = self.generatePositions()
}
}

// TODO: remove, just for testing
private func generateEntries() -> [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..<numEntries).map { i in
ChartDataEntry(x: now + Double(i)/Double(numEntries) * finalTimeSecondsAway, y: Double.random(in: 0..<100))
}
return entries
}

//TODO: remove
// this is just for testing
private func generatePositions() -> [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) }),
]

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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..<numEntries).map { i in
ChartDataEntry(x: now + Double(i)/Double(numEntries) * finalTimeSecondsAway, y: Double.random(in: 0..<100))
}

public init() {}

// TODO: replace with actual data
public func setEntries(entries: [ChartDataEntry] = []) {
let dataSet = LineChartDataSet(entries: entries)
let isPositive = (entries.last?.y ?? -Double.infinity) >= (entries.first?.y ?? -Double.infinity)
let color = isPositive ? ThemeSettings.positiveColor.uiColor : ThemeSettings.negativeColor.uiColor
Expand Down Expand Up @@ -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<AnyCancellable>()
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down Expand Up @@ -182,7 +183,7 @@ private struct dydxVaultView: View {

// MARK: - Section 3 - graph
var chart: some View {
dydxVaultChartViewModel()
viewModel.vaultChart?
.createView()
.frame(height: 174)
}
Expand Down

0 comments on commit d44352b

Please sign in to comment.