|
5 | 5 |
|
6 | 6 | import UIKit |
7 | 7 |
|
| 8 | + //MARK: - UIViewController Properties |
8 | 9 | class WeatherViewController: UIViewController { |
9 | | - |
10 | | - @IBOutlet weak var locationLabel: UILabel! |
11 | | - @IBOutlet weak var iconLabel: UILabel! |
12 | | - @IBOutlet weak var temperatureLabel: UILabel! |
13 | | - @IBOutlet var forecastViews: [ForecastView]! |
14 | | - |
15 | | - override func viewDidLoad() { |
16 | | - super.viewDidLoad() |
17 | | - viewModel = WeatherViewModel() |
18 | | - viewModel?.startLocationService() |
19 | | - } |
20 | | - |
21 | | - // MARK: ViewModel |
22 | | - var viewModel: WeatherViewModel? { |
23 | | - didSet { |
24 | | - viewModel?.location.observe { |
25 | | - [unowned self] in |
26 | | - self.locationLabel.text = $0 |
27 | | - } |
28 | | - |
29 | | - viewModel?.iconText.observe { |
30 | | - [unowned self] in |
31 | | - self.iconLabel.text = $0 |
32 | | - } |
33 | | - |
34 | | - viewModel?.temperature.observe { |
35 | | - [unowned self] in |
36 | | - self.temperatureLabel.text = $0 |
37 | | - } |
38 | | - |
39 | | - viewModel?.forecasts.observe { |
40 | | - [unowned self] (forecastViewModels) in |
41 | | - if forecastViewModels.count >= 4 { |
42 | | - for (index, forecastView) in self.forecastViews.enumerated() { |
43 | | - forecastView.loadViewModel(forecastViewModels[index]) |
44 | | - } |
| 10 | + |
| 11 | + //MARK: - IBOutlets |
| 12 | + @IBOutlet weak var locationLabel: UILabel! |
| 13 | + @IBOutlet weak var iconLabel: UILabel! |
| 14 | + @IBOutlet weak var temperatureLabel: UILabel! |
| 15 | + @IBOutlet var forecastViews: [ForecastView]! |
| 16 | + |
| 17 | + //MARK: - Super Methods |
| 18 | + override func viewDidLoad() { |
| 19 | + super.viewDidLoad() |
| 20 | + viewModel = WeatherViewModel() |
| 21 | + viewModel?.startLocationService() |
| 22 | + } |
| 23 | + |
| 24 | + override func viewWillAppear(_ animated: Bool) { |
| 25 | + super.viewWillAppear(animated) |
| 26 | + |
| 27 | + locationLabel.center.x -= view.bounds.width |
| 28 | + iconLabel.center.x -= view.bounds.width |
| 29 | + temperatureLabel.center.x -= view.bounds.width |
| 30 | + |
| 31 | + iconLabel.alpha = 0.0 |
| 32 | + locationLabel.alpha = 0.0 |
| 33 | + temperatureLabel.alpha = 0.0 |
| 34 | + } |
| 35 | + |
| 36 | + override func viewDidAppear(_ animated: Bool) { |
| 37 | + super.viewDidAppear(animated) |
| 38 | + |
| 39 | + UIView.animate(withDuration: 0.5, animations: { |
| 40 | + self.locationLabel.center.x += self.view.bounds.width |
| 41 | + }) |
| 42 | + |
| 43 | + UIView.animate(withDuration: 0.5, delay: 0.3, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: { |
| 44 | + self.iconLabel.center.x += self.view.bounds.width |
| 45 | + }, completion: nil) |
| 46 | + |
| 47 | + UIView.animate(withDuration: 0.5, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: { |
| 48 | + self.temperatureLabel.center.x += self.view.bounds.width |
| 49 | + }, completion: nil) |
| 50 | + |
| 51 | + |
| 52 | + UIView.animate(withDuration: 0.5, delay: 0.3, options: [], animations: { |
| 53 | + self.iconLabel.alpha = 1.0 |
| 54 | + }, completion: nil) |
| 55 | + |
| 56 | + UIView.animate(withDuration: 0.5, delay: 0.4, options: [], animations: { |
| 57 | + self.locationLabel.alpha = 1.0 |
| 58 | + }, completion: nil) |
| 59 | + |
| 60 | + UIView.animate(withDuration: 0.5, delay: 0.5, options: [], animations: { |
| 61 | + self.temperatureLabel.alpha = 1.0 |
| 62 | + }, completion: nil) |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + // MARK: ViewModel |
| 67 | + var viewModel: WeatherViewModel? { |
| 68 | + didSet { |
| 69 | + viewModel?.location.observe { |
| 70 | + [unowned self] in |
| 71 | + self.locationLabel.text = $0 |
| 72 | + } |
| 73 | + |
| 74 | + viewModel?.iconText.observe { |
| 75 | + [unowned self] in |
| 76 | + self.iconLabel.text = $0 |
| 77 | + } |
| 78 | + |
| 79 | + viewModel?.temperature.observe { |
| 80 | + [unowned self] in |
| 81 | + self.temperatureLabel.text = $0 |
| 82 | + } |
| 83 | + |
| 84 | + viewModel?.forecasts.observe { |
| 85 | + [unowned self] (forecastViewModels) in |
| 86 | + if forecastViewModels.count >= 4 { |
| 87 | + for (index, forecastView) in self.forecastViews.enumerated() { |
| 88 | + forecastView.loadViewModel(forecastViewModels[index]) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
45 | 92 | } |
46 | | - } |
47 | 93 | } |
48 | | - } |
49 | 94 | } |
0 commit comments