Skip to content

Instantly share code, notes, and snippets.

@subdan
Created January 11, 2019 19:10
Show Gist options
  • Save subdan/a79e791881cd9706720928227f121419 to your computer and use it in GitHub Desktop.
Save subdan/a79e791881cd9706720928227f121419 to your computer and use it in GitHub Desktop.
import UIKit
class StationsViewController: GenericViewController<StationsView>,
UICollectionViewDelegate,
StationsPresentationModelDelegate,
UISearchBarDelegate,
FilterControlDelegate,
RadioControllerDelegate {
private var stationsPresentationModel: StationsPresentationModel!
// MARK: - Dependencies
var radioController: RadioController!
var sleepController: SleepController!
// MARK: - ViewController's Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Setup constraints
rootView.setupConstraints(by: topLayoutGuide)
let defaultFilter = FilterAttribute.country(.RU)
rootView.updateFilter(newValue: defaultFilter)
// Presentation model
stationsPresentationModel = StationsPresentationModel(defaultFilter: defaultFilter)
stationsPresentationModel.delegate = self
rootView.collectionView.dataSource = stationsPresentationModel
// Delegates
rootView.searchBar.delegate = self
rootView.filterControl.delegate = self
rootView.collectionView.delegate = self
radioController.delegate = self
// Actions
rootView.nowPlayingButton.addTarget(self, action: #selector(nowPlayingButtonTapped),
for: .touchUpInside)
rootView.refreshControl.addTarget(self, action: #selector(refreshOptions(sender:)),
for: .valueChanged)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
rootView.updateCollectionViewLayout(invalidate: true)
}
override func viewWillDisappear(_ animated: Bool) {
if rootView.searchBar.isFirstResponder {
rootView.searchBar.resignFirstResponder()
}
}
override func viewWillAppear(_ animated: Bool) {
stationsPresentationModel.updateFilter()
rootView.nowPlayingButton.isHidden = radioController.currentState == .stopped
}
// MARK: - Actions
@objc private func nowPlayingButtonTapped() {
if radioController.currentStation != nil {
showNowPlayingScreen()
}
}
@objc private func refreshOptions(sender: UIRefreshControl) {
stationsPresentationModel.refresh {
sender.endRefreshing()
}
}
// MARK: - RadioControllerDelegate
func stateChanged(state: RadioState) {
rootView.nowPlayingButton.isHidden = radioController.currentState == .stopped
}
// MARK: - StationsPresentationModelDelegate
func stationsChanged() {
rootView.collectionView.reloadData()
}
// MARK: - UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
stationsPresentationModel.currentStationIndex = indexPath.row
if let newStation = stationsPresentationModel.station(at: indexPath) {
changeStation(station: newStation, showPlayer: true)
}
}
// MARK: - UISearchBarDelegate
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
stationsPresentationModel.search(forText: searchText)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.text = ""
searchBar.resignFirstResponder()
stationsPresentationModel.search(forText: "")
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(true, animated: true)
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchBar.setShowsCancelButton(false, animated: true)
}
// MARK: - FilterControlDelegate
func filterDidChanged() {
stationsPresentationModel.filter(by: rootView.selectedFilter)
}
// MARK: - Private methods
private func changeStation(station: Station, showPlayer: Bool) {
if radioController.currentStation != station {
radioController.changeStation(station)
}
if showPlayer {
let playerViewController = PlayerViewController()
playerViewController.radioController = radioController
playerViewController.sleepController = sleepController
navigationController?.pushViewController(playerViewController, animated: true)
}
}
private func showNowPlayingScreen() {
let playerViewController = PlayerViewController()
playerViewController.radioController = radioController
playerViewController.sleepController = sleepController
navigationController?.pushViewController(playerViewController, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment