Skip to content

Commit

Permalink
fix: correctly display colors in system panel
Browse files Browse the repository at this point in the history
  • Loading branch information
nik-conder authored and Bnyro committed Jan 31, 2024
1 parent 939668d commit 43eed17
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
37 changes: 36 additions & 1 deletion app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Color
import android.os.Build
import android.text.Spanned
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.text.HtmlCompat
Expand All @@ -19,6 +21,38 @@ import com.google.android.material.color.MaterialColors

object ThemeHelper {

/**
* Set the colors of the system bars (status bat and navigation bar)
*/
fun setSystemBarColors(context: Context, window: Window, isBottomNavVisible: Boolean) {

Check failure on line 27 in app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected after opening parenthesis Raw Output: app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt:27:28: error: Newline expected after opening parenthesis (standard:function-signature)

Check failure on line 27 in app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt:27:46: error: Parameter should start on a newline (standard:function-signature)

Check failure on line 27 in app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt:27:62: error: Parameter should start on a newline (standard:function-signature)

Check failure on line 27 in app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected before closing parenthesis Raw Output: app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt:27:89: error: Newline expected before closing parenthesis (standard:function-signature)
setStatusBarColor(context, window)
setNavigationBarColor(context, window, isBottomNavVisible)
}

/**
* Set the background color of the status bar
*/
private fun setStatusBarColor(context: Context, window: Window) {

Check failure on line 35 in app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected after opening parenthesis Raw Output: app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt:35:35: error: Newline expected after opening parenthesis (standard:function-signature)

Check failure on line 35 in app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Parameter should start on a newline Raw Output: app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt:35:53: error: Parameter should start on a newline (standard:function-signature)

Check failure on line 35 in app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Newline expected before closing parenthesis Raw Output: app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt:35:67: error: Newline expected before closing parenthesis (standard:function-signature)
window.statusBarColor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

Check failure on line 36 in app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 A multiline expression should start on a new line Raw Output: app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt:36:33: error: A multiline expression should start on a new line (standard:multiline-expression-wrapping)
getThemeColor(context, android.R.attr.colorBackground)
} else {
if (isDarkMode(context)) getThemeColor(context, android.R.attr.colorBackground)

Check failure on line 39 in app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Expected a newline Raw Output: app/src/main/java/com/github/libretube/helpers/ThemeHelper.kt:39:38: error: Expected a newline (standard:if-else-wrapping)
else getThemeColor(context, com.google.android.material.R.attr.colorOnBackground)
}
}

/**
* Set the background color of the navigation bar
*/
private fun setNavigationBarColor(context: Context, window: Window, isBottomNavVisible: Boolean) {
window.navigationBarColor = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M && !isDarkMode(context)) {
getThemeColor(context, com.google.android.material.R.attr.colorOnBackground)
} else {
if (isBottomNavVisible) getThemeColor(context, com.google.android.material.R.attr.colorSurfaceContainer)
else getThemeColor(context, android.R.attr.colorBackground)
}
}

/**
* Set the theme, including accent color and night mode
*/
Expand Down Expand Up @@ -129,7 +163,8 @@ object ThemeHelper {
}

fun isDarkMode(context: Context): Boolean {
val darkModeFlag = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val darkModeFlag =
context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return darkModeFlag == Configuration.UI_MODE_NIGHT_YES
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,8 @@ class MainActivity : BaseActivity() {
R.id.homeFragment
}

// sets the navigation bar color to the previously calculated color
window.navigationBarColor = if (binding.bottomNav.menu.size() > 0) {
ThemeHelper.getThemeColor(this, com.google.android.material.R.attr.colorSurfaceContainer)
} else {
ThemeHelper.getThemeColor(this, android.R.attr.colorBackground)
}
// sets the color if the navigation bar is visible
ThemeHelper.setSystemBarColors(this, window, binding.bottomNav.menu.size() > 0)

// set default tab as start fragment
navController.graph = navController.navInflater.inflate(R.navigation.nav).also {
Expand Down Expand Up @@ -165,7 +161,7 @@ class MainActivity : BaseActivity() {

R.id.searchResultFragment -> {
navController.popBackStack(R.id.searchFragment, true) ||
navController.popBackStack()
navController.popBackStack()
}

else -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.libretube.ui.base

import android.content.pm.ActivityInfo
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.github.libretube.R
Expand Down Expand Up @@ -38,6 +39,10 @@ open class BaseActivity : AppCompatActivity() {
// set the app theme (e.g. Material You)
ThemeHelper.updateTheme(this)

// Set the navigation and statusBar color if SDK < 23
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
ThemeHelper.setSystemBarColors(this, window, false)

// set the apps language
LocaleHelper.updateLanguage(this)

Expand Down

0 comments on commit 43eed17

Please sign in to comment.