Skip to content

Commit 9cc34bf

Browse files
author
Saul Urias
committed
Refactor WeatherViewController
1 parent 64b8304 commit 9cc34bf

File tree

1 file changed

+91
-88
lines changed

1 file changed

+91
-88
lines changed

SwiftWeather/WeatherViewController.swift

Lines changed: 91 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -9,113 +9,116 @@ import MobileCoreServices
99

1010
//MARK: - UIViewController Properties
1111
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-
setA11yIdentifiers()
27-
}
28-
29-
override func viewWillAppear(_ animated: Bool) {
30-
super.viewWillAppear(animated)
31-
32-
locationLabel.center.x -= view.bounds.width
33-
iconLabel.center.x -= view.bounds.width
34-
temperatureLabel.center.x -= view.bounds.width
35-
36-
iconLabel.alpha = 0.0
37-
locationLabel.alpha = 0.0
38-
temperatureLabel.alpha = 0.0
39-
}
40-
41-
override func viewDidAppear(_ animated: Bool) {
42-
super.viewDidAppear(animated)
43-
44-
UIView.animate(withDuration: 0.5, animations: {
45-
self.locationLabel.center.x += self.view.bounds.width
46-
})
4712

48-
UIView.animate(withDuration: 0.5, delay: 0.3, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
49-
self.iconLabel.center.x += self.view.bounds.width
50-
}, completion: nil)
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]!
5118

52-
UIView.animate(withDuration: 0.5, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
53-
self.temperatureLabel.center.x += self.view.bounds.width
54-
}, completion: nil)
19+
let identifier = "WeatherIdentifier"
5520

21+
//MARK: - Super Methods
22+
override func viewDidLoad() {
23+
super.viewDidLoad()
24+
viewModel = WeatherViewModel()
25+
viewModel?.startLocationService()
26+
setA11yIdentifiers()
27+
}
5628

57-
UIView.animate(withDuration: 0.5, delay: 0.3, options: [], animations: {
58-
self.iconLabel.alpha = 1.0
59-
}, completion: nil)
60-
61-
UIView.animate(withDuration: 0.5, delay: 0.4, options: [], animations: {
62-
self.locationLabel.alpha = 1.0
63-
}, completion: nil)
64-
65-
UIView.animate(withDuration: 0.5, delay: 0.5, options: [], animations: {
66-
self.temperatureLabel.alpha = 1.0
67-
}, completion: nil)
68-
69-
}
29+
override func viewWillAppear(_ animated: Bool) {
30+
super.viewWillAppear(animated)
31+
configureLabels()
32+
}
7033

71-
72-
// MARK: ViewModel
73-
var viewModel: WeatherViewModel? {
74-
didSet {
75-
viewModel?.location.observe {
76-
[unowned self] in
77-
self.locationLabel.text = $0
78-
79-
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
80-
attributeSet.title = self.locationLabel.text
81-
82-
let item = CSSearchableItem(uniqueIdentifier: self.identifier, domainIdentifier: "com.rushjet.SwiftWeather", attributeSet: attributeSet)
83-
CSSearchableIndex.default().indexSearchableItems([item]){error in
84-
if let error = error {
85-
print("Indexing error: \(error.localizedDescription)")
86-
} else {
87-
print("Location item successfully indexed")
88-
}
89-
}
34+
override func viewDidAppear(_ animated: Bool) {
35+
super.viewDidAppear(animated)
36+
configureLabelsWithAnimation()
9037
}
9138

92-
viewModel?.iconText.observe {
93-
[unowned self] in
94-
self.iconLabel.text = $0
39+
//MARK: Functions
40+
func configureLabels(){
41+
locationLabel.center.x -= view.bounds.width
42+
iconLabel.center.x -= view.bounds.width
43+
temperatureLabel.center.x -= view.bounds.width
44+
45+
iconLabel.alpha = 0.0
46+
locationLabel.alpha = 0.0
47+
temperatureLabel.alpha = 0.0
9548
}
9649

97-
viewModel?.temperature.observe {
98-
[unowned self] in
99-
self.temperatureLabel.text = $0
50+
func configureLabelsWithAnimation(){
51+
UIView.animate(withDuration: 0.5, animations: {
52+
self.locationLabel.center.x += self.view.bounds.width
53+
})
54+
55+
UIView.animate(withDuration: 0.5, delay: 0.3, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
56+
self.iconLabel.center.x += self.view.bounds.width
57+
}, completion: nil)
58+
59+
UIView.animate(withDuration: 0.5, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: [], animations: {
60+
self.temperatureLabel.center.x += self.view.bounds.width
61+
}, completion: nil)
62+
63+
64+
UIView.animate(withDuration: 0.5, delay: 0.3, options: [], animations: {
65+
self.iconLabel.alpha = 1.0
66+
}, completion: nil)
67+
68+
UIView.animate(withDuration: 0.5, delay: 0.4, options: [], animations: {
69+
self.locationLabel.alpha = 1.0
70+
}, completion: nil)
71+
72+
UIView.animate(withDuration: 0.5, delay: 0.5, options: [], animations: {
73+
self.temperatureLabel.alpha = 1.0
74+
}, completion: nil)
10075
}
10176

102-
viewModel?.forecasts.observe {
103-
[unowned self] (forecastViewModels) in
104-
if forecastViewModels.count >= 4 {
105-
for (index, forecastView) in self.forecastViews.enumerated() {
106-
forecastView.loadViewModel(forecastViewModels[index])
77+
//MARK: ViewModel
78+
var viewModel: WeatherViewModel? {
79+
didSet {
80+
viewModel?.location.observe {
81+
[unowned self] in
82+
self.locationLabel.text = $0
83+
84+
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
85+
attributeSet.title = self.locationLabel.text
86+
87+
let item = CSSearchableItem(uniqueIdentifier: self.identifier, domainIdentifier: "com.rushjet.SwiftWeather", attributeSet: attributeSet)
88+
CSSearchableIndex.default().indexSearchableItems([item]){error in
89+
if let error = error {
90+
print("Indexing error: \(error.localizedDescription)")
91+
} else {
92+
print("Location item successfully indexed")
93+
}
94+
}
95+
}
96+
97+
viewModel?.iconText.observe {
98+
[unowned self] in
99+
self.iconLabel.text = $0
100+
}
101+
102+
viewModel?.temperature.observe {
103+
[unowned self] in
104+
self.temperatureLabel.text = $0
107105
}
106+
107+
viewModel?.forecasts.observe {
108+
[unowned self] (forecastViewModels) in
109+
if forecastViewModels.count >= 4 {
110+
for (index, forecastView) in self.forecastViews.enumerated() {
111+
forecastView.loadViewModel(forecastViewModels[index])
112+
}
113+
}
108114
}
109115
}
110-
}
111116
}
112-
117+
113118
//MARK: Accessibility
114119
func setA11yIdentifiers() {
115120
locationLabel.accessibilityIdentifier = "a11y_current_city"
116121
iconLabel.accessibilityIdentifier = "a11y_wheather_icon"
117122
temperatureLabel.accessibilityIdentifier = "a11y_wheather_temperature"
118123
}
119-
120-
121124
}

0 commit comments

Comments
 (0)