Commit e69ff80b by Dmitriy Stepanets

Fixed graph drawning

parent 42c32561
...@@ -30,6 +30,9 @@ ...@@ -30,6 +30,9 @@
"forecast.smoke" = "Smoke"; "forecast.smoke" = "Smoke";
"forecast.unknown" = "Unknown"; "forecast.unknown" = "Unknown";
//Day
"day.today" = "Today";
//Day time //Day time
"dayTime.morning" = "Morning"; "dayTime.morning" = "Morning";
"dayTime.noon" = "Noon"; "dayTime.noon" = "Noon";
......
...@@ -34,7 +34,7 @@ class CityForecastCell: UITableViewCell { ...@@ -34,7 +34,7 @@ class CityForecastCell: UITableViewCell {
} }
public func configure(with location:Location) { public func configure(with location:Location) {
cityImageView.image = UIImage(named: location.imageName) cityImageView.image = UIImage(named: location.imageName ?? "")
temperatureLabel.text = location.today?.temp?.shortString temperatureLabel.text = location.today?.temp?.shortString
let maxTemp = location.today?.maxTemp?.shortString ?? "--" let maxTemp = location.today?.maxTemp?.shortString ?? "--"
let minTemp = location.today?.minTemp?.shortString ?? "--" let minTemp = location.today?.minTemp?.shortString ?? "--"
......
...@@ -40,6 +40,7 @@ class CityForecastTimePeriodCell: UITableViewCell { ...@@ -40,6 +40,7 @@ class CityForecastTimePeriodCell: UITableViewCell {
private let graphView = GraphView() private let graphView = GraphView()
private var graphIsDrawn = false private var graphIsDrawn = false
//MARK:- Cell life cycle
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier) super.init(style: style, reuseIdentifier: reuseIdentifier)
...@@ -62,7 +63,7 @@ class CityForecastTimePeriodCell: UITableViewCell { ...@@ -62,7 +63,7 @@ class CityForecastTimePeriodCell: UITableViewCell {
} }
public func drawGraphIfNeeded() { public func drawGraphIfNeeded() {
guard !graphIsDrawn else { return } guard graphIsDrawn == false else { return }
//Checking for correct height //Checking for correct height
guard guard
...@@ -74,7 +75,7 @@ class CityForecastTimePeriodCell: UITableViewCell { ...@@ -74,7 +75,7 @@ class CityForecastTimePeriodCell: UITableViewCell {
graphView.frame = .init(x: 0, graphView.frame = .init(x: 0,
y: periodButton.graphRect.origin.y, y: periodButton.graphRect.origin.y,
width: scrollView.contentSize.width, width: stackView.frame.width,
height: periodButton.graphRect.height) height: periodButton.graphRect.height)
updateGraphPoints() updateGraphPoints()
...@@ -162,7 +163,11 @@ class CityForecastTimePeriodCell: UITableViewCell { ...@@ -162,7 +163,11 @@ class CityForecastTimePeriodCell: UITableViewCell {
self.graphView.tintMainDotAt(point: maxTempPoints[button.index]) self.graphView.tintMainDotAt(point: maxTempPoints[button.index])
self.graphView.tintAdditionalDotAt(point: minTempsPoints[button.index]) self.graphView.tintAdditionalDotAt(point: minTempsPoints[button.index])
case .hourly: case .hourly:
break guard let points = hourlyGraphPoints?.points else { return }
self.graphView.tintGraphFrom(startPointX: button.frame.origin.x,
endPointX: button.frame.origin.x + button.bounds.width)
self.graphView.tintMainDotAt(point: points[button.index])
} }
} }
...@@ -235,10 +240,11 @@ class CityForecastTimePeriodCell: UITableViewCell { ...@@ -235,10 +240,11 @@ class CityForecastTimePeriodCell: UITableViewCell {
let levelsCount = CGFloat(Set(temps).count) let levelsCount = CGFloat(Set(temps).count)
let levelHeight = (graphView.frame.height / CGFloat(levelsCount)).rounded(.down) let levelHeight = (graphView.frame.height / CGFloat(levelsCount)).rounded(.down)
let tempFrame = CGRect(x: 0, y: 0, width: graphView.frame.width, height: levelsCount * levelHeight) let tempFrame = CGRect(x: 0, y: 0, width: graphView.frame.width, height: graphView.frame.height)
var pointLevel = tempFrame.origin.y + ((maxTemp - temps[index]) * levelHeight) + levelHeight var pointLevel = tempFrame.origin.y + ((maxTemp - temps[index]) * levelHeight)
pointLevel = min(pointLevel, tempFrame.height + 5) pointLevel = max(pointLevel, tempFrame.origin.y + 10)
pointLevel = min(tempFrame.height - 10, pointLevel)
points.append(.init(x: buttonCenterX, y: pointLevel)) points.append(.init(x: buttonCenterX, y: pointLevel))
} }
......
...@@ -9,6 +9,16 @@ import UIKit ...@@ -9,6 +9,16 @@ import UIKit
class PeriodForecastButton: UIControl { class PeriodForecastButton: UIControl {
//Private //Private
private static let hourlyFormatter: DateFormatter = {
let fmt = DateFormatter()
fmt.dateFormat = "h a"
return fmt
}()
private static let dailyFormatter: DateFormatter = {
let fmt = DateFormatter()
fmt.dateFormat = "d, E"
return fmt
}()
private let kGraphInset:CGFloat = 10 private let kGraphInset:CGFloat = 10
private let forecastImageView = UIImageView() private let forecastImageView = UIImageView()
private let tempLabel = UILabel() private let tempLabel = UILabel()
...@@ -73,11 +83,20 @@ class PeriodForecastButton: UIControl { ...@@ -73,11 +83,20 @@ class PeriodForecastButton: UIControl {
public func configure(dailyWeather: DailyWeather) { public func configure(dailyWeather: DailyWeather) {
self.tempLabel.text = dailyWeather.maxTemp?.shortString self.tempLabel.text = dailyWeather.maxTemp?.shortString
self.minTempLabel.text = dailyWeather.minTemp?.shortString self.minTempLabel.text = dailyWeather.minTemp?.shortString
self.indicatorImageView.image = nil
if Calendar.current.isDateInToday(dailyWeather.date) {
self.timeLabel.text = "day.today".localized()
}
else {
self.timeLabel.text = PeriodForecastButton.dailyFormatter.string(from: dailyWeather.date)
}
} }
public func configure(hourlyWeather: HourlyWeather) { public func configure(hourlyWeather: HourlyWeather) {
self.tempLabel.text = hourlyWeather.temp?.shortString self.tempLabel.text = hourlyWeather.temp?.shortString
self.minTempLabel.text = nil self.minTempLabel.text = nil
self.indicatorImageView.image = nil
self.timeLabel.text = PeriodForecastButton.hourlyFormatter.string(from: hourlyWeather.date)
} }
} }
......
...@@ -55,8 +55,9 @@ class TodayViewModel { ...@@ -55,8 +55,9 @@ class TodayViewModel {
moonState: .normal) moonState: .normal)
var daily = [DailyWeather]() var daily = [DailyWeather]()
var dailyStartDate = Date() - TimeInterval(3600 * 24)
for index in 0..<7 { for index in 0..<7 {
let day = DailyWeather(date: Date(), let day = DailyWeather(date: dailyStartDate,
timeZone: .current, timeZone: .current,
weekDay: weekDay(byIndex: index), weekDay: weekDay(byIndex: index),
type: WeatherType.allCases.randomElement() ?? .unknown, type: WeatherType.allCases.randomElement() ?? .unknown,
...@@ -65,17 +66,18 @@ class TodayViewModel { ...@@ -65,17 +66,18 @@ class TodayViewModel {
windSpeed: .init(value: Double.random(in: 0..<10), unit: .milesPerHour), windSpeed: .init(value: Double.random(in: 0..<10), unit: .milesPerHour),
windDirection: WindDirection.allCases.randomElement() ?? .east, windDirection: WindDirection.allCases.randomElement() ?? .east,
precipitationProbability: .some(UInt.random(in: 0..<100)), precipitationProbability: .some(UInt.random(in: 0..<100)),
sunrise: .init(hours: 5, minutes: 23), sunrise: nil,
sunset: .init(hours: 17, minutes: 40), sunset: nil,
sunState: .normal, sunState: .normal,
moonrise: .init(hours: 22, minutes: 15), moonrise: nil,
moonset: .init(hours: 2, minutes: 30), moonset: nil,
moonState: .normal) moonState: .normal)
dailyStartDate = dailyStartDate + TimeInterval(3600 * 24)
daily.append(day) daily.append(day)
} }
var hourly = [HourlyWeather]() var hourly = [HourlyWeather]()
var startDate = Date() var startDate = Date() - TimeInterval(3600)
for _ in 0..<24 { for _ in 0..<24 {
let hour = HourlyWeather(date: startDate, let hour = HourlyWeather(date: startDate,
timeZone: TimeZone(abbreviation: "EST") ?? .current, timeZone: TimeZone(abbreviation: "EST") ?? .current,
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
<key>Cirque.xcscheme_^#shared#^_</key> <key>Cirque.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>4</integer> <integer>6</integer>
</dict> </dict>
<key>Localize-Swift.xcscheme</key> <key>Localize-Swift.xcscheme</key>
<dict> <dict>
...@@ -33,26 +33,26 @@ ...@@ -33,26 +33,26 @@
<key>Localize-Swift.xcscheme_^#shared#^_</key> <key>Localize-Swift.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>5</integer> <integer>3</integer>
</dict> </dict>
<key>Pods-1Weather.xcscheme</key> <key>Pods-1Weather.xcscheme</key>
<dict> <dict>
<key>isShown</key> <key>isShown</key>
<false/> <false/>
<key>orderHint</key> <key>orderHint</key>
<integer>2</integer> <integer>1</integer>
</dict> </dict>
<key>SnapKit.xcscheme</key> <key>SnapKit.xcscheme</key>
<dict> <dict>
<key>isShown</key> <key>isShown</key>
<false/> <false/>
<key>orderHint</key> <key>orderHint</key>
<integer>4</integer> <integer>2</integer>
</dict> </dict>
<key>XMLCoder.xcscheme_^#shared#^_</key> <key>XMLCoder.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>6</integer> <integer>4</integer>
</dict> </dict>
</dict> </dict>
<key>SuppressBuildableAutocreation</key> <key>SuppressBuildableAutocreation</key>
......
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