-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
196 lines (130 loc) · 5.65 KB
/
ViewController.swift
File metadata and controls
196 lines (130 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// ViewController.swift
// WeatherApp1
//
// Created by Aiman Abdullah Anees on 01/10/16.
// Copyright © 2016 Aiman Abdullah Anees. All rights reserved.
//
import UIKit
import Alamofire
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var tempLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var weatherLabel: UILabel!
@IBOutlet weak var weatherImage: UIImageView!
@IBOutlet weak var tableView: UITableView!
//^^^THESE ARE FOR CURRENT TEMPERATURE^^^
var cityName: String!
var date:String!
var currentTemperature:Double!
var weatherType:String!
var convert:Double!
var forecast: Forecast!
var forecasts = [Forecast]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self //Set the delegate
tableView.dataSource = self //Set the datasource
presentWeather {
//Display values
self.downloadForecastDetails {
//Display
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return forecasts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? WeatherCellTableViewCell{
let forecast = forecasts[indexPath.row]
cell.configureCell(forecast: forecast)
return cell
}
else{
return WeatherCellTableViewCell()
}
}
func presentWeather(completed: DownloadComplete){
//^^^^^^^^^DATE OPERATION^^^^^^^^^^^^
let dateFormatter = DateFormatter() //Has different methods to display date & time
dateFormatter.dateStyle = .long //Displays full date
dateFormatter.timeStyle = .none //We dont want time in our app
let currentDate = dateFormatter.string(from: Date()) //Saves current date as a string
self.date = "Today, \(currentDate)"
//^^^^^^^^END OF DATE OPERATION^^^^^^^
//Alamofire Download
let currentWeatherURL = URL(string: CURRENT_URL)
Alamofire.request(currentWeatherURL!).responseJSON { response in
let result = response.result
//To access the dictionary
if let dict = result.value as? Dictionary<String, AnyObject> {
//To get the name of the city
if let name = dict["name"] as? String {
self.cityName = name.capitalized
print(self.cityName)
}
//To get the weather type
if let weather = dict["weather"] as? [Dictionary<String, AnyObject>] {
if let main = weather[0]["main"] as? String {
self.weatherType = main.capitalized
}
}
//To get current temperature
if let main = dict["main"] as? Dictionary<String, AnyObject> {
if let currentTemp = main["temp"] as? Double{
let kelvinToCelcius = currentTemp - 273.15
self.currentTemperature = kelvinToCelcius
}
}
}
self.dateLabel.text = self.date
if(self.weatherType == nil){
self.weatherLabel.text = ""
}
else{
self.weatherLabel.text = self.weatherType
var a = self.weatherType
self.weatherImage.image = UIImage(named: a!)
}
if(self.cityName == nil){
self.locationLabel.text = ""
}
else{
self.locationLabel.text = self.cityName
}
if(self.currentTemperature == nil){
self.tempLabel.text = "0.0"
}
else{
var b = Double(round(100*self.currentTemperature)/100)
print(b)
self.tempLabel.text = "\(b)°"
}
}
completed()
}
func downloadForecastDetails(completed:DownloadComplete){
//Forecast weather data info..
let forecastURL = URL(string: FORECAST_URL)
Alamofire.request(forecastURL!).responseJSON { response in
let result = response.result
if let dict = result.value as? Dictionary<String, AnyObject>{
if let list = dict["list"] as? [Dictionary<String, AnyObject>]{
for obj in list {
let forecast = Forecast(weatherDict: obj)
self.forecasts.append(forecast)
print(obj)
}
self.forecasts.remove(at: 0)
self.tableView.reloadData()
}
}
}
completed()
}
}