-
Notifications
You must be signed in to change notification settings - Fork 417
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
fix showing current tab in suggestions #3562
Merged
+289
−64
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd85257
exclude current tab
brindy 6329573
compare against url instead
brindy d97ba17
Merge branch 'main' into brindy/fix-showing-current-tab
brindy ff8774e
rename var for clarity
brindy 6cd99aa
extract data source into own file and add tests
brindy 2712375
Merge branch 'main' into brindy/fix-showing-current-tab
brindy a103362
remove invalid comment
brindy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nip: can remove unnecessary
return
on line. Same applies to below methods:func bookmarks(for suggestionLoading: Suggestions.SuggestionLoading) -> [Suggestions.Bookmark]
func internalPages(for suggestionLoading: Suggestions.SuggestionLoading) -> [Suggestions.InternalPage]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna leave it. My thinking is that functions should
return
something (unless they are view builders). I would like to think I'm consistent about that ... but who knows. :) Anyway, since it's just a nit I'm gonna go with what I've got.