Skip to content

Commit cf07dd9

Browse files
committed
Retrieve track info from a specified collection
1 parent b3f3309 commit cf07dd9

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

TestSwift/APIController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class APIController {
2424
let url = NSURL(string: path)
2525
let request = NSURLRequest(URL: url)
2626

27+
println("Get \(path)")
2728
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
2829
if error? {
2930
println("ERROR: \(error.localizedDescription)")
@@ -49,12 +50,11 @@ class APIController {
4950

5051
// Now escape anything else that isn't URL-friendly
5152
let escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
52-
let urlPath = "https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=music&entity=album"
53-
get(urlPath)
53+
get("https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=music&entity=album")
5454
}
5555

56-
func lookupTrackWithID(trackID: String) {
57-
56+
func lookupTrack(collectionId: Int) {
57+
get("https://itunes.apple.com/lookup?id=\(collectionId)&entity=song")
5858
}
5959

6060
}

TestSwift/Classes/Album.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ class Album {
1414
var largeImageURL: String?
1515
var itemURL: String?
1616
var artistURL: String?
17-
var collectionID: Int?
17+
var collectionId: Int?
1818

19-
init(name: String!, price: String!, thumbnailImageURL: String!, largeImageURL: String!, itemURL: String!, artistURL: String!, collectionID: Int?) {
19+
init(name: String!, price: String!, thumbnailImageURL: String!, largeImageURL: String!, itemURL: String!, artistURL: String!, collectionId: Int?) {
2020
self.title = name
2121
self.price = price
2222
self.thumbnailImageURL = thumbnailImageURL
2323
self.largeImageURL = largeImageURL
2424
self.itemURL = itemURL
2525
self.artistURL = artistURL
26-
self.collectionID = collectionID
26+
self.collectionId = collectionId
2727
}
2828
}

TestSwift/Classes/DetailsViewController.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88

99
import UIKit
1010

11-
class DetailsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
11+
class DetailsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, APIControllerProtocol {
1212

1313
@IBOutlet var albumCover : UIImageView
1414
@IBOutlet var titleLabel : UILabel
1515

1616
var album: Album?
1717
var tracks: Track[] = []
18+
var api: APIController?
1819

1920
init(coder aDecoder: NSCoder!) {
2021
super.init(coder: aDecoder)
@@ -24,6 +25,17 @@ class DetailsViewController: UIViewController, UITableViewDelegate, UITableViewD
2425
super.viewDidLoad()
2526
titleLabel.text = self.album?.title
2627
albumCover.image = UIImage(data: NSData(contentsOfURL: NSURL(string: self.album?.largeImageURL)))
28+
29+
// Load in tracks
30+
self.api = APIController(delegate: self)
31+
let api = self.api!
32+
if self.album?.collectionId? {
33+
api.lookupTrack(self.album!.collectionId!)
34+
}
35+
}
36+
37+
func didRecieveAPIResults(results: NSDictionary) {
38+
println("Got track deets \(results)")
2739
}
2840

2941

TestSwift/SearchResultsViewController.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
1515
var api: APIController?
1616

1717
@IBOutlet var appsTableView : UITableView
18-
// var selectedAlbum: Album?
1918

2019
var albums: Album[] = []
2120
var imageCache = Dictionary<String, UIImage>()
@@ -136,9 +135,9 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
136135
itemURL = result["trackViewUrl"] as? String
137136
}
138137

139-
var collectionID = result["collectionId"] as? Int
138+
var collectionId = result["collectionId"] as? Int
140139

141-
var newAlbum = Album(name: name!, price: price!, thumbnailImageURL: thumbnailURL!, largeImageURL: imageURL!, itemURL: itemURL!, artistURL: artistURL!, collectionID: collectionID!)
140+
var newAlbum = Album(name: name!, price: price!, thumbnailImageURL: thumbnailURL!, largeImageURL: imageURL!, itemURL: itemURL!, artistURL: artistURL!, collectionId: collectionId!)
142141
albums.append(newAlbum)
143142
}
144143

0 commit comments

Comments
 (0)