| 1 |
//
|
|
| 2 |
// CharactersViewController.swift
|
|
| 3 |
// Marvel
|
|
| 4 |
//
|
|
| 5 |
// Created by Thiago Lioy on 14/11/16.
|
|
| 6 |
// Copyright �� 2016 Thiago Lioy. All rights reserved.
|
|
| 7 |
//
|
|
| 8 |
|
|
| 9 |
import UIKit
|
|
| 10 |
|
|
| 11 |
protocol CharactersDelegate {
|
|
| 12 |
func didSelectCharacter(at index: IndexPath)
|
|
| 13 |
}
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
final class CharactersViewController: UIViewController {
|
|
| 17 |
var apiManager: MarvelAPICalls = MarvelAPIManager()
|
|
| 18 |
|
|
| 19 |
var tableDatasource: CharactersDatasource?
|
|
| 20 |
var tableDelegate: CharactersTableDelegate?
|
|
| 21 |
|
|
| 22 |
var collectionDatasource: CharactersCollectionDatasource?
|
|
| 23 |
var collectionDelegate: CharactersCollectionDelegate?
|
|
| 24 |
|
|
| 25 |
var characters: [Character] = []
|
|
| 26 |
|
|
| 27 |
@IBOutlet weak var searchBar: UISearchBar!
|
|
| 28 |
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
|
|
| 29 |
@IBOutlet weak var tableView: UITableView!
|
|
| 30 |
@IBOutlet weak var collectionView: UICollectionView!
|
|
| 31 |
|
|
| 32 |
}
|
|
| 33 |
|
|
| 34 |
extension CharactersViewController {
|
|
| 35 |
override func viewDidLoad() {
|
29x |
| 36 |
super.viewDidLoad()
|
29x |
| 37 |
setupSearchBar()
|
29x |
| 38 |
fetchCharacters()
|
29x |
| 39 |
}
|
29x |
| 40 |
}
|
|
| 41 |
|
|
| 42 |
extension CharactersViewController {
|
|
| 43 |
func fetchCharacters(for query: String? = nil) {
|
30x |
| 44 |
tableView.isHidden = true
|
30x |
| 45 |
collectionView.isHidden = true
|
30x |
| 46 |
activityIndicator.startAnimating()
|
30x |
| 47 |
apiManager.characters(query: query) { characters in
|
29x |
| 48 |
self.activityIndicator.stopAnimating()
|
29x |
| 49 |
if let characters = characters {
|
29x |
| 50 |
self.setupTableView(with: characters)
|
29x |
| 51 |
}
|
29x |
| 52 |
}
|
29x |
| 53 |
}
|
30x |
| 54 |
|
|
| 55 |
func setupSearchBar() {
|
29x |
| 56 |
self.searchBar.delegate = self
|
29x |
| 57 |
}
|
29x |
| 58 |
|
|
| 59 |
func setupTableView(with characters: [Character]) {
|
30x |
| 60 |
self.characters = characters
|
30x |
| 61 |
tableView.isHidden = false
|
30x |
| 62 |
collectionView.isHidden = true
|
30x |
| 63 |
tableDelegate = CharactersTableDelegate(self)
|
30x |
| 64 |
tableDatasource = CharactersDatasource(items: characters, tableView: self.tableView, delegate: tableDelegate!)
|
30x |
| 65 |
}
|
30x |
| 66 |
|
|
| 67 |
func setupCollectionView(with characters: [Character]) {
|
8x |
| 68 |
self.characters = characters
|
8x |
| 69 |
collectionView.isHidden = false
|
8x |
| 70 |
tableView.isHidden = true
|
8x |
| 71 |
collectionDelegate = CharactersCollectionDelegate(self)
|
8x |
| 72 |
collectionDatasource = CharactersCollectionDatasource(items: characters, collectionView: self.collectionView, delegate: collectionDelegate!)
|
8x |
| 73 |
}
|
8x |
| 74 |
}
|
|
| 75 |
|
|
| 76 |
extension CharactersViewController {
|
|
| 77 |
@IBAction func showAsGrid(_ sender: UIButton) {
|
8x |
| 78 |
setupCollectionView(with: characters)
|
8x |
| 79 |
}
|
8x |
| 80 |
|
|
| 81 |
@IBAction func showAsTable(_ sender: UIButton) {
|
1x |
| 82 |
setupTableView(with: characters)
|
1x |
| 83 |
}
|
1x |
| 84 |
}
|
|
| 85 |
|
|
| 86 |
extension CharactersViewController: CharactersDelegate {
|
|
| 87 |
func didSelectCharacter(at index: IndexPath) {
|
1x |
| 88 |
searchBar.resignFirstResponder()
|
1x |
| 89 |
guard let nextController = Storyboard.Main.characterViewControllerScene
|
1x |
| 90 |
.viewController() as? CharacterViewController else {
|
! |
| 91 |
return
|
! |
| 92 |
}
|
1x |
| 93 |
|
1x |
| 94 |
let character = characters[index.row]
|
1x |
| 95 |
nextController.character = character
|
1x |
| 96 |
self.navigationController?.pushViewController(nextController, animated: true)
|
1x |
| 97 |
}
|
1x |
| 98 |
}
|
|
| 99 |
|
|
| 100 |
extension CharactersViewController: UISearchBarDelegate {
|
|
| 101 |
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
|
2x |
| 102 |
searchBar.resignFirstResponder()
|
2x |
| 103 |
let query = searchBar.text ?? ""
|
! |
| 104 |
if !query.isEmpty {
|
1x |
| 105 |
fetchCharacters(for: query)
|
1x |
| 106 |
}
|
2x |
| 107 |
}
|
2x |
| 108 |
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
|
1x |
| 109 |
searchBar.resignFirstResponder()
|
1x |
| 110 |
}
|
1x |
| 111 |
}
|
|
| 112 |
|
|