connect search cells to player
MODIFY
Podcast/SearchEpisodeTableViewCell.swift
Podcast/SearchEpisodeTableViewCell.swift
import UIKit
protocol SearchEpisodeTableViewCellDelegate: class {
- func searchEpisodeTableViewCell(cell: SearchEpisodeTableViewCell, didSetPlayButton toNewValue: Bool)
+ func searchEpisodeTableViewCellDidPressPlayButton(cell: SearchEpisodeTableViewCell)
}
class SearchEpisodeTableViewCell: UITableViewCell {
@@ -85,7 +85,7 @@ class SearchEpisodeTableViewCell: UITableViewCell {
}
func didPressPlayButton() {
- delegate?.searchEpisodeTableViewCell(cell: self, didSetPlayButton: playButtonActivated)
+ delegate?.searchEpisodeTableViewCellDidPressPlayButton(cell: self)
}
func setPlayButtonToState(isPlaying: Bool) {

MODIFY
Podcast/SearchTableViewController.swift
Podcast/SearchTableViewController.swift
@@ -36,7 +36,7 @@ protocol SearchTableViewControllerDelegate {
func searchTableViewControllerNeedsFetch(controller: SearchTableViewController)
}
-class SearchTableViewController: UITableViewController {
+class SearchTableViewController: UITableViewController, SearchEpisodeTableViewCellDelegate {
var searchType: SearchType = .episodes
let cellIdentifiersClasses: [SearchType: (String, AnyClass)] =
@@ -58,6 +58,8 @@ class SearchTableViewController: UITableViewController {
var cellDelegate: SearchTableViewControllerDelegate?
+ var currentlyPlayingIndexPath: IndexPath?
+
override func viewDidLoad() {
super.viewDidLoad()
guard let (cellIdentifier, cellClass) = cellIdentifiersClasses[searchType] else { return }
@@ -69,6 +71,13 @@ class SearchTableViewController: UITableViewController {
}
}
+ override func viewDidAppear(_ animated: Bool) {
+ if let indexPath = currentlyPlayingIndexPath, let cell = tableView.cellForRow(at: indexPath) as? SearchEpisodeTableViewCell {
+ cell.setPlayButtonToState(isPlaying: false)
+ }
+ currentlyPlayingIndexPath = nil
+ }
+
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
@@ -88,6 +97,12 @@ class SearchTableViewController: UITableViewController {
case .episodes:
guard let episodes = results as? [Episode], let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? SearchEpisodeTableViewCell else { return UITableViewCell() }
cell.configure(for: episodes[indexPath.row], index: indexPath.row)
+ if indexPath == currentlyPlayingIndexPath {
+ cell.setPlayButtonToState(isPlaying: true)
+ } else {
+ cell.setPlayButtonToState(isPlaying: false)
+ }
+ cell.delegate = self
return cell
case .series:
guard let series = results as? [Series], let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as? SearchSeriesTableViewCell else { return UITableViewCell() }
@@ -130,4 +145,16 @@ class SearchTableViewController: UITableViewController {
return [searchTableViewControllerEpisodes, searchTableViewControllerSeries, searchTableViewControllerPeople, searchTableViewControllerTags]
}
+
+ func searchEpisodeTableViewCellDidPressPlayButton(cell: SearchEpisodeTableViewCell) {
+ guard let indexPath = tableView.indexPath(for: cell), indexPath != currentlyPlayingIndexPath, let episode = searchResults[.episodes]?[indexPath.row] as? Episode, let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
+
+ if let previousIndexPath = currentlyPlayingIndexPath, let previousCell = tableView.cellForRow(at: previousIndexPath) as? SearchEpisodeTableViewCell {
+ previousCell.setPlayButtonToState(isPlaying: false)
+ }
+ currentlyPlayingIndexPath = indexPath
+ cell.setPlayButtonToState(isPlaying: true)
+ appDelegate.showPlayer(animated: true)
+ Player.sharedInstance.playEpisode(episode: episode)
+ }
}

connect search cells to player