Skip to content

Commit

Permalink
feat: 支持主题
Browse files Browse the repository at this point in the history
  • Loading branch information
jing332 committed Jun 5, 2023
1 parent 7adf357 commit bf56816
Show file tree
Hide file tree
Showing 14 changed files with 1,803 additions and 70 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ dependencies {
// https://github.com/aclassen/ComposeReorderable
implementation "org.burnoutcrew.composereorderable:reorderable:0.9.6"

// https://github.com/gyf-dev/ImmersionBar
implementation 'com.geyifeng.immersionbar:immersionbar:3.2.2'
implementation 'com.geyifeng.immersionbar:immersionbar-ktx:3.2.2'

def accompanist_version = '0.31.3-beta'
implementation "com.google.accompanist:accompanist-systemuicontroller:${accompanist_version}"
implementation "com.google.accompanist:accompanist-navigation-animation:${accompanist_version}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.jing332.tts_dict_editor.const

object ConfigConst {
const val KEY_THEME = "theme"
const val KEY_SOFT_KEYBOARD_TOOLBAR = "softKeyboardToolbar"
const val KEY_DICT_EXPORT_FORMAT = "exportFormat"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import com.funny.data_saver.core.DataSaverPreferences
import com.funny.data_saver.core.mutableDataSaverStateOf
import com.github.jing332.tts_dict_editor.app
import com.github.jing332.tts_dict_editor.const.ConfigConst
import com.github.jing332.tts_dict_editor.ui.theme.AppTheme
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@OptIn(ExperimentalStdlibApi::class)
object AppConfig {
@OptIn(ExperimentalSerializationApi::class)
private val json by lazy {
Expand All @@ -33,10 +35,23 @@ object AppConfig {
list
}
)

registerTypeConverters(
save = { it.id },
restore = { value ->
AppTheme.values().find { it.id == value } ?: AppTheme.DEFAULT
}
)
}

val dataSaverPref = DataSaverPreferences(app.getSharedPreferences("app", 0))

val theme = mutableDataSaverStateOf(
dataSaverInterface = dataSaverPref,
key = ConfigConst.KEY_THEME,
initialValue = AppTheme.DEFAULT
)

val softKeyboardToolbar = mutableDataSaverStateOf<List<Pair<String, String>>>(
dataSaverInterface = dataSaverPref,
key = ConfigConst.KEY_SOFT_KEYBOARD_TOOLBAR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.github.jing332.tts_dict_editor.ui.edit.DictFileEditScreen
import com.github.jing332.tts_dict_editor.ui.theme.AppTheme
import com.github.jing332.tts_dict_editor.ui.widget.Widgets.TransparentSystemBars
import com.github.jing332.tts_server_android.util.longToast
import io.github.lumyuan.turingbox.ui.theme.DictEditorTheme

val LocalNavController = staticCompositionLocalOf<NavHostController> {
error("NavController has not been initialized! ")
Expand All @@ -55,8 +56,8 @@ class MainActivity : ComponentActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)

setContent {
AppTheme {
TransparentSystemBars()
DictEditorTheme {
// TransparentSystemBars()
Surface {
AppNavigation()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jing332.tts_dict_editor.ui

import android.os.Build
import android.os.Bundle
import android.os.SystemClock
import android.widget.Toast
Expand All @@ -21,11 +22,10 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.ripple.LocalRippleTheme
import androidx.compose.material.icons.filled.Style
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.Divider
import androidx.compose.material3.DrawerState
Expand All @@ -37,6 +37,7 @@ import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Text
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
Expand All @@ -63,17 +64,22 @@ import androidx.navigation.Navigator
import com.github.jing332.tts_dict_editor.BuildConfig
import com.github.jing332.tts_dict_editor.R
import com.github.jing332.tts_dict_editor.ui.home.HomeScreen
import com.github.jing332.tts_dict_editor.ui.theme.AppTheme
import com.talhafaki.composablesweettoast.util.SweetToastUtil
import io.github.lumyuan.turingbox.ui.theme.getAppTheme
import io.github.lumyuan.turingbox.ui.theme.setAppTheme
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout
import java.text.SimpleDateFormat
import java.util.Locale


@Composable
fun MainScreen(
drawerState: DrawerState = rememberDrawerState(DrawerValue.Closed),
onFinishedActivity: () -> Unit,
) {
val context = LocalContext.current
// var lastBackDownTime by remember { mutableLongStateOf(0L) }
var toastMsg by remember { mutableStateOf("") }
if (toastMsg.isNotEmpty()) {
Expand All @@ -85,6 +91,28 @@ fun MainScreen(
toastMsg = ""
}

var warnMsg by remember { mutableStateOf("") }
if (warnMsg.isNotEmpty()) {
SweetToastUtil.SweetWarning(
message = warnMsg,
Toast.LENGTH_LONG,
PaddingValues(bottom = 32.dp)
)
warnMsg = ""
}

val isVisibleThemeDialog = remember { mutableStateOf(false) }
if (isVisibleThemeDialog.value)
ThemeSettingsDialog(
onDismissRequest = { isVisibleThemeDialog.value = false },
currentTheme = getAppTheme(),
onChangeTheme = {
if (it == AppTheme.DYNAMIC_COLOR && Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {// SDK < A12
warnMsg = context.getString(R.string.device_not_support_dynamic_theme)
} else setAppTheme(it)
}
)

val snackbarHostState = LocalSnackbarHostState.current
val navController = LocalNavController.current
val scope = rememberCoroutineScope()
Expand Down Expand Up @@ -118,7 +146,8 @@ fun MainScreen(
.width(300.dp)
.background(MaterialTheme.colorScheme.background)
.padding(12.dp),
navController = navController
navController = navController,
isVisibleThemeDialog,
)
}
) {
Expand All @@ -128,7 +157,11 @@ fun MainScreen(

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun DrawerContent(modifier: Modifier, navController: NavHostController) {
private fun DrawerContent(
modifier: Modifier,
navController: NavHostController,
isVisibleThemeDialog: MutableState<Boolean>
) {
val drawerItemIcon = @Composable { img: ImageVector, contentDescription: String ->
Icon(
img,
Expand All @@ -138,15 +171,19 @@ private fun DrawerContent(modifier: Modifier, navController: NavHostController)
)
}

val drawerItem = @Composable { img: ImageVector, targetScreen: AppNavRoutes ->
@Composable
fun drawerItem(
img: ImageVector,
targetScreen: AppNavRoutes,
onClick: () -> Unit = { navController.navigateSingleTop(targetScreen.route) }
) {
NavigationDrawerItem(
icon = { drawerItemIcon(img, stringResource(id = targetScreen.titleResId)) },
label = { Text(text = stringResource(id = targetScreen.titleResId)) },
selected = false,
onClick = { navController.navigateSingleTop(targetScreen.route) }
onClick = onClick,
)
}

Column(modifier = modifier.verticalScroll(rememberScrollState())) {
Spacer(modifier = Modifier.height(24.dp))
val context = LocalContext.current
Expand Down Expand Up @@ -209,6 +246,14 @@ private fun DrawerContent(modifier: Modifier, navController: NavHostController)
.padding(vertical = 16.dp, horizontal = 4.dp)
)

NavigationDrawerItem(
icon = { drawerItemIcon(Icons.Filled.Style, stringResource(id = R.string.theme)) },
label = { Text(text = stringResource(id = R.string.theme)) },
selected = false,
onClick = {
isVisibleThemeDialog.value = true
},
)
drawerItem(Icons.Filled.Info, AppNavRoutes.About)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github.jing332.tts_dict_editor.ui

import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import com.github.jing332.tts_dict_editor.R
import com.github.jing332.tts_dict_editor.ui.theme.AppTheme

//@Preview
//@Composable
//private fun PreviewDialog() {
// var isVisible by remember { mutableStateOf(true) }
// if (isVisible)
// ThemeSettingsDialog({
// isVisible = false
// }, {
//
// })
//
//}

@OptIn(ExperimentalLayoutApi::class, ExperimentalMaterial3Api::class)
@Composable
fun ThemeSettingsDialog(
onDismissRequest: () -> Unit,
currentTheme: AppTheme,
onChangeTheme: (AppTheme) -> Unit
) {
AlertDialog(onDismissRequest = onDismissRequest,
title = {
Text(text = stringResource(id = R.string.theme))
}, confirmButton = {
TextButton(onClick = onDismissRequest) {
Text(stringResource(id = R.string.confirm))
}
}, text = {
FlowRow {
AppTheme.values().forEach {
val leadingIcon: @Composable () -> Unit = { Icon(Icons.Default.Check, null) }
// var selected by remember { mutableStateOf(it == AppTheme.DEFAULT) }
val selected = currentTheme.id == it.id
FilterChip(
selected,
leadingIcon = if (selected) leadingIcon else null,
onClick = {
onChangeTheme(it)
},
label = { Text(stringResource(id = it.stringResId)) }
)
}
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ import com.github.jing332.tts_dict_editor.R
import com.github.jing332.tts_dict_editor.const.IntentKeys
import com.github.jing332.tts_dict_editor.data.entites.DictFile
import com.github.jing332.tts_dict_editor.ui.widget.Widgets
import com.github.jing332.tts_dict_editor.ui.theme.AppTheme
import com.github.jing332.tts_dict_editor.utils.ASFUriUtils.getPath
import com.github.jing332.tts_dict_editor.utils.FileUriTools.toContentUri
import io.github.lumyuan.turingbox.ui.theme.DictEditorTheme
import me.saket.cascade.CascadeDropdownMenu

@Suppress("DEPRECATION")
Expand All @@ -59,8 +59,8 @@ class DictFileEditActivity : ComponentActivity() {
vm.init(dictFile)

setContent {
AppTheme {
Widgets.TransparentSystemBars()
DictEditorTheme {
// Widgets.TransparentSystemBars()
Scaffold(
modifier = Modifier.imePadding(),
topBar = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import com.github.jing332.tts_dict_editor.utils.FileTools
import com.github.jing332.tts_dict_editor.utils.FileUriTools.toContentUri
import com.github.jing332.tts_server_android.util.longToast
import com.github.jing332.tts_server_android.util.toast
import io.github.lumyuan.turingbox.ui.theme.DictEditorTheme
import java.net.URLDecoder
import java.net.URLEncoder

Expand Down Expand Up @@ -132,8 +133,8 @@ class FilePickerActivity : ComponentActivity() {
checkFileReadPermission()

setContent {
AppTheme {
Widgets.TransparentSystemBars()
DictEditorTheme {
// Widgets.TransparentSystemBars()
var title by remember { mutableStateOf("") }
Scaffold(
topBar = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ import com.github.jing332.tts_dict_editor.help.ReplaceRule
import com.github.jing332.tts_dict_editor.help.ReplaceRuleGroup
import com.github.jing332.tts_dict_editor.ui.navigateSingleTop
import com.github.jing332.tts_dict_editor.ui.replace.edit.RuleEditScreen
import com.github.jing332.tts_dict_editor.ui.theme.AppTheme
import com.github.jing332.tts_dict_editor.ui.widget.Widgets
import com.github.jing332.tts_dict_editor.utils.ASFUriUtils.getPath
import com.github.jing332.tts_dict_editor.utils.observeNoSticky
import io.github.lumyuan.turingbox.ui.theme.DictEditorTheme
import kotlinx.coroutines.launch

var isKeyboardVisibleState = mutableStateOf(false)
Expand Down Expand Up @@ -94,8 +93,8 @@ class ReplaceRuleActivity : ComponentActivity() {
}

setContent {
AppTheme {
Widgets.TransparentSystemBars()
DictEditorTheme {
// Widgets.TransparentSystemBars()
CompositionLocalProvider(
LocalDataSaver provides AppConfig.dataSaverPref,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.jing332.tts_dict_editor.ui.theme

import com.github.jing332.tts_dict_editor.R

enum class AppTheme(val id: String, val stringResId: Int = -1) {
DEFAULT("", R.string.theme_default),
DYNAMIC_COLOR("dynamicColor", R.string.dynamic_color),
GREEN("green", R.string.green),
RED("red", R.string.red),
PINK("pink", R.string.pink ),
BLUE("blue", R.string.blue),
CYAN("cyan", R.string.cyan),
ORANGE("orange", R.string.orange),
PURPLE("purple", R.string.purple),
BROWN("brown", R.string.brown),
GRAY("gray", R.string.gray)
}
Loading

0 comments on commit bf56816

Please sign in to comment.