From bc53b94feb82325cb93b278cdc7436e5b4973234 Mon Sep 17 00:00:00 2001 From: Jeff Boek Date: Tue, 2 Jun 2020 23:03:20 -0700 Subject: [PATCH] For #10989 - Fixes memory leak caused by retaining the TabTrayView and TabsFeature --- .../fenix/tabtray/TabTrayDialogFragment.kt | 31 +++++++++++++++++-- .../org/mozilla/fenix/tabtray/TabTrayView.kt | 22 ++++--------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayDialogFragment.kt b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayDialogFragment.kt index cc3387693..492bbf440 100644 --- a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayDialogFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayDialogFragment.kt @@ -24,7 +24,9 @@ import mozilla.components.browser.state.selector.privateTabs import mozilla.components.browser.state.state.BrowserState import mozilla.components.concept.engine.prompt.ShareData import mozilla.components.concept.tabstray.Tab +import mozilla.components.feature.tabs.tabstray.TabsFeature import mozilla.components.lib.state.ext.consumeFrom +import mozilla.components.support.base.feature.ViewBoundFeatureWrapper import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.R import org.mozilla.fenix.browser.browsingmode.BrowsingMode @@ -36,7 +38,10 @@ import org.mozilla.fenix.utils.allowUndo @SuppressWarnings("TooManyFunctions") class TabTrayDialogFragment : AppCompatDialogFragment(), TabTrayInteractor { - private lateinit var tabTrayView: TabTrayView + private val tabsFeature = ViewBoundFeatureWrapper() + private var _tabTrayView: TabTrayView? = null + private val tabTrayView: TabTrayView + get() = _tabTrayView!! override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -59,11 +64,26 @@ class TabTrayDialogFragment : AppCompatDialogFragment(), TabTrayInteractor { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - tabTrayView = TabTrayView( + val isPrivate = (activity as HomeActivity).browsingModeManager.mode.isPrivate + + _tabTrayView = TabTrayView( view.tabLayout, this, - (activity as HomeActivity).browsingModeManager.mode.isPrivate, + isPrivate, requireContext().resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE + ) { tabsFeature.get()?.filterTabs(it) } + + tabsFeature.set( + TabsFeature( + tabTrayView.view.tabsTray, + view.context.components.core.store, + view.context.components.useCases.tabsUseCases, + view.context.components.useCases.thumbnailUseCases, + { it.content.private == isPrivate }, + { } + ), + viewLifecycleOwner, + view ) tabLayout.setOnClickListener { @@ -90,6 +110,11 @@ class TabTrayDialogFragment : AppCompatDialogFragment(), TabTrayInteractor { } } + override fun onDestroyView() { + super.onDestroyView() + _tabTrayView = null + } + override fun onTabClosed(tab: Tab) { val sessionManager = view?.context?.components?.core?.sessionManager val snapshot = sessionManager diff --git a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt index 72b9f745f..6bd46bb6d 100644 --- a/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt +++ b/app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt @@ -26,7 +26,6 @@ import mozilla.components.browser.state.state.TabSessionState import mozilla.components.browser.tabstray.BrowserTabsTray import mozilla.components.concept.tabstray.Tab import mozilla.components.concept.tabstray.TabsTray -import mozilla.components.feature.tabs.tabstray.TabsFeature import org.mozilla.fenix.R import org.mozilla.fenix.ext.components @@ -45,8 +44,9 @@ interface TabTrayInteractor { class TabTrayView( private val container: ViewGroup, private val interactor: TabTrayInteractor, - private val isPrivate: Boolean, - private val startingInLandscape: Boolean + isPrivate: Boolean, + startingInLandscape: Boolean, + private val filterTabs: ((TabSessionState) -> Boolean) -> Unit ) : LayoutContainer, TabsTray.Observer, TabLayout.OnTabSelectedListener { val fabView = LayoutInflater.from(container.context) .inflate(R.layout.component_tabstray_fab, container, true) @@ -57,7 +57,7 @@ class TabTrayView( val isPrivateModeSelected: Boolean get() = view.tab_layout.selectedTabPosition == PRIVATE_TAB_ID private val behavior = BottomSheetBehavior.from(view.tab_wrapper) - private var tabsFeature: TabsFeature + private var tabTrayItemMenu: TabTrayItemMenu private var hasLoaded = false @@ -96,15 +96,6 @@ class TabTrayView( view.tab_layout.addOnTabSelectedListener(this) - tabsFeature = TabsFeature( - view.tabsTray, - view.context.components.core.store, - view.context.components.useCases.tabsUseCases, - view.context.components.useCases.thumbnailUseCases, - { it.content.private == isPrivate }, - { } - ) - val tabs = if (isPrivate) { view.context.components.core.store.state.privateTabs } else { @@ -160,8 +151,7 @@ class TabTrayView( interactor.onNewTabTapped(isPrivateModeSelected) } - tabsTray.register(this) - tabsFeature.start() + tabsTray.register(this, view) } fun expand() { @@ -180,7 +170,7 @@ class TabTrayView( } toggleFabText(isPrivateModeSelected) - tabsFeature.filterTabs(filter) + filterTabs.invoke(filter) updateState(view.context.components.core.store.state) }