-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TRCL-3713 PNL Chart / No position view (#327)
* Add feature flag and mode selector for simple UI * WIP * Search * Onboarding and pnls * Move the swiftui chart logic to dydxLineChartViewModel for reuse * Chart * Add delay to reduce loading state updates * Default to show 90D PNL chart
- Loading branch information
Showing
18 changed files
with
481 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
dydx/dydxPresenters/dydxPresenters/_v4/Actions/dydxPostOnboardingAction.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// dydxPostOnboardingAction.swift | ||
// dydxPresenters | ||
// | ||
// Created by Rui Huang on 24/12/2024. | ||
// | ||
|
||
import Foundation | ||
import Utilities | ||
import RoutingKit | ||
import dydxStateManager | ||
import Combine | ||
import dydxFormatter | ||
|
||
public class dydxPostOnboardingActionBuilder: NSObject, ObjectBuilderProtocol { | ||
public func build<T>() -> T? { | ||
let action = dydxPostOnboardingAction() | ||
return action as? T | ||
} | ||
} | ||
|
||
private class dydxPostOnboardingAction: NSObject, NavigableProtocol { | ||
private var subscriptions = Set<AnyCancellable>() | ||
|
||
func navigate(to request: RoutingRequest?, animated: Bool, completion: RoutingCompletionBlock?) { | ||
switch request?.path { | ||
case "/action/post_onboarding": | ||
let walletId = parser.asString(request?.params?["walletId"]) | ||
if let ethereumAddress = parser.asString(request?.params?["ethereumAddress"]) { | ||
if let cosmoAddress = parser.asString(request?.params?["cosmoAddress"]), | ||
let mnemonic = parser.asString(request?.params?["mnemonic"]) { | ||
AbacusStateManager.shared.setV4(ethereumAddress: ethereumAddress, walletId: walletId, cosmoAddress: cosmoAddress, mnemonic: mnemonic) | ||
} else if let apiKey = parser.asString(request?.params?["apiKey"]), | ||
let secret = parser.asString(request?.params?["secret"]), | ||
let passPhrase = parser.asString(request?.params?["passPhrase"]) { | ||
AbacusStateManager.shared.setV3(ethereumAddress: ethereumAddress, walletId: walletId, apiKey: apiKey, secret: secret, passPhrase: passPhrase) | ||
} | ||
} | ||
Router.shared?.navigate(to: RoutingRequest(path: "/"), animated: animated, completion: completion) | ||
default: | ||
completion?(nil, false) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...s/dydxPresenters/_v4/SimpleUI/Markets/components/dydxSimpleUIPortfolioViewPresenter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// dydxSimpleUPortfolioViewPresenter.swift | ||
// dydxPresenters | ||
// | ||
// Created by Rui Huang on 23/12/2024. | ||
// | ||
|
||
import Utilities | ||
import dydxViews | ||
import PlatformParticles | ||
import RoutingKit | ||
import ParticlesKit | ||
import PlatformUI | ||
import dydxStateManager | ||
import Abacus | ||
import Combine | ||
import dydxFormatter | ||
|
||
protocol dydxSimpleUIPortfolioViewPresenterProtocol: HostedViewPresenterProtocol { | ||
var viewModel: dydxSimpleUIPortfolioViewModel? { get } | ||
} | ||
|
||
class dydxSimpleUIPortfolioViewPresenter: HostedViewPresenter<dydxSimpleUIPortfolioViewModel>, dydxSimpleUIPortfolioViewPresenterProtocol { | ||
|
||
private let accountPresenter = SharedAccountPresenter() | ||
private lazy var childPresenters: [HostedViewPresenterProtocol] = [ | ||
accountPresenter | ||
] | ||
|
||
private var loadingStartTime: Date? | ||
|
||
private static let loadingDelay: TimeInterval = 2.0 | ||
|
||
override init() { | ||
let viewModel = dydxSimpleUIPortfolioViewModel() | ||
|
||
accountPresenter.$viewModel.assign(to: &viewModel.$sharedAccountViewModel) | ||
|
||
super.init() | ||
|
||
self.viewModel = viewModel | ||
|
||
attachChildren(workers: childPresenters) | ||
} | ||
|
||
override func start() { | ||
super.start() | ||
|
||
AbacusStateManager.shared.setHistoricalPNLPeriod(period: HistoricalPnlPeriod.period90d) | ||
|
||
loadingStartTime = Date() | ||
Publishers.CombineLatest4( | ||
AbacusStateManager.shared.state.selectedSubaccount, | ||
AbacusStateManager.shared.state.selectedSubaccountPNLs, | ||
AbacusStateManager.shared.state.onboarded, | ||
Timer.publish(every: Self.loadingDelay, on: .main, in: .default).autoconnect() | ||
) | ||
.sink { [weak self] subaccount, pnls, onboarded, _ in | ||
if subaccount?.freeCollateral?.current?.doubleValue ?? 0 > 0 { | ||
self?.viewModel?.state = .hasBalance | ||
self?.viewModel?.buttonAction = nil | ||
if let subaccount = subaccount { | ||
self?.updatePNLs(pnls: pnls, subaccount: subaccount) | ||
} | ||
} else if let loadingStartTime = self?.loadingStartTime, Date().timeIntervalSince(loadingStartTime) > Self.loadingDelay { | ||
if onboarded { | ||
self?.viewModel?.state = .walletConnected | ||
self?.viewModel?.buttonAction = { | ||
Router.shared?.navigate(to: RoutingRequest(path: "/transfer"), animated: true, completion: nil) | ||
} | ||
} else { | ||
self?.viewModel?.state = .loggedOut | ||
self?.viewModel?.buttonAction = { | ||
Router.shared?.navigate(to: RoutingRequest(path: "/onboard"), animated: true, completion: nil) | ||
} | ||
} | ||
} | ||
} | ||
.store(in: &subscriptions) | ||
|
||
attachChild(worker: accountPresenter) | ||
} | ||
|
||
private func updatePNLs(pnls: [SubaccountHistoricalPNL], subaccount: Subaccount) { | ||
let firstTotalPnl = pnls.first?.totalPnl | ||
let targetTotalPnl = subaccount.pnlTotal?.doubleValue ?? pnls.last?.totalPnl | ||
let beginning = pnls.first?.equity | ||
|
||
if let firstTotalPnl = firstTotalPnl, let targetTotalPnl = targetTotalPnl, let beginning = beginning, beginning != 0 { | ||
viewModel?.pnlAmount = dydxFormatter.shared.dollar(number: targetTotalPnl - firstTotalPnl, digits: 2) | ||
let percent = dydxFormatter.shared.percent(number: abs(targetTotalPnl - firstTotalPnl) / beginning, digits: 2) | ||
viewModel?.pnlPercent = SignedAmountViewModel(text: percent, sign: targetTotalPnl >= firstTotalPnl ? .plus : .minus, coloringOption: .textOnly) | ||
} | ||
|
||
var chartEntries = pnls.compactMap { | ||
let date = $0.createdAtMilliseconds / 1000 | ||
let value = $0.equity | ||
return dydxLineChartViewModel.Entry(date: date, value: value) | ||
} | ||
if let currentValue = subaccount.equity?.current?.doubleValue { | ||
chartEntries.append(dydxLineChartViewModel.Entry(date: Double(Date().millisecondsSince1970) / 1000, value: currentValue)) | ||
} | ||
let maxValue = chartEntries.max { $0.value < $1.value }?.value ?? 0 | ||
let minValue = chartEntries.min { $0.value < $1.value }?.value ?? 0 | ||
viewModel?.chart.entries = chartEntries | ||
viewModel?.chart.showYLabels = false | ||
viewModel?.chart.valueLowerBoundOffset = (maxValue - minValue) * 0.8 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.