Created
June 7, 2017 14:08
-
-
Save Savchukv/b29b0ff4efd852e7a31826b5dfe2f8ac to your computer and use it in GitHub Desktop.
Example ViewController with CollectionView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GenreViewController.swift | |
// | |
// Created by Vasiliy Savchuk on 09.02.17. | |
// Copyright © 2017 All rights reserved. | |
// | |
class GenreViewController: BaseViewController { | |
@IBOutlet weak var mainGenreCollectionView: UICollectionView! | |
var viewModel = GenreViewModel() | |
//MARK: - Lifecicle Object | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
load() | |
addRefreshControl() | |
} | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
self.updateStatusBarUI() | |
} | |
//MARK: - Methods | |
func updateStatusBarUI() { | |
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent | |
} | |
private func load() { | |
view.showProgress() | |
viewModel.loadViewModels(handler: { [weak self] in | |
self?.mainGenreCollectionView.reloadData() | |
self?.view.hideProgress() | |
}) | |
} | |
override func updateUI() { | |
guard Reachability.sharedInstance.isReachable else { | |
self.mainGenreCollectionView.hidden() | |
self.informView.showMe() | |
return | |
} | |
self.informView.hidden() | |
self.mainGenreCollectionView.showMe() | |
} | |
func addRefreshControl() { | |
self.mainGenreCollectionView.addSubview(self.createRefreshControl()) | |
self.mainGenreCollectionView.alwaysBounceVertical = true | |
} | |
//MARK: Actions | |
@IBAction func backButtonDidPress(_ sender: Any) { | |
_ = self.navigationController?.popViewController(animated: true) | |
} | |
} | |
//MARK: - UICollectionViewDataSource | |
extension GenreViewController: UICollectionViewDataSource { | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return viewModel.dataSource.count | |
} | |
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
let type = viewModel.moduleType(index: indexPath.row) | |
let title = viewModel.moduleName(type: type) | |
let subViewModel = viewModel.dataSource[indexPath.row] | |
switch type { | |
case .randomMusicCell: | |
let playRandomCell = mainGenreCollectionView.dequeueReusableCell(withReuseIdentifier: PlayRandomCollectionViewCell.className, | |
for: indexPath) as! PlayRandomCollectionViewCell | |
playRandomCell.viewModel = subViewModel | |
let track: Track = subViewModel.dataSource[indexPath.row] as! Track | |
playRandomCell.fillWith(model: track) | |
return playRandomCell | |
case .playlistsCell: | |
let playlistsCell = mainGenreCollectionView.dequeueReusableCell(withReuseIdentifier: AlbumsCollectionViewCell.className, | |
for: indexPath) as! AlbumsCollectionViewCell | |
playlistsCell.titleLabel.text = NSLocalizedString(title, comment: "") | |
playlistsCell.viewModel = subViewModel | |
playlistsCell.configureGenrePlaylistView(withAllPlaylist: true) | |
playlistsCell.collectionView.reloadData() | |
return playlistsCell | |
default: | |
let albumsCell = mainGenreCollectionView.dequeueReusableCell(withReuseIdentifier: AlbumsCollectionViewCell.className, | |
for: indexPath) as! AlbumsCollectionViewCell | |
albumsCell.collectionView.backgroundColor = (indexPath.row % 2) != 0 ? #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) : #colorLiteral(red: 0.9625374675, green: 0.9625598788, blue: 0.9625478387, alpha: 1) | |
albumsCell.titleLabel.text = NSLocalizedString(title, comment: "") | |
albumsCell.viewModel = subViewModel | |
albumsCell.configureGenreAlbumView(withAllAlbums: true) | |
albumsCell.collectionView.reloadData() | |
return albumsCell | |
} | |
} | |
} | |
//MARK: - UICollectionViewDelegate | |
extension GenreViewController: UICollectionViewDelegate { | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
guard Reachability.sharedInstance.isReachable else { | |
showInternetAlert() | |
return | |
} | |
} | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { | |
let type = viewModel.moduleType(index: indexPath.row) | |
switch type { | |
case .randomMusicCell: | |
return PlayRandomCollectionViewCell.size | |
case .playlistsCell: | |
return AlbumsCollectionViewCell.playlistGanreSize | |
default: | |
return AlbumsCollectionViewCell.size | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment