From 6d0a4fdb4d8203fce63f154c7d9eb388587c1451 Mon Sep 17 00:00:00 2001 From: ekager Date: Fri, 28 Jun 2019 09:07:02 -0700 Subject: [PATCH] Revert "For #3609 - Remove FIP Integration, Use FIP Feature" This reverts commit 428643d598e938f58a2fe4abfee417c46edec167. --- .../mozilla/fenix/browser/BrowserFragment.kt | 23 ++--- .../fenix/components/FindInPageIntegration.kt | 98 +++++++++++++++++++ 2 files changed, 106 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/org/mozilla/fenix/components/FindInPageIntegration.kt diff --git a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt index 264b73c3b3..dab4835d48 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt @@ -41,7 +41,6 @@ import mozilla.components.browser.session.SessionManager import mozilla.components.feature.app.links.AppLinksFeature import mozilla.components.feature.contextmenu.ContextMenuFeature import mozilla.components.feature.downloads.DownloadsFeature -import mozilla.components.feature.findinpage.FindInPageFeature import mozilla.components.feature.intent.IntentProcessor import mozilla.components.feature.prompts.PromptFeature import mozilla.components.feature.readerview.ReaderViewFeature @@ -67,6 +66,7 @@ import org.mozilla.fenix.collections.CreateCollectionViewModel import org.mozilla.fenix.collections.SaveCollectionStep import org.mozilla.fenix.collections.getStepForCollectionsSize import org.mozilla.fenix.components.FenixSnackbar +import org.mozilla.fenix.components.FindInPageIntegration import org.mozilla.fenix.components.TabCollectionStorage import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.components.metrics.Event.BrowserMenuItemTapped.Item @@ -114,7 +114,7 @@ class BrowserFragment : Fragment(), BackHandler { private val downloadsFeature = ViewBoundFeatureWrapper() private val appLinksFeature = ViewBoundFeatureWrapper() private val promptsFeature = ViewBoundFeatureWrapper() - private val findInPageFeature = ViewBoundFeatureWrapper() + private val findInPageIntegration = ViewBoundFeatureWrapper() private val toolbarIntegration = ViewBoundFeatureWrapper() private val readerViewFeature = ViewBoundFeatureWrapper() private val sitePermissionsFeature = ViewBoundFeatureWrapper() @@ -270,11 +270,10 @@ class BrowserFragment : Fragment(), BackHandler { view = view ) - findInPageFeature.set( - feature = FindInPageFeature(requireComponents.core.sessionManager, view.findInPageView, view.engineView) { - toolbar.visibility = View.VISIBLE - findInPageView.visibility = View.GONE - }, + findInPageIntegration.set( + feature = FindInPageIntegration( + requireComponents.core.sessionManager, view.findInPageView, view.engineView, toolbar + ), owner = this, view = view ) @@ -634,7 +633,7 @@ class BrowserFragment : Fragment(), BackHandler { override fun onBackPressed(): Boolean { return when { - findInPageFeature.onBackPressed() -> true + findInPageIntegration.onBackPressed() -> true fullScreenFeature.onBackPressed() -> true readerViewFeature.onBackPressed() -> true sessionFeature.onBackPressed() -> true @@ -733,13 +732,7 @@ class BrowserFragment : Fragment(), BackHandler { (activity as HomeActivity).browsingModeManager.mode = BrowsingModeManager.Mode.Private } ToolbarMenu.Item.FindInPage -> { - toolbar.visibility = View.GONE - findInPageView.visibility = View.VISIBLE - findInPageFeature.withFeature { - getSessionById()?.let { session -> - it.bind(session) - } - } + FindInPageIntegration.launch?.invoke() requireComponents.analytics.metrics.track(Event.FindInPageOpened) } ToolbarMenu.Item.ReportIssue -> getSessionById()?.let { session -> diff --git a/app/src/main/java/org/mozilla/fenix/components/FindInPageIntegration.kt b/app/src/main/java/org/mozilla/fenix/components/FindInPageIntegration.kt new file mode 100644 index 0000000000..2ab153887f --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/components/FindInPageIntegration.kt @@ -0,0 +1,98 @@ +/* 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.components + +import android.content.Context +import android.util.AttributeSet +import android.view.View +import androidx.coordinatorlayout.widget.CoordinatorLayout +import mozilla.components.browser.session.SessionManager +import mozilla.components.browser.toolbar.BrowserToolbar +import mozilla.components.concept.engine.EngineView +import mozilla.components.feature.findinpage.FindInPageFeature +import mozilla.components.feature.findinpage.view.FindInPageBar +import mozilla.components.feature.findinpage.view.FindInPageView +import mozilla.components.support.base.feature.BackHandler +import mozilla.components.support.base.feature.LifecycleAwareFeature +import org.mozilla.fenix.test.Mockable + +@Mockable +class FindInPageIntegration( + private val sessionManager: SessionManager, + private val view: FindInPageView, + engineView: EngineView, + private val toolbar: BrowserToolbar +) : LifecycleAwareFeature, BackHandler { + private val feature = FindInPageFeature(sessionManager, view, engineView, ::onClose) + + override fun start() { + feature.start() + + FindInPageIntegration.launch = this::launch + } + + override fun stop() { + feature.stop() + + FindInPageIntegration.launch = null + } + + override fun onBackPressed(): Boolean { + return feature.onBackPressed() + } + + private fun onClose() { + toolbar.visibility = View.VISIBLE + view.asView().visibility = View.GONE + } + + private fun launch() { + val session = sessionManager.selectedSession ?: return + + toolbar.visibility = View.GONE + view.asView().visibility = View.VISIBLE + feature.bind(session) + } + + companion object { + // This is a workaround to let the menu item find this integration and active "Find in Page" mode. That's a bit + // ridiculous and there's no need that we create the toolbar menu items at app start time. Instead the + // ToolbarIntegration should create them and get the FindInPageIntegration injected as a dependency if the + // menu items need them. + var launch: (() -> Unit)? = null + private set + } +} + +/** + * [CoordinatorLayout.Behavior] that will always position the [FindInPageBar] above the [BrowserToolbar] (including + * when the browser toolbar is scrolling or performing a snap animation). + */ +@Suppress("unused") // Referenced from XML +class FindInPageBarBehavior( + context: Context, + attrs: AttributeSet +) : CoordinatorLayout.Behavior(context, attrs) { + override fun layoutDependsOn(parent: CoordinatorLayout, child: FindInPageBar, dependency: View): Boolean { + if (dependency is BrowserToolbar) { + return true + } + + return super.layoutDependsOn(parent, child, dependency) + } + + override fun onDependentViewChanged(parent: CoordinatorLayout, child: FindInPageBar, dependency: View): Boolean { + return if (dependency is BrowserToolbar) { + repositionFindInPageBar(child, dependency) + true + } else { + false + } + } + + private fun repositionFindInPageBar(findInPageView: FindInPageBar, toolbar: BrowserToolbar) { + findInPageView.translationY = (toolbar.translationY + toolbar.height * -1.0).toFloat() + } +}