Created
November 14, 2016 10:47
-
-
Save arto-heino/32830b5eb6cb285c1e7031f38277e88d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PodcastTableViewController.swift | |
class PodcastTableViewController: UITableViewController, DataParserObserver { | |
var podcasts = [Podcast]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.podcasts = [Podcast]() | |
let dataParser = HttpRequesting() | |
// Set and Get the podcasts to observer | |
dataParser.httpGetPodCasts(parserObserver: self) | |
} | |
// Run after the podcasts have been parsed in HttpRequesting | |
func podcastsParsed(podcasts: [Podcast]) { | |
self.podcasts = podcasts | |
DispatchQueue.main.async { | |
self.tableView.reloadData() | |
return | |
} | |
} | |
HttpRequestin.swift | |
class HttpRequesting { | |
var message: String | |
var apiKey: String | |
var alertMessage: String | |
var error: Bool | |
var done: Bool | |
init () { | |
self.message = "" | |
self.alertMessage = "" | |
self.error = true | |
self.done = false | |
self.apiKey = "" | |
} | |
func setMessage(statusMessage: String) { | |
self.alertMessage = statusMessage | |
} | |
func setStatus(status: Bool) { | |
self.done = status | |
} | |
func setError(error: Bool) { | |
self.error = error | |
} | |
func setApiKey(apiKey: String) { | |
self.apiKey = apiKey | |
} | |
func getMessage() -> String { | |
return self.alertMessage | |
} | |
func getApiKey() -> String { | |
return self.apiKey | |
} | |
func getStatus() -> Bool { | |
return self.done | |
} | |
func getError() -> Bool { | |
return self.error | |
} | |
// Gets the apikey from the server | |
func httpGetApi () { | |
let parameters: Parameters = ["username": "username", "password": "password"] | |
Alamofire.request("addr", method: .post, parameters:parameters, encoding: JSONEncoding.default) | |
.responseJSON{response in | |
if let json = response.result.value as? [String: String] { | |
// Set the apikey | |
self.setApiKey(apiKey: json["api_key"]!) | |
}else{ | |
self.setMessage(statusMessage: "Ei toimi") | |
} | |
} | |
} | |
// Gets podcast from the server using apikey and category | |
func httpGetPodCasts (parserObserver: DataParserObserver) { | |
// Sets the apikey, It don´t get the apikey fast enough before it will start alamofire request so key is empty. | |
httpGetApi() | |
let parameters: Parameters = ["key": self.getApiKey(), "category": ""] | |
var podcasts: [Podcast] = [Podcast]() | |
Alamofire.request("addr", method: .get, parameters:parameters) | |
.responseJSON{response in | |
if let json = response.result.value { | |
if let array = json as? [Any] { | |
for (_, item) in array.enumerated() { | |
if let details = item as? [[String:Any]] { | |
for (_, item) in details.enumerated() { | |
let tags = item["Tags"] as! [String] | |
let cName = item["Collection name"] as! [String] | |
let duration = item["Length (sec)"] as! [String] | |
let description = item["Description"] as! [String] | |
let photo = UIImage(named: "defaultImage")! | |
let podcast = Podcast(collection: cName.first!, photo: photo, description: description.first!, duration: duration.first!, tags: tags) | |
podcasts.append(podcast!) | |
parserObserver.podcastsParsed(podcasts: podcasts) | |
} | |
} | |
} | |
} | |
}else{ | |
print("Ei mene if lauseen läpi") | |
} | |
} | |
} | |
} | |
DataParserObserver.swift | |
protocol DataParserObserver { | |
func podcastsParsed (podcasts: [Podcast]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment