Skip to content

Commit

Permalink
fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Jun 18, 2024
1 parent b27a8a3 commit 1640326
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 56 deletions.
2 changes: 1 addition & 1 deletion DXFeedFramework/Native/Utils/NativeTimeUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
20 changes: 17 additions & 3 deletions Samples/DXFeedCandleChartMac/CandleChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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)
Expand Down
112 changes: 60 additions & 52 deletions Samples/DXFeedCandleChartMac/CandleChartModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit 1640326

Please sign in to comment.