/
ModernSearchBar.swift
509 lines (403 loc) · 18.9 KB
/
ModernSearchBar.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//
// SearchBarCompletion.swift
// SearchBarCompletion
//
// Created by Philippe on 03/03/2017.
// Copyright © 2017 CookMinute. All rights reserved.
//
import UIKit
import Foundation
public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate {
public enum Choice {
case normal
case withUrl
}
//MARK: DATAS
private var isSuggestionsViewOpened: Bool!
private var suggestionList: Array<String> = Array<String>()
private var suggestionListFiltred: Array<String> = Array<String>()
private var suggestionListWithUrl: Array<ModernSearchBarModel> = Array<ModernSearchBarModel>()
private var suggestionListWithUrlFiltred: Array<ModernSearchBarModel> = Array<ModernSearchBarModel>()
private var choice: Choice = .normal
private var keyboardHeight: CGFloat = 0
//MARKS: VIEWS
private var suggestionsView: UITableView!
private var suggestionsShadow: UIView!
//MARK: DELEGATE
public var delegateModernSearchBar: ModernSearchBarDelegate?
//PUBLICS OPTIONS
public var shadowView_alpha: CGFloat = 0.3
public var searchImage: UIImage! = ModernSearchBarIcon.Icon.search.image
public var searchLabel_font: UIFont?
public var searchLabel_textColor: UIColor?
public var searchLabel_backgroundColor: UIColor?
public var suggestionsView_maxHeight: CGFloat!
public var suggestionsView_backgroundColor: UIColor?
public var suggestionsView_contentViewColor: UIColor?
public var suggestionsView_separatorStyle: UITableViewCellSeparatorStyle = .none
public var suggestionsView_selectionStyle: UITableViewCellSelectionStyle = UITableViewCellSelectionStyle.none
public var suggestionsView_verticalSpaceWithSearchBar: CGFloat = 3
public var suggestionsView_searchIcon_height: CGFloat = 17
public var suggestionsView_searchIcon_width: CGFloat = 17
public var suggestionsView_searchIcon_isRound = true
public var suggestionsView_spaceWithKeyboard:CGFloat = 3
//MARK: INITIALISERS
required public init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
setup()
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
init () {
super.init(frame: CGRect.zero)
setup()
}
private func setup(){
self.delegate = self
self.isSuggestionsViewOpened = false
self.interceptOrientationChange()
self.interceptKeyboardChange()
}
private func configureViews(){
///Configure suggestionsView (TableView)
self.suggestionsView = UITableView(frame: CGRect(x: getSuggestionsViewX(), y: getSuggestionsViewY(), width: getSuggestionsViewWidth(), height: 0))
self.suggestionsView.delegate = self
self.suggestionsView.dataSource = self
self.suggestionsView.register(ModernSearchBarCell.self, forCellReuseIdentifier: "cell")
self.suggestionsView.rowHeight = UITableViewAutomaticDimension
self.suggestionsView.estimatedRowHeight = 100
self.suggestionsView.separatorStyle = self.suggestionsView_separatorStyle
if let backgroundColor = suggestionsView_backgroundColor { self.suggestionsView.backgroundColor = backgroundColor }
///Configure the suggestions shadow (Behing the TableView)
self.suggestionsShadow = UIView(frame: CGRect(x: getShadowX(), y: getShadowY(), width: getShadowWidth(), height: getShadowHeight()))
self.suggestionsShadow.backgroundColor = UIColor.black.withAlphaComponent(self.shadowView_alpha)
///Configure the gesture to handle click on shadow and improve focus on searchbar
let gestureShadow = UITapGestureRecognizer(target: self, action: #selector (self.onClickShadowView (_:)))
self.suggestionsShadow.addGestureRecognizer(gestureShadow)
let gestureRemoveFocus = UITapGestureRecognizer(target: self, action: #selector (self.removeFocus (_:)))
gestureRemoveFocus.cancelsTouchesInView = false
self.getTopViewController()?.view.addGestureRecognizer(gestureRemoveFocus)
///Reload datas of suggestionsView
self.suggestionsView.reloadData()
}
// --------------------------------
// SET DATAS
// --------------------------------
public func setDatas(datas: Array<String>){
if (!self.suggestionListWithUrl.isEmpty) { fatalError("You have already filled 'suggestionListWithUrl' ! You can fill only one list. ") }
self.suggestionList = datas
self.choice = .normal
}
public func setDatasWithUrl(datas: Array<ModernSearchBarModel>){
if (!self.suggestionList.isEmpty) { fatalError("You have already filled 'suggestionList' ! You can fill only one list. ") }
self.suggestionListWithUrl = datas
self.choice = .withUrl
}
// --------------------------------
// DELEGATE METHODS SEARCH BAR
// --------------------------------
public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.searchWhenUserTyping(caracters: searchText)
self.delegateModernSearchBar?.searchBar?(searchBar, textDidChange: searchText)
}
public func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
if self.suggestionsView == nil { self.configureViews() }
self.delegateModernSearchBar?.searchBarTextDidEndEditing?(searchBar)
}
public func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.closeSuggestionsView()
self.delegateModernSearchBar?.searchBarTextDidEndEditing?(searchBar)
}
public func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.closeSuggestionsView()
self.delegateModernSearchBar?.searchBarSearchButtonClicked?(searchBar)
self.endEditing(true)
}
public func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.closeSuggestionsView()
self.delegateModernSearchBar?.searchBarCancelButtonClicked?(searchBar)
self.endEditing(true)
}
public func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
if let shouldEndEditing = self.delegateModernSearchBar?.searchBarShouldEndEditing?(searchBar) {
return shouldEndEditing
} else {
return true
}
}
public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
if let shouldBeginEditing = self.delegateModernSearchBar?.searchBarShouldBeginEditing?(searchBar) {
return shouldBeginEditing
} else {
return true
}
}
// --------------------------------
// ACTIONS
// --------------------------------
///Handle click on shadow view
func onClickShadowView(_ sender:UITapGestureRecognizer){
self.delegateModernSearchBar?.onClickShadowView?(shadowView: self.suggestionsShadow)
self.closeSuggestionsView()
}
///Remove focus when you tap outside the searchbar
func removeFocus(_ sender:UITapGestureRecognizer){
if (!isSuggestionsViewOpened){ self.endEditing(true) }
}
// --------------------------------
// DELEGATE METHODS TABLE VIEW
// --------------------------------
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch self.choice {
case .normal:
return self.suggestionListFiltred.count
case .withUrl:
return self.suggestionListWithUrlFiltred.count
}
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:ModernSearchBarCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ModernSearchBarCell
var title = ""
switch self.choice {
case .normal:
title = self.suggestionListFiltred[indexPath.row]
break
case .withUrl:
title = self.suggestionListWithUrlFiltred[indexPath.row].title
break
}
///Configure label
cell.labelModelSearchBar.text = title
if let font = self.searchLabel_font { cell.labelModelSearchBar.font = font }
if let textColor = self.searchLabel_textColor { cell.labelModelSearchBar.textColor = textColor }
if let backgroundColor = self.searchLabel_backgroundColor { cell.labelModelSearchBar.backgroundColor = backgroundColor }
///Configure content
if let contentColor = suggestionsView_contentViewColor { cell.contentView.backgroundColor = contentColor }
cell.selectionStyle = self.suggestionsView_selectionStyle
///Configure Image
cell.configureImage(choice: self.choice, searchImage: self.searchImage, suggestionsListWithUrl: self.suggestionListWithUrlFiltred, position: indexPath.row, isImageRound: self.suggestionsView_searchIcon_isRound, heightImage: self.suggestionsView_searchIcon_height)
//self.fetchImageFromUrl(model: self.suggestionListWithUrlFiltred[indexPath.row], cell: cell, indexPath: indexPath)
///Configure max width label size
cell.configureWidthMaxLabel(suggestionsViewWidth: self.suggestionsView.frame.width, searchImageWidth: self.suggestionsView_searchIcon_width)
///Configure Constraints
cell.configureConstraints(heightImage: self.suggestionsView_searchIcon_height, widthImage: self.suggestionsView_searchIcon_width)
return cell
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch self.choice {
case .normal:
self.delegateModernSearchBar?.onClickItemSuggestionsView?(item: self.suggestionListFiltred[indexPath.row])
case .withUrl:
self.delegateModernSearchBar?.onClickItemWithUrlSuggestionsView?(item: self.suggestionListWithUrlFiltred[indexPath.row])
}
}
// --------------------------------
// SEARCH FUNCTION
// --------------------------------
///Main function that is called when user searching
private func searchWhenUserTyping(caracters: String){
switch self.choice {
///Case normal (List of string)
case .normal:
var suggestionListFiltredTmp = Array<String>()
DispatchQueue.global(qos: .background).async {
for item in self.suggestionList {
if (self.researchCaracters(stringSearched: caracters, stringQueried: item)){
suggestionListFiltredTmp.append(item)
}
}
DispatchQueue.main.async {
self.suggestionListFiltred.removeAll()
self.suggestionListFiltred.append(contentsOf: suggestionListFiltredTmp)
self.updateAfterSearch(caracters: caracters)
}
}
break
///Case with URL (List of ModernSearchBarModel)
case .withUrl:
var suggestionListFiltredWithUrlTmp = Array<ModernSearchBarModel>()
DispatchQueue.global(qos: .background).async {
for item in self.suggestionListWithUrl {
if (self.researchCaracters(stringSearched: caracters, stringQueried: item.title)){
suggestionListFiltredWithUrlTmp.append(item)
}
}
DispatchQueue.main.async {
self.suggestionListWithUrlFiltred.removeAll()
self.suggestionListWithUrlFiltred.append(contentsOf: suggestionListFiltredWithUrlTmp)
self.updateAfterSearch(caracters: caracters)
}
}
break
}
}
private func researchCaracters(stringSearched: String, stringQueried: String) -> Bool {
return ((stringQueried.range(of: stringSearched, options: String.CompareOptions.caseInsensitive, range: nil, locale: nil)) != nil)
}
private func updateAfterSearch(caracters: String){
self.suggestionsView.reloadData()
self.updateSizeSuggestionsView()
caracters.isEmpty ? self.closeSuggestionsView() : self.openSuggestionsView()
}
// --------------------------------
// SUGGESTIONS VIEW UTILS
// --------------------------------
private func haveToOpenSuggestionView() -> Bool {
switch self.choice {
case .normal:
return !self.suggestionListFiltred.isEmpty
case .withUrl:
return !self.suggestionListWithUrlFiltred.isEmpty
}
}
private func openSuggestionsView(){
if (self.haveToOpenSuggestionView()){
if (!self.isSuggestionsViewOpened){
self.animationOpening()
self.addViewToParent(view: self.suggestionsShadow)
self.addViewToParent(view: self.suggestionsView)
self.isSuggestionsViewOpened = true
}
}
}
private func closeSuggestionsView(){
if (self.isSuggestionsViewOpened == true){
self.animationClosing()
self.isSuggestionsViewOpened = false
}
}
private func animationOpening(){
UIView.animate(withDuration: 0.3, delay: 0.0, options: [], animations: {
self.suggestionsView.alpha = 1.0
self.suggestionsShadow.alpha = 1.0
}, completion: nil)
}
private func animationClosing(){
UIView.animate(withDuration: 0.3, delay: 0.0, options: [], animations: {
self.suggestionsView.alpha = 0.0
self.suggestionsShadow.alpha = 0.0
}, completion: nil)
}
// --------------------------------
// VIEW UTILS
// --------------------------------
private func getSuggestionsViewX() -> CGFloat {
return self.getGlobalPointEditText().x
}
private func getSuggestionsViewY() -> CGFloat {
return self.getShadowY().subtracting(self.suggestionsView_verticalSpaceWithSearchBar)
}
private func getSuggestionsViewWidth() -> CGFloat {
return self.getEditText().frame.width
}
private func getSuggestionsViewHeight() -> CGFloat {
return self.getEditText().frame.height
}
private func getShadowX() -> CGFloat {
return 0
}
private func getShadowY() -> CGFloat {
return self.getGlobalPointEditText().y.adding(self.getEditText().frame.height)
}
private func getShadowHeight() -> CGFloat {
return (self.getTopViewController()?.view.frame.height)!
}
private func getShadowWidth() -> CGFloat {
return (self.getTopViewController()?.view.frame.width)!
}
private func updateSizeSuggestionsView(){
var frame: CGRect = self.suggestionsView.frame
frame.size.height = self.getExactMaxHeightSuggestionsView(newHeight: self.suggestionsView.contentSize.height)
UIView.animate(withDuration: 0.3) {
self.suggestionsView.frame = frame
self.suggestionsView.layoutIfNeeded()
self.suggestionsView.sizeToFit()
}
}
private func getExactMaxHeightSuggestionsView(newHeight: CGFloat) -> CGFloat {
var estimatedMaxView: CGFloat!
if self.suggestionsView_maxHeight != nil {
estimatedMaxView = self.suggestionsView_maxHeight
} else {
estimatedMaxView = self.getEstimateHeightSuggestionsView()
}
if (newHeight > estimatedMaxView) {
return estimatedMaxView
} else {
return newHeight
}
}
private func getEstimateHeightSuggestionsView() -> CGFloat {
return self.getViewTopController().frame.height
.subtracting(self.getShadowY())
.subtracting(self.keyboardHeight)
.subtracting(self.suggestionsView_spaceWithKeyboard)
}
// --------------------------------
// UTILS
// --------------------------------
private func addViewToParent(view: UIView){
if let topController = getTopViewController() {
let superView: UIView = topController.view
superView.addSubview(view)
}
}
private func getViewTopController() -> UIView{
return self.getTopViewController()!.view
}
private func getTopViewController() -> UIViewController? {
var topController: UIViewController? = UIApplication.shared.keyWindow?.rootViewController
while topController?.presentedViewController != nil {
topController = topController?.presentedViewController
}
return topController
}
private func getEditText() -> UITextField {
return self.value(forKey: "searchField") as! UITextField
}
private func getText() -> String{
if let text = self.getEditText().text {
return text
} else {
return ""
}
}
private func getGlobalPointEditText() -> CGPoint {
return self.getEditText().superview!.convert(self.getEditText().frame.origin, to: nil)
}
// --------------------------------
// OBSERVERS CHANGES
// --------------------------------
private func interceptOrientationChange(){
self.getEditText().addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
}
override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == "frame"){
self.closeSuggestionsView()
self.configureViews()
}
}
private func interceptKeyboardChange(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc private func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo as! [String: NSObject] as NSDictionary
let keyboardFrame = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! CGRect
let keyboardHeight = keyboardFrame.height
self.keyboardHeight = keyboardHeight
self.updateSizeSuggestionsView()
}
@objc private func keyboardWillHide(notification: NSNotification) {
self.keyboardHeight = 0
self.updateSizeSuggestionsView()
}
// --------------------------------
// PUBLIC ACCESS
// --------------------------------
public func getSuggestionsView() -> UITableView {
return self.suggestionsView
}
}