Skip to content

Commit

Permalink
For mozilla-mobile#11660:added prefetch for topsites
Browse files Browse the repository at this point in the history
TopSites will be prefetched with observerOnce (wrapper around observerForever).
Also, the SessionControlView.update() is called right away instead of waiting from consumeFrom
in the HomeFragment.onCreateView() which will allow the UI to render all at once on its first
perform traversal
  • Loading branch information
MarcLeclair committed Jun 17, 2020
1 parent 291a29b commit 82d5296
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/org/mozilla/fenix/FenixApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ open class FenixApplication : LocaleAwareApplication() {
}
}

prefetchForHomeFragment()
setupLeakCanary()
if (settings().isTelemetryEnabled) {
components.analytics.metrics.start(MetricServiceType.Data)
Expand Down Expand Up @@ -229,6 +230,13 @@ open class FenixApplication : LocaleAwareApplication() {
// 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.
private fun prefetchForHomeFragment() {
StrictMode.allowThreadDiskReads().resetPoliciesAfter {
components.core.topSiteStorage.prefetch()
}
}
private fun setupPush() {
// Sets the PushFeature as the singleton instance for push messages to go to.
// We need the push feature setup here to deliver messages in the case where the service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +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.settings
import org.mozilla.fenix.settings.SupportUtils
import org.mozilla.fenix.settings.advanced.getSelectedLocale
Expand Down Expand Up @@ -86,4 +87,10 @@ class TopSiteStorage(private val context: Context) {
context.settings().defaultTopSitesAdded = true
}
}

fun prefetch() {
getTopSites().observeOnce {
cachedTopSites = it
}
}
}
20 changes: 20 additions & 0 deletions app/src/main/java/org/mozilla/fenix/ext/LiveData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.ext

import androidx.lifecycle.LiveData
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) {
observeForever(object : Observer<T> {
override fun onChanged(value: T) {
removeObserver(this)
observer(value)
}
})
}
6 changes: 6 additions & 0 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ class HomeFragment : Fragment() {
sessionControlInteractor,
homeViewModel
)

// This has to be called separately from the consumeFrom block since the coroutine doesn't
// allow our UI to be updated right away and delays our start up. The init block allows us to
// force our UI to render with the data available in our store at fragment creation time.
sessionControlView?.update(homeFragmentStore.state)

activity.themeManager.applyStatusBarTheme(activity)

view.consumeFrom(homeFragmentStore, viewLifecycleOwner) {
Expand Down

0 comments on commit 82d5296

Please sign in to comment.