-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterView.swift
More file actions
238 lines (190 loc) · 9.74 KB
/
FilterView.swift
File metadata and controls
238 lines (190 loc) · 9.74 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// FilterView.swift
// HDAugmentedRealityDemo
//
// Created by Andrey Apet on 3/7/17.
// Copyright © 2017 Danijel Huis. All rights reserved.
//
import UIKit
class FilterView: UIView, UITableViewDelegate, UITableViewDataSource, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
override init(frame: CGRect) {
super.init(frame:frame)
showFilterView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var view = UIView()
var tableView: UITableView = UITableView()
var picker: UIPickerView = UIPickerView()
let toolBar = UIToolbar()
let filterData = Filter()
var pickerData = [[String]]()
var pickerArray = [String]()
var selectedTF: UITextField?
var indexPicker = [0,0,0,0,0]
var tagPicker = 0
var nameForFilter = ""
var yearForFilter = ""
var roundForFilter = ""
var holeForFilter = ""
var sortedData = [InfoShot]()
var updateButton: UIButton = {
let button = UIButton()
button.setTitle("Select shot data to view", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 25)
button.backgroundColor = UIColor(red: 101/255, green: 156/255, blue: 53/255, alpha: 1)
return button
}()
var titleLabel: UILabel = {
let label = UILabel()
label.text = "Select Shot Data to View"
label.backgroundColor = .white
label.font = UIFont.boldSystemFont(ofSize: 24)
label.textColor = UIColor(red: 230/255, green: 76/255, blue: 60/255, alpha: 1)
label.textAlignment = .center
return label
}()
func update() {
print("Update tapped")
self.sort()
}
func sort() {
self.sortedData = self.filterData.filtering(year: self.yearForFilter, playerName: self.nameForFilter, round: self.roundForFilter, hole: self.holeForFilter)
}
func showFilterView() {
let screenBounds = UIScreen.main.bounds
print(screenBounds.height)
view = UIView(frame: CGRect(x: 0, y: screenBounds.height / 2 - 50, width: screenBounds.width, height: screenBounds.height))
view.backgroundColor = .white
titleLabel.frame = CGRect(x: 0, y: 0, width: screenBounds.width, height: 50)
view.addSubview(titleLabel)
tableView = UITableView(frame: CGRect(x: 0, y: 50, width: screenBounds.width, height: screenBounds.height / 2 - 70))
tableView.backgroundColor = .black
tableView.separatorInset = UIEdgeInsets.zero
tableView.allowsSelection = false
tableView.isScrollEnabled = false
tableView.delegate = self
tableView.dataSource = self
tableView.register(FilterCell.self, forCellReuseIdentifier: "Cell")
view.addSubview(tableView)
picker = UIPickerView(frame: CGRect(x: 0, y: 0, width: screenBounds.width, height: screenBounds.height / 2 + 10))
picker.backgroundColor = UIColor(red: 136/255, green: 192/255, blue: 87/255, alpha: 0.6)
picker.tintColor = .white
picker.showsSelectionIndicator = true
picker.delegate = self
picker.dataSource = self
toolBar.barStyle = UIBarStyle.default
toolBar.barTintColor = UIColor(red: 136/255, green: 192/255, blue: 87/255, alpha: 0.3)
toolBar.isTranslucent = false
toolBar.tintColor = .white
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(donePicker))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancelPicker))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
updateButton.frame = CGRect(x: 0, y: screenBounds.height / 2 - 20, width: screenBounds.width, height: 70)
updateButton.addTarget(self, action: #selector(update), for: UIControlEvents.touchUpInside)
//view.addSubview(updateButton)
addSubview(view)
pickerData = [filterData.tournaments, filterData.golfers, filterData.years, filterData.rounds, filterData.holes]
}
//MARK: TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FilterCell
cell.label.textColor = .black
cell.textField.inputView = picker
cell.textField.inputAccessoryView = toolBar
cell.textField.tag = indexPath.row
cell.textField.delegate = self
switch indexPath.row {
case 1:
cell.label.text = "Golfer:"
cell.textField.attributedPlaceholder = NSAttributedString(string: "Choose Golfer",attributes: [NSForegroundColorAttributeName: UIColor.lightGray])
case 2:
cell.label.text = "Year:"
cell.textField.attributedPlaceholder = NSAttributedString(string: "Choose Year",attributes: [NSForegroundColorAttributeName: UIColor.lightGray])
case 3:
cell.label.text = "Round:"
cell.textField.attributedPlaceholder = NSAttributedString(string: "Choose Round",attributes: [NSForegroundColorAttributeName: UIColor.lightGray])
case 4:
cell.label.text = "Hole:"
cell.textField.attributedPlaceholder = NSAttributedString(string: "Choose Hole",attributes: [NSForegroundColorAttributeName: UIColor.lightGray])
default:
cell.label.text = "Tournament:"
cell.textField.text = "The Players Championship"
cell.textField.font = UIFont.boldSystemFont(ofSize: 15)
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return (UIScreen.main.bounds.height / 2 - 70) / 5
}
func textFieldDidBeginEditing(_ textField: UITextField) {
pickerArray = pickerData[textField.tag]
(textField.inputView as! UIPickerView).reloadAllComponents()
selectedTF = textField
tagPicker = textField.tag
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField.tag == 1 {
self.selectedTF?.text = pickerArray[indexPicker[tagPicker]]
self.nameForFilter = (self.selectedTF?.text)!
// self.updateButton.setTitle("\(String(self.filterData.filtering(year: self.yearForFilter, playerName: self.nameForFilter, round: self.roundForFilter, hole: self.holeForFilter).count)) POI have for Show", for: .normal)
} else if textField.tag == 2 {
self.yearForFilter = (self.selectedTF?.text)!
// self.updateButton.setTitle("\(String(self.filterData.filtering(year: self.yearForFilter, playerName: self.nameForFilter, round: self.roundForFilter, hole: self.holeForFilter).count)) POI have for Show", for: .normal)
} else if textField.tag == 3 {
if self.selectedTF?.text != "" {
self.roundForFilter = "0\((self.selectedTF?.text)!)"
}
// self.updateButton.setTitle("\(String(self.filterData.filtering(year: self.yearForFilter, playerName: self.nameForFilter, round: self.roundForFilter, hole: self.holeForFilter).count)) POI have for Show", for: .normal)
} else if textField.tag == 4 {
if self.selectedTF?.text != "" {
if Double((self.selectedTF?.text)!)! < 10.0 {
self.holeForFilter = "0\((self.selectedTF?.text)!)"
} else if Double((self.selectedTF?.text)!)! > 10.0 {
self.holeForFilter = (self.selectedTF?.text!)!
}
}
// self.updateButton.setTitle("\(String(self.filterData.filtering(year: self.yearForFilter, playerName: self.nameForFilter, round: self.roundForFilter, hole: self.holeForFilter).count)) POI have for Show", for: .normal)
}
// uncomment if need to show coount of sorted data on update button
}
//MARK: PickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerArray[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if selectedTF != nil {
selectedTF!.text = pickerArray[row]
}
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = pickerArray[row]
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20), NSForegroundColorAttributeName:UIColor.white])
return myTitle
}
func donePicker(sender:UIBarButtonItem) {
tableView.reloadData()
self.picker.resignFirstResponder()
}
func cancelPicker(sender:UIBarButtonItem) {
if selectedTF != nil {
selectedTF!.text = ""
}
tableView.reloadData()
self.picker.resignFirstResponder()
}
}