From 64aa8c17bd33d95957bbfa0a84a89df996e958a0 Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Fri, 28 Aug 2020 13:28:08 -0700 Subject: [PATCH] [fenix] Remove un-needed nullables (https://github.com/mozilla-mobile/fenix/pull/13816) --- .../fenix/browser/BaseBrowserFragment.kt | 34 ++++++----- .../mozilla/fenix/browser/BrowserAnimator.kt | 4 +- .../mozilla/fenix/search/SearchFragment.kt | 7 ++- .../fenix/search/awesomebar/AwesomeBarView.kt | 56 +++++++++---------- .../searchdialog/SearchDialogFragment.kt | 7 ++- .../SitePermissionsExceptionsFragment.kt | 4 +- 6 files changed, 60 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt index bbf9cc074b..690837b546 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt @@ -65,7 +65,6 @@ import mozilla.components.feature.session.SwipeRefreshFeature import mozilla.components.feature.session.behavior.EngineViewBottomBehavior import mozilla.components.feature.sitepermissions.SitePermissions import mozilla.components.feature.sitepermissions.SitePermissionsFeature -import mozilla.components.feature.sitepermissions.SitePermissionsRules import mozilla.components.lib.state.ext.flowScoped import mozilla.components.service.sync.logins.DefaultLoginValidationDelegate import mozilla.components.support.base.feature.PermissionsFeature @@ -82,6 +81,7 @@ import org.mozilla.fenix.OnBackLongPressedListener import org.mozilla.fenix.R import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.browser.readermode.DefaultReaderModeController +import org.mozilla.fenix.components.Components import org.mozilla.fenix.components.FenixSnackbar import org.mozilla.fenix.components.FindInPageIntegration import org.mozilla.fenix.components.StoreProvider @@ -123,8 +123,10 @@ import java.lang.ref.WeakReference @Suppress("TooManyFunctions", "LargeClass") abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, SessionManager.Observer, OnBackLongPressedListener, AccessibilityManager.AccessibilityStateChangeListener { + private lateinit var browserFragmentStore: BrowserFragmentStore private lateinit var browserAnimator: BrowserAnimator + private lateinit var components: Components private var _browserInteractor: BrowserToolbarViewInteractor? = null protected val browserInteractor: BrowserToolbarViewInteractor @@ -170,8 +172,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session container: ViewGroup?, savedInstanceState: Bundle? ): View { - require(arguments != null) - customTabSessionId = arguments?.getString(EXTRA_SESSION_ID) + customTabSessionId = requireArguments().getString(EXTRA_SESSION_ID) // Diagnostic breadcrumb for "Display already aquired" crash: // https://github.com/mozilla-mobile/android-components/issues/7960 @@ -193,6 +194,8 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session ) } + components = requireComponents + return view } @@ -216,6 +219,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session engineView = WeakReference(engineView), swipeRefresh = WeakReference(swipeRefresh), viewLifecycleScope = WeakReference(viewLifecycleOwner.lifecycleScope), + settings = context.components.settings, firstContentfulHappened = ::didFirstContentfulHappen ).apply { beginAnimateInIfNecessary() @@ -587,9 +591,9 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session context.settings().setSitePermissionSettingListener(viewLifecycleOwner) { // If the user connects to WIFI while on the BrowserFragment, this will update the // SitePermissionsRules (specifically autoplay) accordingly - assignSitePermissionsRules(context) + assignSitePermissionsRules() } - assignSitePermissionsRules(context) + assignSitePermissionsRules() fullScreenFeature.set( feature = FullScreenFeature( @@ -941,15 +945,13 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session * Returns the layout [android.view.Gravity] for the quick settings and ETP dialog. */ protected fun getAppropriateLayoutGravity(): Int = - context?.settings()?.toolbarPosition?.androidGravity ?: Gravity.BOTTOM + components.settings.toolbarPosition.androidGravity /** * Updates the site permissions rules based on user settings. */ - private fun assignSitePermissionsRules(context: Context) { - val settings = context.settings() - - val rules: SitePermissionsRules = settings.getSitePermissionsCustomSettingsRules() + private fun assignSitePermissionsRules() { + val rules = components.settings.getSitePermissionsCustomSettingsRules() sitePermissionsFeature.withFeature { it.sitePermissionsRules = rules @@ -995,7 +997,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session * Returns the current session. */ protected fun getSessionById(): Session? { - val sessionManager = context?.components?.core?.sessionManager ?: return null + val sessionManager = components.core.sessionManager val localCustomTabId = customTabSessionId return if (localCustomTabId != null) { sessionManager.findSessionById(localCustomTabId) @@ -1106,10 +1108,12 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session } private fun didFirstContentfulHappen() = - if (!requireContext().settings().waitToShowPageUntilFirstPaint) true else - context?.components?.core?.store?.state?.findTabOrCustomTabOrSelectedTab( - customTabSessionId - )?.content?.firstContentfulPaint ?: false + if (components.settings.waitToShowPageUntilFirstPaint) { + val tab = components.core.store.state.findTabOrCustomTabOrSelectedTab(customTabSessionId) + tab?.content?.firstContentfulPaint ?: false + } else { + true + } /* * Dereference these views when the fragment view is destroyed to prevent memory leaks diff --git a/app/src/main/java/org/mozilla/fenix/browser/BrowserAnimator.kt b/app/src/main/java/org/mozilla/fenix/browser/BrowserAnimator.kt index 559e594cf7..fcaaadde1d 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BrowserAnimator.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BrowserAnimator.kt @@ -18,6 +18,7 @@ import mozilla.components.concept.engine.EngineView import org.mozilla.fenix.R import org.mozilla.fenix.components.toolbar.ToolbarPosition import org.mozilla.fenix.ext.settings +import org.mozilla.fenix.utils.Settings import java.lang.ref.WeakReference /** @@ -29,6 +30,7 @@ class BrowserAnimator( private val engineView: WeakReference, private val swipeRefresh: WeakReference, private val viewLifecycleScope: WeakReference, + private val settings: Settings, private val firstContentfulHappened: () -> Boolean ) { @@ -39,7 +41,7 @@ class BrowserAnimator( get() = swipeRefresh.get() fun beginAnimateInIfNecessary() { - if (unwrappedSwipeRefresh?.context?.settings()?.waitToShowPageUntilFirstPaint == true) { + if (settings.waitToShowPageUntilFirstPaint) { if (firstContentfulHappened()) { viewLifecycleScope.get()?.launch { delay(ANIMATION_DELAY) diff --git a/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt b/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt index 955dc0844e..01877979e4 100644 --- a/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/search/SearchFragment.kt @@ -113,8 +113,11 @@ class SearchFragment : Fragment(), UserInteractionHandler { searchController ) - awesomeBarView = AwesomeBarView(requireContext(), searchInteractor, - view.findViewById(R.id.awesomeBar)) + awesomeBarView = AwesomeBarView( + activity, + searchInteractor, + view.findViewById(R.id.awesomeBar) + ) setShortcutsChangedListener(CustomSearchEngineStore.PREF_FILE_SEARCH_ENGINES) setShortcutsChangedListener(FenixSearchEngineProvider.PREF_FILE_SEARCH_ENGINES) diff --git a/app/src/main/java/org/mozilla/fenix/search/awesomebar/AwesomeBarView.kt b/app/src/main/java/org/mozilla/fenix/search/awesomebar/AwesomeBarView.kt index 5e96712e69..d95323aa21 100644 --- a/app/src/main/java/org/mozilla/fenix/search/awesomebar/AwesomeBarView.kt +++ b/app/src/main/java/org/mozilla/fenix/search/awesomebar/AwesomeBarView.kt @@ -4,7 +4,6 @@ package org.mozilla.fenix.search.awesomebar -import android.content.Context import androidx.appcompat.content.res.AppCompatResources.getDrawable import androidx.core.graphics.BlendModeColorFilterCompat.createBlendModeColorFilterCompat import androidx.core.graphics.BlendModeCompat.SRC_IN @@ -25,7 +24,7 @@ import mozilla.components.feature.tabs.TabsUseCases import mozilla.components.support.ktx.android.content.getColorFromAttr import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.R -import org.mozilla.fenix.ext.asActivity +import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.ext.components import org.mozilla.fenix.search.SearchEngineSource import org.mozilla.fenix.search.SearchFragmentState @@ -34,7 +33,7 @@ import org.mozilla.fenix.search.SearchFragmentState * View that contains and configures the BrowserAwesomeBar */ class AwesomeBarView( - private val context: Context, + private val activity: HomeActivity, val interactor: AwesomeBarInteractor, val view: BrowserAwesomeBar ) { @@ -90,18 +89,20 @@ class AwesomeBarView( init { view.itemAnimator = null - val components = context.components - val primaryTextColor = context.getColorFromAttr(R.attr.primaryText) + val components = activity.components + val primaryTextColor = activity.getColorFromAttr(R.attr.primaryText) - val engineForSpeculativeConnects = - if (!isBrowsingModePrivate()) components.core.engine else null + val engineForSpeculativeConnects = when (activity.browsingModeManager.mode) { + BrowsingMode.Normal -> components.core.engine + BrowsingMode.Private -> null + } sessionProvider = SessionSuggestionProvider( - context.resources, + activity.resources, components.core.store, selectTabUseCase, components.core.icons, - getDrawable(context, R.drawable.ic_search_results_tab), + getDrawable(activity, R.drawable.ic_search_results_tab), excludeSelectedSession = true ) @@ -118,17 +119,17 @@ class AwesomeBarView( bookmarksStorage = components.core.bookmarksStorage, loadUrlUseCase = loadUrlUseCase, icons = components.core.icons, - indicatorIcon = getDrawable(context, R.drawable.ic_search_results_bookmarks), + indicatorIcon = getDrawable(activity, R.drawable.ic_search_results_bookmarks), engine = engineForSpeculativeConnects ) - val searchBitmap = getDrawable(context, R.drawable.ic_search)!!.apply { + val searchBitmap = getDrawable(activity, R.drawable.ic_search)!!.apply { colorFilter = createBlendModeColorFilterCompat(primaryTextColor, SRC_IN) }.toBitmap() defaultSearchSuggestionProvider = SearchSuggestionProvider( - context = context, + context = activity, searchEngineManager = components.search.searchEngineManager, searchUseCase = searchUseCase, fetchClient = components.core.client, @@ -143,7 +144,7 @@ class AwesomeBarView( defaultSearchActionProvider = SearchActionProvider( searchEngineGetter = suspend { - components.search.searchEngineManager.getDefaultSearchEngineAsync(context) + components.search.searchEngineManager.getDefaultSearchEngineAsync(activity) }, searchUseCase = searchUseCase, icon = searchBitmap, @@ -153,7 +154,7 @@ class AwesomeBarView( shortcutsEnginePickerProvider = ShortcutsSuggestionProvider( searchEngineProvider = components.search.provider, - context = context, + context = activity, selectShortcutEngine = interactor::onSearchShortcutEngineSelected, selectShortcutEngineSettings = interactor::onClickSearchEngineSettings ) @@ -218,7 +219,7 @@ class AwesomeBarView( providersToAdd.addAll(getSelectedSearchSuggestionProvider(state)) } - if (!isBrowsingModePrivate()) { + if (activity.browsingModeManager.mode == BrowsingMode.Normal) { providersToAdd.add(sessionProvider) } @@ -242,18 +243,13 @@ class AwesomeBarView( providersToRemove.addAll(getSelectedSearchSuggestionProvider(state)) } - if (isBrowsingModePrivate()) { + if (activity.browsingModeManager.mode == BrowsingMode.Private) { providersToRemove.add(sessionProvider) } return providersToRemove } - private fun isBrowsingModePrivate(): Boolean { - return (context.asActivity() as? HomeActivity)?.browsingModeManager?.mode?.isPrivate - ?: false - } - private fun getSelectedSearchSuggestionProvider(state: SearchFragmentState): List { return when (state.searchEngineSource) { is SearchEngineSource.Default -> listOf( @@ -275,18 +271,20 @@ class AwesomeBarView( private fun getSuggestionProviderForEngine(engine: SearchEngine): List { return searchSuggestionProviderMap.getOrPut(engine) { - val components = context.components - val primaryTextColor = context.getColorFromAttr(R.attr.primaryText) + val components = activity.components + val primaryTextColor = activity.getColorFromAttr(R.attr.primaryText) - val searchBitmap = getDrawable(context, R.drawable.ic_search)?.apply { + val searchBitmap = getDrawable(activity, R.drawable.ic_search)!!.apply { colorFilter = createBlendModeColorFilterCompat(primaryTextColor, SRC_IN) - }?.toBitmap() + }.toBitmap() - val engineForSpeculativeConnects = - if (!isBrowsingModePrivate()) components.core.engine else null + val engineForSpeculativeConnects = when (activity.browsingModeManager.mode) { + BrowsingMode.Normal -> components.core.engine + BrowsingMode.Private -> null + } val searchEngine = - components.search.provider.installedSearchEngines(context).list.find { it.name == engine.name } - ?: components.search.provider.getDefaultEngine(context) + components.search.provider.installedSearchEngines(activity).list.find { it.name == engine.name } + ?: components.search.provider.getDefaultEngine(activity) listOf( SearchActionProvider( diff --git a/app/src/main/java/org/mozilla/fenix/searchdialog/SearchDialogFragment.kt b/app/src/main/java/org/mozilla/fenix/searchdialog/SearchDialogFragment.kt index 7d0454ba83..c67ed7b96e 100644 --- a/app/src/main/java/org/mozilla/fenix/searchdialog/SearchDialogFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/searchdialog/SearchDialogFragment.kt @@ -99,12 +99,13 @@ class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler { ): View? { val args by navArgs() val view = inflater.inflate(R.layout.fragment_search_dialog, container, false) + val activity = requireActivity() as HomeActivity requireComponents.analytics.metrics.track(Event.InteractWithSearchURLArea) store = SearchDialogFragmentStore( createInitialSearchFragmentState( - activity as HomeActivity, + activity, requireComponents, tabId = args.sessionId, pastedText = args.pastedText, @@ -114,7 +115,7 @@ class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler { interactor = SearchDialogInteractor( SearchDialogController( - activity = requireActivity() as HomeActivity, + activity = activity, sessionManager = requireComponents.core.sessionManager, store = store, navController = findNavController(), @@ -137,7 +138,7 @@ class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler { ).also(::addSearchButton) awesomeBarView = AwesomeBarView( - requireContext(), + activity, interactor, view.awesome_bar ) diff --git a/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsExceptionsFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsExceptionsFragment.kt index 813e24591a..f941ce29bc 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsExceptionsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsExceptionsFragment.kt @@ -118,8 +118,8 @@ class SitePermissionsExceptionsFragment : } } - override fun onClick(view: View?) { - val sitePermissions = view?.tag as SitePermissions + override fun onClick(view: View) { + val sitePermissions = view.tag as SitePermissions val directions = SitePermissionsExceptionsFragmentDirections .actionSitePermissionsToExceptionsToSitePermissionsDetails(sitePermissions) nav(R.id.sitePermissionsExceptionsFragment, directions)