connect recommended episode cells to the player
MODIFY
Podcast/DiscoverViewController.swift
Podcast/DiscoverViewController.swift
@@ -73,6 +73,11 @@ class DiscoverViewController: UIViewController, UITableViewDelegate, UITableView
episodes = Array(repeating: episode, count: 5)
}
+ override func viewWillAppear(_ animated: Bool) {
+ super.viewWillAppear(animated)
+ tableView.reloadData()
+ }
+
//MARK: - TableView DataSource & Delegate
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
@@ -86,6 +91,7 @@ class DiscoverViewController: UIViewController, UITableViewDelegate, UITableView
} else if let cell = cell as? RecommendedEpisodesOuterTableViewCell {
cell.dataSource = self
cell.delegate = self
+ cell.updateUIForNowPlayingEpisode(episode: Player.sharedInstance.currentEpisode)
}
return cell
}
@@ -173,7 +179,9 @@ class DiscoverViewController: UIViewController, UITableViewDelegate, UITableView
}
func recommendedEpisodeOuterTableViewCellDidPressPlayButton(episodeTableViewCell: EpisodeTableViewCell, episode: Episode) {
-
+ guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
+ appDelegate.showPlayer(animated: true)
+ Player.sharedInstance.playEpisode(episode: episode)
}
func recommendedEpisodeOuterTableViewCellDidPressBookmarkButton(episodeTableViewCell: EpisodeTableViewCell, episode: Episode) {

MODIFY
Podcast/RecommendedEpisodesOuterTableViewCell.swift
Podcast/RecommendedEpisodesOuterTableViewCell.swift
@@ -27,6 +27,7 @@ class RecommendedEpisodesOuterTableViewCell: UITableViewCell, UITableViewDelegat
var tableView: UITableView!
var dataSource: RecommendedEpisodesOuterTableViewCellDataSource?
var delegate: RecommendedEpisodesOuterTableViewCellDelegate?
+ var currentlyPlayingIndexPath: IndexPath?
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
@@ -56,6 +57,9 @@ class RecommendedEpisodesOuterTableViewCell: UITableViewCell, UITableViewDelegat
let episode = dataSource?.recommendedEpisodesTableViewCell(cell: self, dataForItemAt: indexPath) ?? Episode()
cell.setupWithEpisode(episode: episode)
cell.delegate = self
+ if indexPath == currentlyPlayingIndexPath {
+ cell.setPlayButtonToState(isPlaying: true)
+ }
return cell
}
@@ -80,6 +84,13 @@ class RecommendedEpisodesOuterTableViewCell: UITableViewCell, UITableViewDelegat
func episodeTableViewCellDidPressPlayPauseButton(episodeTableViewCell: EpisodeTableViewCell) {
guard let episodeIndexPath = tableView.indexPath(for: episodeTableViewCell), let episode = dataSource?.recommendedEpisodesTableViewCell(cell: self, dataForItemAt: episodeIndexPath) else { return }
+ if let previousIndexPath = currentlyPlayingIndexPath, previousIndexPath != episodeIndexPath, let previousCell = tableView.cellForRow(at: previousIndexPath) as? EpisodeTableViewCell {
+ previousCell.setPlayButtonToState(isPlaying: false)
+ }
+
+ currentlyPlayingIndexPath = episodeIndexPath
+
+ episodeTableViewCell.setPlayButtonToState(isPlaying: true)
delegate?.recommendedEpisodeOuterTableViewCellDidPressPlayButton(episodeTableViewCell: episodeTableViewCell, episode: episode)
}
@@ -104,4 +115,12 @@ class RecommendedEpisodesOuterTableViewCell: UITableViewCell, UITableViewDelegat
func episodeTableViewCellDidPressMoreActionsButton(episodeTableViewCell: EpisodeTableViewCell) {
delegate?.recommendedEpisodesOuterTableViewCellDidPressShowActionSheet(episodeTableViewCell: episodeTableViewCell)
}
+
+ func updateUIForNowPlayingEpisode(episode: Episode?) {
+ guard let indexPath = currentlyPlayingIndexPath, let previousEpisode = dataSource?.recommendedEpisodesTableViewCell(cell: self, dataForItemAt: indexPath) else { return }
+ // check if previously playing episode is no longer playing
+ if previousEpisode.id != episode?.id {
+ (tableView.cellForRow(at: indexPath) as? EpisodeTableViewCell)?.setPlayButtonToState(isPlaying: false)
+ }
+ }
}

MODIFY
Podcast/SearchEpisodeTableViewCell.swift
Podcast/SearchEpisodeTableViewCell.swift
@@ -85,8 +85,14 @@ class SearchEpisodeTableViewCell: UITableViewCell {
}
func didPressPlayButton() {
- playButtonActivated = !playButtonActivated
- playButton.isSelected = playButtonActivated
delegate?.searchEpisodeTableViewCell(cell: self, didSetPlayButton: playButtonActivated)
}
+
+ func setPlayButtonToState(isPlaying: Bool) {
+ if isPlaying {
+ playButton.isSelected = true
+ } else {
+ playButton.isSelected = false
+ }
+ }
}

connect recommended episode cells to the player