From 5536dd48df51ea09af94871c62067d7db1b36acb Mon Sep 17 00:00:00 2001 From: OxygenCobalt Date: Fri, 29 Jul 2022 14:57:08 -0600 Subject: [PATCH] ui: fix bottom sheet initialization Fix an issue on some devices where bottom sheet transitions would not initialize correctly. Turns out I can't try to do transitions using the callback and simply initalizing them on startup. Apparently on some devices, CoordinatorLayout will simply not lay out until some arbitrary point past onResume. This breaks our transitions. Fix it by using a pre-draw listener instead. I cannot believe I have to do this. --- .../bottomsheet/NeoBottomSheetBehavior.java | 5 ++++ .../java/org/oxycblt/auxio/MainFragment.kt | 27 ++++++------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/google/android/material/bottomsheet/NeoBottomSheetBehavior.java b/app/src/main/java/com/google/android/material/bottomsheet/NeoBottomSheetBehavior.java index 9d5fdffd6..7882d1798 100644 --- a/app/src/main/java/com/google/android/material/bottomsheet/NeoBottomSheetBehavior.java +++ b/app/src/main/java/com/google/android/material/bottomsheet/NeoBottomSheetBehavior.java @@ -506,6 +506,8 @@ public class NeoBottomSheetBehavior extends CoordinatorLayout.Be } if (viewRef == null) { + + Log.d("NeoBottomSheetBehavior", "Lay out time"); // First layout with this behavior. peekHeightMin = parent.getResources().getDimensionPixelSize(R.dimen.design_bottom_sheet_peek_height_min); @@ -1076,6 +1078,7 @@ public class NeoBottomSheetBehavior extends CoordinatorLayout.Be */ public float calculateSlideOffset() { if (viewRef == null) { + Log.d("NeoBottomSheetBehavior", "No view ref"); return Float.MIN_VALUE; } @@ -1084,6 +1087,8 @@ public class NeoBottomSheetBehavior extends CoordinatorLayout.Be return calculateSlideOffset(bottomSheet.getTop()); } + Log.d("NeoBottomSheetBehavior", "No bottom sheet"); + return Float.MIN_VALUE; } diff --git a/app/src/main/java/org/oxycblt/auxio/MainFragment.kt b/app/src/main/java/org/oxycblt/auxio/MainFragment.kt index deb75c451..d4556d864 100644 --- a/app/src/main/java/org/oxycblt/auxio/MainFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/MainFragment.kt @@ -75,23 +75,11 @@ class MainFragment : ViewBindingFragment() { val playbackSheetBehavior = binding.playbackSheet.coordinatorLayoutBehavior as PlaybackSheetBehavior - - playbackSheetBehavior.addBottomSheetCallback( - object : NeoBottomSheetBehavior.BottomSheetCallback() { - override fun onSlide(bottomSheet: View, slideOffset: Float) { - handleSheetTransitions() - } - - override fun onStateChanged(bottomSheet: View, newState: Int) {} - }) - val queueSheetBehavior = binding.queueSheet.coordinatorLayoutBehavior as QueueSheetBehavior queueSheetBehavior.addBottomSheetCallback( object : NeoBottomSheetBehavior.BottomSheetCallback() { - override fun onSlide(bottomSheet: View, slideOffset: Float) { - handleSheetTransitions() - } + override fun onSlide(bottomSheet: View, slideOffset: Float) {} override fun onStateChanged(bottomSheet: View, newState: Int) { playbackSheetBehavior.isDraggable = @@ -100,11 +88,13 @@ class MainFragment : ViewBindingFragment() { } }) - binding.root.post { + // We would use the onSlide callback, but doing that would require us to initialize + // when the view first starts up, and that may not always work due to the insanity of + // the CoordinatorLayout lifecycle. Instead, do the less efficient alternative of updating + // the transition on every redraw. + binding.playbackSheet.viewTreeObserver.addOnPreDrawListener { handleSheetTransitions() - playbackSheetBehavior.isDraggable = - !playbackSheetBehavior.isHideable && - queueSheetBehavior.state == BottomSheetBehavior.STATE_COLLAPSED + true } // --- VIEWMODEL SETUP --- @@ -164,8 +154,7 @@ class MainFragment : ViewBindingFragment() { private fun handleMainNavigation(action: MainNavigationAction?) { if (action == null) return - - val binding = requireBinding() + when (action) { is MainNavigationAction.Expand -> tryExpandAll() is MainNavigationAction.Collapse -> tryCollapseAll()