Skip to content

Instantly share code, notes, and snippets.

@pnalvarez
Created March 29, 2020 19:33
Show Gist options
  • Save pnalvarez/e143008bd7ed34ae413904362302d315 to your computer and use it in GitHub Desktop.
Save pnalvarez/e143008bd7ed34ae413904362302d315 to your computer and use it in GitHub Desktop.
class PokemonListViewController: UIViewController {
@IBOutlet weak var sortSegmentedControl: UISegmentedControl!
@IBOutlet weak var tableView: UITableView!
var presenter: PokemonListPresenterInput?
override func viewDidLoad() {
super.viewDidLoad()
setUpTableView()
setUpNavigation()
presenter?.viewDidLoad()
}
@IBAction func sortIndexChanged(_ sender: Any) {
presenter?.sortBy(selectedIndex: sortSegmentedControl.selectedSegmentIndex)
}
}
extension PokemonListViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return presenter?.pokemonCount ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: PokemonTableViewCell.defaultReuseIdentifier, for: indexPath) as! PokemonTableViewCell
cell.display = presenter?.getPokemon(at: indexPath.row)
cell.setUp()
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
presenter?.didSelectCell(at: indexPath.row)
}
}
extension PokemonListViewController: PokemonListPresenterOutput {
func updateUIList() {
DispatchQueue.main.async {
self.view.stopActivity()
self.tableView.reloadData()
}
}
func updateUISortOtions() {
DispatchQueue.main.async {
self.setUpSegmentedControl()
}
}
func presentLoading() {
DispatchQueue.main.async {
self.view.presentActivity()
}
}
}
extension PokemonListViewController {
private func setUpSegmentedControl() {
sortSegmentedControl.removeAllSegments()
guard let sortOptions = presenter?.sortOptionsModel else { return }
for option in sortOptions {
guard let index = sortOptions.firstIndex(of: option) else { return }
sortSegmentedControl.insertSegment(withTitle: option, at: index, animated: true)
}
sortSegmentedControl.selectedSegmentIndex = 1
}
private func setUpTableView() {
tableView.registerNib(nibName: PokemonTableViewCell.defaultReuseIdentifier, bundle: nil)
tableView.assignTo(delegate: self, dataSource: self)
}
private func setUpNavigation() {
title = presenter?.title
navigationController?.navigationBar.backgroundColor = .primary
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(self.refresh))
}
private func setUpSelectedDefault() {
sortSegmentedControl.selectedSegmentIndex = 1
}
@objc private func refresh() {
presenter?.viewDidLoad()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment