Fixed bottom sheet for Immersive mode

This commit is contained in:
Prathamesh More 2021-10-13 13:08:25 +05:30
parent 6a391219a8
commit c2647b9160
5 changed files with 25 additions and 28 deletions

View file

@ -14,17 +14,16 @@
*/ */
package code.name.monkey.retromusic.activities.base package code.name.monkey.retromusic.activities.base
import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewTreeObserver import android.view.ViewTreeObserver
import android.widget.FrameLayout import android.widget.FrameLayout
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.commit import androidx.fragment.app.commit
import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.RetroBottomSheetBehavior import code.name.monkey.retromusic.RetroBottomSheetBehavior
import code.name.monkey.retromusic.databinding.SlidingMusicPanelLayoutBinding import code.name.monkey.retromusic.databinding.SlidingMusicPanelLayoutBinding
@ -55,7 +54,6 @@ import code.name.monkey.retromusic.fragments.queue.PlayingQueueFragment
import code.name.monkey.retromusic.helper.MusicPlayerRemote import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.model.CategoryInfo import code.name.monkey.retromusic.model.CategoryInfo
import code.name.monkey.retromusic.util.PreferenceUtil import code.name.monkey.retromusic.util.PreferenceUtil
import code.name.monkey.retromusic.util.RetroUtil
import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.bottomsheet.BottomSheetBehavior.* import com.google.android.material.bottomsheet.BottomSheetBehavior.*
import org.koin.androidx.viewmodel.ext.android.viewModel import org.koin.androidx.viewmodel.ext.android.viewModel
@ -66,6 +64,7 @@ abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity() {
var fromNotification: Boolean = false var fromNotification: Boolean = false
} }
private var windowInsets: WindowInsetsCompat? = null
protected val libraryViewModel by viewModel<LibraryViewModel>() protected val libraryViewModel by viewModel<LibraryViewModel>()
private lateinit var bottomSheetBehavior: RetroBottomSheetBehavior<FrameLayout> private lateinit var bottomSheetBehavior: RetroBottomSheetBehavior<FrameLayout>
private var playerFragment: AbsPlayerFragment? = null private var playerFragment: AbsPlayerFragment? = null
@ -81,8 +80,6 @@ abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity() {
override fun onSlide(bottomSheet: View, slideOffset: Float) { override fun onSlide(bottomSheet: View, slideOffset: Float) {
setMiniPlayerAlphaProgress(slideOffset) setMiniPlayerAlphaProgress(slideOffset)
binding.dimBackground.show()
binding.dimBackground.alpha = slideOffset
} }
override fun onStateChanged(bottomSheet: View, newState: Int) { override fun onStateChanged(bottomSheet: View, newState: Int) {
@ -92,7 +89,6 @@ abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity() {
} }
STATE_COLLAPSED -> { STATE_COLLAPSED -> {
onPanelCollapsed() onPanelCollapsed()
binding.dimBackground.hide()
if (fromNotification) { if (fromNotification) {
hideBottomBar(MusicPlayerRemote.playingQueue.isEmpty()) hideBottomBar(MusicPlayerRemote.playingQueue.isEmpty())
fromNotification = false fromNotification = false
@ -117,17 +113,16 @@ abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding = createContentView() binding = createContentView()
setContentView(binding.root) setContentView(binding.root)
ViewCompat.setOnApplyWindowInsetsListener(
binding.root
) { _, insets ->
windowInsets = insets
insets
}
binding.bottomNavigationView.drawAboveNavBarWithPadding() binding.bottomNavigationView.drawAboveNavBarWithPadding()
chooseFragmentForTheme() chooseFragmentForTheme()
setupSlidingUpPanel() setupSlidingUpPanel()
setupBottomSheet() setupBottomSheet()
val themeColor = resolveColor(android.R.attr.windowBackground, Color.GRAY)
binding.dimBackground.setBackgroundColor(ColorUtil.withAlpha(themeColor, 0.5f))
binding.dimBackground.setOnClickListener {
println("dimBackground")
collapsePanel()
}
} }
private fun setupBottomSheet() { private fun setupBottomSheet() {
@ -291,13 +286,15 @@ abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity() {
} }
fun hideBottomBar(hide: Boolean) { fun hideBottomBar(hide: Boolean) {
val heightOfBar = RetroUtil.getNavigationBarHeight() + val heightOfBar =
windowInsets.safeGetBottomInsets() +
if (MusicPlayerRemote.isCasting) dip(R.dimen.cast_mini_player_height) else dip(R.dimen.mini_player_height) if (MusicPlayerRemote.isCasting) dip(R.dimen.cast_mini_player_height) else dip(R.dimen.mini_player_height)
val heightOfBarWithTabs = heightOfBar + dip(R.dimen.bottom_nav_height) val heightOfBarWithTabs = heightOfBar + dip(R.dimen.bottom_nav_height)
val isVisible = binding.bottomNavigationView.isVisible val isVisible = binding.bottomNavigationView.isVisible
if (hide) { if (hide) {
bottomSheetBehavior.isHideable = true bottomSheetBehavior.isHideable = true
bottomSheetBehavior.peekHeight = 0 bottomSheetBehavior.peekHeight = 0
bottomSheetBehavior.state = STATE_HIDDEN
libraryViewModel.setFabMargin(if (isVisible) dip(R.dimen.bottom_nav_height) else 0) libraryViewModel.setFabMargin(if (isVisible) dip(R.dimen.bottom_nav_height) else 0)
ViewCompat.setElevation(binding.slidingPanel, 0f) ViewCompat.setElevation(binding.slidingPanel, 0f)
ViewCompat.setElevation(binding.bottomNavigationView, 10f) ViewCompat.setElevation(binding.bottomNavigationView, 10f)
@ -312,11 +309,11 @@ abstract class AbsSlidingMusicPanelActivity : AbsMusicServiceActivity() {
if (bottomSheetBehavior.state != STATE_EXPANDED) if (bottomSheetBehavior.state != STATE_EXPANDED)
getBottomNavigationView().translateYAnimate(0F) getBottomNavigationView().translateYAnimate(0F)
bottomSheetBehavior.peekHeightAnimate(heightOfBarWithTabs) bottomSheetBehavior.peekHeightAnimate(heightOfBarWithTabs)
libraryViewModel.setFabMargin(heightOfBarWithTabs - RetroUtil.getNavigationBarHeight()) libraryViewModel.setFabMargin(heightOfBarWithTabs - windowInsets.safeGetBottomInsets())
} else { } else {
println("Details") println("Details")
bottomSheetBehavior.peekHeight = heightOfBar bottomSheetBehavior.peekHeight = heightOfBar
libraryViewModel.setFabMargin(heightOfBar - RetroUtil.getNavigationBarHeight()) libraryViewModel.setFabMargin(heightOfBar - windowInsets.safeGetBottomInsets())
} }
} }
} }

View file

@ -0,0 +1,7 @@
package code.name.monkey.retromusic.extensions
import androidx.core.view.WindowInsetsCompat
fun WindowInsetsCompat?.safeGetBottomInsets(): Int {
return this?.getInsets(WindowInsetsCompat.Type.navigationBars())?.bottom ?: 0
}

View file

@ -28,6 +28,7 @@ import androidx.core.animation.doOnEnd
import androidx.core.animation.doOnStart import androidx.core.animation.doOnStart
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsCompat.Type.navigationBars
import androidx.core.view.updateLayoutParams import androidx.core.view.updateLayoutParams
import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.util.TintHelper import code.name.monkey.appthemehelper.util.TintHelper
@ -141,7 +142,7 @@ fun View.drawAboveNavBar() {
) { v: View, insets: WindowInsetsCompat -> ) { v: View, insets: WindowInsetsCompat ->
v.updateLayoutParams<MarginLayoutParams> { v.updateLayoutParams<MarginLayoutParams> {
bottomMargin = bottomMargin =
insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom insets.getInsets(navigationBars()).bottom
} }
insets insets
} }
@ -154,7 +155,7 @@ fun View.drawAboveNavBarWithPadding() {
ViewCompat.setOnApplyWindowInsetsListener( ViewCompat.setOnApplyWindowInsetsListener(
(this) (this)
) { v: View, insets: WindowInsetsCompat -> ) { v: View, insets: WindowInsetsCompat ->
val navBarHeight = insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom val navBarHeight = insets.getInsets(navigationBars()).bottom
v.updatePadding(bottom = navBarHeight) v.updatePadding(bottom = navBarHeight)
insets insets
} }

View file

@ -16,14 +16,6 @@
app:defaultNavHost="true" app:defaultNavHost="true"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" /> app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
<View
android:id="@+id/dimBackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:visibility="gone" />
<FrameLayout <FrameLayout
android:id="@+id/slidingPanel" android:id="@+id/slidingPanel"
style="@style/BottomSheetStyle" style="@style/BottomSheetStyle"
@ -31,7 +23,6 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:behavior_hideable="true" app:behavior_hideable="true"
app:behavior_peekHeight="0dp" app:behavior_peekHeight="0dp"
app:gestureInsetBottomIgnored="true"
app:layout_behavior="code.name.monkey.retromusic.RetroBottomSheetBehavior"> app:layout_behavior="code.name.monkey.retromusic.RetroBottomSheetBehavior">
<FrameLayout <FrameLayout

View file

@ -191,6 +191,7 @@
<style name="BottomSheetStyle" parent="Widget.Material3.BottomSheet"> <style name="BottomSheetStyle" parent="Widget.Material3.BottomSheet">
<item name="android:maxWidth">@empty</item> <item name="android:maxWidth">@empty</item>
<item name="android:backgroundTint">?attr/bottomSheetTint</item> <item name="android:backgroundTint">?attr/bottomSheetTint</item>
<item name="enableEdgeToEdge">true</item>
</style> </style>
<style name="MaterialCardViewStroke"> <style name="MaterialCardViewStroke">