Commit caac91a4 by Dmitry Stepanets

[IOS-172]: Working on weather type cache

parent cfb301df
...@@ -3,4 +3,22 @@ ...@@ -3,4 +3,22 @@
uuid = "55281C35-FE9F-4CED-865E-FBED0E7393F6" uuid = "55281C35-FE9F-4CED-865E-FBED0E7393F6"
type = "0" type = "0"
version = "2.0"> version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "75F9A873-CE27-44A5-80C1-ACB69F8CF7B8"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "1Weather/UI/SharedViews/MinutelyForecastView/MinutelyForecastView.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "283"
endingLineNumber = "283"
landmarkName = "scrollViewDidScroll(_:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket> </Bucket>
...@@ -57,6 +57,7 @@ class MinutelyForecastView: UIView { ...@@ -57,6 +57,7 @@ class MinutelyForecastView: UIView {
private let scrollView = UIScrollView() private let scrollView = UIScrollView()
private let centerDashline = CAShapeLayer() private let centerDashline = CAShapeLayer()
private var levelsPositionXCache = [Int : CGFloat]() private var levelsPositionXCache = [Int : CGFloat]()
private var weatherTypeCache = [Int : WeatherType]()
private lazy var dateFormatter: DateFormatter = { private lazy var dateFormatter: DateFormatter = {
let formatter = DateFormatter() let formatter = DateFormatter()
formatter.dateFormat = "h:mm a" formatter.dateFormat = "h:mm a"
...@@ -100,9 +101,35 @@ class MinutelyForecastView: UIView { ...@@ -100,9 +101,35 @@ class MinutelyForecastView: UIView {
colors: kTemperatureColors) colors: kTemperatureColors)
} }
updateWeatherTypeCahce()
updateChart() updateChart()
} }
private func updateWeatherTypeCahce() {
weatherTypeCache.removeAll()
guard
let currentLocation = self.location,
let minutelyForecast = currentLocation.minutely
else {
return
}
let allHours = minutelyForecast.forecast.compactMap{ $0.hourComponent }
let maxHour = allHours.max{ $0 > $1 } ?? 0
for hourlyWeather in currentLocation.hourly {
guard let hourComponent = Calendar.current.dateComponents([.hour], from: hourlyWeather.date).hour else {
continue
}
if hourComponent > maxHour {
return
}
weatherTypeCache[hourComponent] = hourlyWeather.type
}
}
private func updateChart() { private func updateChart() {
verticalStackView.removeAll() verticalStackView.removeAll()
levelsStackView.removeAll() levelsStackView.removeAll()
...@@ -173,7 +200,7 @@ class MinutelyForecastView: UIView { ...@@ -173,7 +200,7 @@ class MinutelyForecastView: UIView {
levelsPositionXCache[index] = view.frame.origin.x levelsPositionXCache[index] = view.frame.origin.x
} }
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
...@@ -249,10 +276,10 @@ extension MinutelyForecastView: UIScrollViewDelegate { ...@@ -249,10 +276,10 @@ extension MinutelyForecastView: UIScrollViewDelegate {
return return
} }
let forecast = location?.minutely?.forecast[cachedValue.key] let forecast = location?.minutely?.forecast[cachedValue.key]
detailsInfoView.configure(valueStirng: forecast?.temp.shortString ?? "--", detailsInfoView.configure(valueStirng: forecast?.temp.shortString ?? "--",
date: forecast?.time, date: forecast?.time,
colors: kTemperatureColors) colors: kTemperatureColors)
print("[min] Target current index \(cachedValue.key)") print("[min] Target current index \(cachedValue.key)")
} }
} }
...@@ -13,6 +13,7 @@ public struct MinutelyItem { ...@@ -13,6 +13,7 @@ public struct MinutelyItem {
public let precipitation: Double public let precipitation: Double
public let windSpeed: WindSpeed public let windSpeed: WindSpeed
public let pressure: Pressure public let pressure: Pressure
public let hourComponent: Int?
public init(time: Date, temp: Temperature, precipitation: Double, windSpeed: WindSpeed, pressure: Pressure) { public init(time: Date, temp: Temperature, precipitation: Double, windSpeed: WindSpeed, pressure: Pressure) {
self.time = time self.time = time
...@@ -20,5 +21,9 @@ public struct MinutelyItem { ...@@ -20,5 +21,9 @@ public struct MinutelyItem {
self.precipitation = precipitation self.precipitation = precipitation
self.windSpeed = windSpeed self.windSpeed = windSpeed
self.pressure = pressure self.pressure = pressure
let calendar = Calendar.current
let components = calendar.dateComponents([.hour], from: time)
self.hourComponent = components.hour
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment