Skip to content

Commit

Permalink
Fix issues with duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisBarreiro committed Apr 10, 2024
1 parent 0478c35 commit a7ccf35
Showing 1 changed file with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class AutoCompleteApi @Inject constructor(
.zipWith(
getAutoCompleteFavoritesResults(query),
) { bookmarks, favorites ->
(favorites + bookmarks)
(favorites + bookmarks.filter { favorites.none { favorite -> it.savedSite.url == favorite.savedSite.url } })
}.map {
it.sortedByDescending { it.score }.mapNotNull {
val savedSite = it.savedSite
Expand Down Expand Up @@ -133,14 +133,15 @@ class AutoCompleteApi @Inject constructor(
}
},
) { bookmarksAndFavorites, historyItems ->
(bookmarksAndFavorites + historyItems)
val searchHistory = historyItems.filterIsInstance<AutoCompleteHistorySearchSuggestion>()
val navigationHistory = historyItems.filterIsInstance<AutoCompleteHistorySuggestion>()
removeDuplicates(navigationHistory, bookmarksAndFavorites) + searchHistory
}

return savedSitesObservable.zipWith(
getAutoCompleteSearchResults(query),
) { bookmarksResults, searchResults ->

val topHits = bookmarksResults.filter {
) { bookmarksAndHistory, searchResults ->
val topHits = (searchResults + bookmarksAndHistory).filter {
when (it) {
is AutoCompleteHistorySearchSuggestion -> true
is AutoCompleteHistorySuggestion -> it.isAllowedInTopHits
Expand All @@ -151,8 +152,8 @@ class AutoCompleteApi @Inject constructor(

val maxBottomSection = maximumNumberOfSuggestions - (topHits.size + minimumNumberInSuggestionGroup)
val filteredBookmarks =
bookmarksResults
.filter { bookmarkSuggestion -> topHits.none { it.phrase == bookmarkSuggestion.phrase } }
bookmarksAndHistory
.filter { suggestion -> topHits.none { it.phrase == suggestion.phrase } }
.take(maxBottomSection)
val maxSearchResults = maximumNumberOfSuggestions - (topHits.size + filteredBookmarks.size)
val filteredSearchResults = searchResults
Expand All @@ -166,6 +167,23 @@ class AutoCompleteApi @Inject constructor(
}
}

private fun removeDuplicates(
historySuggestions: List<AutoCompleteHistorySuggestion>,
bookmarkSuggestions: List<AutoCompleteBookmarkSuggestion>,
): List<AutoCompleteSuggestion> {
val historyMap = historySuggestions.associateBy { it.phrase }
val bookmarkMap = bookmarkSuggestions.associateBy { it.phrase }

val uniqueHistorySuggestions = historySuggestions.filter { !bookmarkMap.containsKey(it.phrase) }
val updatedBookmarkSuggestions = bookmarkSuggestions.map { bookmarkSuggestion ->
historyMap[bookmarkSuggestion.phrase]?.let { historySuggestion ->
bookmarkSuggestion.copy(isAllowedInTopHits = historySuggestion.isAllowedInTopHits)
} ?: bookmarkSuggestion
}

return uniqueHistorySuggestions + updatedBookmarkSuggestions
}

private fun isAllowedInTopHits(entry: HistoryEntry): Boolean {
return entry.visits.size > 3 || entry.url.isRoot()
}
Expand Down

0 comments on commit a7ccf35

Please sign in to comment.