connect player to feed and bookmark tableview cells
MODIFY
Podcast/AppDelegate.swift
Podcast/AppDelegate.swift
@@ -90,7 +90,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}
func showPlayer(animated: Bool) {
+ if tabBarController.accessoryViewController == playerViewController { return }
tabBarController.addAccessoryViewController(accessoryViewController: playerViewController)
+ collapsePlayer(animated: false)
}
func didFinishAuthenticatingUser() {

MODIFY
Podcast/BookmarkViewController.swift
Podcast/BookmarkViewController.swift
@@ -15,6 +15,7 @@ class BookmarkViewController: UIViewController, UITableViewDelegate, UITableView
///
var bookmarkTableView: UITableView!
var cards: [Card] = []
+ var currentlyPlayingIndexPath: IndexPath?
override func viewDidLoad() {
super.viewDidLoad()
@@ -43,8 +44,16 @@ class BookmarkViewController: UIViewController, UITableViewDelegate, UITableView
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
- bookmarkTableView.reloadData()
+ // check before reloading data whether the Player has stopped playing the currentlyPlayingIndexPath
+ if let indexPath = currentlyPlayingIndexPath {
+ if let card = cards[indexPath.row] as? EpisodeCard {
+ if Player.sharedInstance.currentEpisode?.id != card.episode.id {
+ currentlyPlayingIndexPath = nil
+ }
+ }
+ }
+ bookmarkTableView.reloadData()
}
override func didReceiveMemoryWarning() {
@@ -66,6 +75,9 @@ class BookmarkViewController: UIViewController, UITableViewDelegate, UITableView
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CardTableViewCellIdentifier") as? CardTableViewCell else { return UITableViewCell() }
cell.delegate = self
cell.setupWithCard(card: cards[indexPath.row])
+ if indexPath == currentlyPlayingIndexPath {
+ cell.setPlayButtonToState(isPlaying: true)
+ }
return cell
}
@@ -100,8 +112,18 @@ class BookmarkViewController: UIViewController, UITableViewDelegate, UITableView
func cardTableViewCellDidPressPlayPauseButton(cardTableViewCell: CardTableViewCell) {
guard let cardIndexPath = bookmarkTableView.indexPath(for: cardTableViewCell), let card = cards[cardIndexPath.row] as? EpisodeCard else { return }
- //card.isPlaying = !card.isPlaying
- //cardTableViewCell.setPlayButtonToState(isPlaying: card.isPlaying)
+ if cardIndexPath == currentlyPlayingIndexPath { return }
+
+ if let indexPath = currentlyPlayingIndexPath {
+ if let cell = bookmarkTableView.cellForRow(at: indexPath) as? CardTableViewCell {
+ cell.setPlayButtonToState(isPlaying: false)
+ }
+ }
+ currentlyPlayingIndexPath = cardIndexPath
+ cardTableViewCell.setPlayButtonToState(isPlaying: true)
+ let appDelegate = UIApplication.shared.delegate as! AppDelegate
+ appDelegate.showPlayer(animated: true)
+ Player.sharedInstance.playEpisode(episode: card.episode)
}
func cardTableViewCellDidPressTagButton(cardTableViewCell: CardTableViewCell, index: Int) {

MODIFY
Podcast/Episode.swift
Podcast/Episode.swift
@@ -40,7 +40,7 @@ class Episode: NSObject {
self.smallArtworkImageURL = smallArtworkImageURL
self.largeArtworkImageURL = largeArtworkImageURL
if audioURL == nil { //TAKE THIS OUT LATER ONLY FOR PLAYER STATIC DATA
- self.audioURL = URL(string: "http://cf-media.sndcdn.com/stEcTqU4a27v.128.mp3?Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiKjovL2NmLW1lZGlhLnNuZGNkbi5jb20vc3RFY1RxVTRhMjd2LjEyOC5tcDMiLCJDb25kaXRpb24iOnsiRGF0ZUxlc3NUaGFuIjp7IkFXUzpFcG9jaFRpbWUiOjE0ODg3NDQzMzJ9fX1dfQ__&Signature=SgM4MzYFr9CDFnX-4f-q6uWdawoVVtEqTCRf5AXPAlF83LXjXNzPVEv9PW2CVFaq-2qFZdabgXHv8Tjsz924k1mnfnu3qwpZjmez4MxnzajH47giPPfmd~F9Kv7WEP4vSLfUukrT4WMslxFFwNH~C7Hpwyrs8aUFU8naTEOH2LLIa6mE4IDa7eErsGkYZ5n-Zenata-kiTQHWYRVeJAzLRm69GX313YhUJQGGeYIE9TW2iYrteQkTmTr5JpJ2pwrbjDUfc1r3rkekpMLBuLBtt6qrB~IuL8nvm5kkDJENXABTBsoT7wHDxaAcWsnlAbL06Z5SIBWAst4rut5jZI0Cg__&Key-Pair-Id=APKAJAGZ7VMH2PFPW6UQ")
+ self.audioURL = URL(string: "https://play.podtrac.com/npr-344098539/npr.mc.tritondigital.com/WAITWAIT_PODCAST/media/anon.npr-podcasts/podcast/344098539/517256651/npr_517256651.mp3?orgId=1&d=2981&p=344098539&story=517256651&t=podcast&e=517256651&ft=pod&f=344098539")
} else {
self.audioURL = audioURL
}

MODIFY
Podcast/EpisodeTableViewCell.swift
Podcast/EpisodeTableViewCell.swift
@@ -288,12 +288,6 @@ class EpisodeTableViewCell: UITableViewCell {
if episode.isRecommended == true {
recommendedButton.setImage(#imageLiteral(resourceName: "heart_icon_selected"), for: .normal)
}
- /*
- if episode.isPlaying == true {
- playButton.setImage(#imageLiteral(resourceName: "play_feed_icon_selected"), for: .normal)
- playLabel.text = "Now Playing"
- }
- */
cardID = episode.id
}

MODIFY
Podcast/FeedViewController.swift
Podcast/FeedViewController.swift
@@ -23,6 +23,7 @@ class FeedViewController: UIViewController, UITableViewDelegate, UITableViewData
///
var feedTableView: UITableView!
var cards: [Card] = []
+ var currentlyPlayingIndexPath: IndexPath?
override func viewDidLoad() {
super.viewDidLoad()
@@ -51,8 +52,16 @@ class FeedViewController: UIViewController, UITableViewDelegate, UITableViewData
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
- feedTableView.reloadData()
+ // check before reloading data whether the Player has stopped playing the currentlyPlayingIndexPath
+ if let indexPath = currentlyPlayingIndexPath {
+ if let card = cards[indexPath.row] as? EpisodeCard {
+ if Player.sharedInstance.currentEpisode?.id != card.episode.id {
+ currentlyPlayingIndexPath = nil
+ }
+ }
+ }
+ feedTableView.reloadData()
}
override func didReceiveMemoryWarning() {
@@ -74,6 +83,9 @@ class FeedViewController: UIViewController, UITableViewDelegate, UITableViewData
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CardTableViewCellIdentifier") as? CardTableViewCell else { return UITableViewCell() }
cell.delegate = self
cell.setupWithCard(card: cards[indexPath.row])
+ if indexPath == currentlyPlayingIndexPath {
+ cell.setPlayButtonToState(isPlaying: true)
+ }
return cell
}
@@ -108,8 +120,18 @@ class FeedViewController: UIViewController, UITableViewDelegate, UITableViewData
func cardTableViewCellDidPressPlayPauseButton(cardTableViewCell: CardTableViewCell) {
guard let cardIndexPath = feedTableView.indexPath(for: cardTableViewCell), let card = cards[cardIndexPath.row] as? EpisodeCard else { return }
- //card.isPlaying = !card.isPlaying
- //cardTableViewCell.setPlayButtonToState(isPlaying: card.isPlaying)
+ if cardIndexPath == currentlyPlayingIndexPath { return }
+
+ if let indexPath = currentlyPlayingIndexPath {
+ if let cell = feedTableView.cellForRow(at: indexPath) as? CardTableViewCell {
+ cell.setPlayButtonToState(isPlaying: false)
+ }
+ }
+ currentlyPlayingIndexPath = cardIndexPath
+ cardTableViewCell.setPlayButtonToState(isPlaying: true)
+ let appDelegate = UIApplication.shared.delegate as! AppDelegate
+ appDelegate.showPlayer(animated: true)
+ Player.sharedInstance.playEpisode(episode: card.episode)
}
func cardTableViewCellDidPressTagButton(cardTableViewCell: CardTableViewCell, index: Int) {

connect player to feed and bookmark tableview cells