|
4 | 4 | // |
5 | 5 |
|
6 | 6 | import UIKit |
| 7 | +import CoreSpotlight |
| 8 | +import MobileCoreServices |
7 | 9 |
|
8 | | - //MARK: - UIViewController Properties |
| 10 | +//MARK: - UIViewController Properties |
9 | 11 | class WeatherViewController: UIViewController { |
| 12 | + |
| 13 | + //MARK: - IBOutlets |
| 14 | + @IBOutlet weak var locationLabel: UILabel! |
| 15 | + @IBOutlet weak var iconLabel: UILabel! |
| 16 | + @IBOutlet weak var temperatureLabel: UILabel! |
| 17 | + @IBOutlet var forecastViews: [ForecastView]! |
| 18 | + |
| 19 | + let identifier = "WeatherIdentifier" |
| 20 | + |
| 21 | + //MARK: - Super Methods |
| 22 | + override func viewDidLoad() { |
| 23 | + super.viewDidLoad() |
| 24 | + viewModel = WeatherViewModel() |
| 25 | + viewModel?.startLocationService() |
| 26 | + } |
| 27 | + |
| 28 | + override func viewWillAppear(_ animated: Bool) { |
| 29 | + super.viewWillAppear(animated) |
10 | 30 |
|
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]! |
| 31 | + locationLabel.center.x -= view.bounds.width |
| 32 | + iconLabel.center.x -= view.bounds.width |
| 33 | + temperatureLabel.center.x -= view.bounds.width |
16 | 34 |
|
17 | | - //MARK: - Super Methods |
18 | | - override func viewDidLoad() { |
19 | | - super.viewDidLoad() |
20 | | - viewModel = WeatherViewModel() |
21 | | - viewModel?.startLocationService() |
22 | | - } |
| 35 | + iconLabel.alpha = 0.0 |
| 36 | + locationLabel.alpha = 0.0 |
| 37 | + temperatureLabel.alpha = 0.0 |
| 38 | + } |
| 39 | + |
| 40 | + override func viewDidAppear(_ animated: Bool) { |
| 41 | + super.viewDidAppear(animated) |
| 42 | + |
| 43 | + UIView.animate(withDuration: 0.5, animations: { |
| 44 | + self.locationLabel.center.x += self.view.bounds.width |
| 45 | + }) |
| 46 | + |
| 47 | + UIView.animate(withDuration: 0.5, delay: 0.3, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: { |
| 48 | + self.iconLabel.center.x += self.view.bounds.width |
| 49 | + }, completion: nil) |
| 50 | + |
| 51 | + UIView.animate(withDuration: 0.5, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: { |
| 52 | + self.temperatureLabel.center.x += self.view.bounds.width |
| 53 | + }, completion: nil) |
| 54 | + |
| 55 | + |
| 56 | + UIView.animate(withDuration: 0.5, delay: 0.3, options: [], animations: { |
| 57 | + self.iconLabel.alpha = 1.0 |
| 58 | + }, completion: nil) |
| 59 | + |
| 60 | + UIView.animate(withDuration: 0.5, delay: 0.4, options: [], animations: { |
| 61 | + self.locationLabel.alpha = 1.0 |
| 62 | + }, completion: nil) |
| 63 | + |
| 64 | + UIView.animate(withDuration: 0.5, delay: 0.5, options: [], animations: { |
| 65 | + self.temperatureLabel.alpha = 1.0 |
| 66 | + }, completion: nil) |
| 67 | + |
| 68 | + } |
23 | 69 |
|
24 | | - override func viewWillAppear(_ animated: Bool) { |
25 | | - super.viewWillAppear(animated) |
| 70 | + |
| 71 | + // MARK: ViewModel |
| 72 | + var viewModel: WeatherViewModel? { |
| 73 | + didSet { |
| 74 | + viewModel?.location.observe { |
| 75 | + [unowned self] in |
| 76 | + self.locationLabel.text = $0 |
26 | 77 |
|
27 | | - locationLabel.center.x -= view.bounds.width |
28 | | - iconLabel.center.x -= view.bounds.width |
29 | | - temperatureLabel.center.x -= view.bounds.width |
| 78 | + let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String) |
| 79 | + attributeSet.title = self.locationLabel.text |
30 | 80 |
|
31 | | - iconLabel.alpha = 0.0 |
32 | | - locationLabel.alpha = 0.0 |
33 | | - temperatureLabel.alpha = 0.0 |
| 81 | + let item = CSSearchableItem(uniqueIdentifier: self.identifier, domainIdentifier: "com.rushjet.SwiftWeather", attributeSet: attributeSet) |
| 82 | + CSSearchableIndex.default().indexSearchableItems([item]){error in |
| 83 | + if let error = error { |
| 84 | + print("Indexing error: \(error.localizedDescription)") |
| 85 | + } else { |
| 86 | + print("Location item successfully indexed") |
| 87 | + } |
| 88 | + } |
34 | 89 | } |
35 | 90 |
|
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 | | - |
| 91 | + viewModel?.iconText.observe { |
| 92 | + [unowned self] in |
| 93 | + self.iconLabel.text = $0 |
64 | 94 | } |
65 | 95 |
|
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 |
| 96 | + viewModel?.temperature.observe { |
| 97 | + [unowned self] in |
| 98 | + self.temperatureLabel.text = $0 |
| 99 | + } |
| 100 | + |
| 101 | + viewModel?.forecasts.observe { |
| 102 | + [unowned self] (forecastViewModels) in |
| 103 | + if forecastViewModels.count >= 4 { |
| 104 | + for (index, forecastView) in self.forecastViews.enumerated() { |
| 105 | + forecastView.loadViewModel(forecastViewModels[index]) |
82 | 106 | } |
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 | 107 | } |
92 | 108 | } |
| 109 | + } |
93 | 110 | } |
94 | | -} |
| 111 | + } |
0 commit comments