From 519c3bde5df413175e4da6478e33cd50a362b4a8 Mon Sep 17 00:00:00 2001 From: Colin Lee Date: Wed, 17 Jul 2019 13:54:59 -0500 Subject: [PATCH] For #4066: Provide lazy inflation of Find In Page View --- .../fenix/components/FindInPageIntegration.kt | 27 ++++++++++++++----- app/src/main/res/layout/fragment_browser.xml | 18 ++++--------- app/src/main/res/layout/stub_find_in_page.xml | 16 +++++++++++ 3 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 app/src/main/res/layout/stub_find_in_page.xml diff --git a/app/src/main/java/org/mozilla/fenix/components/FindInPageIntegration.kt b/app/src/main/java/org/mozilla/fenix/components/FindInPageIntegration.kt index 5d7ad0b6a..d1dcfe1f3 100644 --- a/app/src/main/java/org/mozilla/fenix/components/FindInPageIntegration.kt +++ b/app/src/main/java/org/mozilla/fenix/components/FindInPageIntegration.kt @@ -5,8 +5,11 @@ package org.mozilla.fenix.components import android.content.Context +import android.os.Looper import android.util.AttributeSet import android.view.View +import android.view.ViewStub +import androidx.annotation.UiThread import androidx.coordinatorlayout.widget.CoordinatorLayout import mozilla.components.browser.session.SessionManager import mozilla.components.browser.session.runWithSessionIdOrSelected @@ -19,40 +22,50 @@ import mozilla.components.support.base.feature.BackHandler import mozilla.components.support.base.feature.LifecycleAwareFeature import org.mozilla.fenix.test.Mockable +/** + * This class provides lazy loading of the Find in Page View. + * It should be launched on the app's UI thread. + */ @Mockable class FindInPageIntegration( private val sessionManager: SessionManager, private val sessionId: String? = null, - private val view: FindInPageView, + private val stub: ViewStub, engineView: EngineView, private val toolbar: BrowserToolbar ) : LifecycleAwareFeature, BackHandler { - private val feature = FindInPageFeature(sessionManager, view, engineView, ::onClose) + + private var view: FindInPageView? = null + private val feature: FindInPageFeature by lazy(LazyThreadSafetyMode.NONE) { + view = stub.inflate() as FindInPageView + FindInPageFeature(sessionManager, view!!, engineView, ::onClose).also { it.start() } + } override fun start() { - feature.start() } override fun stop() { - feature.stop() + if (view != null) feature.stop() } override fun onBackPressed(): Boolean { - return feature.onBackPressed() + return if (view != null) feature.onBackPressed() else false } private fun onClose() { toolbar.visibility = View.VISIBLE - view.asView().visibility = View.GONE + view?.asView()?.visibility = View.GONE } + @UiThread fun launch() { + require(Looper.myLooper() == Looper.getMainLooper()) { "This method should be run on the main UI thread." } sessionManager.runWithSessionIdOrSelected(sessionId) { if (!it.isCustomTabSession()) { toolbar.visibility = View.GONE } - view.asView().visibility = View.VISIBLE feature.bind(it) + view?.asView()?.visibility = View.VISIBLE } } } diff --git a/app/src/main/res/layout/fragment_browser.xml b/app/src/main/res/layout/fragment_browser.xml index 33a5ca52e..46026dcf6 100644 --- a/app/src/main/res/layout/fragment_browser.xml +++ b/app/src/main/res/layout/fragment_browser.xml @@ -4,7 +4,6 @@ - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> - + + +