Skip to content

Commit

Permalink
设置新增深色模式的配置
Browse files Browse the repository at this point in the history
Former-commit-id: 51b876f
  • Loading branch information
FunnySaltyFish committed Dec 15, 2023
1 parent f6c77da commit 1a77457
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 17 deletions.
46 changes: 37 additions & 9 deletions base-core/src/main/java/com/funny/translation/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.annotation.StringRes
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
Expand All @@ -17,16 +18,19 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.getValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import com.funny.cmaterialcolors.MaterialColors
import com.funny.data_saver.core.mutableDataSaverStateOf
import com.funny.translation.AppConfig
import com.funny.translation.BaseApplication
import com.funny.translation.core.R
import com.funny.translation.helper.DataSaverUtils
import com.funny.translation.helper.DateUtils
import com.funny.translation.helper.DeviceUtils
import com.funny.translation.helper.string
import com.funny.translation.helper.toastOnUi
import com.kyant.monet.LocalTonalPalettes
import com.kyant.monet.PaletteStyle
Expand Down Expand Up @@ -150,9 +154,18 @@ sealed class ThemeType(val id: Int) {
}
}

enum class LightDarkMode(@StringRes val descId: Int) {
Light(R.string.always_light), Dark(R.string.always_dark), System(R.string.follow_system);

override fun toString(): String {
return string(descId)
}
}

object ThemeConfig {
private const val TAG = "ThemeConfig"
var sThemeType: MutableState<ThemeType> = mutableDataSaverStateOf(DataSaverUtils, "theme_type", ThemeType.Default)
val sThemeType: MutableState<ThemeType> = mutableDataSaverStateOf(DataSaverUtils, "theme_type", ThemeType.Default)
val lightDarkMode: MutableState<LightDarkMode> = mutableDataSaverStateOf(DataSaverUtils, "light_dark_mode", LightDarkMode.System)

fun updateThemeType(new: ThemeType){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S && new == ThemeType.DynamicNative) {
Expand All @@ -172,11 +185,27 @@ object ThemeConfig {
sThemeType.value = new
Log.d(TAG, "updateThemeType: $new")
}

fun updateLightDarkMode(new: LightDarkMode){
lightDarkMode.value = new
// Log.d(TAG, "updateLightDarkMode: $new")
}
}

@Composable
@ReadOnlyComposable
private fun calcDark(): Boolean {
val lightDarkMode by ThemeConfig.lightDarkMode
return when(lightDarkMode) {
LightDarkMode.Light -> false
LightDarkMode.Dark -> true
LightDarkMode.System -> isSystemInDarkTheme()
}
}

@Composable
fun TransTheme(
dark: Boolean = isSystemInDarkTheme(),
dark: Boolean = calcDark(),
hideStatusBar: Boolean = false,
content: @Composable () -> Unit
) {
Expand All @@ -196,18 +225,17 @@ fun TransTheme(

val mContent = @Composable {
// SystemBarSettings(hideStatusBar)
val darkTheme = isSystemInDarkTheme()
val context = LocalContext.current as ComponentActivity
val c = MaterialTheme.colorScheme.primaryContainer.toArgb()
DisposableEffect(darkTheme) {
DisposableEffect(dark) {
context.enableEdgeToEdge(
statusBarStyle = SystemBarStyle.auto(
android.graphics.Color.TRANSPARENT,
android.graphics.Color.TRANSPARENT,
) { darkTheme },
) { dark },
navigationBarStyle =
if (darkTheme) SystemBarStyle.dark(transparent)
else SystemBarStyle.light(transparent, c),
if (dark) SystemBarStyle.dark(transparent)
else SystemBarStyle.light(transparent, c),
)
onDispose {}
}
Expand Down Expand Up @@ -239,7 +267,7 @@ fun TransTheme(
val ColorScheme.isLight: Boolean
@Composable
@ReadOnlyComposable
get() = !isSystemInDarkTheme()
get() = !calcDark()

@Composable
fun MonetTheme(color: Color, content: @Composable () -> Unit) {
Expand All @@ -252,7 +280,7 @@ fun MonetTheme(color: Color, content: @Composable () -> Unit) {
)
) {
MaterialTheme(
colorScheme = dynamicColorScheme(isDark = isSystemInDarkTheme()),
colorScheme = dynamicColorScheme(isDark = calcDark()),
content = content
)
}
Expand Down
2 changes: 2 additions & 0 deletions base-core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@
<string name="unsupported_language">此语种暂不支持</string>
<string name="not_registered">您似乎没有注册过,请先注册账号吧~</string>
<string name="err_write_text">写入文件时发生错误</string>
<string name="always_light">始终浅色</string>
<string name="always_dark">始终深色</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.funny.jetsetting.core

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ListItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Switch
Expand Down Expand Up @@ -138,6 +140,7 @@ fun JetSettingDialog(
imageVector: ImageVector? = null,
resourceId: Int? = null,
text: String,
description: String? = null,
dialogTitle: String = stringResource(id = R.string.hint),
confirmButtonAction: (() -> Unit)? = EmptyAction,
confirmButtonText: String = stringResource(id = R.string.confirm),
Expand Down Expand Up @@ -182,6 +185,7 @@ fun JetSettingDialog(
JetSettingTile(
modifier = modifier,
text = text,
description = description,
imageVector = imageVector,
resourceId = resourceId,
onClick = {
Expand All @@ -195,6 +199,7 @@ fun <E> JetSettingListDialog(
modifier: Modifier = Modifier,
list: ImmutableList<E>,
text: String,
description: String? = null,
imageVector: ImageVector? = null,
resourceId: Int? = null,
selected: E,
Expand All @@ -207,6 +212,7 @@ fun <E> JetSettingListDialog(
JetSettingDialog(
modifier = modifier,
text = text,
description = description,
resourceId = resourceId,
imageVector = imageVector,
confirmButtonAction = confirmButtonAction,
Expand All @@ -218,17 +224,14 @@ fun <E> JetSettingListDialog(
verticalArrangement = Arrangement.spacedBy(4.dp),
) {
itemsIndexed(list) { i, item ->
SettingBaseItem(
modifier = Modifier.padding(vertical = 4.dp),
title = {
ListItem(
modifier = Modifier.clickable { updateSelected(item) },
headlineContent = {
Text(text = item.toString())
},
action = {
trailingContent = {
RadioButton(selected = selected == item, onClick = { updateSelected(item) })
},
onClick = {
updateSelected(item)
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.funny.translation.codeeditor.ui.editor.EditorSchemes
import com.funny.translation.helper.DeviceUtils
import com.funny.translation.helper.JsonX
import com.funny.translation.sign.SignUtils
import com.funny.translation.theme.LightDarkMode
import com.funny.translation.theme.ThemeType
import com.funny.translation.translate.ui.TranslateScreen
import com.funny.translation.translate.ui.long_text.EditablePrompt
Expand Down Expand Up @@ -110,6 +111,7 @@ class FunnyApplication : BaseApplication() {
registerTypeConverters<TranslateScreen>(save = TranslateScreen.Saver, restore = TranslateScreen.Restorer)
registerTypeConverters<ChatMemory>(save = ChatMemory.Saver, restore = ChatMemory.Restorer)
registerTypeConverters<EditablePrompt>(save = { JsonX.toJson(it) }, restore = { JsonX.fromJson(it) })
registerTypeConverters<LightDarkMode>(save = { it.name }, restore = { LightDarkMode.valueOf(it) })
}

@RequiresApi(Build.VERSION_CODES.O)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DarkMode
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Sort
import androidx.compose.material3.Checkbox
Expand Down Expand Up @@ -63,6 +64,8 @@ import com.funny.translation.helper.LocaleUtils
import com.funny.translation.helper.string
import com.funny.translation.helper.toastOnUi
import com.funny.translation.network.ServiceCreator
import com.funny.translation.theme.LightDarkMode
import com.funny.translation.theme.ThemeConfig
import com.funny.translation.translate.Language
import com.funny.translation.translate.LocalNavController
import com.funny.translation.translate.R
Expand All @@ -84,6 +87,9 @@ import org.burnoutcrew.reorderable.rememberReorderState
import org.burnoutcrew.reorderable.reorderable

private const val TAG = "SettingScreen"
private val languages = AppLanguage.values().toList().toImmutableList()
private val lightDarkModes = LightDarkMode.values().toList().toImmutableList()


@Composable
fun SettingsScreen() {
Expand Down Expand Up @@ -113,6 +119,8 @@ fun SettingsScreen() {
// 设置应用显示的语言
// 跟随系统、简体中文、英语
SelectAppLanguage()
// 设置应用的深浅色模式
SelectLightDarkMode()
}
SettingItemCategory(
title = {
Expand Down Expand Up @@ -234,13 +242,13 @@ fun SettingsScreen() {

@Composable
private fun SelectAppLanguage() {
val languages = AppLanguage.values().toList().toImmutableList()
val context = LocalContext.current
var tempLanguage by remember { mutableStateOf(LocaleUtils.getAppLanguage()) }

JetSettingListDialog(
list = languages,
text = stringResource(id = R.string.app_language),
description = tempLanguage.description,
resourceId = R.drawable.ic_language_select,
selected = tempLanguage,
updateSelected = {
Expand All @@ -257,6 +265,22 @@ private fun SelectAppLanguage() {
)
}

@Composable
private fun SelectLightDarkMode() {
var lightDarkMode by ThemeConfig.lightDarkMode

JetSettingListDialog(
list = lightDarkModes,
text = stringResource(id = R.string.app_light_dark_mode),
description = stringResource(id = lightDarkMode.descId),
imageVector = Icons.Default.DarkMode,
selected = lightDarkMode,
updateSelected = {
lightDarkMode = it
}
)
}

@Composable
private fun DevSetBaseUrl() {
var text by remember {
Expand Down
1 change: 1 addition & 0 deletions translate/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,5 @@
<string name="reset">还原</string>
<string name="unparseable_prompt">无法解析的错误输出:</string>
<string name="chat_prompt_help">Prompt指定了模型的任务,它能很大程度上影响翻译质量。如果有特殊的需求(比如语种、表达方式、专业领域等),建议指定、\n\n必须和外语类学习、翻译相关,修改时会进行判定。无论判定是否通过,均会扣除判定所用的点数,谨慎修改,</string>
<string name="app_light_dark_mode">深色模式</string>
</resources>

0 comments on commit 1a77457

Please sign in to comment.