From 1640326e2e2e54a25c2a586cb76418ac79ebab3f Mon Sep 17 00:00:00 2001 From: kosyloa Date: Tue, 18 Jun 2024 10:02:52 +0200 Subject: [PATCH] fix warnings --- .../Native/Utils/NativeTimeUtil.swift | 2 +- .../DXFeedCandleChartMac/CandleChart.swift | 20 +++- .../CandleChartModel.swift | 112 ++++++++++-------- 3 files changed, 78 insertions(+), 56 deletions(-) diff --git a/DXFeedFramework/Native/Utils/NativeTimeUtil.swift b/DXFeedFramework/Native/Utils/NativeTimeUtil.swift index 651b2826c..2907647fa 100644 --- a/DXFeedFramework/Native/Utils/NativeTimeUtil.swift +++ b/DXFeedFramework/Native/Utils/NativeTimeUtil.swift @@ -23,7 +23,7 @@ class NativeTimeUtil { let result = try ErrorCheck.nativeCall(thread, dxfg_TimeFormat_format(thread, timeFormat.native, - value)) + value)) return try String(nullable: result).value() } diff --git a/Samples/DXFeedCandleChartMac/CandleChart.swift b/Samples/DXFeedCandleChartMac/CandleChart.swift index 8b3802d4e..4bba48176 100644 --- a/Samples/DXFeedCandleChartMac/CandleChart.swift +++ b/Samples/DXFeedCandleChartMac/CandleChart.swift @@ -212,8 +212,9 @@ struct CandleChart: View { let dateInterval = Calendar.current.dateInterval(of: .minute, for: selectedPrice.timestamp)! let startPositionX1 = proxy.position(forX: "\(dateInterval.start.timeIntervalSince1970)") ?? 0 - let lineX = startPositionX1 + geo[proxy.plotAreaFrame].origin.x - let lineHeight = geo[proxy.plotAreaFrame].maxY + let rect = plotFrameRect(proxy: proxy, in: geo) + let lineX = startPositionX1 + rect.origin.x + let lineHeight = rect.maxY let boxWidth: CGFloat = min(geo.size.width, 400) let boxOffset = max(0, min(geo.size.width - boxWidth, lineX - boxWidth / 2)) @@ -260,8 +261,21 @@ struct CandleChart: View { } } + private func plotFrameRect(proxy: ChartProxy, in geo: GeometryProxy) -> CGRect { + var rect = CGRect.zero + if #available(iOS 17, *) { + if let plotFrame = proxy.plotFrame { + rect = geo[plotFrame] + } + } else { + rect = geo[proxy.plotAreaFrame] + } + return rect + } + private func findElement(location: CGPoint, proxy: ChartProxy, geometry: GeometryProxy) -> CandleModel? { - let relativeXPosition = location.x - geometry[proxy.plotAreaFrame].origin.x + let rect = plotFrameRect(proxy: proxy, in: geometry) + let relativeXPosition = location.x - rect.origin.x if let date = proxy.value(atX: relativeXPosition) as String?, let timeInterval = TimeInterval(date) { // Find the closest date element. let date = Date(timeIntervalSince1970: timeInterval) diff --git a/Samples/DXFeedCandleChartMac/CandleChartModel.swift b/Samples/DXFeedCandleChartMac/CandleChartModel.swift index e24aaa1f0..1276930ca 100644 --- a/Samples/DXFeedCandleChartMac/CandleChartModel.swift +++ b/Samples/DXFeedCandleChartMac/CandleChartModel.swift @@ -158,62 +158,70 @@ extension CandleChartModel: SnapshotDelegate { let result = events.map { marketEvent in marketEvent.candle } - DispatchQueue.main.async { + DispatchQueue.main.async { [self] in if isSnapshot { - self.loadingInProgress = false - var maxValue = Double.zero - var minValue = Double.greatestFiniteMagnitude - let firstNElements = result.prefix(CandleChartModel.maxCout) - let temp = firstNElements.map { candle in - maxValue = max(maxValue, candle.max()) - minValue = min(minValue, candle.min()) - let price = CandleModel(candle: candle, currency: self.currency) - return price + receivedSnapshot(events: result) + } else { + receivedUpdate(events: result) + } + } + } + + private func receivedSnapshot(events: [Candle]) { + self.loadingInProgress = false + var maxValue = Double.zero + var minValue = Double.greatestFiniteMagnitude + let firstNElements = events.prefix(CandleChartModel.maxCout) + let temp = firstNElements.map { candle in + maxValue = max(maxValue, candle.max()) + minValue = min(minValue, candle.min()) + let price = CandleModel(candle: candle, currency: self.currency) + return price + } + self.maxValue = maxValue + self.minValue = minValue + self.xAxisLabels = CandleChartModel.calculateXaxisValues(with: self.type, values: temp) + self.candles = temp + let xValues = Array(temp.map({ stock in + stock.stringtimeStamp + }).reversed()) + self.xValues = xValues + // scroll to last page + let pointsOnScreen = CandleChartModel.visiblePointsOnScreen(type: self.type, valuesCount: temp.count) + self.xScrollPosition = temp[pointsOnScreen - 1].stringtimeStamp + } + + private func receivedUpdate(events: [Candle]) { + events.forEach { candle in + let newPrice = CandleModel(candle: candle, currency: self.currency) + if candle.isRemove() { + // remove + self.candles.removeAll { price in + price.index == newPrice.index } - self.maxValue = maxValue - self.minValue = minValue - self.xAxisLabels = CandleChartModel.calculateXaxisValues(with: self.type, values: temp) - self.candles = temp - let xValues = Array(temp.map({ stock in - stock.stringtimeStamp - }).reversed()) - self.xValues = xValues - // scroll to last page - let pointsOnScreen = CandleChartModel.visiblePointsOnScreen(type: self.type, valuesCount: temp.count) - self.xScrollPosition = temp[pointsOnScreen - 1].stringtimeStamp } else { - result.forEach { candle in - let newPrice = CandleModel(candle: candle, currency: self.currency) - if candle.isRemove() { - // remove - self.candles.removeAll { price in - price.index == newPrice.index - } - } else { - self.maxValue = max(self.maxValue, candle.max()) - self.minValue = min(self.minValue, candle.min()) - - if let index = self.candles.firstIndex(where: { price in - price.timestamp == newPrice.timestamp - }) { - // update - self.candles.safeReplace(newPrice, at: index) - } else { - // insert - self.candles.insert(newPrice, at: 0) - let temp = self.candles - self.xAxisLabels = CandleChartModel.calculateXaxisValues(with: self.type, values: temp) - let currentScroll = self.xScrollPosition - - let xValues = Array(temp.enumerated().map({ index, stock in - if stock.stringtimeStamp == currentScroll { - self.xScrollPosition = temp[index - 1].stringtimeStamp - } - return stock.stringtimeStamp - }).reversed()) - self.xValues = xValues + self.maxValue = max(self.maxValue, candle.max()) + self.minValue = min(self.minValue, candle.min()) + + if let index = self.candles.firstIndex(where: { price in + price.timestamp == newPrice.timestamp + }) { + // update + self.candles.safeReplace(newPrice, at: index) + } else { + // insert + self.candles.insert(newPrice, at: 0) + let temp = self.candles + self.xAxisLabels = CandleChartModel.calculateXaxisValues(with: self.type, values: temp) + let currentScroll = self.xScrollPosition + + let xValues = Array(temp.enumerated().map({ index, stock in + if stock.stringtimeStamp == currentScroll { + self.xScrollPosition = temp[index - 1].stringtimeStamp } - } + return stock.stringtimeStamp + }).reversed()) + self.xValues = xValues } } }