Skip to content

Commit

Permalink
For mozilla-mobile#12384 - Add optional filter for top frecent sites.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandru2909 committed Jun 24, 2022
1 parent b172382 commit 80b7bec
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.browser.storage.sync.PlacesHistoryStorage
import mozilla.components.concept.storage.FrecencyThresholdOption
import mozilla.components.feature.top.sites.ext.hasUrl
import mozilla.components.feature.top.sites.ext.toTopSite
import mozilla.components.feature.top.sites.facts.emitTopSitesCountFact
Expand Down Expand Up @@ -86,7 +85,7 @@ class DefaultTopSitesStorage(
@Suppress("ComplexCondition", "TooGenericExceptionCaught")
override suspend fun getTopSites(
totalSites: Int,
frecencyConfig: FrecencyThresholdOption?,
frecencyConfig: TopSitesFrecencyConfig?,
providerConfig: TopSitesProviderConfig?
): List<TopSite> {
val topSites = ArrayList<TopSite>()
Expand Down Expand Up @@ -114,13 +113,17 @@ class DefaultTopSitesStorage(

topSites.addAll(pinnedSites)

if (frecencyConfig != null && numSitesRequired > 0) {
if (frecencyConfig?.frecencyTresholdOption != null && numSitesRequired > 0) {
// Get 'totalSites' sites for duplicate entries with
// existing pinned sites
val frecentSites = historyStorage
.getTopFrecentSites(totalSites, frecencyConfig)
.getTopFrecentSites(totalSites, frecencyConfig.frecencyTresholdOption)
.map { it.toTopSite() }
.filter { !pinnedSites.hasUrl(it.url) && !providerTopSites.hasUrl(it.url) }
.filter {
!pinnedSites.hasUrl(it.url) &&
!providerTopSites.hasUrl(it.url) &&
frecencyConfig.frecencyFilter?.invoke(it) ?: true
}
.take(numSitesRequired)

topSites.addAll(frecentSites)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import mozilla.components.concept.storage.FrecencyThresholdOption
* whether or not to include top frecent sites in the top sites feature.
*
* @property totalSites A total number of sites that will be displayed.
* @property frecencyConfig If [frecencyConfig] is specified, only visited sites with a frecency
* score above the given threshold will be returned. Otherwise, frecent top site results are
* not included.
* @property frecencyConfig Instance of [TopSitesFrecencyConfig] that specifies which top
* frecent sites should be included.
* @property providerConfig An instance of [TopSitesProviderConfig] that specifies whether or
* not to fetch top sites from the [TopSitesProvider].
*/
data class TopSitesConfig(
val totalSites: Int,
val frecencyConfig: FrecencyThresholdOption? = null,
val frecencyConfig: TopSitesFrecencyConfig? = null,
val providerConfig: TopSitesProviderConfig? = null
)

Expand All @@ -36,3 +35,16 @@ data class TopSitesProviderConfig(
val maxThreshold: Int = Int.MAX_VALUE,
val providerFilter: ((TopSite) -> Boolean)? = null
)

/**
* Top sites frecency configuration used to specify which top frecent sites should be included.
*
* @property frecencyTresholdOption If [frecencyTresholdOption] is specified, only visited sites with a frecency
* score above the given threshold will be returned. Otherwise, frecent top site results are
* not included.
* @property frecencyFilter Optional function used to filter the top frecent sites.
*/
data class TopSitesFrecencyConfig(
val frecencyTresholdOption: FrecencyThresholdOption? = null,
val frecencyFilter: ((TopSite) -> Boolean)? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package mozilla.components.feature.top.sites

import mozilla.components.browser.storage.sync.PlacesHistoryStorage
import mozilla.components.concept.storage.FrecencyThresholdOption
import mozilla.components.support.base.observer.Observable

/**
Expand Down Expand Up @@ -43,15 +42,14 @@ interface TopSitesStorage : Observable<TopSitesStorage.Observer> {
* If `frecencyConfig` is specified, fill in any missing top sites with frecent top site results.
*
* @param totalSites A total number of sites that will be retrieve if possible.
* @param frecencyConfig If [frecencyConfig] is specified, only visited sites with a frecency
* score above the given threshold will be returned. Otherwise, frecent top site results are
* not included.
* @param frecencyConfig Instance of [TopSitesFrecencyConfig] that specifies which top
* frecent sites to be included.
* @param providerConfig An instance of [TopSitesProviderConfig] that specifies whether or
* not to fetch top sites from the [TopSitesProvider].
*/
suspend fun getTopSites(
totalSites: Int,
frecencyConfig: FrecencyThresholdOption? = null,
frecencyConfig: TopSitesFrecencyConfig? = null,
providerConfig: TopSitesProviderConfig? = null
): List<TopSite>

Expand Down

0 comments on commit 80b7bec

Please sign in to comment.