-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainViewController.swift
195 lines (173 loc) · 7.12 KB
/
MainViewController.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/// Copyright (c) 2020 Razeware LLC
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// Notwithstanding the foregoing, you may not use, copy, modify, merge, publish,
/// distribute, sublicense, create a derivative work, and/or sell copies of the
/// Software in any work that is designed, intended, or marketed for pedagogical or
/// instructional purposes related to programming, coding, application development,
/// or information technology. Permission for such use, copying, modification,
/// merger, publication, distribution, sublicensing, creation of derivative works,
/// or sale is expressly withheld.
///
/// This project and source code may use libraries or frameworks that are
/// released under various Open-Source licenses. Use of those libraries and
/// frameworks are governed by their own individual licenses.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
import UIKit
class MainViewController: UITableViewController {
var countries = Country.countries()
var searchController: UISearchController!
var resultsTableViewController: ResultsTableViewController!
var searchContinents: [String] {
// 1
let tokens = searchController.searchBar.searchTextField.tokens
// 2
return tokens.compactMap {
($0.representedObject as? Continent)?.description
}
}
var isSearchingByTokens: Bool {
return
searchController.isActive &&
searchController.searchBar.searchTextField.tokens.count > 0
}
override func viewDidLoad() {
super.viewDidLoad()
resultsTableViewController = storyboard!.instantiateViewController(withIdentifier: "resultsViewController") as? ResultsTableViewController
resultsTableViewController.delegate = self
searchController = UISearchController(searchResultsController: resultsTableViewController)
navigationItem.searchController = searchController
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Find a country"
searchController.searchBar.scopeButtonTitles = Year.allCases.map { $0.description }
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
searchController.automaticallyShowsScopeBar = false
searchController.searchBar.searchTextField.textColor = .rwGreen()
searchController.searchBar.searchTextField.tokenBackgroundColor = .rwGreen()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return Continent.allCases.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return Continent.allCases[section].description
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let continentForSection = Continent.allCases[section]
return countries[continentForSection]?.count ?? 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "country", for: indexPath) as! CountryCell
let continentForRow = Continent.allCases[indexPath.section]
cell.country = countries[continentForRow]?[indexPath.row]
return cell
}
}
// MARK: -
extension MainViewController {
func searchFor(_ searchText: String?) {
// 1
guard searchController.isActive else { return }
// 2
guard let searchText = searchText else {
resultsTableViewController.countries = nil
return
}
// 3
let selectedYear = selectedScopeYear()
let allCountries = countries.values.joined()
let filteredCountries = allCountries.filter { (country: Country) -> Bool in
// 4
let isMatchingYear = selectedYear == Year.all.description ?
true : (country.year.description == selectedYear)
// 5
let isMatchingTokens = searchContinents.count == 0 ?
true : searchContinents.contains(country.continent.description)
// 6
if !searchText.isEmpty {
return
isMatchingYear &&
isMatchingTokens &&
country.name.lowercased().contains(searchText.lowercased())
// 7
} else if isSearchingByTokens {
return isMatchingYear && isMatchingTokens
}
// 8
return false
}
// 9
resultsTableViewController.countries =
filteredCountries.count > 0 ? filteredCountries : nil
}
func selectedScopeYear() -> String {
guard let scopeButtonTitles = searchController.searchBar.scopeButtonTitles else {
return Year.all.description
}
return scopeButtonTitles[searchController.searchBar.selectedScopeButtonIndex]
}
func showScopeBar(_ show: Bool) {
guard searchController.searchBar.showsScopeBar != show else { return }
searchController.searchBar.setShowsScope(show, animated: true)
view.setNeedsLayout()
}
}
// MARK: -
extension MainViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
guard let searchText = searchController.searchBar.text else { return }
searchFor(searchText)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchFor(searchText)
let showScope = !searchText.isEmpty
showScopeBar(showScope)
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
resultsTableViewController.countries = nil
showScopeBar(false)
searchController.searchBar.searchTextField.backgroundColor = nil
}
}
// MARK: -
extension MainViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
// 1
if searchController.searchBar.searchTextField.isFirstResponder {
searchController.showsSearchResultsController = true
// 2
searchController.searchBar
.searchTextField.backgroundColor = UIColor.rwGreen().withAlphaComponent(0.1)
} else {
// 3
searchController.searchBar.searchTextField.backgroundColor = nil
}
}
}
extension MainViewController: ResultsTableViewDelegate {
func didSelect(token: UISearchToken) {
// 1
let searchTextField = searchController.searchBar.searchTextField
// 2
searchTextField.insertToken(token, at: searchTextField.tokens.count)
// 3
searchFor(searchController.searchBar.text)
showScopeBar(true)
}
}