/
ViewController.swift
170 lines (123 loc) · 5.16 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// ViewController.swift
// pokeDex
//
// Created by Ambar Maqsood on 2016-09-20.
// Copyright © 2016 Ambar Maqsood. All rights reserved.
//
import UIKit
import AVFoundation
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UISearchBarDelegate {
@IBOutlet weak var collection: UICollectionView!
@IBOutlet weak var searchBar: UISearchBar!
var pokemon = [Pokemon]()
var filteredPokemon = [Pokemon]()
var musicPlayer: AVAudioPlayer!
var inSearchMode = false
override func viewDidLoad() {
super.viewDidLoad()
collection.dataSource = self
collection.delegate = self
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done //Searchbar dissapears when you hit done
parsePokemonCSV()
initAudio()
}
func initAudio() {
let path = Bundle.main.path(forResource: "music", ofType: "mp3")
do {
musicPlayer = try AVAudioPlayer(contentsOf: URL(string: path!)!)
musicPlayer.prepareToPlay()
musicPlayer.numberOfLoops = -1 //continous
musicPlayer.play()
}catch let err as NSError {
print(err.debugDescription)
}
}
func parsePokemonCSV() {
let path = Bundle.main.path(forResource: "pokemon", ofType: "csv")!
do {
let csv = try CSV(contentsOfURL: path)
let rows = csv.rows
print(rows)
for row in rows {
let pokeID = Int(row["id"]!)!
let name = row["identifier"]!
let poke = Pokemon(name: name, pokeDexID: pokeID)
pokemon.append(poke)
}
}catch let err as NSError {
print(err.debugDescription)
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PokeCell", for: indexPath) as? PokeCell {
let poke: Pokemon!
if inSearchMode {
poke = filteredPokemon[indexPath.row]
cell.configureCell(poke)
}else {
poke = pokemon[indexPath.row]
cell.configureCell(poke)
}
return cell
}else {
return UICollectionViewCell()
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
var poke: Pokemon!
if inSearchMode {
poke = filteredPokemon[indexPath.row]
}else {
poke = pokemon[indexPath.row]
}
performSegue(withIdentifier: "PokemonDetailVC", sender: poke)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if inSearchMode {
return filteredPokemon.count
}
return pokemon.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 105, height: 105)
}
@IBAction func musicBtnPressed(_ sender: UIButton) {
//Since sender is the button we dont have to create an IBOutlet and add the alpha there, we can directly add it here as the line sender.alpha
if musicPlayer.isPlaying {
musicPlayer.pause()
sender.alpha = 0.2
}else {
musicPlayer.play()
sender.alpha = 1.0
}
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
view.endEditing(true)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
inSearchMode = false
collection.reloadData() //If its empty or nil we stick or go back to the original data
view.endEditing(true) //makes the keyboard disappear
}else {
inSearchMode = true
let lower = searchBar.text!.lowercased()
filteredPokemon = pokemon.filter({$0.name.range(of: lower) != nil}) //$0 placeholder for each item in array and compares it the search bar contents and then adds it to the new array we created filteredPokemon
collection.reloadData() //collection cell re-populates with the new data
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "PokemonDetailVC" {
if let detailsVC = segue.destination as? PokemonDetailVC {
if let poke = sender as? Pokemon {
detailsVC.pokemon = poke
}
}
}
}
}