Repository navigation
Expand file tree
/
Copy pathDetailsViewController.swift
More file actions
90 lines (72 loc) · 2.89 KB
/
Copy pathDetailsViewController.swift
File metadata and controls
90 lines (72 loc) · 2.89 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
//
// DetailsViewController.swift
// SwiftTutorial
//
// Created by Jameson Quave on 6/16/14.
// Copyright (c) 2014 JQ Software LLC. All rights reserved.
//
import UIKit
import MediaPlayer
import QuartzCore
class DetailsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, APIControllerProtocol {
var mediaPlayer: MPMoviePlayerController = MPMoviePlayerController()
var tracks: Track[] = []
@IBOutlet var albumCover : UIImageView
@IBOutlet var titleLabel : UILabel
@IBOutlet var tracksTableView : UITableView
@lazy var api: APIController = APIController(delegate: self)
var album: Album?
init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = self.album?.title
albumCover.image = UIImage(data: NSData(contentsOfURL: NSURL(string: self.album?.largeImageURL)))
// Load in tracks
if self.album?.collectionId? {
api.lookupAlbum(self.album!.collectionId!)
}
}
func didReceiveAPIResults(results: NSDictionary) {
if let allResults = results["results"] as? NSDictionary[] {
for trackInfo in allResults {
// Create the track
if let kind = trackInfo["kind"] as? String {
if kind=="song" {
var track = Track(dict: trackInfo)
tracks.append(track)
}
}
}
}
dispatch_async(dispatch_get_main_queue(), {
self.tracksTableView.reloadData()
})
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return tracks.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell = tableView.dequeueReusableCellWithIdentifier("TrackCell") as TrackCell
var track = tracks[indexPath.row]
cell.titleLabel.text = track.title
cell.playIcon.text = "▶️"
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
var track = tracks[indexPath.row]
mediaPlayer.stop()
mediaPlayer.contentURL = NSURL(string: track.previewUrl)
mediaPlayer.play()
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? TrackCell {
cell.playIcon.text = "YOUR_STOP_ICON"
}
}
func tableView(tableView: UITableView!, willDisplayCell cell: UITableViewCell!, forRowAtIndexPath indexPath: NSIndexPath!) {
cell.layer.transform = CATransform3DMakeScale(0.1,0.1,1)
UIView.animateWithDuration(0.25, animations: {
cell.layer.transform = CATransform3DMakeScale(1,1,1)
})
}
}