Skip to content

Commit

Permalink
use bar location for autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
brindy committed Oct 6, 2023
1 parent 69daa4d commit a9698f5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
28 changes: 10 additions & 18 deletions DuckDuckGo/AutocompleteViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class AutocompleteViewController: UIViewController {
weak var delegate: AutocompleteViewControllerDelegate?
weak var presentationDelegate: AutocompleteViewControllerPresentationDelegate?

let appSettings: AppSettings

private var lastRequest: AutocompleteRequest?
private var receivedResponse = false
private var pendingRequest = false
Expand All @@ -45,10 +43,15 @@ class AutocompleteViewController: UIViewController {

private var bookmarksSearch: BookmarksStringSearch!

var isAddressBarAtBottom: Bool {
guard let rect = delegate?.autocompleteDidRequestSearchBarRect() else { return false }
return rect.minY > view.frame.midY
}

var backgroundColor: UIColor {
appSettings.currentAddressBarPosition == .top ?
UIColor.black.withAlphaComponent(0.2) :
UIColor(designSystemColor: .background)
isAddressBarAtBottom ?
UIColor(designSystemColor: .background) :
UIColor.black.withAlphaComponent(0.2)
}

var showBackground = true {
Expand All @@ -72,24 +75,13 @@ class AutocompleteViewController: UIViewController {
static func loadFromStoryboard(bookmarksSearch: BookmarksStringSearch) -> AutocompleteViewController {
let storyboard = UIStoryboard(name: "Autocomplete", bundle: nil)

guard let controller = storyboard.instantiateInitialViewController(creator: { coder in
AutocompleteViewController(coder: coder, appSettings: AppDependencyProvider.shared.appSettings)
}) else {
guard let controller = storyboard.instantiateInitialViewController() as? AutocompleteViewController else {
fatalError("Failed to instatiate correct Autocomplete view controller")
}
controller.bookmarksSearch = bookmarksSearch
return controller
}

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

required init?(coder: NSCoder, appSettings: AppSettings) {
self.appSettings = appSettings
super.init(coder: coder)
}

override func viewDidLoad() {
super.viewDidLoad()
configureTableView()
Expand Down Expand Up @@ -230,7 +222,7 @@ extension AutocompleteViewController: UITableViewDataSource {

let currentTheme = ThemeManager.shared.currentTheme

cell.updateFor(query: query, suggestion: suggestions[indexPath.row], with: currentTheme, appSettings: appSettings)
cell.updateFor(query: query, suggestion: suggestions[indexPath.row], with: currentTheme, isAddressBarAtBottom: isAddressBarAtBottom)
cell.plusButton.tag = indexPath.row

let color = indexPath.row == selectedItem ? currentTheme.tableCellSelectedColor : UIColor(designSystemColor: .panel)
Expand Down
2 changes: 2 additions & 0 deletions DuckDuckGo/AutocompleteViewControllerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ protocol AutocompleteViewControllerDelegate: AnyObject {
func autocomplete(pressedPlusButtonForSuggestion suggestion: Suggestion)

func autocompleteWasDismissed()

func autocompleteDidRequestSearchBarRect() -> CGRect
}

protocol AutocompleteViewControllerPresentationDelegate: AnyObject {
Expand Down
12 changes: 10 additions & 2 deletions DuckDuckGo/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class MainViewController: UIViewController {
return tabManager?.current
}

var searchBarRect: CGRect {
let view = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first?.rootViewController?.view
return omniBar.searchContainer.convert(omniBar.searchContainer.bounds, to: view)
}

var keyModifierFlags: UIKeyModifierFlags?
var showKeyboardAfterFireButton: DispatchWorkItem?

Expand Down Expand Up @@ -1435,6 +1440,10 @@ extension MainViewController: AutocompleteViewControllerDelegate {
func autocompleteWasDismissed() {
dismissOmniBar()
}

func autocompleteDidRequestSearchBarRect() -> CGRect {
searchBarRect
}
}

extension MainViewController: HomeControllerDelegate {
Expand Down Expand Up @@ -1635,8 +1644,7 @@ extension MainViewController: TabDelegate {
}

func tabDidRequestSearchBarRect(tab: TabViewController) -> CGRect {
let view = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first?.rootViewController?.view
return omniBar.searchContainer.convert(omniBar.searchContainer.bounds, to: view)
searchBarRect
}

func tab(_ tab: TabViewController,
Expand Down
7 changes: 3 additions & 4 deletions DuckDuckGo/SuggestionTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SuggestionTableViewCell: UITableViewCell {
@IBOutlet weak var typeImage: UIImageView!
@IBOutlet weak var plusButton: UIButton!

func updateFor(query: String, suggestion: Suggestion, with theme: Theme, appSettings: AppSettings) {
func updateFor(query: String, suggestion: Suggestion, with theme: Theme, isAddressBarAtBottom: Bool) {

switch suggestion.source {
case .local:
Expand All @@ -49,10 +49,9 @@ class SuggestionTableViewCell: UITableViewCell {
}

self.plusButton.accessibilityLabel = UserText.voiceoverActionAutocomplete
switch appSettings.currentAddressBarPosition {
case .bottom:
if isAddressBarAtBottom {
self.plusButton.setImage(UIImage(named: "Arrow-Down-Left-24"), for: .normal)
case .top:
} else {
self.plusButton.setImage(UIImage(named: "Arrow-Top-Left-24"), for: .normal)
}

Expand Down

0 comments on commit a9698f5

Please sign in to comment.