-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathDetailsViewController.swift
More file actions
78 lines (64 loc) · 2.68 KB
/
DetailsViewController.swift
File metadata and controls
78 lines (64 loc) · 2.68 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
//
// DetailsViewController.swift
// MusicPlayer
//
// Created by Jameson Quave on 9/16/14.
// Copyright (c) 2014 JQ Software LLC. All rights reserved.
//
import UIKit
import MediaPlayer
class DetailsViewController: UIViewController, APIControllerProtocol, UITableViewDelegate, UITableViewDataSource {
var album: Album?
var tracks = [Track]()
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var albumCover: UIImageView!
@IBOutlet weak var tracksTableView: UITableView!
lazy var api : APIController = APIController(delegate: self)
var mediaPlayer: MPMoviePlayerController = MPMoviePlayerController()
required 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)!)!)
if self.album != nil {
api.lookupAlbum(self.album!.collectionId)
}
}
// MARK: UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tracks.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TrackCell") as TrackCell
let 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 = "◾️"
}
}
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)
})
}
// MARK: APIControllerProtocol
func didReceiveAPIResults(results: NSDictionary) {
var resultsArr: NSArray = results["results"] as NSArray
dispatch_async(dispatch_get_main_queue(), {
self.tracks = Track.tracksWithJSON(resultsArr)
self.tracksTableView.reloadData()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
})
}
}