android: Use navigation bar shade view

This commit is contained in:
Charles Lombardo 2023-05-01 16:52:00 -04:00 committed by bunnei
parent 21e8a8277a
commit 86e395595a
4 changed files with 56 additions and 51 deletions

View file

@ -65,23 +65,8 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
window.statusBarColor = window.statusBarColor =
ContextCompat.getColor(applicationContext, android.R.color.transparent) ContextCompat.getColor(applicationContext, android.R.color.transparent)
ThemeHelper.setNavigationBarColor( window.navigationBarColor =
this, ContextCompat.getColor(applicationContext, android.R.color.transparent)
ElevationOverlayProvider(binding.navigationView.context).compositeOverlay(
MaterialColors.getColor(binding.navigationView, R.attr.colorSurface),
binding.navigationView.elevation
)
)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
setUpNavigation(navHostFragment.navController)
(binding.navigationView as NavigationBarView).setOnItemReselectedListener {
when (it.itemId) {
R.id.gamesFragment -> gamesViewModel.setShouldScrollToTop(true)
R.id.searchFragment -> gamesViewModel.setSearchFocused(true)
}
}
binding.statusBarShade.setBackgroundColor( binding.statusBarShade.setBackgroundColor(
ThemeHelper.getColorWithOpacity( ThemeHelper.getColorWithOpacity(
@ -92,6 +77,27 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
ThemeHelper.SYSTEM_BAR_ALPHA ThemeHelper.SYSTEM_BAR_ALPHA
) )
) )
if (InsetsHelper.getSystemGestureType(applicationContext) != InsetsHelper.GESTURE_NAVIGATION) {
binding.navigationBarShade.setBackgroundColor(
ThemeHelper.getColorWithOpacity(
MaterialColors.getColor(
binding.root,
R.attr.colorSurface
),
ThemeHelper.SYSTEM_BAR_ALPHA
)
)
}
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment
setUpNavigation(navHostFragment.navController)
(binding.navigationView as NavigationBarView).setOnItemReselectedListener {
when (it.itemId) {
R.id.gamesFragment -> gamesViewModel.setShouldScrollToTop(true)
R.id.searchFragment -> gamesViewModel.setSearchFocused(true)
}
}
// Prevents navigation from being drawn for a short time on recreation if set to hidden // Prevents navigation from being drawn for a short time on recreation if set to hidden
if (!homeViewModel.navigationVisible.value?.first!!) { if (!homeViewModel.navigationVisible.value?.first!!) {
@ -116,14 +122,6 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
navController.navigate(R.id.action_firstTimeSetupFragment_to_gamesFragment) navController.navigate(R.id.action_firstTimeSetupFragment_to_gamesFragment)
(binding.navigationView as NavigationBarView).setupWithNavController(navController) (binding.navigationView as NavigationBarView).setupWithNavController(navController)
showNavigation(visible = true, animated = true) showNavigation(visible = true, animated = true)
ThemeHelper.setNavigationBarColor(
this,
ElevationOverlayProvider(binding.navigationView.context).compositeOverlay(
MaterialColors.getColor(binding.navigationView, R.attr.colorSurface),
binding.navigationView.elevation
)
)
} }
private fun setUpNavigation(navController: NavController) { private fun setUpNavigation(navController: NavController) {
@ -210,11 +208,18 @@ class MainActivity : AppCompatActivity(), ThemeProvider {
} }
private fun setInsets() = private fun setInsets() =
ViewCompat.setOnApplyWindowInsetsListener(binding.statusBarShade) { view: View, windowInsets: WindowInsetsCompat -> ViewCompat.setOnApplyWindowInsetsListener(binding.root) { _: View, windowInsets: WindowInsetsCompat ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
val mlpShade = view.layoutParams as MarginLayoutParams val mlpStatusShade = binding.statusBarShade.layoutParams as MarginLayoutParams
mlpShade.height = insets.top mlpStatusShade.height = insets.top
binding.statusBarShade.layoutParams = mlpShade binding.statusBarShade.layoutParams = mlpStatusShade
// The only situation where we care to have a nav bar shade is when it's at the bottom
// of the screen where scrolling list elements can go behind it.
val mlpNavShade = binding.navigationBarShade.layoutParams as MarginLayoutParams
mlpNavShade.height = insets.bottom
binding.navigationBarShade.layoutParams = mlpNavShade
windowInsets windowInsets
} }

View file

@ -44,28 +44,6 @@ object ThemeHelper {
} }
} }
@JvmStatic
fun setNavigationBarColor(activity: Activity, @ColorInt color: Int) {
val gestureType = InsetsHelper.getSystemGestureType(activity.applicationContext)
val orientation = activity.resources.configuration.orientation
if ((gestureType == InsetsHelper.THREE_BUTTON_NAVIGATION ||
gestureType == InsetsHelper.TWO_BUTTON_NAVIGATION) &&
orientation == Configuration.ORIENTATION_LANDSCAPE
) {
activity.window.navigationBarColor = color
} else if (gestureType == InsetsHelper.THREE_BUTTON_NAVIGATION ||
gestureType == InsetsHelper.TWO_BUTTON_NAVIGATION
) {
activity.window.navigationBarColor = getColorWithOpacity(color, SYSTEM_BAR_ALPHA)
} else {
activity.window.navigationBarColor = ContextCompat.getColor(
activity.applicationContext,
android.R.color.transparent
)
}
}
@ColorInt @ColorInt
fun getColorWithOpacity(@ColorInt color: Int, alphaFactor: Float): Int { fun getColorWithOpacity(@ColorInt color: Int, alphaFactor: Float): Int {
return Color.argb( return Color.argb(

View file

@ -43,4 +43,15 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" /> app:layout_constraintStart_toStartOf="parent" />
<View
android:id="@+id/navigation_bar_shade"
android:layout_width="0dp"
android:layout_height="1px"
android:background="@android:color/transparent"
android:clickable="false"
android:focusable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -43,4 +43,15 @@
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" /> app:layout_constraintStart_toStartOf="parent" />
<View
android:id="@+id/navigation_bar_shade"
android:layout_width="0dp"
android:layout_height="1px"
android:background="@android:color/transparent"
android:clickable="false"
android:focusable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>