Merge pull request #767 from Bnyro/master

cleanup themes
This commit is contained in:
Bnyro 2022-07-12 15:41:49 +00:00 committed by GitHub
commit a235f584a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 41 deletions

View File

@ -37,7 +37,6 @@ import com.github.libretube.util.CronetHelper
import com.github.libretube.util.LocaleHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.ThemeHelper
import com.google.android.material.color.DynamicColors
import com.google.android.material.elevation.SurfaceColors
import com.google.android.material.navigation.NavigationBarView
@ -50,20 +49,9 @@ class MainActivity : AppCompatActivity() {
private var startFragmentId = R.id.homeFragment
override fun onCreate(savedInstanceState: Bundle?) {
/**
* apply dynamic colors if enabled
*/
val materialColorsEnabled = PreferenceHelper
.getString(this, "accent_color", "purple") == "my"
if (materialColorsEnabled) {
// apply dynamic colors to the current activity
DynamicColors.applyToActivityIfAvailable(this)
// apply dynamic colors to the all other activities
DynamicColors.applyToActivitiesIfAvailable(application)
}
// set the theme
// set the app theme (e.g. Material You)
ThemeHelper.updateTheme(this)
// set the language
LocaleHelper.updateLanguage(this)

View File

@ -5,25 +5,15 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.github.libretube.R
import com.github.libretube.databinding.ActivityNointernetBinding
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.ThemeHelper
import com.google.android.material.color.DynamicColors
import com.google.android.material.snackbar.Snackbar
class NoInternetActivity : AppCompatActivity() {
private lateinit var binding: ActivityNointernetBinding
override fun onCreate(savedInstanceState: Bundle?) {
/**
* apply dynamic colors if enabled
*/
val materialColorsEnabled = PreferenceHelper
.getString(this, "accent_color", "purple") == "my"
if (materialColorsEnabled) {
DynamicColors.applyToActivityIfAvailable(this)
}
ThemeHelper.updateTheme(this)
super.onCreate(savedInstanceState)
binding = ActivityNointernetBinding.inflate(layoutInflater)

View File

@ -8,17 +8,15 @@ import com.github.libretube.R
import com.github.libretube.databinding.ActivitySettingsBinding
import com.github.libretube.preferences.MainSettings
import com.github.libretube.util.ThemeHelper
import com.google.android.material.color.DynamicColors
class SettingsActivity : AppCompatActivity() {
val TAG = "SettingsActivity"
lateinit var binding: ActivitySettingsBinding
override fun onCreate(savedInstanceState: Bundle?) {
DynamicColors.applyToActivityIfAvailable(this)
ThemeHelper.updateTheme(this)
// makes the preference dialogs use material dialogs
// apply the theme for the preference dialogs
setTheme(R.style.MaterialAlertDialog)
super.onCreate(savedInstanceState)

View File

@ -7,8 +7,8 @@ import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.github.libretube.BuildConfig
import com.github.libretube.Globals
import com.github.libretube.R
import com.github.libretube.activities.isCurrentViewMainSettings
import com.github.libretube.dialogs.RequireRestartDialog
import com.github.libretube.util.ThemeHelper
import com.github.libretube.util.checkUpdate
@ -94,7 +94,7 @@ class MainSettings : PreferenceFragmentCompat() {
}
private fun navigateToSettingsFragment(newFragment: Fragment) {
isCurrentViewMainSettings = false
Globals.isCurrentViewMainSettings = false
parentFragmentManager.beginTransaction()
.replace(R.id.settings, newFragment)
.commitNow()

View File

@ -12,23 +12,42 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.core.text.HtmlCompat
import com.github.libretube.R
import com.github.libretube.preferences.PreferenceHelper
import com.google.android.material.color.DynamicColors
object ThemeHelper {
fun updateTheme(context: Context) {
updateAccentColor(context)
updateThemeMode(context)
fun updateTheme(activity: AppCompatActivity) {
updateAccentColor(activity)
updateThemeMode(activity)
}
private fun updateAccentColor(context: Context) {
when (PreferenceHelper.getString(context, "accent_color", "purple")) {
"my" -> context.setTheme(R.style.MaterialYou)
"red" -> context.setTheme(R.style.Theme_Red)
"blue" -> context.setTheme(R.style.Theme_Blue)
"yellow" -> context.setTheme(R.style.Theme_Yellow)
"green" -> context.setTheme(R.style.Theme_Green)
"purple" -> context.setTheme(R.style.Theme_Purple)
private fun updateAccentColor(activity: AppCompatActivity) {
val theme = when (
PreferenceHelper.getString(
activity,
"accent_color",
"purple"
)
) {
"my" -> {
applyDynamicColors(activity)
R.style.MaterialYou
}
"red" -> R.style.Theme_Red
"blue" -> R.style.Theme_Blue
"yellow" -> R.style.Theme_Yellow
"green" -> R.style.Theme_Green
"purple" -> R.style.Theme_Purple
else -> R.style.Theme_Purple
}
activity.setTheme(theme)
}
private fun applyDynamicColors(activity: AppCompatActivity) {
/**
* apply dynamic colors to the activity
*/
DynamicColors.applyToActivityIfAvailable(activity)
}
private fun updateThemeMode(context: Context) {