This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
/
SearchViewController.swift
759 lines (635 loc) · 27.9 KB
/
SearchViewController.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import UIKit
import Shared
import Storage
import Preferences
import Data
import Favicon
// MARK: - SearchViewControllerDelegate
protocol SearchViewControllerDelegate: AnyObject {
func searchViewController(_ searchViewController: SearchViewController, didSubmit query: String, braveSearchPromotion: Bool)
func searchViewController(_ searchViewController: SearchViewController, didSelectURL url: URL)
func searchViewController(_ searchViewController: SearchViewController, didSelectOpenTab tabInfo: (id: UUID?, url: URL))
func searchViewController(_ searchViewController: SearchViewController, didLongPressSuggestion suggestion: String)
func presentSearchSettingsController()
func searchViewController(_ searchViewController: SearchViewController, didHighlightText text: String, search: Bool)
func searchViewController(_ searchViewController: SearchViewController, shouldFindInPage query: String)
func searchViewControllerAllowFindInPage() -> Bool
}
// MARK: - SearchViewController
public class SearchViewController: SiteTableViewController, LoaderListener {
// MARK: SearchViewControllerUX
private struct SearchViewControllerUX {
static let engineButtonHeight: Float = 44
static let engineButtonWidth = engineButtonHeight * 1.4
static let searchImageWidth: Float = 24
static let searchButtonMargin: CGFloat = 8
static let faviconSize: CGFloat = 29
static let iconBorderColor = UIColor(white: 0, alpha: 0.1)
static let iconBorderWidth: CGFloat = 0.5
}
// MARK: Properties
// Views for displaying the bottom scrollable search engine list. searchEngineScrollView is the
// scrollable container; searchEngineScrollViewContent contains the actual set of search engine buttons.
private let searchEngineScrollView = ButtonScrollView().then { scrollView in
scrollView.decelerationRate = .fast
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
scrollView.clipsToBounds = false
let border = UIView.separatorLine
scrollView.addSubview(border)
border.snp.makeConstraints {
$0.bottom.equalTo(scrollView.snp.top)
$0.leading.trailing.equalTo(scrollView.frameLayoutGuide)
}
}
private let backgroundView = UIVisualEffectView(effect: UIBlurEffect(style: .systemUltraThinMaterial))
private let searchEngineScrollViewContent = UIView()
private lazy var suggestionLongPressGesture = UILongPressGestureRecognizer(
target: self,
action: #selector(onSuggestionLongPressed(_:)))
var searchDelegate: SearchViewControllerDelegate?
var isUsingBottomBar: Bool = false {
didSet {
layoutSearchEngineScrollView()
}
}
let dataSource: SearchSuggestionDataSource
public static var userAgent: String?
private var browserColors: any BrowserColors
// MARK: Lifecycle
init(with dataSource: SearchSuggestionDataSource, browserColors: some BrowserColors) {
self.dataSource = dataSource
self.browserColors = browserColors
super.init(nibName: nil, bundle: nil)
dataSource.delegate = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
NotificationCenter.default.removeObserver(self, name: .dynamicFontChanged, object: nil)
}
public override func viewSafeAreaInsetsDidChange() {
super.viewSafeAreaInsetsDidChange()
layoutSearchEngineScrollView()
}
override public func viewDidLoad() {
super.viewDidLoad()
view.insertSubview(backgroundView, belowSubview: tableView)
backgroundView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
// Have to apply a custom alpha here because UIVisualEffectView blurs come with their own tint
backgroundView.contentView.backgroundColor = browserColors.containerFrostedGlass.withAlphaComponent(0.8)
searchEngineScrollView.backgroundColor = browserColors.containerBackground
setupSearchEngineScrollViewIfNeeded()
KeyboardHelper.defaultHelper.addDelegate(self)
tableView.do {
$0.keyboardDismissMode = .interactive
$0.separatorStyle = .none
$0.sectionHeaderTopPadding = 5
$0.backgroundColor = .clear
$0.addGestureRecognizer(suggestionLongPressGesture)
$0.register(SearchSuggestionPromptCell.self, forCellReuseIdentifier: SearchSuggestionPromptCell.identifier)
$0.register(SuggestionCell.self, forCellReuseIdentifier: SuggestionCell.identifier)
$0.register(BraveSearchPromotionCell.self, forCellReuseIdentifier: BraveSearchPromotionCell.identifier)
$0.register(UITableViewCell.self, forCellReuseIdentifier: "default")
}
NotificationCenter.default.addObserver(self, selector: #selector(dynamicFontChanged), name: .dynamicFontChanged, object: nil)
}
override public func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
reloadSearchEngines()
reloadData()
}
override public func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// The height of the suggestions row may change, so call reloadData() to recalculate cell heights.
coordinator.animate(
alongsideTransition: { _ in
self.tableView.reloadData()
}, completion: nil)
}
private func animateSearchEnginesWithKeyboard(_ keyboardState: KeyboardState) {
layoutSearchEngineScrollView()
UIViewPropertyAnimator(duration: keyboardState.animationDuration, curve: keyboardState.animationCurve) {
self.view.layoutIfNeeded()
}
.startAnimation()
}
@objc func dynamicFontChanged(_ notification: Notification) {
if notification.name == .dynamicFontChanged {
reloadData()
}
}
// MARK: Internal
private func setupSearchEngineScrollViewIfNeeded() {
if !dataSource.hasQuickSearchEngines { return }
view.addSubview(searchEngineScrollView)
searchEngineScrollView.addSubview(searchEngineScrollViewContent)
layoutSearchEngineScrollView()
layoutTable()
searchEngineScrollViewContent.snp.makeConstraints { make in
make.center.equalTo(searchEngineScrollView).priority(.low)
// Left-align the engines on iphones, center on ipad
if UIScreen.main.traitCollection.horizontalSizeClass == .compact {
make.leading.equalTo(searchEngineScrollView).priority(.required)
} else {
make.leading.greaterThanOrEqualTo(searchEngineScrollView).priority(.required)
}
make.bottom.trailing.top.equalTo(searchEngineScrollView)
}
}
private func layoutSearchEngineScrollView() {
if !dataSource.hasQuickSearchEngines { return }
let keyboardHeight = KeyboardHelper.defaultHelper.currentState?.intersectionHeightForView(view) ?? 0
searchEngineScrollView.snp.remakeConstraints { make in
make.leading.trailing.equalTo(view)
if isUsingBottomBar {
let offset: CGFloat
if keyboardHeight > 0 {
offset = keyboardHeight - abs(additionalSafeAreaInsets.bottom - view.safeAreaInsets.bottom)
} else {
offset = 0
}
make.bottom.equalTo(view.safeArea.bottom).offset(-offset)
} else {
make.bottom.equalTo(view).offset(-keyboardHeight)
}
}
}
private func layoutSuggestionsOptInPrompt() {
if dataSource.tabType.isPrivate ||
dataSource.searchEngines?.shouldShowSearchSuggestionsOptIn == false {
reloadData()
return
}
layoutTable()
}
private func layoutTable() {
tableView.snp.remakeConstraints { make in
make.top.equalTo(view.snp.top)
make.leading.trailing.equalTo(view)
make.bottom.equalTo(dataSource.hasQuickSearchEngines ?
searchEngineScrollView.snp.top :
self.view)
}
}
override func reloadData() {
dataSource.querySuggestClient()
}
func setupSearchEngineList() {
// Cancel requests that are not executed
dataSource.cancelPendingSuggestionsRequests()
// Query and reload the table with new search suggestions.
dataSource.querySuggestClient()
dataSource.setupSearchClient()
// Reload the footer list of search engines.
reloadSearchEngines()
layoutSuggestionsOptInPrompt()
}
func setSearchQuery(query: String) {
dataSource.searchQuery = query
dataSource.querySuggestClient()
}
private func reloadSearchEngines() {
searchEngineScrollViewContent.subviews.forEach { $0.removeFromSuperview() }
var leftEdge = searchEngineScrollViewContent.snp.left
// search settings icon
let searchButton = UIButton()
searchButton.setImage(UIImage(named: "quickSearch", in: .module, compatibleWith: nil)!.template, for: [])
searchButton.imageView?.contentMode = .center
searchButton.backgroundColor = .clear
searchButton.addTarget(self, action: #selector(didClickSearchButton), for: .touchUpInside)
searchButton.accessibilityLabel = Strings.searchSettingsButtonTitle
searchButton.tintColor = .braveBlurpleTint
searchButton.imageView?.snp.makeConstraints { make in
make.width.height.equalTo(SearchViewControllerUX.searchImageWidth)
return
}
searchEngineScrollViewContent.addSubview(searchButton)
searchButton.snp.makeConstraints { make in
make.size.equalTo(SearchViewControllerUX.faviconSize)
// offset the left edge to align with search results
make.left.equalTo(leftEdge).offset(SearchViewControllerUX.searchButtonMargin * 2)
make.top.equalTo(searchEngineScrollViewContent).offset(SearchViewControllerUX.searchButtonMargin)
make.bottom.equalTo(searchEngineScrollViewContent).offset(-SearchViewControllerUX.searchButtonMargin)
}
// search engines
leftEdge = searchButton.snp.right
for engine in dataSource.quickSearchEngines {
let engineButton = UIButton()
engineButton.setImage(engine.image, for: [])
engineButton.imageView?.contentMode = .scaleAspectFit
engineButton.backgroundColor = .clear
engineButton.addTarget(self, action: #selector(didSelectEngine), for: .touchUpInside)
engineButton.accessibilityLabel = String(format: Strings.searchEngineFormatText, engine.shortName)
engineButton.imageView?.snp.makeConstraints { make in
make.width.height.equalTo(SearchViewControllerUX.faviconSize)
return
}
searchEngineScrollViewContent.addSubview(engineButton)
engineButton.snp.makeConstraints { make in
make.width.equalTo(SearchViewControllerUX.engineButtonWidth)
make.left.equalTo(leftEdge)
make.top.equalTo(searchEngineScrollViewContent)
make.bottom.equalTo(searchEngineScrollViewContent)
if engine === dataSource.searchEngines?.quickSearchEngines.last {
make.right.equalTo(searchEngineScrollViewContent)
}
}
leftEdge = engineButton.snp.right
}
}
public func loader(dataLoaded data: [Site]) {
self.data = Array(data.prefix(5))
tableView.reloadData()
}
private func isBraveSearchPrompt(for indexPath: IndexPath) -> Bool {
guard let section = dataSource.availableSections[safe: indexPath.section] else {
return false
}
switch section {
case .searchSuggestionsOptIn:
return indexPath.row == 1 && dataSource.braveSearchPromotionAvailable
case .searchSuggestions:
let index = traitCollection.horizontalSizeClass == .regular && UIDevice.current.orientation.isPortrait ? 4 : 2
switch dataSource.suggestions.count {
case 0...index:
return indexPath.row == dataSource.suggestions.count && dataSource.braveSearchPromotionAvailable
default:
return indexPath.row == index && dataSource.braveSearchPromotionAvailable
}
default:
return false
}
}
private func changeBraveSearchPromotionState() {
// The promotion state will changed "maybeLaterUpcomingSession" next launch
if Preferences.BraveSearch.braveSearchPromotionCompletionState.value !=
BraveSearchPromotionState.maybeLaterSameSession.rawValue {
Preferences.BraveSearch.braveSearchPromotionCompletionState.value += 1
}
}
private func submitSeachTemplateQuery(isBraveSearchPromotion: Bool) {
if !dataSource.tabType.isPrivate {
RecentSearch.addItem(type: .text, text: dataSource.searchQuery, websiteUrl: nil)
}
searchDelegate?.searchViewController(
self,
didSubmit: dataSource.searchQuery,
braveSearchPromotion: isBraveSearchPromotion)
}
// MARK: Actions
@objc func didSelectEngine(_ sender: UIButton) {
// The UIButtons are the same cardinality and order as the array of quick search engines.
// Subtract 1 from index to account for magnifying glass accessory.
guard let index = searchEngineScrollViewContent.subviews.firstIndex(of: sender) else {
assertionFailure()
return
}
let engine = dataSource.quickSearchEngines[index - 1]
let localSearchQuery = dataSource.searchQuery.lowercased()
guard let url = engine.searchURLForQuery(localSearchQuery) else {
assertionFailure()
return
}
if !dataSource.tabType.isPrivate {
RecentSearch.addItem(type: .website, text: localSearchQuery, websiteUrl: url.absoluteString)
}
searchDelegate?.searchViewController(self, didSelectURL: url)
}
@objc func didClickSearchButton() {
self.searchDelegate?.presentSearchSettingsController()
}
// MARK: UITableViewDelegate, UITableViewDataSource
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let section = dataSource.availableSections[safe: indexPath.section] else { return }
switch section {
case .quickBar:
submitSeachTemplateQuery(isBraveSearchPromotion: false)
case .searchSuggestionsOptIn: return
case .searchSuggestions:
if !isBraveSearchPrompt(for: indexPath) {
// Assume that only the default search engine can provide search suggestions.
let engine = dataSource.searchEngines?.defaultEngine(forType: dataSource.tabType == .private ? .privateMode : .standard)
let suggestion = dataSource.suggestions[indexPath.row]
var url = URIFixup.getURL(suggestion)
if url == nil {
url = engine?.searchURLForQuery(suggestion)
}
if let url = url {
if !dataSource.tabType.isPrivate {
RecentSearch.addItem(type: .website, text: suggestion, websiteUrl: url.absoluteString)
}
searchDelegate?.searchViewController(self, didSelectURL: url)
}
}
case .openTabsAndHistoryAndBookmarks:
let site = data[indexPath.row]
if let url = URL(string: site.url) {
if site.siteType == .tab {
var tabId: UUID?
if let siteId = site.tabID {
tabId = UUID(uuidString: siteId)
}
searchDelegate?.searchViewController(self, didSelectOpenTab: (tabId, url))
} else {
searchDelegate?.searchViewController(self, didSelectURL: url)
}
}
case .findInPage:
let localSearchQuery = dataSource.searchQuery.lowercased()
searchDelegate?.searchViewController(self, shouldFindInPage: localSearchQuery)
}
}
override public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let currentSection = dataSource.availableSections[safe: indexPath.section] {
switch currentSection {
case .quickBar:
return super.tableView(tableView, heightForRowAt: indexPath)
case .searchSuggestionsOptIn:
return isBraveSearchPrompt(for: indexPath) ? UITableView.automaticDimension : 100.0
case .searchSuggestions:
return isBraveSearchPrompt(for: indexPath) ? UITableView.automaticDimension : 44.0
case .openTabsAndHistoryAndBookmarks:
return super.tableView(tableView, heightForRowAt: indexPath)
case .findInPage:
return super.tableView(tableView, heightForRowAt: indexPath)
}
}
return 0
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
guard let searchSection = dataSource.availableSections[safe: section] else { return nil }
switch searchSection {
case .quickBar: return nil
case .searchSuggestionsOptIn: return nil
case .searchSuggestions:
if let defaultSearchEngine = dataSource.searchEngines?.defaultEngine(forType: dataSource.tabType == .private ? .privateMode : .standard) {
if defaultSearchEngine.shortName.contains(Strings.searchSuggestionSectionTitleNoSearchFormat) || defaultSearchEngine.shortName.lowercased().contains("search") {
return defaultSearchEngine.displayName
}
return String(format: Strings.searchSuggestionSectionTitleFormat, defaultSearchEngine.displayName)
}
return Strings.searchSuggestionsSectionHeader
case .openTabsAndHistoryAndBookmarks: return Strings.searchHistorySectionHeader
case .findInPage: return Strings.findOnPageSectionHeader
}
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.textLabel?.textColor = .bravePrimary
}
}
override public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
guard let searchSection = dataSource.availableSections[safe: section] else { return 0 }
let headerHeight: CGFloat = 22
switch searchSection {
case .quickBar:
return 0.0
case .searchSuggestionsOptIn:
return 0.0
case .searchSuggestions:
return dataSource.suggestions.isEmpty ? 0 : headerHeight * 2.0
case .openTabsAndHistoryAndBookmarks:
// Check for History Bookmarks Open Tabs suggestions
// Show Browser Suggestions Preference effects all the modes
if !Preferences.Search.showBrowserSuggestions.value {
return 0
}
// Private Browsing Mode (PBM) should *not* show items from normal mode History etc
// when search suggestions is not enabled
if Preferences.Privacy.privateBrowsingOnly.value,
dataSource.searchEngines?.shouldShowSearchSuggestions == false {
return 0
}
return data.isEmpty ? 0 : 2.0 * headerHeight
case .findInPage:
if let sd = searchDelegate, sd.searchViewControllerAllowFindInPage() {
return headerHeight
}
return 0.0
}
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
guard let searchSection = dataSource.availableSections[safe: section] else { return 0 }
let footerHeight: CGFloat = 10.0
switch searchSection {
case .quickBar, .searchSuggestions, .findInPage:
return CGFloat.leastNormalMagnitude
case .searchSuggestionsOptIn:
return footerHeight
case .openTabsAndHistoryAndBookmarks:
return footerHeight
}
}
override public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
func createSearchSuggestionPromotionCell() -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: BraveSearchPromotionCell.identifier, for: indexPath)
if let promotionSearchCell = cell as? BraveSearchPromotionCell {
promotionSearchCell.trySearchEngineTapped = { [weak self] in
self?.submitSeachTemplateQuery(isBraveSearchPromotion: true)
}
promotionSearchCell.dismissTapped = { [weak self] in
self?.changeBraveSearchPromotionState()
tableView.reloadData()
}
}
return cell
}
guard let section = dataSource.availableSections[safe: indexPath.section] else {
return UITableViewCell()
}
switch section {
case .quickBar:
let cell = TwoLineTableViewCell()
cell.textLabel?.text = dataSource.searchQuery
cell.textLabel?.textColor = .bravePrimary
cell.imageView?.image = UIImage(named: "search_bar_find_in_page_icon", in: .module, compatibleWith: nil)?.withRenderingMode(.alwaysTemplate)
cell.imageView?.tintColor = browserColors.iconDefault
cell.imageView?.contentMode = .center
cell.backgroundColor = .clear
return cell
case .searchSuggestionsOptIn:
var cell: UITableViewCell?
if isBraveSearchPrompt(for: indexPath) {
cell = createSearchSuggestionPromotionCell()
} else {
cell = tableView.dequeueReusableCell(withIdentifier: SearchSuggestionPromptCell.identifier, for: indexPath)
if let promptCell = cell as? SearchSuggestionPromptCell {
promptCell.selectionStyle = .none
promptCell.onOptionSelected = { [weak self] option in
guard let self = self else { return }
self.dataSource.searchEngines?.shouldShowSearchSuggestions = option
self.dataSource.searchEngines?.shouldShowSearchSuggestionsOptIn = false
if option {
self.dataSource.querySuggestClient()
}
self.layoutSuggestionsOptInPrompt()
self.reloadSearchEngines()
self.tableView.reloadData()
}
}
}
guard let tableViewCell = cell else { return UITableViewCell() }
tableViewCell.separatorInset = .zero
return tableViewCell
case .searchSuggestions:
var cell: UITableViewCell?
if isBraveSearchPrompt(for: indexPath) {
cell = createSearchSuggestionPromotionCell()
} else {
cell = tableView.dequeueReusableCell(withIdentifier: SuggestionCell.identifier, for: indexPath)
if let suggestionCell = cell as? SuggestionCell, let suggestion = dataSource.suggestions[safe: indexPath.row] {
suggestionCell.setTitle(suggestion)
suggestionCell.separatorInset = UIEdgeInsets(top: 0.0, left: view.bounds.width, bottom: 0.0, right: -view.bounds.width)
suggestionCell.openButtonActionHandler = { [weak self] in
guard let self = self else { return }
self.searchDelegate?.searchViewController(self, didLongPressSuggestion: suggestion)
}
}
}
guard let tableViewCell = cell else { return UITableViewCell() }
tableViewCell.separatorInset = .zero
return tableViewCell
case .openTabsAndHistoryAndBookmarks:
let cell = super.tableView(tableView, cellForRowAt: indexPath)
let site = data[indexPath.row]
let detailTextForTabSuggestions = NSMutableAttributedString()
detailTextForTabSuggestions.append(
NSAttributedString(
string: Strings.searchSuggestionOpenTabActionTitle,
attributes: [
.font: DynamicFontHelper.defaultHelper.SmallSizeBoldWeightAS,
.foregroundColor: browserColors.textSecondary
]))
detailTextForTabSuggestions.append(
NSAttributedString(
string: " · \(site.url)",
attributes: [
.font: DynamicFontHelper.defaultHelper.SmallSizeRegularWeightAS,
.foregroundColor: browserColors.textSecondary
]))
if let cell = cell as? TwoLineTableViewCell {
cell.textLabel?.textColor = browserColors.textPrimary
if site.siteType == .tab {
cell.setLines(site.title, detailText: nil, detailAttributedText: detailTextForTabSuggestions)
} else {
cell.setLines(site.title, detailText: site.url)
}
cell.setRightBadge(site.siteType.icon?.template ?? nil)
cell.accessoryView?.tintColor = browserColors.iconDefault
cell.imageView?.contentMode = .scaleAspectFit
cell.imageView?.layer.borderColor = SearchViewControllerUX.iconBorderColor.cgColor
cell.imageView?.layer.borderWidth = SearchViewControllerUX.iconBorderWidth
cell.imageView?.loadFavicon(for: site.tileURL, isPrivateBrowsing: dataSource.tabType.isPrivate)
cell.backgroundColor = .clear
}
return cell
case .findInPage:
let cell = tableView.dequeueReusableCell(withIdentifier: "default", for: indexPath)
cell.textLabel?.text = String(format: Strings.findInPageFormat, dataSource.searchQuery)
cell.textLabel?.textColor = browserColors.textPrimary
cell.textLabel?.numberOfLines = 2
cell.textLabel?.font = .systemFont(ofSize: 15.0)
cell.textLabel?.lineBreakMode = .byWordWrapping
cell.textLabel?.textAlignment = .left
cell.backgroundColor = .clear
return cell
}
}
override public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let section = dataSource.availableSections[safe: section] else {
return 0
}
switch section {
case .quickBar:
return 1
case .searchSuggestionsOptIn:
return dataSource.braveSearchPromotionAvailable ? 2 : 1
case .searchSuggestions:
guard let shouldShowSuggestions = dataSource.searchEngines?.shouldShowSearchSuggestions else { return 0 }
var searchSuggestionsCount = min(dataSource.suggestions.count, dataSource.maxSearchSuggestions)
if dataSource.braveSearchPromotionAvailable {
searchSuggestionsCount += 1
}
return shouldShowSuggestions &&
!dataSource.searchQuery.looksLikeAURL() &&
!dataSource.tabType.isPrivate ? searchSuggestionsCount : 0
case .openTabsAndHistoryAndBookmarks:
// Check for History Bookmarks Open Tabs suggestions
// Show Browser Suggestions Preference effects all the modes
if !Preferences.Search.showBrowserSuggestions.value {
return 0
}
// Private Browsing Mode (PBM) should *not* show items from normal mode History etc
// when search suggestions is not enabled
if Preferences.Privacy.privateBrowsingOnly.value, dataSource.searchEngines?.shouldShowSearchSuggestions == false {
return 0
}
return data.count
case .findInPage:
if let sd = searchDelegate, sd.searchViewControllerAllowFindInPage() {
return 1
}
return 0
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return dataSource.availableSections.count
}
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
guard let section = dataSource.availableSections[safe: indexPath.section] else {
return
}
if section == .openTabsAndHistoryAndBookmarks {
let suggestion = data[indexPath.item]
searchDelegate?.searchViewController(self, didHighlightText: suggestion.url, search: false)
}
}
}
// MARK: - SearchSuggestionDataSourceDelegate
extension SearchViewController: SearchSuggestionDataSourceDelegate {
func searchSuggestionDataSourceReloaded() {
tableView.reloadData()
}
}
// MARK: - KeyboardHelperDelegate
extension SearchViewController: KeyboardHelperDelegate {
public func keyboardHelper(_ keyboardHelper: KeyboardHelper, keyboardWillShowWithState state: KeyboardState) {
animateSearchEnginesWithKeyboard(state)
}
public func keyboardHelper(_ keyboardHelper: KeyboardHelper, keyboardWillHideWithState state: KeyboardState) {
animateSearchEnginesWithKeyboard(state)
}
}
// MARK: - UILongPressGestureRecognizer
extension SearchViewController {
func onSuggestionLongPressed(_ gestureRecognizer: UILongPressGestureRecognizer) {
if gestureRecognizer.state == .began {
let location = gestureRecognizer.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: location),
let section = dataSource.availableSections[safe: indexPath.section],
let suggestion = dataSource.suggestions[safe: indexPath.row],
section == .searchSuggestions {
searchDelegate?.searchViewController(self, didLongPressSuggestion: suggestion)
}
}
}
}
// MARK: - ButtonScrollView
/**
* UIScrollView that prevents buttons from interfering with scroll.
*/
private class ButtonScrollView: UIScrollView {
fileprivate override func touchesShouldCancel(in view: UIView) -> Bool {
return true
}
}