Merge pull request #5952 from Bnyro/master

fix: wrong navigation bar color
This commit is contained in:
Bnyro 2024-05-01 17:57:14 +02:00 committed by GitHub
commit 73e1bbdde1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 19 deletions

View File

@ -9,6 +9,7 @@ import android.graphics.Color
import android.os.Build import android.os.Build
import android.text.Spanned import android.text.Spanned
import android.view.Window import android.view.Window
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
@ -24,24 +25,21 @@ object ThemeHelper {
/** /**
* Set the colors of the system bars (status bat and navigation bar) * Set the colors of the system bars (status bat and navigation bar)
*/ */
fun setSystemBarColors(context: Context, window: Window, isBottomNavVisible: Boolean) { fun setSystemBarColors(context: Context, window: Window, @ColorInt bottomNavColor: Int? = null) {
setStatusBarColor(context, window) setStatusBarColor(context, window)
setNavigationBarColor(context, window, isBottomNavVisible) setNavigationBarColor(context, window, bottomNavColor)
} }
/** /**
* Set the background color of the status bar * Set the background color of the status bar
*/ */
private fun setStatusBarColor(context: Context, window: Window) { private fun setStatusBarColor(context: Context, window: Window) {
window.statusBarColor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { window.statusBarColor =
getThemeColor(context, android.R.attr.colorBackground) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M && !isDarkMode(context)) {
} else {
if (isDarkMode(context)) {
getThemeColor(context, android.R.attr.colorBackground)
} else {
getThemeColor(context, com.google.android.material.R.attr.colorOnBackground) getThemeColor(context, com.google.android.material.R.attr.colorOnBackground)
} else {
getThemeColor(context, android.R.attr.colorBackground)
} }
}
} }
/** /**
@ -50,17 +48,14 @@ object ThemeHelper {
private fun setNavigationBarColor( private fun setNavigationBarColor(
context: Context, context: Context,
window: Window, window: Window,
isBottomNavVisible: Boolean @ColorInt bottomNavColor: Int?
) { ) {
window.navigationBarColor = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M && !isDarkMode(context)) { window.navigationBarColor =
getThemeColor(context, com.google.android.material.R.attr.colorOnBackground) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M && !isDarkMode(context)) {
} else { getThemeColor(context, com.google.android.material.R.attr.colorOnBackground)
if (isBottomNavVisible) {
getThemeColor(context, com.google.android.material.R.attr.colorSurfaceContainer)
} else { } else {
getThemeColor(context, android.R.attr.colorBackground) bottomNavColor ?: getThemeColor(context, android.R.attr.colorBackground)
} }
}
} }
/** /**

View File

@ -50,8 +50,10 @@ import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.models.SearchViewModel import com.github.libretube.ui.models.SearchViewModel
import com.github.libretube.ui.models.SubscriptionsViewModel import com.github.libretube.ui.models.SubscriptionsViewModel
import com.github.libretube.util.UpdateChecker import com.github.libretube.util.UpdateChecker
import com.google.android.material.elevation.SurfaceColors
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import okhttp3.internal.toHexString
class MainActivity : BaseActivity() { class MainActivity : BaseActivity() {
lateinit var binding: ActivityMainBinding lateinit var binding: ActivityMainBinding
@ -111,7 +113,10 @@ class MainActivity : BaseActivity() {
} }
// sets the color if the navigation bar is visible // sets the color if the navigation bar is visible
ThemeHelper.setSystemBarColors(this, window, binding.bottomNav.menu.size() > 0) val bottomNavColor = SurfaceColors.getColorForElevation(this, binding.bottomNav.elevation).takeIf {
binding.bottomNav.menu.size() > 0
}
ThemeHelper.setSystemBarColors(this, window, bottomNavColor)
// set default tab as start fragment // set default tab as start fragment
navController.graph = navController.navInflater.inflate(R.navigation.nav).also { navController.graph = navController.navInflater.inflate(R.navigation.nav).also {

View File

@ -39,7 +39,7 @@ open class BaseActivity : AppCompatActivity() {
// Set the navigation and statusBar color if SDK < 23 // Set the navigation and statusBar color if SDK < 23
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
ThemeHelper.setSystemBarColors(this, window, false) ThemeHelper.setSystemBarColors(this, window)
} }
// set the apps language // set the apps language