Skip to content

Commit

Permalink
Merge branch 'main' into dominik/xcode-16
Browse files Browse the repository at this point in the history
* main:
  Release 7.145.0-2 (#3570)
  Fix favorites grid layout issues on New Tab Page (#3568)
  Import Add To Dock Translations (#3566)
  Release 7.145.0-1 (#3567)
  Remove Old onboarding intro (#3554)
  fix showing current tab in suggestions (#3562)
  • Loading branch information
samsymons committed Nov 13, 2024
2 parents 045e596 + 508a68d commit 7f9ac0e
Show file tree
Hide file tree
Showing 45 changed files with 1,034 additions and 548 deletions.
78 changes: 45 additions & 33 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions DuckDuckGo/AutocompleteSuggestionsDataSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// AutocompleteSuggestionsDataSource.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Core
import BrowserServicesKit
import Suggestions
import History
import Persistence
import Networking

final class AutocompleteSuggestionsDataSource: SuggestionLoadingDataSource {

typealias SuggestionsRequestCompletion = (Data?, Error?) -> Void
typealias SuggestionsRequest = (URLRequest, @escaping SuggestionsRequestCompletion) -> Void

private let historyManager: HistoryManaging
private let bookmarksDatabase: CoreDataDatabase
private let featureFlagger: FeatureFlagger
private let tabsModel: TabsModel

private var performSuggestionsRequest: SuggestionsRequest

/// Specifically open tabs that do not have the same URL as the current tab so that we avoid shown them in the results.
private lazy var candidateOpenTabs: [BrowserTab] = {
tabsModel.tabs.compactMap {
guard let url = $0.link?.url,
tabsModel.currentTab?.link?.url != $0.link?.url
else { return nil }

return OpenTab(title: $0.link?.displayTitle ?? "", url: url)
}
}()

private lazy var cachedBookmarks: CachedBookmarks = {
CachedBookmarks(bookmarksDatabase)
}()

var historyCoordinator: HistoryCoordinating {
historyManager.historyCoordinator
}

var platform: Platform {
.mobile
}

init(historyManager: HistoryManaging, bookmarksDatabase: CoreDataDatabase, featureFlagger: FeatureFlagger, tabsModel: TabsModel, performSuggestionsRequest: @escaping SuggestionsRequest) {
self.historyManager = historyManager
self.bookmarksDatabase = bookmarksDatabase
self.featureFlagger = featureFlagger
self.tabsModel = tabsModel
self.performSuggestionsRequest = performSuggestionsRequest
}

func history(for suggestionLoading: Suggestions.SuggestionLoading) -> [HistorySuggestion] {
return historyCoordinator.history ?? []
}

func bookmarks(for suggestionLoading: Suggestions.SuggestionLoading) -> [Suggestions.Bookmark] {
return cachedBookmarks.all
}

func internalPages(for suggestionLoading: Suggestions.SuggestionLoading) -> [Suggestions.InternalPage] {
return []
}

func openTabs(for suggestionLoading: any SuggestionLoading) -> [BrowserTab] {
if featureFlagger.isFeatureOn(.autcompleteTabs) {
return candidateOpenTabs
}
return []
}

func suggestionLoading(_ suggestionLoading: Suggestions.SuggestionLoading, suggestionDataFromUrl url: URL, withParameters parameters: [String: String], completion: @escaping (Data?, Error?) -> Void) {
var queryURL = url
parameters.forEach {
queryURL = queryURL.appendingParameter(name: $0.key, value: $0.value)
}
var request = URLRequest.developerInitiated(queryURL)
request.allHTTPHeaderFields = APIRequest.Headers().httpHeaders

performSuggestionsRequest(request, completion)
}

}
82 changes: 20 additions & 62 deletions DuckDuckGo/AutocompleteViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,34 @@ class AutocompleteViewController: UIHostingController<AutocompleteView> {
weak var delegate: AutocompleteViewControllerDelegate?
weak var presentationDelegate: AutocompleteViewControllerPresentationDelegate?

private let historyManager: HistoryManaging
var historyCoordinator: HistoryCoordinating {
historyManager.historyCoordinator
}

private let bookmarksDatabase: CoreDataDatabase
private let appSettings: AppSettings
private let model: AutocompleteViewModel

private var task: URLSessionDataTask?

@Published private var query = ""
private var queryDebounceCancellable: AnyCancellable?

private lazy var cachedBookmarks: CachedBookmarks = {
CachedBookmarks(bookmarksDatabase)
}()

private lazy var openTabs: [BrowserTab] = {
tabsModel.tabs.compactMap {
guard let url = $0.link?.url else { return nil }
return OpenTab(title: $0.link?.displayTitle ?? "", url: url)
}
}()

private var lastResults: SuggestionResult?
private var loader: SuggestionLoader?
private var historyMessageManager: HistoryMessageManager
private var tabsModel: TabsModel
private var featureFlagger: FeatureFlagger
private let historyManager: HistoryManaging
private let bookmarksDatabase: CoreDataDatabase
private let tabsModel: TabsModel

private var task: URLSessionDataTask?

lazy var dataSource: AutocompleteSuggestionsDataSource = {
return AutocompleteSuggestionsDataSource(
historyManager: historyManager,
bookmarksDatabase: bookmarksDatabase,
featureFlagger: featureFlagger,
tabsModel: tabsModel) { [weak self] request, completion in
self?.task = Self.session.dataTask(with: request) { data, _, error in
completion(data, error)
}
self?.task?.resume()
}
}()

init(historyManager: HistoryManaging,
bookmarksDatabase: CoreDataDatabase,
Expand All @@ -81,6 +79,7 @@ class AutocompleteViewController: UIHostingController<AutocompleteView> {
self.tabsModel = tabsModel
self.historyManager = historyManager
self.bookmarksDatabase = bookmarksDatabase

self.appSettings = appSettings
self.historyMessageManager = historyMessageManager
self.featureFlagger = featureFlagger
Expand Down Expand Up @@ -191,7 +190,7 @@ class AutocompleteViewController: UIHostingController<AutocompleteView> {
return url
})

loader?.getSuggestions(query: query, usingDataSource: self) { [weak self] result, error in
loader?.getSuggestions(query: query, usingDataSource: dataSource) { [weak self] result, error in
guard let self, error == nil else { return }
let updatedResults = result ?? .empty
self.lastResults = updatedResults
Expand Down Expand Up @@ -283,47 +282,6 @@ extension AutocompleteViewController: AutocompleteViewModelDelegate {
}
}

extension AutocompleteViewController: SuggestionLoadingDataSource {

var platform: Platform {
.mobile
}

func history(for suggestionLoading: Suggestions.SuggestionLoading) -> [HistorySuggestion] {
return historyCoordinator.history ?? []
}

func bookmarks(for suggestionLoading: Suggestions.SuggestionLoading) -> [Suggestions.Bookmark] {
return cachedBookmarks.all
}

func internalPages(for suggestionLoading: Suggestions.SuggestionLoading) -> [Suggestions.InternalPage] {
return []
}

func openTabs(for suggestionLoading: any SuggestionLoading) -> [BrowserTab] {
if featureFlagger.isFeatureOn(.autcompleteTabs) {
return openTabs
}
return []
}

func suggestionLoading(_ suggestionLoading: Suggestions.SuggestionLoading, suggestionDataFromUrl url: URL, withParameters parameters: [String: String], completion: @escaping (Data?, Error?) -> Void) {
var queryURL = url
parameters.forEach {
queryURL = queryURL.appendingParameter(name: $0.key, value: $0.value)
}

var request = URLRequest.developerInitiated(queryURL)
request.allHTTPHeaderFields = APIRequest.Headers().httpHeaders
task = Self.session.dataTask(with: request) { data, _, error in
completion(data, error)
}
task?.resume()
}

}

private extension SuggestionResult {
static let empty = SuggestionResult(topHits: [], duckduckgoSuggestions: [], localSuggestions: [])
}
Expand Down
Loading

0 comments on commit 7f9ac0e

Please sign in to comment.