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

Bug 1812144 - Homescreen is scrollable even if there is no content #28813

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import org.mozilla.fenix.perf.MarkersFragmentLifecycleCallbacks
import org.mozilla.fenix.perf.runBlockingIncrement
import org.mozilla.fenix.search.toolbar.SearchSelectorMenu
import org.mozilla.fenix.tabstray.TabsTrayAccessPoint
import org.mozilla.fenix.utils.AppBarLayoutBehaviorEmptyRecyclerView
import org.mozilla.fenix.utils.Settings.Companion.TOP_SITES_PROVIDER_MAX_THRESHOLD
import org.mozilla.fenix.utils.ToolbarPopupWindow
import org.mozilla.fenix.utils.allowUndo
Expand Down Expand Up @@ -503,6 +504,15 @@ class HomeFragment : Fragment() {
}
}

private fun appBarScrollingBehaviorWhenEmptyData() {
val appbarLayoutParams = binding.homeAppBar.layoutParams
if (appbarLayoutParams is CoordinatorLayout.LayoutParams) {
val behavior = AppBarLayoutBehaviorEmptyRecyclerView()
appbarLayoutParams.behavior = behavior
}
binding.homeAppBar.setExpanded(true, true)
}

private fun updateLayout(view: View) {
when (requireContext().settings().toolbarPosition) {
ToolbarPosition.TOP -> {
Expand Down Expand Up @@ -640,6 +650,8 @@ class HomeFragment : Fragment() {
}
}

appBarScrollingBehaviorWhenEmptyData()

// DO NOT MOVE ANYTHING BELOW THIS addMarker CALL!
requireComponents.core.engine.profiler?.addMarker(
MarkersFragmentLifecycleCallbacks.MARKER_NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.mozilla.fenix.utils

import android.view.MotionEvent
import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.appbar.AppBarLayout

/**
* This class is a workaround for the issue described in [https://bugzilla.mozilla.org/show_bug.cgi?id=1812144]
* It is used to prevent the app bar from collapsing when the recycler view is empty.
*/
@Suppress("MaxLineLength")
class AppBarLayoutBehaviorEmptyRecyclerView : AppBarLayout.Behavior() {
private var isRecyclerViewScrollable = false

override fun onInterceptTouchEvent(parent: CoordinatorLayout, child: AppBarLayout, ev: MotionEvent): Boolean {
return isRecyclerViewScrollable && super.onInterceptTouchEvent(parent, child, ev)
}

override fun onStartNestedScroll(
parent: CoordinatorLayout,
child: AppBarLayout,
directTargetChild: View,
target: View,
nestedScrollAxes: Int,
type: Int,
): Boolean {
updateScrollableState(target)
return isRecyclerViewScrollable && super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes, type)
}

override fun onNestedFling(
coordinatorLayout: CoordinatorLayout,
child: AppBarLayout,
target: View,
velocityX: Float,
velocityY: Float,
consumed: Boolean,
): Boolean {
updateScrollableState(target)
return isRecyclerViewScrollable && super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed)
}

/**
* If the child is a [RecyclerView], check if it is scrollable. Otherwise, assume it is scrollable.
* This is a workaround because the RecyclerView is having itemCount 2 when all items are disabled,
* so we are checking that instead 0 count.
*/
private fun updateScrollableState(child: View) {
isRecyclerViewScrollable = if (child is RecyclerView) {
val rvAdapter = child.adapter
rvAdapter != null && rvAdapter.itemCount > 2
} else {
true
}
}
}