|
4 | 4 | // |
5 | 5 |
|
6 | 6 | import UIKit |
| 7 | +import CoreSpotlight |
| 8 | +import MobileCoreServices |
7 | 9 |
|
| 10 | +//MARK: - UIViewController Properties |
8 | 11 | class WeatherViewController: UIViewController { |
9 | 12 |
|
| 13 | + //MARK: - IBOutlets |
10 | 14 | @IBOutlet weak var locationLabel: UILabel! |
11 | 15 | @IBOutlet weak var iconLabel: UILabel! |
12 | 16 | @IBOutlet weak var temperatureLabel: UILabel! |
13 | 17 | @IBOutlet var forecastViews: [ForecastView]! |
14 | 18 |
|
| 19 | + let identifier = "WeatherIdentifier" |
| 20 | + |
| 21 | + //MARK: - Super Methods |
15 | 22 | override func viewDidLoad() { |
16 | 23 | super.viewDidLoad() |
17 | 24 | viewModel = WeatherViewModel() |
18 | 25 | viewModel?.startLocationService() |
19 | 26 | } |
20 | 27 |
|
| 28 | + override func viewWillAppear(_ animated: Bool) { |
| 29 | + super.viewWillAppear(animated) |
| 30 | + |
| 31 | + locationLabel.center.x -= view.bounds.width |
| 32 | + iconLabel.center.x -= view.bounds.width |
| 33 | + temperatureLabel.center.x -= view.bounds.width |
| 34 | + |
| 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 | + } |
| 69 | + |
| 70 | + |
21 | 71 | // MARK: ViewModel |
22 | 72 | var viewModel: WeatherViewModel? { |
23 | 73 | didSet { |
24 | | - viewModel?.location.observe { |
| 74 | + viewModel?.location.observe { |
25 | 75 | [unowned self] in |
26 | 76 | self.locationLabel.text = $0 |
27 | | - } |
28 | | - |
29 | | - viewModel?.iconText.observe { |
| 77 | + |
| 78 | + let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String) |
| 79 | + attributeSet.title = self.locationLabel.text |
| 80 | + |
| 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 | + } |
| 89 | + } |
| 90 | + |
| 91 | + viewModel?.iconText.observe { |
30 | 92 | [unowned self] in |
31 | 93 | self.iconLabel.text = $0 |
32 | | - } |
33 | | - |
34 | | - viewModel?.temperature.observe { |
| 94 | + } |
| 95 | + |
| 96 | + viewModel?.temperature.observe { |
35 | 97 | [unowned self] in |
36 | 98 | self.temperatureLabel.text = $0 |
37 | | - } |
38 | | - |
39 | | - viewModel?.forecasts.observe { |
| 99 | + } |
| 100 | + |
| 101 | + viewModel?.forecasts.observe { |
40 | 102 | [unowned self] (forecastViewModels) in |
41 | 103 | if forecastViewModels.count >= 4 { |
42 | | - for (index, forecastView) in self.forecastViews.enumerated() { |
43 | | - forecastView.loadViewModel(forecastViewModels[index]) |
44 | | - } |
| 104 | + for (index, forecastView) in self.forecastViews.enumerated() { |
| 105 | + forecastView.loadViewModel(forecastViewModels[index]) |
| 106 | + } |
| 107 | + } |
45 | 108 | } |
46 | 109 | } |
47 | 110 | } |
48 | 111 | } |
49 | | -} |
|
0 commit comments