Commit 39aef5b9 by Dmitry Stepanets

[IOS-182]: Implemented dynamic update munitely forecast every minute

parent 4ae087ec
...@@ -63,6 +63,9 @@ class MinutelyForecastDetailsView: UIView { ...@@ -63,6 +63,9 @@ class MinutelyForecastDetailsView: UIView {
tempLabel.text = valueStirng tempLabel.text = valueStirng
forecastImage.image = weatherImage forecastImage.image = weatherImage
forecastImage.snp.updateConstraints { update in
update.width.equalTo(weatherImage == nil ? 0 : 28)
}
} }
} }
...@@ -105,7 +108,7 @@ private extension MinutelyForecastDetailsView { ...@@ -105,7 +108,7 @@ private extension MinutelyForecastDetailsView {
separator.snp.makeConstraints { make in separator.snp.makeConstraints { make in
make.width.equalTo(1) make.width.equalTo(1)
make.height.equalToSuperview().multipliedBy(0.65) make.height.equalToSuperview().multipliedBy(0.65)
make.left.equalToSuperview().inset(70) make.right.equalTo(tempLabel.snp.left).offset(-6)
make.centerY.equalToSuperview() make.centerY.equalToSuperview()
} }
......
...@@ -74,6 +74,7 @@ class MinutelyForecastView: UIView { ...@@ -74,6 +74,7 @@ class MinutelyForecastView: UIView {
private var weatherTypeCache = [Int : UIImage]() private var weatherTypeCache = [Int : UIImage]()
private var lastSelectedLevelIndex = 0 private var lastSelectedLevelIndex = 0
private var minutelyForecast = [MinutelyItem]() private var minutelyForecast = [MinutelyItem]()
private var timer: Timer?
private var forecastType = MinutelyForecastType.temperature private var forecastType = MinutelyForecastType.temperature
private lazy var dateFormatter: DateFormatter = { private lazy var dateFormatter: DateFormatter = {
let formatter = DateFormatter() let formatter = DateFormatter()
...@@ -85,12 +86,22 @@ class MinutelyForecastView: UIView { ...@@ -85,12 +86,22 @@ class MinutelyForecastView: UIView {
init() { init() {
super.init(frame: .zero) super.init(frame: .zero)
NotificationCenter.default.addObserver(self,
selector: #selector(updateCurrentSelectedLevel),
name: Notification.Name.NSSystemClockDidChange,
object: nil)
prepareDetailView() prepareDetailView()
prepareCenterDashLine() prepareCenterDashLine()
prepareScrollView() prepareScrollView()
prepareVerticalStackView() prepareVerticalStackView()
} }
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() { override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
...@@ -130,10 +141,27 @@ class MinutelyForecastView: UIView { ...@@ -130,10 +141,27 @@ class MinutelyForecastView: UIView {
: kPrecipitationColors.last?.cgColor : kPrecipitationColors.last?.cgColor
prepareMinutelyItems() prepareMinutelyItems()
if let firstMinutelyItem = minutelyForecast.first {
self.updateDetailsView(minutelyItem: firstMinutelyItem)
}
updateChart() updateChart()
configureTimer()
updateCurrentSelectedLevel()
}
private func configureTimer() {
if timer == nil {
let calendar = Calendar(identifier: .gregorian)
var dateComponents = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date())
if let oldMinutes = dateComponents.minute {
dateComponents.minute = oldMinutes + 1 // dateComponents handle rolling over 60.
}
dateComponents.second = 0
let firstFireDate = calendar.date(from: dateComponents) ?? Date().addingTimeInterval(5)
let newTimer = Timer(fire: firstFireDate, interval: 30.0, repeats: true, block: { [weak self] _ in
self?.updateCurrentSelectedLevel()
})
RunLoop.main.add(newTimer, forMode: .common)
self.timer = newTimer
}
} }
private func updateDetailsView(minutelyItem: MinutelyItem) { private func updateDetailsView(minutelyItem: MinutelyItem) {
...@@ -171,6 +199,8 @@ class MinutelyForecastView: UIView { ...@@ -171,6 +199,8 @@ class MinutelyForecastView: UIView {
return minutelyForecastItem.time >= thisHour && minutelyForecastItem.time < nextHour return minutelyForecastItem.time >= thisHour && minutelyForecastItem.time < nextHour
}) })
else { else {
minutelyForecastItem.weatherTypeImage = nil
minutelyForecast.append(minutelyForecastItem)
continue continue
} }
...@@ -330,8 +360,24 @@ class MinutelyForecastView: UIView { ...@@ -330,8 +360,24 @@ class MinutelyForecastView: UIView {
} }
} }
required init?(coder: NSCoder) { @objc private func updateCurrentSelectedLevel() {
fatalError("init(coder:) has not been implemented") for (index, minutelyForecastItem) in minutelyForecast.enumerated() {
let thisMinute = Date()
let prevMinute = thisMinute.addingTimeInterval(-60)
if minutelyForecastItem.time >= prevMinute && minutelyForecastItem.time < thisMinute {
if lastSelectedLevelIndex == index {
continue
}
if let positionX = levelsPositionXCache[index] {
scrollView.setContentOffset(.init(x: positionX - scrollView.contentInset.left, y: 0), animated: true)
}
self.updateDetailsView(minutelyItem: minutelyForecastItem)
self.lastSelectedLevelIndex = index
return
}
}
} }
} }
...@@ -396,6 +442,11 @@ private extension MinutelyForecastView { ...@@ -396,6 +442,11 @@ private extension MinutelyForecastView {
//MARK:- UIScrollView Delegate //MARK:- UIScrollView Delegate
extension MinutelyForecastView: UIScrollViewDelegate { extension MinutelyForecastView: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) { func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.state == .began {
timer?.invalidate()
timer = nil
}
let targetPointX = Double(scrollView.contentInset.left + scrollView.contentOffset.x) let targetPointX = Double(scrollView.contentInset.left + scrollView.contentOffset.x)
guard let cachedValue = (levelsPositionXCache.first { guard let cachedValue = (levelsPositionXCache.first {
......
...@@ -10,9 +10,6 @@ import OneWeatherCore ...@@ -10,9 +10,6 @@ import OneWeatherCore
class TodayForecastTimePeriodCell: UITableViewCell { class TodayForecastTimePeriodCell: UITableViewCell {
//Private //Private
// private let periodSegmentedControl = ForecastTimePeriodControl(items: ["forecast.timePeriod.daily".localized(),
// "forecast.timePeriod.hourly".localized(),
// "forecast.timePeriod.minutely".localized()])
private let periodSegmentedControl = ForecastTimePeriodControl(items: nil) private let periodSegmentedControl = ForecastTimePeriodControl(items: nil)
private let forecastTimePeriodView = ForecastTimePeriodView() private let forecastTimePeriodView = ForecastTimePeriodView()
private let minutelyForecastView = MinutelyForecastView() private let minutelyForecastView = MinutelyForecastView()
......
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