From 323ec8867522971b9ad3a495db40079d6b267caa Mon Sep 17 00:00:00 2001 From: kosyloa Date: Tue, 14 May 2024 14:26:36 +0200 Subject: [PATCH] redesign market depth: show without scroll --- .../QuoteTableApp/Base.lproj/Main.storyboard | 115 +----------------- .../MarketDepthViewController.swift | 74 ++++++----- Samples/QuoteTableApp/OrderCell.swift | 7 +- 3 files changed, 42 insertions(+), 154 deletions(-) diff --git a/Samples/QuoteTableApp/Base.lproj/Main.storyboard b/Samples/QuoteTableApp/Base.lproj/Main.storyboard index 76b6a491b..811cc496c 100644 --- a/Samples/QuoteTableApp/Base.lproj/Main.storyboard +++ b/Samples/QuoteTableApp/Base.lproj/Main.storyboard @@ -201,33 +201,8 @@ - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -420,33 +328,20 @@ - - - - - - - - - - + - - - + - - diff --git a/Samples/QuoteTableApp/MarketDepthViewController.swift b/Samples/QuoteTableApp/MarketDepthViewController.swift index 85a92e0d0..5866b2b95 100644 --- a/Samples/QuoteTableApp/MarketDepthViewController.swift +++ b/Samples/QuoteTableApp/MarketDepthViewController.swift @@ -13,7 +13,9 @@ class MarketDepthViewController: UIViewController { case buy = 1 case sell = 0 } - + private static let defaultCellSize: CGFloat = 40 + private var cellSize: CGFloat = defaultCellSize + var numberOfRows = 0 private var endpoint: DXEndpoint! private var feed: DXFeed! var symbol: String! @@ -22,11 +24,9 @@ class MarketDepthViewController: UIViewController { var orderBook = OrderBook() var maxValue: Double = 0 - @IBOutlet var connectButton: UIButton! - @IBOutlet var sourcesTextField: UITextField! - @IBOutlet var limitTextfield: UITextField! @IBOutlet var ordersTableView: UITableView! @IBOutlet var headerStackView: UIStackView! + @IBOutlet var headerConstraint: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() @@ -42,34 +42,37 @@ class MarketDepthViewController: UIViewController { self.ordersTableView.sectionHeaderTopPadding = 5 } self.view.backgroundColor = .tableBackground - self.sourcesTextField.backgroundColor = .cellBackground - self.sourcesTextField.placeholder = "Input sources splitted by ," - self.sourcesTextField.textColor = .text - self.limitTextfield.backgroundColor = .cellBackground - self.limitTextfield.textColor = .text - self.limitTextfield.placeholder = "Input limit" - self.limitTextfield.text = "5" - - self.limitTextfield.delegate = self - self.sourcesTextField.delegate = self -// self.sourcesTextField.text = "ntv" } - @IBAction func connectModel(_ sender: UIButton) { + private func calculateCells() { + let viewSize = ordersTableView.frame.size.height + numberOfRows = Int((viewSize / MarketDepthViewController.defaultCellSize) / 2) + cellSize = viewSize / CGFloat(numberOfRows) / 2 + } + + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + calculateCells() + model?.setDepthLimit(numberOfRows) + + self.ordersTableView.reloadData() + } + + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + calculateCells() + do { - let sources = try sourcesTextField.text?.split(separator: ",").map { str in - try OrderSource.valueOf(name: String(str)) - } - - if let sources = sources { - model = try MarketDepthModel(symbol: symbol, - sources: sources, - aggregationPeriodMillis: 0, - mode: .multiple, - feed: feed, - listener: self) - model?.setDepthLimit(Int(self.limitTextfield.text ?? "") ?? 0) - } + let sources = [OrderSource.agregateAsk!, OrderSource.agregateBid!] + + model = try MarketDepthModel(symbol: symbol, + sources: sources, + aggregationPeriodMillis: 0, + mode: .multiple, + feed: feed, + listener: self) + model?.setDepthLimit(numberOfRows) + } catch { print("MarketDepthModel: \(error)") } @@ -98,7 +101,7 @@ extension MarketDepthViewController: MarketDepthListener { extension MarketDepthViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { - return 45 + return cellSize } } @@ -111,9 +114,9 @@ extension MarketDepthViewController: UITableViewDataSource { let sectionIndex = SectionIndex(rawValue: section) switch sectionIndex { case .buy: - return orderBook.buyOrders.count + return min(orderBook.buyOrders.count, numberOfRows) case .sell: - return orderBook.sellOrders.count + return min(orderBook.sellOrders.count, numberOfRows) default: return 0 } @@ -159,10 +162,3 @@ extension MarketDepthViewController: UITableViewDataSource { return cell } } - -extension MarketDepthViewController: UITextFieldDelegate { - func textFieldShouldReturn(_ textField: UITextField) -> Bool { - textField.resignFirstResponder() - return true - } -} diff --git a/Samples/QuoteTableApp/OrderCell.swift b/Samples/QuoteTableApp/OrderCell.swift index 1c855a71e..4350848d1 100644 --- a/Samples/QuoteTableApp/OrderCell.swift +++ b/Samples/QuoteTableApp/OrderCell.swift @@ -26,7 +26,7 @@ class OrderCell: UITableViewCell { { let formatter = NumberFormatter() formatter.minimumFractionDigits = 0 - formatter.maximumFractionDigits = 8 + formatter.maximumFractionDigits = 4 return formatter }() @@ -61,9 +61,6 @@ class OrderCell: UITableViewCell { isBuy: Bool) { let price = order.price let size = order.size - let marketMaker = order.marketMaker - let source = order.eventSource - let scope = order.scope priceLabel.text = formatter.string(from: NSNumber(value: price)) @@ -77,7 +74,7 @@ class OrderCell: UITableViewCell { sizeSellContentView.backgroundColor = isBuy ?OrderCell.greenBarColor : OrderCell.redBarColor var multiplier = min(size/maxSize, 1) - if !multiplier.isFinite { + if !multiplier.isFinite || multiplier.isInfinite { multiplier = 0 } update(constraint: &sizeBuyConstraint, multiplier: multiplier, on: sizeBuyContentView)