-
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.
unopened isolated positions pt2 - cancel flow (#195)
* fix adjust margin % horizontal sizings * remove unused files * dismiss on success, update button state on submission * multi-line receipt line item title * stubbed unopened isolated positions UI * wire up abacus data for unopened positions * put back fixed size, clean up * remove max action * update button state after validation * clean up * ui tweaks * Update Package.resolved * stubbed unopened isolated positions UI * wire up abacus data for unopened positions * update button state after validation * change routing to orders tab, populate unopened isolated position * fix `filterByMarketId` not triggering a list update * show unopened isolated position in market info view * stub UI for cancel orders * cancel orders
- Loading branch information
Showing
17 changed files
with
496 additions
and
30 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
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
127 changes: 127 additions & 0 deletions
127
...esenters/dydxPresenters/_v4/CancelOrders/dydxCancelPendingIsolatedOrdersViewBuilder.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,127 @@ | ||
// | ||
// dydxCancelPendingIsolatedOrdersViewBuilder.swift | ||
// dydxUI | ||
// | ||
// Created by Michael Maguire on 6/17/24. | ||
// Copyright © 2024 dYdX Trading Inc. All rights reserved. | ||
// | ||
import Utilities | ||
import dydxViews | ||
import PlatformParticles | ||
import RoutingKit | ||
import ParticlesKit | ||
import PlatformUI | ||
import dydxStateManager | ||
import Combine | ||
|
||
public class dydxCancelPendingIsolatedOrdersViewBuilder: NSObject, ObjectBuilderProtocol { | ||
public func build<T>() -> T? { | ||
let presenter = dydxCancelPendingIsolatedOrdersViewBuilderPresenter() | ||
let view = presenter.viewModel?.createView() ?? PlatformViewModel().createView() | ||
return dydxCancelPendingIsolatedOrdersViewBuilderController(presenter: presenter, view: view, configuration: .default) as? T | ||
} | ||
} | ||
|
||
private class dydxCancelPendingIsolatedOrdersViewBuilderController: HostingViewController<PlatformView, dydxCancelPendingIsolatedOrdersViewModel> { | ||
override public func arrive(to request: RoutingRequest?, animated: Bool) -> Bool { | ||
if let marketId = request?.params?["market"] as? String, | ||
request?.path == "/portfolio/cancel_pending_position", | ||
let presenter = presenter as? dydxCancelPendingIsolatedOrdersViewBuilderPresenterProtocol { | ||
presenter.marketId = marketId | ||
return true | ||
} | ||
return false | ||
} | ||
} | ||
|
||
private protocol dydxCancelPendingIsolatedOrdersViewBuilderPresenterProtocol: HostedViewPresenterProtocol { | ||
var viewModel: dydxCancelPendingIsolatedOrdersViewModel? { get } | ||
var marketId: String? { get set } | ||
} | ||
|
||
private class dydxCancelPendingIsolatedOrdersViewBuilderPresenter: HostedViewPresenter<dydxCancelPendingIsolatedOrdersViewModel>, dydxCancelPendingIsolatedOrdersViewBuilderPresenterProtocol { | ||
fileprivate var marketId: String? | ||
|
||
override init() { | ||
super.init() | ||
|
||
self.viewModel = .init(marketLogoUrl: nil, marketName: "", marketId: "", orderCount: 0, cancelAction: {}) | ||
} | ||
|
||
override func start() { | ||
super.start() | ||
|
||
Publishers.CombineLatest( | ||
AbacusStateManager.shared.state.configsAndAssetMap, | ||
AbacusStateManager.shared.state.selectedSubaccountOrders | ||
) | ||
.receive(on: RunLoop.main) | ||
.sink { [weak self] configsAndAssetMap, orders in | ||
guard let self = self, | ||
let marketId = self.marketId, | ||
let asset = configsAndAssetMap[marketId]?.asset | ||
else { return } | ||
let pendingOrders = orders.filter { $0.marketId == marketId && $0.status == .open } | ||
self.viewModel?.marketLogoUrl = URL(string: asset.resources?.imageUrl ?? "") | ||
self.viewModel?.marketName = asset.name ?? "--" | ||
self.viewModel?.marketId = asset.id | ||
self.viewModel?.orderCount = pendingOrders.count | ||
self.viewModel?.failureCount = self.viewModel?.failureCount | ||
self.viewModel?.cancelAction = { [weak self] in | ||
self?.tryCancelOrders(orderIds: orders.map(\.id)) | ||
} | ||
} | ||
.store(in: &subscriptions) | ||
} | ||
|
||
private func tryCancelOrders(orderIds: [String]) { | ||
viewModel?.state = viewModel?.failureCount == nil ? .submitting : .resubmitting | ||
Task { [weak self] in | ||
guard let self = self else { return } | ||
|
||
// Create an array to hold the results of the cancellations | ||
var results: [Result<AbacusStateManager.SubmissionStatus, Error>] = [] | ||
|
||
// Use a TaskGroup to kick off multiple calls and wait for all to finish | ||
await withTaskGroup(of: Result<AbacusStateManager.SubmissionStatus, Error>.self) { group in | ||
for orderId in orderIds { | ||
group.addTask { | ||
do { | ||
let status = try await AbacusStateManager.shared.cancelOrder(orderId: orderId) | ||
return .success(status) | ||
} catch { | ||
return .failure(error) | ||
} | ||
} | ||
} | ||
|
||
// Collect the results of all tasks | ||
for await result in group { | ||
results.append(result) | ||
} | ||
} | ||
|
||
// Count the number of failed cancellations | ||
let failureCount = results.filter { result in | ||
if case .failure = result { | ||
return true | ||
} | ||
return false | ||
}.count | ||
|
||
await updateState(failureCount: failureCount) | ||
} | ||
} | ||
|
||
@MainActor | ||
private func updateState(failureCount: Int) { | ||
self.viewModel?.failureCount = failureCount | ||
|
||
if failureCount > 0 { | ||
self.viewModel?.state = .failed | ||
} else { | ||
Router.shared?.navigate(to: RoutingRequest(path: "/action/dismiss"), animated: true, completion: nil) | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.