Skip to content

Commit

Permalink
For 11660: fixing nits for previous mozilla-mobile#11668 (mozilla-mob…
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcLeclair authored Aug 26, 2020
1 parent 9288612 commit bd7a537
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 deletions app/src/main/java/org/mozilla/fenix/FenixApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ open class FenixApplication : LocaleAwareApplication(), Provider {
// no-op, LeakCanary is disabled by default
}

// This is for issue https://github.com/mozilla-mobile/fenix/issues/11660. We prefetch our info for startup
// so that we're sure that we have all the data available as our fragment is launched.
/**
* See [TopsiteStore.prefetch] for details on pre fetching.
*/
private fun prefetchForHomeFragment() {
StrictMode.allowThreadDiskReads().resetPoliciesAfter {
components.core.topSiteStorage.prefetch()
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/org/mozilla/fenix/components/TopSiteStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import mozilla.components.feature.top.sites.TopSite
import mozilla.components.feature.top.sites.TopSiteStorage
import mozilla.components.support.locale.LocaleManager
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.observeOnce
import org.mozilla.fenix.ext.observeOnceAndRemoveObserver
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.settings.advanced.getSelectedLocale
Expand Down Expand Up @@ -88,8 +88,15 @@ class TopSiteStorage(private val context: Context) {
}
}

/**
* This is for issue https://github.com/mozilla-mobile/fenix/issues/11660. We prefetch the top
* sites for startup so that we're sure that we have all the data available as our fragment is
* launched to make sure that we can display everything on the home screen on the first drawing pass.
* This method doesn't negatively affect performance since the [getTopSites] runs on the a
* background thread.
*/
fun prefetch() {
getTopSites().observeOnce {
getTopSites().observeOnceAndRemoveObserver {
cachedTopSites = it
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/mozilla/fenix/ext/LiveData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import androidx.lifecycle.Observer
/**
* Observe a LiveData once and unregister from it as soon as the live data returns a value
*/
fun <T> LiveData<T>.observeOnce(observer: (T) -> Unit) {
fun <T> LiveData<T>.observeOnceAndRemoveObserver(callback: (T) -> Unit) {
observeForever(object : Observer<T> {
override fun onChanged(value: T) {
removeObserver(this)
observer(value)
callback(value)
}
})
}

0 comments on commit bd7a537

Please sign in to comment.