Skip to content

Commit

Permalink
theme changes
Browse files Browse the repository at this point in the history
  • Loading branch information
crackededed committed Jan 16, 2024
1 parent 552f597 commit d7abee6
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.andreyasadchy.xtra.ui.login

import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.ViewGroup
import android.webkit.CookieManager
Expand Down Expand Up @@ -177,15 +176,7 @@ class LoginActivity : AppCompatActivity() {
clearCookies()
}
with(binding.webView) {
val theme = if (prefs().getBoolean(C.UI_THEME_FOLLOW_SYSTEM, false)) {
when (resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
Configuration.UI_MODE_NIGHT_YES -> prefs().getString(C.UI_THEME_DARK_ON, "0")!!
else -> prefs().getString(C.UI_THEME_DARK_OFF, "2")!!
}
} else {
prefs().getString(C.THEME, "0")!!
}
if (!theme.isLightTheme) {
if (!isLightTheme) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(this.settings, WebSettingsCompat.FORCE_DARK_ON)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class MainActivity : AppCompatActivity(), SlidingLayout.Listener {
}
}
private lateinit var prefs: SharedPreferences
private var theme: String? = null

//Lifecycle methods

Expand Down Expand Up @@ -165,7 +164,7 @@ class MainActivity : AppCompatActivity(), SlidingLayout.Listener {
IntegrityDialog.show(supportFragmentManager)
}
}
theme = applyTheme()
applyTheme()
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setNavBarColor(isInPortraitOrientation)
Expand Down Expand Up @@ -212,17 +211,17 @@ class MainActivity : AppCompatActivity(), SlidingLayout.Listener {
window.navigationBarColor = if (isPortrait) {
Color.TRANSPARENT
} else {
ContextCompat.getColor(this, if (!theme.isLightTheme) R.color.darkScrim else R.color.lightScrim)
ContextCompat.getColor(this, if (!isLightTheme) R.color.darkScrim else R.color.lightScrim)
}
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> {
if (!theme.isLightTheme) {
if (!isLightTheme) {
window.navigationBarColor = if (isPortrait) Color.TRANSPARENT else ContextCompat.getColor(this, R.color.darkScrim)
}
}
Build.VERSION.SDK_INT < Build.VERSION_CODES.M -> {
@Suppress("DEPRECATION")
if (!theme.isLightTheme) {
if (!isLightTheme) {
if (isPortrait) {
window.navigationBarColor = Color.TRANSPARENT
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,7 @@ abstract class BasePlayerFragment : BaseNetworkFragment(), LifecycleListener, Sl
isPortrait = activity.isInPortraitOrientation
activity.onBackPressedDispatcher.addCallback(this, backPressedCallback)
WindowCompat.getInsetsController(requireActivity().window, requireActivity().window.decorView).systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
val theme = if (prefs.getBoolean(C.UI_THEME_FOLLOW_SYSTEM, false)) {
when (resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
Configuration.UI_MODE_NIGHT_YES -> prefs.getString(C.UI_THEME_DARK_ON, "0")!!
else -> prefs.getString(C.UI_THEME_DARK_OFF, "2")!!
}
} else {
prefs.getString(C.THEME, "0")!!
}
isLightTheme = theme.isLightTheme
isLightTheme = requireContext().isLightTheme
}

override fun onStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.core.content.res.use
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.preference.PreferenceManager
Expand All @@ -36,7 +37,7 @@ fun Context.convertPixelsToDp(pixels: Float) = TypedValue.applyDimension(TypedVa
val Context.displayDensity
get() = this.resources.displayMetrics.density

fun Activity.applyTheme(): String {
fun Activity.applyTheme() {
val theme = if (prefs().getBoolean(C.UI_THEME_FOLLOW_SYSTEM, false)) {
when (resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
Configuration.UI_MODE_NIGHT_YES -> prefs().getString(C.UI_THEME_DARK_ON, "0")!!
Expand Down Expand Up @@ -70,9 +71,11 @@ fun Activity.applyTheme(): String {
if (listOf("4", "6", "5").contains(theme)) {
DynamicColors.applyToActivityIfAvailable(this,
DynamicColorsOptions.Builder().apply {
if (theme == "6") {
setThemeOverlay(R.style.AmoledDynamicOverlay)
}
setThemeOverlay(when(theme) {
"6" -> R.style.AmoledDynamicOverlay
"5" -> R.style.LightDynamicOverlay
else -> R.style.DarkDynamicOverlay
})
}.build()
)
}
Expand All @@ -87,7 +90,7 @@ fun Activity.applyTheme(): String {
else -> R.style.AppCompatDarkTheme
})
}
val isLightTheme = theme.isLightTheme
val isLightTheme = this.isLightTheme
WindowInsetsControllerCompat(window, window.decorView).run {
isAppearanceLightStatusBars = isLightTheme
isAppearanceLightNavigationBars = isLightTheme
Expand Down Expand Up @@ -117,7 +120,6 @@ fun Activity.applyTheme(): String {
else -> WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT
}
}
return theme
}

fun Context.getAlertDialogBuilder(): AlertDialog.Builder {
Expand All @@ -128,6 +130,11 @@ fun Context.getAlertDialogBuilder(): AlertDialog.Builder {
}
}

val Context.isLightTheme
get() = obtainStyledAttributes(intArrayOf(androidx.appcompat.R.attr.isLightTheme)).use {
it.getBoolean(0, false)
}

val Context.isInPortraitOrientation
get() = resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package com.github.andreyasadchy.xtra.util

fun String.nullIfEmpty() = takeIf { it.isNotEmpty() }

val String?.isLightTheme
get() = listOf("2", "5").contains(this)
fun String.nullIfEmpty() = takeIf { it.isNotEmpty() }
1 change: 1 addition & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<color name="darkScrimOnLightSurface">#8D8B8D</color>
<color name="accent">#007DCA</color>
<color name="surfaceAmoled">#ff000000</color>
<color name="surfaceContainerAmoled">#0C0C0C</color>
<color name="switchTrackAmoled">#4F5053</color>
<color name="primaryBlue">#b5c4ff</color>
<color name="secondaryContainerBlue">#254290</color>
Expand Down
60 changes: 59 additions & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,24 @@
<item name="colorPrimary">@android:color/white</item>
<item name="colorSurface">@color/surfaceAmoled</item>
<item name="android:colorBackground">@color/surfaceAmoled</item>
<item name="colorSurfaceContainerLow">@color/surfaceAmoled</item>
<item name="colorSurfaceContainer">@color/surfaceContainerAmoled</item>
<item name="chatStatus">@color/chatStatusDark</item>
<item name="sortBarTextColor">?android:textColorSecondary</item>
<item name="dialogPadding">8dp</item>
<item name="dialogLayoutPadding">8dp</item>
<item name="cardStyle">@style/Card</item>
<item name="cardStyle">@style/CardAmoled</item>
<item name="bottomSheetButtonStyle">@style/Widget.Material3.Button.ElevatedButton</item>
<item name="elevatedButtonStyle">@style/Widget.Material3.Button.ElevatedButton</item>
<item name="dialogButtonStyle">@style/Widget.Material3.Button.TextButton.Dialog</item>
<item name="tabStyle">@style/Widget.Material3.TabLayout.OnSurface</item>
<item name="radioButtonStyle">@style/RadioButton</item>
<item name="checkboxStyle">@style/CheckBox</item>
<item name="materialSwitchStyle">@style/SwitchAmoled</item>
<item name="linearProgressIndicatorStyle">@style/LinearProgressIndicatorAmoled</item>
<item name="bottomNavigationStyle">@style/BottomNavigationAmoled</item>
<item name="bottomSheetDialogTheme">@style/BottomSheet</item>
<item name="materialAlertDialogTheme">@style/Dialog</item>
<item name="preferenceTheme">@style/PreferenceTheme</item>
<item name="android:navigationBarColor">@color/darkScrim</item>
<item name="android:statusBarColor">@android:color/transparent</item>
Expand Down Expand Up @@ -149,9 +155,25 @@
<style name="AppCompatAmoledTheme" parent="BaseAppCompatAmoledTheme" />
<style name="AppCompatBlueTheme" parent="BaseAppCompatBlueTheme" />

<style name="DarkDynamicOverlay" parent="ThemeOverlay.Material3.DynamicColors.Dark">
<item name="materialSwitchStyle">@style/Widget.Material3.CompoundButton.MaterialSwitch</item>
</style>

<style name="LightDynamicOverlay" parent="ThemeOverlay.Material3.DynamicColors.Light">
<item name="materialSwitchStyle">@style/Widget.Material3.CompoundButton.MaterialSwitch</item>
</style>

<style name="AmoledDynamicOverlay" parent="ThemeOverlay.Material3.DynamicColors.Dark">
<item name="colorSurface">@color/surfaceAmoled</item>
<item name="android:colorBackground">@color/surfaceAmoled</item>
<item name="colorSurfaceContainerLow">@color/surfaceAmoled</item>
<item name="colorSurfaceContainer">@color/surfaceContainerAmoled</item>
<item name="cardStyle">@style/CardAmoled</item>
<item name="materialSwitchStyle">@style/Widget.Material3.CompoundButton.MaterialSwitch</item>
<item name="linearProgressIndicatorStyle">@style/LinearProgressIndicatorAmoled</item>
<item name="bottomNavigationStyle">@style/BottomNavigationAmoled</item>
<item name="bottomSheetDialogTheme">@style/BottomSheet</item>
<item name="materialAlertDialogTheme">@style/Dialog</item>
</style>

<style name="Thumbnail">
Expand Down Expand Up @@ -190,6 +212,15 @@
<item name="android:layout_margin">8dp</item>
</style>

<style name="CardAmoled" parent="Widget.Material3.CardView.Elevated">
<item name="materialThemeOverlay">@style/CardOverlayAmoled</item>
<item name="android:layout_margin">8dp</item>
</style>

<style name="CardOverlayAmoled">
<item name="colorPrimary">?attr/colorSurface</item>
</style>

<style name="RadioButton" parent="Widget.Material3.CompoundButton.RadioButton">
<item name="android:padding">10dp</item>
</style>
Expand Down Expand Up @@ -218,6 +249,20 @@
<item name="colorPrimaryContainer">@android:color/white</item>
</style>

<style name="LinearProgressIndicatorAmoled" parent="Widget.Material3.LinearProgressIndicator">
<item name="indicatorColor">@android:color/white</item>
</style>

<style name="BottomNavigationAmoled" parent="Widget.Material3.BottomNavigationView">
<item name="materialThemeOverlay">@style/BottomNavigationOverlayAmoled</item>
<item name="elevation">0dp</item>
</style>

<style name="BottomNavigationOverlayAmoled">
<item name="colorSurfaceContainer">@android:color/black</item>
<item name="colorSecondaryContainer">@color/surfaceContainerAmoled</item>
</style>

<style name="BottomNavigationBlue" parent="Widget.Material3.BottomNavigationView">
<item name="materialThemeOverlay">@style/BottomNavigationOverlayBlue</item>
</style>
Expand All @@ -226,6 +271,18 @@
<item name="colorSecondaryContainer">@color/secondaryContainerBlue</item>
</style>

<style name="BottomSheet" parent="ThemeOverlay.Material3.BottomSheetDialog">
<item name="bottomSheetStyle">@style/BottomSheetModal</item>
</style>

<style name="BottomSheetModal" parent="Widget.Material3.BottomSheet.Modal">
<item name="android:elevation">0dp</item>
</style>

<style name="Dialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
<item name="android:background">?attr/colorSurface</item>
</style>

<style name="PreferenceTheme" parent="@style/PreferenceThemeOverlay">
<!-- icons -->
<item name="android:tint">?attr/colorControlNormal</item>
Expand Down Expand Up @@ -363,6 +420,7 @@
<item name="colorSurface">@android:color/black</item>
<item name="android:windowBackground">?attr/colorSurface</item>
<item name="android:colorBackground">?attr/colorSurface</item>
<item name="colorSurfaceContainer">@color/surfaceContainerAmoled</item>
<item name="colorControlNormal">@android:color/white</item>
<item name="android:textColor">?attr/colorControlNormal</item>
<item name="android:textColorSecondary">@color/secondaryTextColorDark</item>
Expand Down

0 comments on commit d7abee6

Please sign in to comment.