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 5d7ad0b6ab..d1dcfe1f3b 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 33a5ca52ee..46026dcf66 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/. -->
-
+
+
+