Skip to content

Commit

Permalink
Spotify iOS SDK v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jackfreeman committed Dec 5, 2018
1 parent 25971e2 commit 333515d
Show file tree
Hide file tree
Showing 61 changed files with 1,426 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
2BAD4B251CFD66290033DE75 /* My-Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BAD4B241CFD66290033DE75 /* My-Extensions.swift */; };
54266D9121ACB40D00E10A41 /* SpeedPickerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54266D9021ACB40D00E10A41 /* SpeedPickerViewController.swift */; };
546D86B52139DB76002AD06E /* SpotifyiOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 546D86B42139DB76002AD06E /* SpotifyiOS.framework */; };
998FF9BE1D3E54AC003E339B /* ConnectionStatusIndicatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 998FF9BD1D3E54AC003E339B /* ConnectionStatusIndicatorView.swift */; };
99A0FFE41D38F9B600A90453 /* PlaybackButtonGraphics.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99A0FFE31D38F9B600A90453 /* PlaybackButtonGraphics.swift */; };
Expand All @@ -23,6 +24,7 @@

/* Begin PBXFileReference section */
2BAD4B241CFD66290033DE75 /* My-Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "My-Extensions.swift"; sourceTree = "<group>"; };
54266D9021ACB40D00E10A41 /* SpeedPickerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpeedPickerViewController.swift; sourceTree = "<group>"; };
546D86B42139DB76002AD06E /* SpotifyiOS.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SpotifyiOS.framework; path = ../../SpotifyiOS.framework; sourceTree = "<group>"; };
998FF9BD1D3E54AC003E339B /* ConnectionStatusIndicatorView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectionStatusIndicatorView.swift; sourceTree = "<group>"; };
99A0FFE31D38F9B600A90453 /* PlaybackButtonGraphics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlaybackButtonGraphics.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -84,6 +86,7 @@
F4F974081CAD62D40036411D /* NowPlayingView-Bridging-Header.h */,
99A0FFE31D38F9B600A90453 /* PlaybackButtonGraphics.swift */,
998FF9BD1D3E54AC003E339B /* ConnectionStatusIndicatorView.swift */,
54266D9021ACB40D00E10A41 /* SpeedPickerViewController.swift */,
);
path = NowPlayingView;
sourceTree = "<group>";
Expand Down Expand Up @@ -178,6 +181,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
54266D9121ACB40D00E10A41 /* SpeedPickerViewController.swift in Sources */,
99A0FFE41D38F9B600A90453 /* PlaybackButtonGraphics.swift in Sources */,
F6EB42E31D2BB0D800F57322 /* ContentCollectionViewController.swift in Sources */,
F4F973F21CAD60320036411D /* ViewController.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "skipback15.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "skipforward15.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ContentCollectionViewController : UICollectionViewController, UICollection
self.collectionView?.reloadData()
}
} else {
appRemote.contentAPI?.fetchRecommendedContentItems(for: .default) { (items, error) in
appRemote.contentAPI?.fetchRootContentItems(forType: SPTAppRemoteContentTypeDefault) { (items, error) in
if let contentItems = items as? [SPTAppRemoteContentItem] {
self.contentItems = contentItems
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

protocol SpeedPickerViewControllerDelegate {
func speedPicker(viewController: SpeedPickerViewController, didChoose speed:SPTAppRemotePodcastPlaybackSpeed)
func speedPickerDidCancel(viewController: SpeedPickerViewController)
}


class SpeedPickerViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var delegate: SpeedPickerViewControllerDelegate?
fileprivate let podcastSpeeds: [SPTAppRemotePodcastPlaybackSpeed]
fileprivate var selectedSpeed: SPTAppRemotePodcastPlaybackSpeed
fileprivate var selectedIndex: Int = 0
fileprivate let cellIdentifier = "SpeedCell"

lazy var tableView: UITableView = {
let tableView = UITableView(frame: self.view.bounds)
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: self.cellIdentifier)
return tableView
}()

init(podcastSpeeds: [SPTAppRemotePodcastPlaybackSpeed], selectedSpeed: SPTAppRemotePodcastPlaybackSpeed) {
self.podcastSpeeds = podcastSpeeds
self.selectedSpeed = selectedSpeed
super.init(nibName: nil, bundle: nil)
updateSelectedindex()
view.addSubview(tableView)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Podcast Playback Speed"
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(didPressCancel))
}

fileprivate func updateSelectedindex() {
let values = podcastSpeeds.map { $0.value }
selectedIndex = values.distance(from: values.startIndex, to:values.index(of: self.selectedSpeed.value)!)
}

@objc func didPressCancel() {
delegate?.speedPickerDidCancel(viewController: self)
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.speedPicker(viewController: self, didChoose: podcastSpeeds[indexPath.row])
selectedSpeed = podcastSpeeds[indexPath.row]
selectedIndex = indexPath.row
tableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return podcastSpeeds.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
cell.textLabel?.text = String(format: "%.1fx", podcastSpeeds[indexPath.row].value.floatValue)
if indexPath.row == selectedIndex {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
return cell
}
}
83 changes: 83 additions & 0 deletions DemoProjects/NowPlayingView/NowPlayingView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import StoreKit
class ViewController: UIViewController,
SPTAppRemotePlayerStateDelegate,
SPTAppRemoteUserAPIDelegate,
SpeedPickerViewControllerDelegate,
SKStoreProductViewControllerDelegate {

fileprivate let playURI = "spotify:album:5uMfshtC2Jwqui0NUyUYIL"
fileprivate let trackIdentifier = "spotify:track:32ftxJzxMPgUFCM6Km9WTS"
fileprivate let name = "Now Playing View"

fileprivate var currentPodcastSpeed: SPTAppRemotePodcastPlaybackSpeed?

// MARK: - Lifecycle

fileprivate var connectionIndicatorView = ConnectionStatusIndicatorView()
Expand All @@ -31,6 +34,11 @@ class ViewController: UIViewController,
prevButton.setTitle("", for: UIControlState.normal)
prevButton.setImage(PlaybackButtonGraphics.previousButtonImage(), for: UIControlState.normal)
prevButton.setImage(PlaybackButtonGraphics.previousButtonImage(), for: UIControlState.highlighted)

skipBackward15Button.setImage(skipBackward15Button.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal)
skipForward15Button.setImage(skipForward15Button.imageView?.image?.withRenderingMode(.alwaysTemplate), for: .normal)
skipBackward15Button.isHidden = true
skipForward15Button.isHidden = true
}

// MARK: - View
Expand All @@ -41,6 +49,10 @@ class ViewController: UIViewController,
@IBOutlet var nextButton: UIButton!
@IBOutlet var prevButton: UIButton!

@IBOutlet var skipForward15Button: UIButton!
@IBOutlet var skipBackward15Button: UIButton!
@IBOutlet var podcastSpeedButton: UIButton!

fileprivate func updateViewWithPlayerState(_ playerState: SPTAppRemotePlayerState) {
updatePlayPauseButtonState(playerState.isPaused)
updateRepeatModeLabel(playerState.playbackOptions.repeatMode)
Expand All @@ -50,6 +62,7 @@ class ViewController: UIViewController,
self.updateAlbumArtWithImage(image)
}
updateViewWithRestrictions(playerState.playbackRestrictions)
updateInterfaceForPodcast(playerState: playerState)
}

fileprivate func updateViewWithRestrictions(_ restrictions: SPTAppRemotePlaybackRestrictions) {
Expand All @@ -75,6 +88,22 @@ class ViewController: UIViewController,
}
}

// MARK: Podcast Support

fileprivate func updateInterfaceForPodcast(playerState: SPTAppRemotePlayerState) {
skipForward15Button.isHidden = !playerState.track.isEpisode
skipBackward15Button.isHidden = !playerState.track.isEpisode
podcastSpeedButton.isHidden = !playerState.track.isPodcast
nextButton.isHidden = !skipForward15Button.isHidden
prevButton.isHidden = !skipBackward15Button.isHidden
getCurrentPodcastSpeed()
}

fileprivate func updatePodcastSpeed(speed: SPTAppRemotePodcastPlaybackSpeed) {
currentPodcastSpeed = speed
podcastSpeedButton.setTitle(String(format: "%0.1fx", speed.value.floatValue), for: .normal);
}

// MARK: Player Control

@IBOutlet weak var playPauseButton: UIButton!
Expand Down Expand Up @@ -104,6 +133,18 @@ class ViewController: UIViewController,
playTrack()
}

@IBAction func didPressSkipForward15Button(_ sender: UIButton) {
seekForward15Seconds()
}

@IBAction func didPressSkipBackward15Button(_ sender: UIButton) {
seekBackward15Seconds()
}

@IBAction func didPressChangePodcastPlaybackSpeedButton(_ sender: UIButton) {
pickPodcastSpeed()
}

@IBAction func didPressEnqueueTrackButton(_ sender: AnyObject) {
enqueueTrack()
}
Expand Down Expand Up @@ -266,6 +307,25 @@ class ViewController: UIViewController,
}
}

fileprivate func seekForward15Seconds() {
appRemote.playerAPI?.seekForward15Seconds(defaultCallback)
}

fileprivate func seekBackward15Seconds() {
appRemote.playerAPI?.seekBackward15Seconds(defaultCallback)
}

fileprivate func pickPodcastSpeed() {
appRemote.playerAPI?.getAvailablePodcastPlaybackSpeeds({ (speeds, error) in
if error == nil, let speeds = speeds as? [SPTAppRemotePodcastPlaybackSpeed], let current = self.currentPodcastSpeed {
let vc = SpeedPickerViewController(podcastSpeeds: speeds, selectedSpeed: current)
vc.delegate = self
let nav = UINavigationController(rootViewController: vc)
self.present(nav, animated: true, completion: nil)
}
})
}

fileprivate func skipNext() {
appRemote.playerAPI?.skip(toNext: defaultCallback)
}
Expand Down Expand Up @@ -304,6 +364,13 @@ class ViewController: UIViewController,
}
}

fileprivate func getCurrentPodcastSpeed() {
appRemote.playerAPI?.getCurrentPodcastPlaybackSpeed({ (speed, error) in
guard error == nil, let speed = speed as? SPTAppRemotePodcastPlaybackSpeed else { return }
self.updatePodcastSpeed(speed: speed)
})
}

fileprivate func playTrackWithIdentifier(_ identifier: String) {
appRemote.playerAPI?.play(identifier, callback: defaultCallback)
}
Expand Down Expand Up @@ -422,4 +489,20 @@ class ViewController: UIViewController,
self.subscribedToCapabilities = false
enableInterface(false)
}

// MARK: - SpeedPickerViewController

func speedPickerDidCancel(viewController: SpeedPickerViewController) {
viewController.dismiss(animated: true, completion: nil)
}

func speedPicker(viewController: SpeedPickerViewController, didChoose speed: SPTAppRemotePodcastPlaybackSpeed) {
appRemote.playerAPI?.setPodcastPlaybackSpeed(speed, callback: { (_, error) in
guard error == nil else {
return
}
self.updatePodcastSpeed(speed: speed)
})
viewController.dismiss(animated: true, completion: nil)
}
}
Loading

0 comments on commit 333515d

Please sign in to comment.