Commit 5fbd9a09 by Dmitry Stepanets

Working on updating information on scroll

parent 300a5415
...@@ -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 = "451CF0CC-97B9-44A7-9228-3653F9EC81B0"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "1Weather/UI/SharedViews/MinutelyForecastView/MinutelyForecastView.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "250"
endingLineNumber = "250"
landmarkName = "scrollViewDidScroll(_:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket> </Bucket>
...@@ -50,11 +50,13 @@ private class MinutelyLevelView: UIView { ...@@ -50,11 +50,13 @@ private class MinutelyLevelView: UIView {
class MinutelyForecastView: UIView { class MinutelyForecastView: UIView {
//Private //Private
private let kLevelWidth = 3
private let detailsInfoView = MinutelyForecastDetailsView() private let detailsInfoView = MinutelyForecastDetailsView()
private let levelsStackView = UIStackView() private let levelsStackView = UIStackView()
private let verticalStackView = UIStackView() private let verticalStackView = UIStackView()
private let scrollView = UIScrollView() private let scrollView = UIScrollView()
private let centerDashline = CAShapeLayer() private let centerDashline = CAShapeLayer()
private var levelsPositionXCache = [Int : CGFloat]()
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"
...@@ -89,14 +91,18 @@ class MinutelyForecastView: UIView { ...@@ -89,14 +91,18 @@ class MinutelyForecastView: UIView {
func configure(with location: Location) { func configure(with location: Location) {
self.location = location self.location = location
centerDashline.strokeColor = kTemperatureColors.last?.cgColor centerDashline.strokeColor = kTemperatureColors.last?.cgColor
self.detailsInfoView.configure(hourly: location.hourly[0], if !location.hourly.isEmpty {
colors: kTemperatureColors) self.detailsInfoView.configure(hourly: location.hourly[0],
colors: kTemperatureColors)
}
updateChart() updateChart()
} }
private func updateChart() { private func updateChart() {
verticalStackView.removeAll() verticalStackView.removeAll()
levelsStackView.removeAll() levelsStackView.removeAll()
levelsPositionXCache.removeAll()
scrollView.subviews.forEach { scrollView.subviews.forEach {
if $0.isKind(of: UILabel.self) { if $0.isKind(of: UILabel.self) {
$0.removeFromSuperview() $0.removeFromSuperview()
...@@ -119,10 +125,10 @@ class MinutelyForecastView: UIView { ...@@ -119,10 +125,10 @@ class MinutelyForecastView: UIView {
let midPoint = (uniqTemps.count / 2) let midPoint = (uniqTemps.count / 2)
let firstHalf = uniqTemps[..<midPoint] let firstHalf = uniqTemps[..<midPoint]
let firstHalfTemp = firstHalf[firstHalf.startIndex + firstHalf.count % 2] let firstHalfTemp = firstHalf[firstHalf.startIndex + firstHalf.count / 2]
let seconHalf = uniqTemps[midPoint...] let seconHalf = uniqTemps[midPoint...]
let secondHalfTemp = seconHalf[seconHalf.startIndex + seconHalf.count % 2] let secondHalfTemp = seconHalf[seconHalf.startIndex + seconHalf.count / 2]
uniqTemps = [uniqMax, firstHalfTemp, secondHalfTemp, uniqMin] uniqTemps = [uniqMax, firstHalfTemp, secondHalfTemp, uniqMin]
} }
...@@ -140,7 +146,7 @@ class MinutelyForecastView: UIView { ...@@ -140,7 +146,7 @@ class MinutelyForecastView: UIView {
levelsStackView.addArrangedSubview(view) levelsStackView.addArrangedSubview(view)
let level = (0.05 + 0.9 * ((forecast[index].temp.value - minTemp.value) / (maxTemp.value - minTemp.value))) let level = (0.05 + 0.9 * ((forecast[index].temp.value - minTemp.value) / (maxTemp.value - minTemp.value)))
view.snp.makeConstraints { make in view.snp.makeConstraints { make in
make.width.equalTo(3) make.width.equalTo(kLevelWidth)
make.height.equalToSuperview().multipliedBy(level) make.height.equalToSuperview().multipliedBy(level)
} }
...@@ -157,6 +163,11 @@ class MinutelyForecastView: UIView { ...@@ -157,6 +163,11 @@ class MinutelyForecastView: UIView {
} }
} }
} }
levelsStackView.layoutIfNeeded()
for (index, view) in levelsStackView.arrangedSubviews.enumerated() {
levelsPositionXCache[index] = view.frame.origin.x
}
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
...@@ -191,6 +202,7 @@ private extension MinutelyForecastView { ...@@ -191,6 +202,7 @@ private extension MinutelyForecastView {
func prepareScrollView() { func prepareScrollView() {
scrollView.backgroundColor = .clear scrollView.backgroundColor = .clear
scrollView.delegate = self
addSubview(scrollView) addSubview(scrollView)
levelsStackView.axis = .horizontal levelsStackView.axis = .horizontal
...@@ -218,3 +230,23 @@ private extension MinutelyForecastView { ...@@ -218,3 +230,23 @@ private extension MinutelyForecastView {
layer.addSublayer(centerDashline) layer.addSublayer(centerDashline)
} }
} }
//MARK:- UIScrollView Delegate
extension MinutelyForecastView: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let targetPointX = Double(scrollView.contentInset.left + scrollView.contentOffset.x)
guard let cachedValue = (levelsPositionXCache.first {
return $1...$1 + Double(kLevelWidth) ~= targetPointX
})
else {
return
}
if 0..<cachedValue.key ~= location?.minutely?.forecast.count ?? 0 {
let forecast = location?.minutely?.forecast[cachedValue.key]
detailsInfoView.configure(hourly: forecast, colors: <#T##[UIColor]#>)
}
print("[min] Target current index \(cachedValue.key)")
}
}
...@@ -2,6 +2,7 @@ import Foundation ...@@ -2,6 +2,7 @@ import Foundation
import CoreData import CoreData
import OneWeatherCore import OneWeatherCore
import OneWeatherAnalytics import OneWeatherAnalytics
import CoreLocation
@objc(CoreLocation) @objc(CoreLocation)
open class CoreLocation: _CoreLocation, CoreDataAppModelConvertable { open class CoreLocation: _CoreLocation, CoreDataAppModelConvertable {
......
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