Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Use ViewBoundFeatureWrapper and BackHandler from the base component.
Browse files Browse the repository at this point in the history
  • Loading branch information
pocmo authored and boek committed Feb 8, 2019
1 parent 97a3b3c commit cf260e4
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 104 deletions.
12 changes: 0 additions & 12 deletions app/src/main/java/org/mozilla/fenix/BackHandler.kt

This file was deleted.

1 change: 1 addition & 0 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import mozilla.components.concept.engine.EngineView
import mozilla.components.feature.intent.IntentProcessor
import mozilla.components.support.base.feature.BackHandler
import mozilla.components.support.utils.SafeIntent
import org.mozilla.fenix.browser.BrowserFragment
import org.mozilla.fenix.ext.components
Expand Down
150 changes: 80 additions & 70 deletions app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,32 @@ import mozilla.components.feature.downloads.DownloadsFeature
import mozilla.components.feature.prompts.PromptFeature
import mozilla.components.feature.session.SessionFeature
import mozilla.components.feature.session.SessionUseCases
import mozilla.components.support.ktx.android.arch.lifecycle.addObservers
import org.mozilla.fenix.BackHandler
import org.mozilla.fenix.DefaultThemeManager
import mozilla.components.support.base.feature.BackHandler
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FindInPageIntegration
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.share
import org.mozilla.fenix.mvi.ActionBusFactory
import org.mozilla.fenix.mvi.getSafeManagedObservable
import org.mozilla.fenix.search.toolbar.SearchAction
import org.mozilla.fenix.search.toolbar.ToolbarComponent
import org.mozilla.fenix.search.toolbar.SearchState
import org.mozilla.fenix.search.toolbar.ToolbarUIView
import org.mozilla.fenix.search.toolbar.ToolbarComponent
import org.mozilla.fenix.search.toolbar.ToolbarIntegration
import org.mozilla.fenix.search.toolbar.ToolbarMenu
import org.mozilla.fenix.search.toolbar.ToolbarUIView

class BrowserFragment : Fragment(), BackHandler {

private lateinit var contextMenuFeature: ContextMenuFeature
private lateinit var downloadsFeature: DownloadsFeature
private lateinit var findInPageIntegration: FindInPageIntegration
private lateinit var promptsFeature: PromptFeature
private lateinit var sessionFeature: SessionFeature
private lateinit var toolbarComponent: ToolbarComponent
private lateinit var customTabsToolbarFeature: CustomTabsToolbarFeature

private val sessionFeature = ViewBoundFeatureWrapper<SessionFeature>()
private val contextMenuFeature = ViewBoundFeatureWrapper<ContextMenuFeature>()
private val downloadsFeature = ViewBoundFeatureWrapper<DownloadsFeature>()
private val promptsFeature = ViewBoundFeatureWrapper<PromptFeature>()
private val findInPageIntegration = ViewBoundFeatureWrapper<FindInPageIntegration>()
private val customTabsToolbarFeature = ViewBoundFeatureWrapper<CustomTabsToolbarFeature>()
private val toolbarIntegration = ViewBoundFeatureWrapper<ToolbarIntegration>()

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -108,63 +110,67 @@ class BrowserFragment : Fragment(), BackHandler {

val sessionManager = requireComponents.core.sessionManager

contextMenuFeature = ContextMenuFeature(
requireFragmentManager(),
sessionManager,
ContextMenuCandidate.defaultCandidates(
contextMenuFeature.set(
feature = ContextMenuFeature(
requireFragmentManager(),
sessionManager,
ContextMenuCandidate.defaultCandidates(
requireContext(),
requireComponents.useCases.tabsUseCases,
view),
view.engineView),
owner = this,
view = view)

downloadsFeature.set(
feature = DownloadsFeature(
requireContext(),
requireComponents.useCases.tabsUseCases,
view),
view.engineView)

downloadsFeature = DownloadsFeature(
requireContext(),
sessionManager = sessionManager,
fragmentManager = childFragmentManager,
onNeedToRequestPermissions = { permissions ->
requestPermissions(permissions, REQUEST_CODE_DOWNLOAD_PERMISSIONS)
}
)

promptsFeature = PromptFeature(
fragment = this,
sessionManager = sessionManager,
fragmentManager = requireFragmentManager(),
onNeedToRequestPermissions = { permissions ->
requestPermissions(permissions, REQUEST_CODE_PROMPT_PERMISSIONS)
}
)

sessionFeature = SessionFeature(
sessionManager,
SessionUseCases(sessionManager),
view.engineView,
sessionId
)

findInPageIntegration = FindInPageIntegration(requireComponents.core.sessionManager, view.findInPageView)

customTabsToolbarFeature = CustomTabsToolbarFeature(
sessionManager,
toolbar,
sessionId
) { requireActivity().finish() }

lifecycle.addObservers(
contextMenuFeature,
downloadsFeature,
findInPageIntegration,
promptsFeature,
sessionFeature,
(toolbarComponent.uiView as ToolbarUIView).toolbarIntegration,
customTabsToolbarFeature
)
}

override fun onDestroyView() {
super.onDestroyView()

lifecycle.removeObserver(sessionFeature)
sessionManager = sessionManager,
fragmentManager = childFragmentManager,
onNeedToRequestPermissions = { permissions ->
requestPermissions(permissions, REQUEST_CODE_DOWNLOAD_PERMISSIONS)
}),
owner = this,
view = view)

promptsFeature.set(
feature = PromptFeature(
fragment = this,
sessionManager = sessionManager,
fragmentManager = requireFragmentManager(),
onNeedToRequestPermissions = { permissions ->
requestPermissions(permissions, REQUEST_CODE_PROMPT_PERMISSIONS)
}),
owner = this,
view = view)

sessionFeature.set(
feature = SessionFeature(
sessionManager,
SessionUseCases(sessionManager),
view.engineView,
sessionId),
owner = this,
view = view)

findInPageIntegration.set(
feature = FindInPageIntegration(requireComponents.core.sessionManager, view.findInPageView),
owner = this,
view = view)

customTabsToolbarFeature.set(
feature = CustomTabsToolbarFeature(
sessionManager,
toolbar,
sessionId,
closeListener = { requireActivity().finish() }),
owner = this,
view = view)

toolbarIntegration.set(
feature = (toolbarComponent.uiView as ToolbarUIView).toolbarIntegration,
owner = this,
view = view)
}

@SuppressWarnings("ReturnCount")
Expand All @@ -180,13 +186,17 @@ class BrowserFragment : Fragment(), BackHandler {

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_CODE_DOWNLOAD_PERMISSIONS -> downloadsFeature.onPermissionsResult(permissions, grantResults)
REQUEST_CODE_PROMPT_PERMISSIONS -> promptsFeature.onPermissionsResult(permissions, grantResults)
REQUEST_CODE_DOWNLOAD_PERMISSIONS -> downloadsFeature.withFeature {
it.onPermissionsResult(permissions, grantResults)
}
REQUEST_CODE_PROMPT_PERMISSIONS -> promptsFeature.withFeature {
it.onPermissionsResult(permissions, grantResults)
}
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
promptsFeature.onActivityResult(requestCode, resultCode, data)
promptsFeature.withFeature { it.onActivityResult(requestCode, resultCode, data) }
}

// This method triggers the complexity warning. However it's actually not that hard to understand.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,35 @@ package org.mozilla.fenix.components

import android.content.Context
import android.util.AttributeSet
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import mozilla.components.browser.session.SessionManager
import mozilla.components.browser.toolbar.BrowserToolbar
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

class FindInPageIntegration(
private val sessionManager: SessionManager,
private val view: FindInPageView
) : LifecycleObserver {
) : LifecycleAwareFeature, BackHandler {
private val feature = FindInPageFeature(sessionManager, view, ::onClose)

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart() {
override fun start() {
feature.start()

FindInPageIntegration.launch = this::launch
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onStop() {
override fun stop() {
feature.stop()

FindInPageIntegration.launch = null
}

fun onBackPressed(): Boolean {
override fun onBackPressed(): Boolean {
return feature.onBackPressed()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
package org.mozilla.fenix.search.toolbar

import android.content.Context
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.navigation.Navigation
import mozilla.components.browser.domains.autocomplete.DomainAutocompleteProvider
import mozilla.components.browser.session.SessionManager
Expand All @@ -17,6 +14,7 @@ import mozilla.components.concept.storage.HistoryStorage
import mozilla.components.feature.toolbar.ToolbarAutocompleteFeature
import mozilla.components.feature.toolbar.ToolbarFeature
import org.mozilla.fenix.DefaultThemeManager
import mozilla.components.support.base.feature.LifecycleAwareFeature
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.application
import org.mozilla.fenix.ext.components
Expand All @@ -29,7 +27,7 @@ class ToolbarIntegration(
historyStorage: HistoryStorage,
sessionManager: SessionManager,
sessionId: String? = null
) : LifecycleObserver {
) : LifecycleAwareFeature {
init {
toolbar.setMenuBuilder(toolbarMenu.menuBuilder)

Expand Down Expand Up @@ -59,18 +57,11 @@ class ToolbarIntegration(
sessionId
)

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun start() {
override fun start() {
toolbarFeature.start()
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun stop() {
override fun stop() {
toolbarFeature.stop()
}

companion object {
const val browserActionMarginDp = 8
const val urlBoxMargin = 8
}
}

0 comments on commit cf260e4

Please sign in to comment.