Commit efb9810d by Dmitry Stepanets

[IOS-172]: Changed minutely precipitation UI

parent 0f90e48f
......@@ -39,7 +39,7 @@ private class MinutelyLevelView: UIView {
layer.shadowColor = UIColor(hex: 0xd8ddfa).withAlphaComponent(0.8).cgColor
layer.shadowOffset = .init(width: 0, height: 3)
layer.shadowRadius = 3
layer.shadowOpacity = 1
layer.shadowOpacity = interfaceStyle == .light ? 1 : 0
}
required init?(coder: NSCoder) {
......@@ -53,6 +53,11 @@ private class MinutelyLevelView: UIView {
gradient.frame = bounds
layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: bounds.width / 2).cgPath
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
layer.shadowOpacity = interfaceStyle == .light ? 1 : 0
}
}
class MinutelyForecastView: UIView {
......@@ -120,7 +125,9 @@ class MinutelyForecastView: UIView {
self.location = location
self.forecastType = forecastType
self.dateFormatter.timeZone = location.timeZone
centerDashline.strokeColor = kTemperatureColors.last?.cgColor
centerDashline.strokeColor = forecastType == .temperature ? kTemperatureColors.last?.cgColor
: kPrecipitationColors.last?.cgColor
prepareMinutelyItems()
if let firstMinutelyItem = minutelyForecast.first {
......@@ -184,10 +191,36 @@ class MinutelyForecastView: UIView {
}
}
switch forecastType {
case .temperature:
updateTemperatureChart()
case .precipitation:
updatePrecipitationChart()
}
levelsStackView.layoutIfNeeded()
verticalStackView.layoutIfNeeded()
//Levels dashline
for _ in 0..<verticalStackView.arrangedSubviews.count {
let levelShape = CAShapeLayer()
levelShape.opacity = 0.3
levelShape.lineWidth = 1
levelShape.lineDashPattern = [4, 5]
levelShape.strokeColor = UIColor(hex: 0x979797).cgColor
layer.insertSublayer(levelShape, at: 0)
levelsDashline.append(levelShape)
}
for (index, view) in levelsStackView.arrangedSubviews.enumerated() {
levelsPositionXCache[index] = view.frame.origin.x
}
}
private func updateTemperatureChart() {
guard
let maxTemp = (minutelyForecast.compactMap{ $0.temp }.max{ $0.value < $1.value} ),
let minTemp = (minutelyForecast.compactMap{ $0.temp }.min{ $0.value < $1.value} )
else {
return
}
......@@ -216,9 +249,10 @@ class MinutelyForecastView: UIView {
}
for index in 0..<minutelyForecast.count {
let view = MinutelyLevelView(forecastType: self.forecastType)
let view = MinutelyLevelView(forecastType: .temperature)
levelsStackView.addArrangedSubview(view)
let level = (0.05 + 0.9 * ((minutelyForecast[index].temp.value - minTemp.value) / (maxTemp.value - minTemp.value)))
let diff = maxTemp.value - minTemp.value == 0 ? 1 : maxTemp.value - minTemp.value
let level = (0.05 + 0.9 * ((minutelyForecast[index].temp.value - minTemp.value) / diff))
view.snp.makeConstraints { make in
make.width.equalTo(kLevelWidth)
make.height.equalToSuperview().multipliedBy(level)
......@@ -237,23 +271,62 @@ class MinutelyForecastView: UIView {
}
}
}
}
levelsStackView.layoutIfNeeded()
verticalStackView.layoutIfNeeded()
private func updatePrecipitationChart() {
guard
let maxPrecip = (minutelyForecast.compactMap{ $0.precipitation }.max{ $0 < $1 }),
let minPrecip = (minutelyForecast.compactMap{ $0.precipitation }.min{ $0 < $1 })
//Levels dashline
for _ in 0..<verticalStackView.arrangedSubviews.count {
let levelShape = CAShapeLayer()
levelShape.opacity = 0.3
levelShape.lineWidth = 1
levelShape.lineDashPattern = [4, 5]
levelShape.strokeColor = UIColor(hex: 0x979797).cgColor
layer.insertSublayer(levelShape, at: 0)
levelsDashline.append(levelShape)
else {
return
}
for (index, view) in levelsStackView.arrangedSubviews.enumerated() {
levelsPositionXCache[index] = view.frame.origin.x
var uniqPrecips = minutelyForecast.compactMap{ $0.precipitation }.unique().sorted{$0 > $1}
if uniqPrecips.count > 4 {
let uniqMax = uniqPrecips.removeFirst()
let uniqMin = uniqPrecips.removeLast()
let midPoint = (uniqPrecips.count / 2)
let firstHalf = uniqPrecips[..<midPoint]
let firstHalfTemp = firstHalf[firstHalf.startIndex + firstHalf.count / 2]
let seconHalf = uniqPrecips[midPoint...]
let secondHalfTemp = seconHalf[seconHalf.startIndex + seconHalf.count / 2]
uniqPrecips = [uniqMax, firstHalfTemp, secondHalfTemp, uniqMin]
}
for precipitation in uniqPrecips {
let label = UILabel()
label.text = "\(Int(precipitation * 100))%"
label.font = AppFont.SFPro.regular(size: 10)
label.setContentHuggingPriority(.fittingSizeLevel, for: .vertical)
label.sizeToFit()
verticalStackView.addArrangedSubview(label)
}
for index in 0..<minutelyForecast.count {
let view = MinutelyLevelView(forecastType: .precipitation)
levelsStackView.addArrangedSubview(view)
let diff = maxPrecip - minPrecip == 0 ? 1 : maxPrecip - minPrecip
let level = (0.05 + 0.9 * ((minutelyForecast[index].precipitation - minPrecip) / diff))
view.snp.makeConstraints { make in
make.width.equalTo(kLevelWidth)
make.height.equalToSuperview().multipliedBy(level)
}
let minutes = Calendar.current.component(.minute, from: minutelyForecast[index].time)
if minutes % 20 == 0 {
let label = UILabel()
label.font = AppFont.SFPro.bold(size: 12)
label.text = dateFormatter.string(from: minutelyForecast[index].time)
scrollView.addSubview(label)
label.snp.makeConstraints { make in
make.top.equalTo(view.snp.bottom).offset(12)
make.centerX.equalTo(view)
}
}
}
}
......
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