Created
January 5, 2017 09:03
-
-
Save Rukomoynikov/46f799b4d25378f6eade96ecc38b89f9 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
// | |
// ViewController.swift | |
// Spot | |
// | |
// Created by Maksim Rukomoynikov on 12/12/2016. | |
// Copyright © 2016 Maksim Rukomoynikov. All rights reserved. | |
// | |
import UIKit | |
import Alamofire | |
struct post { | |
let mainImage : UIImage! | |
let name : String! | |
} | |
class TableViewController: UITableViewController { | |
var posts = [post]() | |
var searchUrl = "https://api.spotify.com/v1/search?q=Shawn+Mendes&type=track" | |
typealias JSONStandard = [String : AnyObject] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
callAlomoFire(url: searchUrl) | |
} | |
func callAlomoFire(url: String) { | |
Alamofire.request(searchUrl).responseJSON(completionHandler: { | |
response in | |
self.parseData(JSONData: response.data!) | |
}) | |
} | |
func parseData(JSONData : Data){ | |
do { | |
let readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! [String : AnyObject] | |
if let tracks = readableJSON["tracks"] as? JSONStandard { | |
if let items = tracks["items"] as? [JSONStandard] { | |
for i in 0..<items.count { | |
let item = items[i] | |
let name = item["name"] as! String | |
if let album = item["album"] as? JSONStandard { | |
if let images = album["images"] as? [JSONStandard] { | |
let imageData = images[0] | |
let mainImageURL = URL(string: imageData["url"] as! String) | |
let mainImageData = NSData(contentsOf: mainImageURL!) | |
var mainImage = UIImage(data: mainImageData as! Data) | |
posts.append(post.init(mainImage: mainImage, name: name)) | |
self.tableView.reloadData() | |
} | |
} | |
} | |
// let numOfItems = items.count | |
} | |
} | |
} | |
catch { | |
print(error) | |
} | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return posts.count | |
} | |
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") | |
let mainImageView = cell?.viewWithTag(2) as! UIImageView | |
mainImageView.image = posts[indexPath.row].mainImage | |
let mainLabel = cell?.viewWithTag(1) as! UILabel | |
mainLabel.text = posts[indexPath.row].name | |
return cell! | |
} | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
let indexPath = self.tableView.indexPathForSelectedRow?.row | |
let vc = segue.destination as! AudioVC | |
vc.image = posts[indexPath!].mainImage | |
vc.mainSongTitle = posts[indexPath!].name | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment