Skip to content

Commit

Permalink
Bug 1907879 - Removed the toolbar top divider when the microsurvey pr…
Browse files Browse the repository at this point in the history
…ompt is displayed r=android-reviewers,amejiamarmol a=RyanVM

Differential Revision: https://phabricator.services.mozilla.com/D216929
  • Loading branch information
t-p-white committed Aug 5, 2024
1 parent 7700abf commit 411eb83
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import androidx.compose.ui.viewinterop.AndroidView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat.getColor
import androidx.core.content.getSystemService
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
Expand Down Expand Up @@ -172,6 +173,7 @@ import org.mozilla.fenix.components.toolbar.DefaultBrowserToolbarController
import org.mozilla.fenix.components.toolbar.DefaultBrowserToolbarMenuController
import org.mozilla.fenix.components.toolbar.FenixTabCounterMenu
import org.mozilla.fenix.components.toolbar.NewTabMenu
import org.mozilla.fenix.components.toolbar.ToolbarContainerView
import org.mozilla.fenix.components.toolbar.ToolbarIntegration
import org.mozilla.fenix.components.toolbar.ToolbarMenu
import org.mozilla.fenix.components.toolbar.ToolbarPosition
Expand Down Expand Up @@ -595,11 +597,7 @@ abstract class BaseBrowserFragment :
}

if (context.settings().microsurveyFeatureEnabled) {
listenForMicrosurveyMessage(
browserToolbar = browserToolbarView.view,
view = view,
context = context,
)
listenForMicrosurveyMessage(context)
}

toolbarIntegration.set(
Expand Down Expand Up @@ -1493,8 +1491,14 @@ abstract class BaseBrowserFragment :
content = {
FirefoxTheme {
Column {
Divider()

if (!activity.isMicrosurveyPromptDismissed.value) {
currentMicrosurvey?.let {
if (isToolbarAtBottom) {
updateBrowserToolbarForMicrosurveyPrompt(browserToolbar)
}

MicrosurveyRequestPrompt(
microsurvey = it,
activity = activity,
Expand All @@ -1509,7 +1513,9 @@ abstract class BaseBrowserFragment :
context.components.appStore.dispatch(
MicrosurveyAction.Dismissed(it.id),
)

context.settings().shouldShowMicrosurveyPrompt = false
activity.isMicrosurveyPromptDismissed.value = true

resumeDownloadDialogState(
getCurrentTab()?.id,
Expand Down Expand Up @@ -1718,12 +1724,12 @@ abstract class BaseBrowserFragment :
}
}

private fun initializeMicrosurveyPrompt(
browserToolbar: BrowserToolbar,
view: View,
context: Context,
) {
private fun initializeMicrosurveyPrompt() {
val context = requireContext()
val view = requireView()

val isToolbarAtBottom = context.isToolbarAtBottom()
val browserToolbar = browserToolbarView.view
// The toolbar view has already been added directly to the container.
// See initializeNavBar for more details on improving this.
if (isToolbarAtBottom) {
Expand All @@ -1737,10 +1743,16 @@ abstract class BaseBrowserFragment :
content = {
FirefoxTheme {
Column {
Divider()

val activity = requireActivity() as HomeActivity

if (!activity.isMicrosurveyPromptDismissed.value) {
currentMicrosurvey?.let {
if (isToolbarAtBottom) {
updateBrowserToolbarForMicrosurveyPrompt(browserToolbar)
}

MicrosurveyRequestPrompt(
microsurvey = it,
activity = activity,
Expand All @@ -1755,7 +1767,9 @@ abstract class BaseBrowserFragment :
context.components.appStore.dispatch(
MicrosurveyAction.Dismissed(it.id),
)

context.settings().shouldShowMicrosurveyPrompt = false
activity.isMicrosurveyPromptDismissed.value = true

resumeDownloadDialogState(
getCurrentTab()?.id,
Expand Down Expand Up @@ -1798,16 +1812,22 @@ abstract class BaseBrowserFragment :
)
}

private fun updateBrowserToolbarForMicrosurveyPrompt(browserToolbar: BrowserToolbar) {
val drawable = ResourcesCompat.getDrawable(
resources,
R.drawable.toolbar_background_no_divider,
null,
)
browserToolbar.background = drawable
browserToolbar.elevation = 0.0f
}

private var currentMicrosurvey: MicrosurveyUIData? = null

/**
* Listens for the microsurvey message and initializes the microsurvey prompt if one is available.
*/
private fun listenForMicrosurveyMessage(
browserToolbar: BrowserToolbar,
view: View,
context: Context,
) {
private fun listenForMicrosurveyMessage(context: Context) {
binding.root.consumeFrom(context.components.appStore, viewLifecycleOwner) { state ->
state.messaging.messageToShow[FenixMessageSurfaceId.MICROSURVEY]?.let { message ->
if (message.id != currentMicrosurvey?.id) {
Expand All @@ -1821,11 +1841,7 @@ abstract class BaseBrowserFragment :
}
reinitializeNavBar()
} else {
initializeMicrosurveyPrompt(
browserToolbar = browserToolbar,
view = view,
context = context,
)
initializeMicrosurveyPrompt()
}
}
}
Expand Down Expand Up @@ -2298,7 +2314,7 @@ abstract class BaseBrowserFragment :
toolbarView = browserToolbarView.view,
bottomToolbarContainerView = _bottomToolbarContainerView?.toolbarContainerView,
reinitializeNavBar = ::reinitializeNavBar,
reinitializeMicrosurveyPrompt = ::reinitializeMicrosurveyPrompt,
reinitializeMicrosurveyPrompt = ::initializeMicrosurveyPrompt,
)
reinitializeEngineView()
}
Expand All @@ -2308,7 +2324,7 @@ abstract class BaseBrowserFragment :
updateMicrosurveyPromptForConfigurationChange(
parent = binding.browserLayout,
bottomToolbarContainerView = _bottomToolbarContainerView?.toolbarContainerView,
reinitializeMicrosurveyPrompt = ::reinitializeMicrosurveyPrompt,
reinitializeMicrosurveyPrompt = ::initializeMicrosurveyPrompt,
)
}
}
Expand All @@ -2322,14 +2338,6 @@ abstract class BaseBrowserFragment :
)
}

private fun reinitializeMicrosurveyPrompt() {
initializeMicrosurveyPrompt(
browserToolbar = browserToolbarView.view,
view = requireView(),
context = requireContext(),
)
}

private fun reinitializeEngineView() {
val isFullscreen = fullScreenFeature.get()?.isFullScreen == true
val topToolbarHeight = requireContext().settings().getTopToolbarHeight(
Expand Down Expand Up @@ -2455,7 +2463,7 @@ abstract class BaseBrowserFragment :
status: DownloadState.Status,
): Boolean {
val isValidStatus = status in listOf(DownloadState.Status.COMPLETED, DownloadState.Status.FAILED)
val isSameTab = downloadState.sessionId == getCurrentTab()?.id ?: false
val isSameTab = downloadState.sessionId == (getCurrentTab()?.id ?: false)

return isValidStatus && isSameTab
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import mozilla.components.ui.widgets.behavior.EngineViewScrollingBehavior
import mozilla.components.ui.widgets.behavior.ViewPosition
import org.mozilla.fenix.R

// TODO the gecko view/engine view height needs changing behind based on this bottom toolbar height FXDROID-2057.

/**
* A helper class to add a bottom toolbar container view to the given [parent].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.DrawableRes
import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -38,8 +39,8 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getColor
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
Expand Down Expand Up @@ -556,7 +557,7 @@ class HomeFragment : Fragment() {
toolbarView = binding.toolbarLayout,
bottomToolbarContainerView = _bottomToolbarContainerView?.toolbarContainerView,
reinitializeNavBar = ::reinitializeNavBar,
reinitializeMicrosurveyPrompt = { initializeMicrosurveyPrompt(requireContext()) },
reinitializeMicrosurveyPrompt = { initializeMicrosurveyPrompt() },
)
toolbarView?.updateButtonVisibility()
}
Expand All @@ -566,7 +567,7 @@ class HomeFragment : Fragment() {
updateMicrosurveyPromptForConfigurationChange(
parent = binding.homeLayout,
bottomToolbarContainerView = _bottomToolbarContainerView?.toolbarContainerView,
reinitializeMicrosurveyPrompt = { initializeMicrosurveyPrompt(requireContext()) },
reinitializeMicrosurveyPrompt = { initializeMicrosurveyPrompt() },
)
}

Expand Down Expand Up @@ -615,13 +616,19 @@ class HomeFragment : Fragment() {
content = {
FirefoxTheme {
Column {
Divider()

if (!activity.isMicrosurveyPromptDismissed.value &&
!context.settings().shouldShowNavigationBarCFR
) {
currentMicrosurvey.let {
if (it == null) {
binding.bottomBarShadow.visibility = View.VISIBLE
} else {
if (isToolbarAtBottom) {
updateToolbarViewUIForMicrosurveyPrompt()
}

MicrosurveyRequestPrompt(
microsurvey = it,
activity = activity,
Expand All @@ -639,10 +646,12 @@ class HomeFragment : Fragment() {
MicrosurveyAction.Dismissed(it.id),
)
context.settings().shouldShowMicrosurveyPrompt = false
activity.isMicrosurveyPromptDismissed.value = true

resetToolbarViewUI()
reinitializeNavBar()
},
)

binding.bottomBarShadow.visibility = View.GONE
}
}
}
Expand Down Expand Up @@ -791,7 +800,9 @@ class HomeFragment : Fragment() {
}
}

private fun initializeMicrosurveyPrompt(context: Context) {
private fun initializeMicrosurveyPrompt() {
val context = requireContext()

val isToolbarAtTheBottom = context.isToolbarAtBottom()
// The toolbar view has already been added directly to the container.
// See initializeNavBar for more details on improving this.
Expand All @@ -805,8 +816,9 @@ class HomeFragment : Fragment() {
content = {
FirefoxTheme {
Column {
val activity = requireActivity() as HomeActivity
Divider()

val activity = requireActivity() as HomeActivity
val shouldShowNavBarCFR =
context.shouldAddNavigationBar() && context.settings().shouldShowNavigationBarCFR

Expand All @@ -815,6 +827,10 @@ class HomeFragment : Fragment() {
if (it == null) {
binding.bottomBarShadow.visibility = View.VISIBLE
} else {
if (isToolbarAtTheBottom) {
updateToolbarViewUIForMicrosurveyPrompt()
}

MicrosurveyRequestPrompt(
microsurvey = it,
activity = activity,
Expand All @@ -830,10 +846,12 @@ class HomeFragment : Fragment() {
MicrosurveyAction.Dismissed(it.id),
)
context.settings().shouldShowMicrosurveyPrompt = false
activity.isMicrosurveyPromptDismissed.value = true

resetToolbarViewUI()
initializeMicrosurveyPrompt()
},
)

binding.bottomBarShadow.visibility = View.GONE
}
}
}
Expand All @@ -849,6 +867,28 @@ class HomeFragment : Fragment() {
)
}

private fun updateToolbarViewUIForMicrosurveyPrompt() {
updateToolbarViewUI(R.drawable.home_bottom_bar_background_no_divider, View.GONE, 0.0f)
}

private fun resetToolbarViewUI() {
_binding?.homeLayout?.removeView(bottomToolbarContainerView.toolbarContainerView)
updateToolbarViewUI(
R.drawable.home_bottom_bar_background,
View.VISIBLE,
requireContext().resources.getDimension(R.dimen.browser_fragment_toolbar_elevation),
)
}

private fun updateToolbarViewUI(@DrawableRes id: Int, visibility: Int, elevation: Float) {
_binding?.bottomBar?.background = compatDrawableFor(id)
_binding?.bottomBarShadow?.visibility = visibility
_binding?.toolbarLayout?.elevation = elevation
}

private fun compatDrawableFor(@DrawableRes id: Int) =
ResourcesCompat.getDrawable(resources, id, null)

private var currentMicrosurvey: MicrosurveyUIData? = null

/**
Expand All @@ -868,7 +908,7 @@ class HomeFragment : Fragment() {
}
reinitializeNavBar()
} else {
initializeMicrosurveyPrompt(requireContext())
initializeMicrosurveyPrompt()
}
}
}
Expand Down Expand Up @@ -1315,12 +1355,7 @@ class HomeFragment : Fragment() {
super.onPause()
if (browsingModeManager.mode == BrowsingMode.Private) {
activity?.window?.setBackgroundDrawable(
ColorDrawable(
ContextCompat.getColor(
requireContext(),
R.color.fx_mobile_private_layer_color_1,
),
),
ColorDrawable(getColor(requireContext(), R.color.fx_mobile_private_layer_color_1)),
)
}

Expand Down
Loading

0 comments on commit 411eb83

Please sign in to comment.