new theme options

This commit is contained in:
Finnley Somdahl 2023-11-26 21:00:46 -06:00
parent b4093b0c47
commit e5f2bb6566
11 changed files with 309 additions and 50 deletions

View file

@ -3,27 +3,40 @@ package ani.dantotsu.themes
import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.graphics.Bitmap
import android.util.Log
import ani.dantotsu.FileUrl
import ani.dantotsu.R
import ani.dantotsu.logger
import ani.dantotsu.media.manga.mangareader.BaseImageAdapter.Companion.loadBitmap
import ani.dantotsu.media.manga.mangareader.BaseImageAdapter.Companion.loadBitmap_old
import com.google.android.material.color.DynamicColors
import com.google.android.material.color.DynamicColorsOptions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
class ThemeManager(private val context: Context) {
fun applyTheme() {
fun applyTheme(fromImage: Bitmap? = null) {
val useOLED = context.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getBoolean("use_oled", false) && isDarkThemeActive(context)
if(context.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getBoolean("use_material_you", false)){
if (useOLED) {
val options = DynamicColorsOptions.Builder()
.setThemeOverlay(R.style.AppTheme_Amoled)
.build()
val activity = context as Activity
DynamicColors.applyToActivityIfAvailable(activity, options)
} else {
val activity = context as Activity
DynamicColors.applyToActivityIfAvailable(activity)
}
val useCustomTheme = context.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getBoolean("use_custom_theme", false)
val customTheme = context.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getInt("custom_theme", 16712221)
val useSource = context.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getBoolean("use_source_theme", false)
val useMaterial = context.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getBoolean("use_material_you", false)
if(useSource){
applyDynamicColors(useMaterial, context, useOLED, fromImage, useCustom = if(useCustomTheme) customTheme else null)
return
} else if (useCustomTheme) {
applyDynamicColors(useMaterial, context, useOLED, useCustom = customTheme)
return
} else {
val returnedEarly = applyDynamicColors(useMaterial, context, useOLED, useCustom = null)
if(!returnedEarly) return
}
val theme = context.getSharedPreferences("Dantotsu", Context.MODE_PRIVATE).getString("theme", "PURPLE")!!
@ -42,6 +55,35 @@ class ThemeManager(private val context: Context) {
context.setTheme(themeToApply)
}
private fun applyDynamicColors(useMaterialYou: Boolean, context: Context, useOLED: Boolean, bitmap: Bitmap? = null, useCustom: Int? = null): Boolean {
val builder = DynamicColorsOptions.Builder()
var needMaterial = true
// Set content-based source if a bitmap is provided
if (bitmap != null) {
builder.setContentBasedSource(bitmap)
needMaterial = false
}else if (useCustom != null){
builder.setContentBasedSource(useCustom)
needMaterial = false
}
// Set the theme overlay based on conditions
if (useOLED) {
builder.setThemeOverlay(R.style.AppTheme_Amoled)
needMaterial = false
}
if(needMaterial && !useMaterialYou) return true
// Build the options
val options = builder.build()
// Apply the dynamic colors to the activity
val activity = context as Activity
DynamicColors.applyToActivityIfAvailable(activity, options)
return false
}
private fun isDarkThemeActive(context: Context): Boolean {
return when (context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_YES -> true