Skip to content

Commit

Permalink
mozilla-mobile#7700 fixed up the offSet to be clearer and be more con…
Browse files Browse the repository at this point in the history
…cise in separating logic from high level functions + clean up
  • Loading branch information
MarcLeclair committed Feb 1, 2020
1 parent 98e24b3 commit 355ff18
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
51 changes: 27 additions & 24 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import kotlin.math.min
@SuppressWarnings("TooManyFunctions", "LargeClass")
class HomeFragment : Fragment() {
private val browsingModeManager get() = (activity as HomeActivity).browsingModeManager
private var offSet = 0
private var homeAppBarOffset = 0
private val singleSessionObserver = object : Session.Observer {
override fun onTitleChanged(session: Session, title: String) {
if (deleteAllSessionsJob == null) emitSessionChanges()
Expand Down Expand Up @@ -130,7 +130,7 @@ class HomeFragment : Fragment() {
private val onboarding by lazy { FenixOnboarding(requireContext()) }
private lateinit var homeFragmentStore: HomeFragmentStore
private lateinit var sessionControlInteractor: SessionControlInteractor
private lateinit var sessionControlView: SessionControlView
private var sessionControlView: SessionControlView? = null
private lateinit var currentMode: CurrentMode

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -203,20 +203,7 @@ class HomeFragment : Fragment() {
}
)

if (::sessionControlView.isInitialized) {
if (offSet < 0) {
(view.homeAppBar.layoutParams as CoordinatorLayout.LayoutParams)
.behavior = AppBarLayout.Behavior()
val behavior = ((view.homeAppBar.layoutParams as CoordinatorLayout.LayoutParams)
.behavior as AppBarLayout.Behavior)
behavior.topAndBottomOffset = offSet
behavior.onNestedPreScroll(view as CoordinatorLayout, view.homeAppBar,
sessionControlView.view, 0, 1, intArrayOf(2),
ViewCompat.TYPE_NON_TOUCH)
} else {
view.homeAppBar.setExpanded(false)
}
}
setOffset(view)

sessionControlView = SessionControlView(homeFragmentStore,
view.sessionControlRecyclerView, sessionControlInteractor)
Expand All @@ -234,7 +221,7 @@ class HomeFragment : Fragment() {
ViewModelProvider.NewInstanceFactory() // this is a workaround for #4652
}
homeViewModel.layoutManagerState?.also { parcelable ->
sessionControlView.view.layoutManager?.onRestoreInstanceState(parcelable)
sessionControlView!!.view.layoutManager?.onRestoreInstanceState(parcelable)
}
homeViewModel.layoutManagerState = null
}
Expand Down Expand Up @@ -316,6 +303,7 @@ class HomeFragment : Fragment() {

override fun onDestroyView() {
homeMenu = null
sessionControlView = null
super.onDestroyView()
}

Expand Down Expand Up @@ -445,7 +433,7 @@ class HomeFragment : Fragment() {
ViewModelProvider.NewInstanceFactory() // this is a workaround for #4652
}
homeViewModel.layoutManagerState =
sessionControlView.view.layoutManager?.onSaveInstanceState()
sessionControlView!!.view.layoutManager?.onSaveInstanceState()
}

override fun onResume() {
Expand All @@ -455,9 +443,7 @@ class HomeFragment : Fragment() {

override fun onPause() {
super.onPause()
val rect = Rect()
view!!.findViewById<AppBarLayout>(R.id.homeAppBar).getGlobalVisibleRect(rect)
offSet = rect.height() - view!!.findViewById<AppBarLayout>(R.id.homeAppBar).totalScrollRange
calculateNewOffset()
}

private fun recommendPrivateBrowsingShortcut() {
Expand Down Expand Up @@ -685,7 +671,7 @@ class HomeFragment : Fragment() {
private fun scrollToTheTop() {
lifecycleScope.launch(Main) {
delay(ANIM_SCROLL_DELAY)
sessionControlView.view.smoothScrollToPosition(0)
sessionControlView!!.view.smoothScrollToPosition(0)
}
}

Expand All @@ -695,7 +681,7 @@ class HomeFragment : Fragment() {
) {
if (view != null) {
viewLifecycleOwner.lifecycleScope.launch {
val recyclerView = sessionControlView.view
val recyclerView = sessionControlView!!.view
delay(ANIM_SCROLL_DELAY)
val tabsSize = getListOfSessions().size

Expand Down Expand Up @@ -738,7 +724,7 @@ class HomeFragment : Fragment() {
private fun animateCollection(addedTabsSize: Int, indexOfCollection: Int) {
viewLifecycleOwner.lifecycleScope.launch {
val viewHolder =
sessionControlView.view.findViewHolderForAdapterPosition(indexOfCollection)
sessionControlView!!.view.findViewHolderForAdapterPosition(indexOfCollection)
val border =
(viewHolder as? CollectionViewHolder)?.view?.findViewById<View>(R.id.selected_border)
val listener = object : Animator.AnimatorListener {
Expand Down Expand Up @@ -805,6 +791,23 @@ class HomeFragment : Fragment() {
}
}

private fun calculateNewOffset(){
homeAppBarOffset = ((view!!.findViewById<AppBarLayout>(R.id.homeAppBar)
.layoutParams as CoordinatorLayout.LayoutParams)
.behavior as AppBarLayout.Behavior).topAndBottomOffset
}

private fun setOffset(currentView: View){
if (homeAppBarOffset <= 0) {
(currentView.homeAppBar.layoutParams as CoordinatorLayout.LayoutParams)
.behavior = AppBarLayout.Behavior().apply {
topAndBottomOffset = this@HomeFragment.homeAppBarOffset
}
} else {
currentView.homeAppBar.setExpanded(false)
}
}

companion object {
private const val NON_TAB_ITEM_NUM = 3
private const val ANIM_SCROLL_DELAY = 100L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,18 @@ private fun collectionTabItems(collection: TabCollection) = collection.tabs.mapI
@ExperimentalCoroutinesApi
class SessionControlView(
private val homeFragmentStore: HomeFragmentStore,
private val container: ViewGroup,
override val containerView: View?,
interactor: SessionControlInteractor
) : LayoutContainer {
override val containerView: View?
get() = container

val view: RecyclerView = container as RecyclerView
val view: RecyclerView = containerView as RecyclerView

private val sessionControlAdapter = SessionControlAdapter(interactor)

init {
view.apply {
adapter = sessionControlAdapter
layoutManager = LinearLayoutManager(container.context)
layoutManager = LinearLayoutManager(containerView!!.context)
val itemTouchHelper =
ItemTouchHelper(
SwipeToDeleteCallback(
Expand Down

0 comments on commit 355ff18

Please sign in to comment.