Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Edit Custom Search Engines #24856

Merged
merged 5 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ public class BrowserViewController: UIViewController {
if let presentedNavigationController = presentedViewController
as? ModalSettingsNavigationController,
let presentedRootController = presentedNavigationController.viewControllers.first,
presentedRootController is SearchSettingsTableViewController
presentedRootController is SearchSettingsViewController
{
searchEngineSettingsDismissed = true
}
Expand Down Expand Up @@ -2967,7 +2967,7 @@ extension BrowserViewController: SearchViewControllerDelegate {
}

func presentSearchSettingsController() {
let settingsNavigationController = SearchSettingsTableViewController(
let settingsNavigationController = SearchSettingsViewController(
profile: profile,
privateBrowsingManager: privateBrowsingManager
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,29 @@ public class SearchEngines {
}
}

/// Edits an engine which was already in the list
func editSearchEngine(_ engine: OpenSearchEngine, with newEngine: OpenSearchEngine) async throws {
guard
let customEngineIndex = customEngines.firstIndex(where: {
$0.shortName.lowercased() == engine.shortName.lowercased()
}),
let orderedEngineIndex = orderedEngines.firstIndex(where: {
$0.shortName.lowercased() == engine.shortName.lowercased()
})
else {
return
}

customEngines[customEngineIndex] = newEngine
orderedEngines[orderedEngineIndex] = newEngine

do {
try await saveCustomEngines()
} catch {
throw SearchEngineError.failedToSave
}
}

func queryForSearchURL(_ url: URL?, forType engineType: DefaultEngineType) -> String? {
return defaultEngine(forType: engineType)?.queryForSearchURL(url)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2024 The Brave Authors. All rights reserved.
// 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 https://mozilla.org/MPL/2.0/.

import BraveUI
import Shared
import UIKit

class SearchEngineTableViewHeader: UITableViewHeaderFooterView, TableViewReusable {

// MARK: UX

struct UX {
static let addButtonInset: CGFloat = 10
}

// MARK: Properties

var titleLabel = UILabel().then {
$0.font = UIFont.preferredFont(forTextStyle: .footnote)
$0.textColor = .secondaryBraveLabel
}

lazy var addEngineButton = OpenSearchEngineButton(
title: Strings.CustomSearchEngine.customEngineAutoAddTitle,
hidesWhenDisabled: false
).then {
$0.addTarget(self, action: #selector(addEngineAuto), for: .touchUpInside)
$0.isHidden = true
}

var actionHandler: (() -> Void)?

// MARK: Lifecycle

override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)

addSubview(titleLabel)
addSubview(addEngineButton)

setConstraints()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: Internal

func setConstraints() {
titleLabel.snp.makeConstraints { make in
make.leading.equalTo(readableContentGuide)
make.bottom.equalToSuperview().inset(4)
}

addEngineButton.snp.makeConstraints { make in
make.trailing.equalTo(readableContentGuide)
make.centerY.equalToSuperview()
make.height.equalTo(snp.height)
}
}

// MARK: Actions

@objc private func addEngineAuto() {
actionHandler?()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2024 The Brave Authors. All rights reserved.
// 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 https://mozilla.org/MPL/2.0/.

import BraveUI
import UIKit

class CustomEngineTitleInputTableViewCell: UITableViewCell, TableViewReusable {

// MARK: UX

struct UX {
static let textFieldInset: CGFloat = 16
}

// MARK: Properties

var textfield: UITextField = UITextField(frame: .zero)

weak var delegate: UITextFieldDelegate? {
didSet {
textfield.delegate = delegate
}
}

// MARK: Lifecycle

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

setup()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: Internal

private func setup() {
textfield = UITextField(
frame: CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height)
)

contentView.addSubview(textfield)

textfield.snp.makeConstraints({ make in
make.leading.trailing.equalToSuperview().inset(UX.textFieldInset)
make.bottom.top.equalToSuperview().inset(UX.textFieldInset)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2024 The Brave Authors. All rights reserved.
// 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 https://mozilla.org/MPL/2.0/.

import BraveUI
import UIKit

class CustomEngineURLInputTableViewCell: UITableViewCell, TableViewReusable {

// MARK: UX

struct UX {
static let textViewHeight: CGFloat = 88
static let textViewInset: CGFloat = 16
}

// MARK: Properties

var textview = UITextView(frame: .zero)

weak var delegate: UITextViewDelegate? {
didSet {
textview.delegate = delegate
}
}
// MARK: Lifecycle

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

setup()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: Internal

private func setup() {
textview = UITextView(
frame: CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height)
).then {
$0.text = "https://"
$0.backgroundColor = .clear
$0.font = UIFont.preferredFont(forTextStyle: .body)
$0.autocapitalizationType = .none
$0.autocorrectionType = .no
$0.spellCheckingType = .no
$0.keyboardType = .URL
$0.textColor = .braveLabel
}

contentView.addSubview(textview)

textview.snp.makeConstraints({ make in
make.leading.trailing.equalToSuperview().inset(UX.textViewInset)
make.bottom.top.equalToSuperview()
make.height.equalTo(UX.textViewHeight)
})
}
}
Loading
Loading