Add system dark theme (#1208)

This commit is contained in:
epireyn 2024-07-29 01:01:45 +02:00 committed by GitHub
parent 150ad5fc9f
commit b2f08847e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 49 additions and 7 deletions

View file

@ -97,7 +97,7 @@
-->
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout|keyboard|keyboardHidden|navigation"
android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout|keyboard|keyboardHidden|navigation|uiMode"
android:exported="true"
android:launchMode="singleTask"
android:resizeableActivity="true"

View file

@ -5,6 +5,7 @@ import android.app.Activity
import android.app.PictureInPictureParams
import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.util.DisplayMetrics
@ -276,12 +277,35 @@ object CommonActivity {
}
}
fun updateTheme(act: Activity) {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(act)
if (settingsManager
.getString(act.getString(R.string.app_theme_key), "AmoledLight") == "System"
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
loadThemes(act)
}
}
private fun mapSystemTheme(act: Activity): Int {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val currentNightMode =
act.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return when (currentNightMode) {
Configuration.UI_MODE_NIGHT_NO -> R.style.LightMode // Night mode is not active, we're using the light theme
else -> R.style.AppTheme // Night mode is active, we're using dark theme
}
} else {
return R.style.AppTheme
}
}
fun loadThemes(act: Activity?) {
if (act == null) return
val settingsManager = PreferenceManager.getDefaultSharedPreferences(act)
val currentTheme =
when (settingsManager.getString(act.getString(R.string.app_theme_key), "AmoledLight")) {
"System" -> mapSystemTheme(act)
"Black" -> R.style.AppTheme
"Light" -> R.style.LightMode
"Amoled" -> R.style.AmoledMode
@ -352,8 +376,8 @@ object CommonActivity {
currentLook = currentLook.parent as? View ?: break
}*/
private fun View.hasContent() : Boolean {
return isShown && when(this) {
private fun View.hasContent(): Boolean {
return isShown && when (this) {
//is RecyclerView -> this.childCount > 0
is ViewGroup -> this.childCount > 0
else -> true

View file

@ -68,6 +68,7 @@ import com.lagradost.cloudstream3.CommonActivity.screenHeight
import com.lagradost.cloudstream3.CommonActivity.setActivityInstance
import com.lagradost.cloudstream3.CommonActivity.showToast
import com.lagradost.cloudstream3.CommonActivity.updateLocale
import com.lagradost.cloudstream3.CommonActivity.updateTheme
import com.lagradost.cloudstream3.databinding.ActivityMainBinding
import com.lagradost.cloudstream3.databinding.ActivityMainTvBinding
import com.lagradost.cloudstream3.databinding.BottomResultviewPreviewBinding
@ -484,6 +485,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener, BiometricCa
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
updateLocale() // android fucks me by chaining lang when rotating the phone
updateTheme(this) // Update if system theme
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment

View file

@ -88,10 +88,9 @@ class SettingsUI : PreferenceFragmentCompat() {
getPref(R.string.app_theme_key)?.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.themes_names).toMutableList()
val prefValues = resources.getStringArray(R.array.themes_names_values).toMutableList()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { // remove monet on android 11 and less
val removeIncompatible = { text: String ->
val toRemove = prefValues
.mapIndexed { idx, s -> if (s.startsWith("Monet")) idx else null }
.mapIndexed { idx, s -> if (s.startsWith(text)) idx else null }
.filterNotNull()
var offset = 0
toRemove.forEach { idx ->
@ -100,6 +99,12 @@ class SettingsUI : PreferenceFragmentCompat() {
offset += 1
}
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { // remove monet on android 11 and less
removeIncompatible("Monet")
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { // Remove system on android 9 and less
removeIncompatible("System")
}
val currentLayout =
settingsManager.getString(getString(R.string.app_theme_key), prefValues.first())
@ -123,7 +128,8 @@ class SettingsUI : PreferenceFragmentCompat() {
}
getPref(R.string.primary_color_key)?.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.themes_overlay_names).toMutableList()
val prefValues = resources.getStringArray(R.array.themes_overlay_names_values).toMutableList()
val prefValues =
resources.getStringArray(R.array.themes_overlay_names_values).toMutableList()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) { // remove monet on android 11 and less
val toRemove = prefValues

View file

@ -247,6 +247,7 @@
<item>Gris</item>
<item>Amoled</item>
<item>Destello</item>
<item>Sistema</item>
<item>Material You</item>
</string-array>
<string-array name="themes_names_values">
@ -254,6 +255,7 @@
<item>Black</item>
<item>Amoled</item>
<item>Light</item>
<item>System</item>
<item>Monet</item>
</string-array>

View file

@ -256,6 +256,7 @@
<item>Szary</item>
<item>Amoled</item>
<item>Flashbang</item>
<item>System</item>
<item>Material You</item>
</string-array>
<string-array name="themes_names_values">
@ -263,6 +264,7 @@
<item>Black</item>
<item>Amoled</item>
<item>Light</item>
<item>System</item>
<item>Monet</item>
</string-array>

View file

@ -281,6 +281,7 @@
<item>Gri</item>
<item>Amoled</item>
<item>Flaş Bombası</item>
<item>Sistem</item>
<item>Material You</item>
</string-array>
<string-array name="themes_names_values">
@ -288,6 +289,7 @@
<item>Black</item>
<item>Amoled</item>
<item>Light</item>
<item>System</item>
<item>Monet</item>
</string-array>

View file

@ -248,6 +248,7 @@
<item>Xám</item>
<item>Amoled</item>
<item>Sáng</item>
<item>Hệ thống</item>
<item>Material You</item>
</string-array>
<string-array name="themes_names_values">
@ -255,6 +256,7 @@
<item>Black</item>
<item>Amoled</item>
<item>Light</item>
<item>System</item>
<item>Monet</item>
</string-array>

View file

@ -318,6 +318,7 @@
<item>Gray</item>
<item>Amoled</item>
<item>Flashbang</item>
<item>System</item>
<item>Material You</item>
</string-array>
<string-array name="themes_names_values">
@ -325,6 +326,7 @@
<item>Black</item>
<item>Amoled</item>
<item>Light</item>
<item>System</item>
<item>Monet</item>
</string-array>