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 442
/
SearchSettingsTableViewController.swift
412 lines (336 loc) · 15.9 KB
/
SearchSettingsTableViewController.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
/* 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 os.log
// MARK: - SearchEnginePickerDelegate
protocol SearchEnginePickerDelegate: AnyObject {
func searchEnginePicker(
_ searchEnginePicker: SearchEnginePicker?,
didSelectSearchEngine engine: OpenSearchEngine?, forType: DefaultEngineType?)
}
// MARK: - SearchSettingsTableViewController
class SearchSettingsTableViewController: UITableViewController {
// MARK: UX
struct UX {
static let iconSize = CGSize(
width: OpenSearchEngine.preferredIconSize,
height: OpenSearchEngine.preferredIconSize)
static let headerHeight: CGFloat = 44
}
// MARK: Constants
struct Constants {
static let addCustomEngineRowIdentifier = "addCustomEngineRowIdentifier"
static let searchEngineRowIdentifier = "searchEngineRowIdentifier"
static let showSearchSuggestionsRowIdentifier = "showSearchSuggestionsRowIdentifier"
static let showRecentSearchesRowIdentifier = "showRecentSearchRowIdentifier"
static let showBrowserSuggestionsRowIdentifier = "showBrowserSuggestionsRowIdentifier"
static let quickSearchEngineRowIdentifier = "quickSearchEngineRowIdentifier"
static let customSearchEngineRowIdentifier = "customSearchEngineRowIdentifier"
}
// MARK: Section
enum Section: Int, CaseIterable {
case current
case customSearch
}
// MARK: CurrentEngineType
enum CurrentEngineType: Int, CaseIterable {
case standard
case `private`
case quick
case searchSuggestions
case recentSearches
case browserSuggestions
}
private var searchEngines: SearchEngines
private let profile: Profile
private var showDeletion = false
private var privateBrowsingManager: PrivateBrowsingManager
private func searchPickerEngines(type: DefaultEngineType) -> [OpenSearchEngine] {
var orderedEngines = searchEngines.orderedEngines
.sorted { $0.shortName < $1.shortName }
.sorted { engine, _ in engine.shortName == OpenSearchEngine.EngineNames.brave }
if let priorityEngine = InitialSearchEngines().priorityEngine?.rawValue {
orderedEngines =
orderedEngines
.sorted { engine, _ in
engine.engineID == priorityEngine
}
}
return orderedEngines
}
private var customSearchEngines: [OpenSearchEngine] {
searchEngines.orderedEngines.filter { $0.isCustomEngine }
}
// MARK: Lifecycle
init(profile: Profile, privateBrowsingManager: PrivateBrowsingManager) {
self.profile = profile
self.privateBrowsingManager = privateBrowsingManager
self.searchEngines = profile.searchEngines
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = Strings.searchEngines
tableView.do {
$0.allowsSelectionDuringEditing = true
$0.registerHeaderFooter(SettingsTableSectionHeaderFooterView.self)
$0.register(UITableViewCell.self, forCellReuseIdentifier: Constants.addCustomEngineRowIdentifier)
$0.register(UITableViewCell.self, forCellReuseIdentifier: Constants.searchEngineRowIdentifier)
$0.register(UITableViewCell.self, forCellReuseIdentifier: Constants.showSearchSuggestionsRowIdentifier)
$0.register(UITableViewCell.self, forCellReuseIdentifier: Constants.showRecentSearchesRowIdentifier)
$0.register(UITableViewCell.self, forCellReuseIdentifier: Constants.showBrowserSuggestionsRowIdentifier)
$0.register(UITableViewCell.self, forCellReuseIdentifier: Constants.quickSearchEngineRowIdentifier)
$0.register(UITableViewCell.self, forCellReuseIdentifier: Constants.customSearchEngineRowIdentifier)
$0.sectionHeaderTopPadding = 5
}
// Insert Done button if being presented outside of the Settings Nav stack
if navigationController?.viewControllers.first === self {
navigationItem.leftBarButtonItem =
UIBarButtonItem(title: Strings.settingsSearchDoneButton, style: .done, target: self, action: #selector(dismissAnimated))
}
let footer = SettingsTableSectionHeaderFooterView(frame: CGRect(width: tableView.bounds.width, height: UX.headerHeight))
tableView.tableFooterView = footer
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateTableEditModeVisibility()
tableView.reloadData()
}
// MARK: Internal
private func configureSearchEnginePicker(_ type: DefaultEngineType) -> SearchEnginePicker {
return SearchEnginePicker(type: type, showCancel: false).then {
// Order alphabetically, so that picker is always consistently ordered.
// Every engine is a valid choice for the default engine, even the current default engine.
$0.engines = searchPickerEngines(type: type)
$0.delegate = self
$0.selectedSearchEngineName = searchEngines.defaultEngine(forType: type).shortName
}
}
private func configureSearchEngineCell(type: DefaultEngineType, engineName: String?) -> UITableViewCell {
guard let searchEngineName = engineName else { return UITableViewCell() }
var text: String
switch type {
case .standard:
text = Strings.standardTabSearch
case .privateMode:
text = Strings.privateTabSearch
}
let cell = UITableViewCell(style: .value1, reuseIdentifier: Constants.searchEngineRowIdentifier).then {
$0.accessoryType = .disclosureIndicator
$0.editingAccessoryType = .disclosureIndicator
$0.accessibilityLabel = text
$0.textLabel?.text = text
$0.accessibilityValue = searchEngineName
$0.detailTextLabel?.text = searchEngineName
}
return cell
}
private func updateTableEditModeVisibility() {
tableView.endEditing(true)
if customSearchEngines.isEmpty {
navigationItem.rightBarButtonItem = nil
} else {
navigationItem.rightBarButtonItem = editButtonItem
}
}
// MARK: TableViewDataSource - TableViewDelegate
override func numberOfSections(in tableView: UITableView) -> Int {
return Section.allCases.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == Section.current.rawValue {
return CurrentEngineType.allCases.count
} else {
// Adding an extra row for Add Search Engine Entry
return customSearchEngines.count + 1
}
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UX.headerHeight
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell?
var engine: OpenSearchEngine?
if indexPath.section == Section.current.rawValue {
switch indexPath.item {
case CurrentEngineType.standard.rawValue:
engine = searchEngines.defaultEngine(forType: .standard)
cell = configureSearchEngineCell(type: .standard, engineName: engine?.displayName)
case CurrentEngineType.private.rawValue:
engine = searchEngines.defaultEngine(forType: .privateMode)
cell = configureSearchEngineCell(type: .privateMode, engineName: engine?.displayName)
case CurrentEngineType.quick.rawValue:
cell = tableView.dequeueReusableCell(withIdentifier: Constants.quickSearchEngineRowIdentifier, for: indexPath).then {
$0.textLabel?.text = Strings.quickSearchEngines
$0.accessoryType = .disclosureIndicator
$0.editingAccessoryType = .disclosureIndicator
}
case CurrentEngineType.searchSuggestions.rawValue:
let toggle = UISwitch().then {
$0.addTarget(self, action: #selector(didToggleSearchSuggestions), for: .valueChanged)
$0.isOn = searchEngines.shouldShowSearchSuggestions
}
cell = tableView.dequeueReusableCell(withIdentifier: Constants.showSearchSuggestionsRowIdentifier, for: indexPath).then {
$0.textLabel?.text = Strings.searchSettingSuggestionCellTitle
$0.accessoryView = toggle
$0.selectionStyle = .none
}
case CurrentEngineType.recentSearches.rawValue:
let toggle = UISwitch().then {
$0.addTarget(self, action: #selector(didToggleRecentSearches), for: .valueChanged)
$0.isOn = searchEngines.shouldShowRecentSearches
}
cell = tableView.dequeueReusableCell(withIdentifier: Constants.showRecentSearchesRowIdentifier, for: indexPath).then {
$0.textLabel?.text = Strings.searchSettingRecentSearchesCellTitle
$0.accessoryView = toggle
$0.selectionStyle = .none
}
case CurrentEngineType.browserSuggestions.rawValue:
let toggle = UISwitch().then {
$0.addTarget(self, action: #selector(didToggleBrowserSuggestions), for: .valueChanged)
$0.isOn = searchEngines.shouldShowBrowserSuggestions
}
cell = UITableViewCell(style: .subtitle, reuseIdentifier: Constants.showBrowserSuggestionsRowIdentifier).then {
$0.textLabel?.text = Strings.searchSettingBrowserSuggestionCellTitle
$0.detailTextLabel?.numberOfLines = 0
$0.detailTextLabel?.textColor = .secondaryBraveLabel
$0.detailTextLabel?.text = Strings.searchSettingBrowserSuggestionCellDescription
$0.accessoryView = toggle
$0.selectionStyle = .none
}
default:
// Should not happen.
break
}
} else {
// Add custom engine
if indexPath.item == customSearchEngines.count {
cell = tableView.dequeueReusableCell(withIdentifier: Constants.addCustomEngineRowIdentifier, for: indexPath).then {
$0.textLabel?.text = Strings.searchSettingAddCustomEngineCellTitle
$0.accessoryType = .disclosureIndicator
$0.editingAccessoryType = .disclosureIndicator
}
} else {
engine = customSearchEngines[indexPath.item]
cell = tableView.dequeueReusableCell(withIdentifier: Constants.customSearchEngineRowIdentifier, for: indexPath).then {
$0.textLabel?.text = engine?.displayName
$0.textLabel?.adjustsFontSizeToFitWidth = true
$0.textLabel?.minimumScaleFactor = 0.5
$0.imageView?.image = engine?.image.createScaled(UX.iconSize)
$0.imageView?.layer.cornerRadius = 4
$0.imageView?.layer.cornerCurve = .continuous
$0.imageView?.layer.masksToBounds = true
$0.selectionStyle = .none
}
}
}
guard let tableViewCell = cell else { return UITableViewCell() }
tableViewCell.separatorInset = .zero
return tableViewCell
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = tableView.dequeueReusableHeaderFooter() as SettingsTableSectionHeaderFooterView
let sectionTitle = section == Section.current.rawValue ? Strings.currentlyUsedSearchEngines : Strings.customSearchEngines
headerView.titleLabel.text = sectionTitle
return headerView
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
if indexPath.section == Section.current.rawValue && indexPath.item == CurrentEngineType.standard.rawValue {
navigationController?.pushViewController(configureSearchEnginePicker(.standard), animated: true)
} else if indexPath.section == Section.current.rawValue && indexPath.item == CurrentEngineType.private.rawValue {
navigationController?.pushViewController(configureSearchEnginePicker(.privateMode), animated: true)
} else if indexPath.section == Section.current.rawValue && indexPath.item == CurrentEngineType.quick.rawValue {
let quickSearchEnginesViewController = SearchQuickEnginesViewController(profile: profile, isPrivateBrowsing: privateBrowsingManager.isPrivateBrowsing)
navigationController?.pushViewController(quickSearchEnginesViewController, animated: true)
} else if indexPath.section == Section.customSearch.rawValue && indexPath.item == customSearchEngines.count {
let customEngineViewController = SearchCustomEngineViewController(profile: profile, privateBrowsingManager: privateBrowsingManager)
navigationController?.pushViewController(customEngineViewController, animated: true)
}
return nil
}
// Determine whether to show delete button in edit mode
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
guard indexPath.section == Section.customSearch.rawValue, indexPath.row != customSearchEngines.count else {
return .none
}
return .delete
}
// Determine whether to indent while in edit mode for deletion
override func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return indexPath.section == Section.customSearch.rawValue && indexPath.row != customSearchEngines.count
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
guard let engine = customSearchEngines[safe: indexPath.row] else { return }
func deleteCustomEngine() {
do {
try searchEngines.deleteCustomEngine(engine)
tableView.deleteRows(at: [indexPath], with: .right)
tableView.reloadData()
updateTableEditModeVisibility()
} catch {
Logger.module.error("Search Engine Error while deleting")
}
}
if engine == searchEngines.defaultEngine(forType: .standard) {
let alert = UIAlertController(
title: String(format: Strings.CustomSearchEngine.deleteEngineAlertTitle, engine.displayName),
message: Strings.CustomSearchEngine.deleteEngineAlertDescription,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: Strings.cancelButtonTitle, style: .cancel))
alert.addAction(UIAlertAction(title: Strings.delete, style: .destructive) { [weak self] _ in
guard let self = self else { return }
self.searchEngines.updateDefaultEngine(
self.searchEngines.defaultEngine(forType: .privateMode).shortName,
forType: .standard)
deleteCustomEngine()
})
UIImpactFeedbackGenerator(style: .medium).bzzt()
present(alert, animated: true, completion: nil)
} else {
deleteCustomEngine()
}
}
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return indexPath.section == Section.customSearch.rawValue
}
}
// MARK: - Actions
extension SearchSettingsTableViewController {
@objc func didToggleSearchSuggestions(_ toggle: UISwitch) {
// Setting the value in settings dismisses any opt-in.
searchEngines.shouldShowSearchSuggestions = toggle.isOn
searchEngines.shouldShowSearchSuggestionsOptIn = false
}
@objc func didToggleRecentSearches(_ toggle: UISwitch) {
// Setting the value in settings dismisses any opt-in.
searchEngines.shouldShowRecentSearches = toggle.isOn
searchEngines.shouldShowRecentSearchesOptIn = false
}
@objc func didToggleBrowserSuggestions(_ toggle: UISwitch) {
// Setting the value effects all the modes private normal pbo
searchEngines.shouldShowBrowserSuggestions = toggle.isOn
}
@objc func dismissAnimated() {
self.dismiss(animated: true, completion: nil)
}
}
// MARK: SearchEnginePickerDelegate
extension SearchSettingsTableViewController: SearchEnginePickerDelegate {
func searchEnginePicker(
_ searchEnginePicker: SearchEnginePicker?,
didSelectSearchEngine searchEngine: OpenSearchEngine?, forType: DefaultEngineType?
) {
if let engine = searchEngine, let type = forType {
searchEngines.updateDefaultEngine(engine.shortName, forType: type)
self.tableView.reloadData()
}
_ = navigationController?.popViewController(animated: true)
}
}