switch Material you theme and black theme without relaunch app

This commit is contained in:
BinTianqi
2024-05-12 21:24:03 +08:00
parent 2009baac40
commit bbb6875be9
16 changed files with 85 additions and 96 deletions

View File

@@ -5,9 +5,7 @@ import android.widget.Toast
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
@@ -25,7 +23,6 @@ import androidx.compose.ui.unit.dp
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavHostController
import com.bintianqi.owndroid.R
import com.bintianqi.owndroid.ui.theme.bgColor
import com.bintianqi.owndroid.writeClipBoard
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@@ -173,8 +170,12 @@ fun TopBar(
TopAppBar(
//Text(text = stringResource(titleMap[backStackEntry?.destination?.route]?:R.string.user_restrict))
title = title,
navigationIcon = {NavIcon{if(backStackEntry?.destination?.route=="Home"){navCtrl.navigateUp()}else{localNavCtrl.navigateUp()}}},
colors = TopAppBarDefaults.topAppBarColors(containerColor = bgColor)
navigationIcon = {
NavIcon{
if(backStackEntry?.destination?.route=="Home"){ navCtrl.navigateUp() }else{ localNavCtrl.navigateUp() }
}
},
colors = TopAppBarDefaults.topAppBarColors(containerColor = colorScheme.background)
)
}

View File

@@ -2,11 +2,9 @@ package com.bintianqi.owndroid.ui.theme
import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Build.VERSION
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.*
import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
@@ -79,41 +77,31 @@ private val LightColorScheme = lightColorScheme(
scrim = md_theme_light_scrim
)
var bgColor = Color(0xFF000000)
@Composable
fun SetDarkTheme(){
val dark = isSystemInDarkTheme()
val sharedPref = LocalContext.current.getSharedPreferences("data", Context.MODE_PRIVATE)
val bg = colorScheme.background
val lightBg = colorScheme.primary.copy(alpha = 0.05F)
bgColor = if(dark){
if(sharedPref.getBoolean("blackTheme",true)){ Color(0xFF000000) }else{ bg }
}else{
lightBg
}
}
@Composable
fun OwnDroidTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
materialYou: Boolean,
blackTheme: Boolean,
content: @Composable () -> Unit
) {
SetDarkTheme()
val darkTheme = isSystemInDarkTheme()
val context = LocalContext.current
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
if(!sharedPref.contains("dynamicColor")&&VERSION.SDK_INT>=32){
sharedPref.edit().putBoolean("dynamicColor",true).apply()
}
val dynamicColor = sharedPref.getBoolean("dynamicColor",false)
val colorScheme = when {
dynamicColor && VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
var colorScheme = when {
materialYou && VERSION.SDK_INT>=31 -> {
if(darkTheme){ dynamicDarkColorScheme(context) }else{ dynamicLightColorScheme(context) }
}
darkTheme -> DarkColorScheme
else -> LightColorScheme
}
if(darkTheme&&blackTheme){
colorScheme = colorScheme.copy(background = Color.Black)
}
if(!darkTheme){
colorScheme = colorScheme.copy(background = colorScheme.primary.copy(alpha = 0.05f))
}
val view = LocalView.current
SideEffect {
val window = (view.context as Activity).window