Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

动态配色支持、UI 整体修复与改进 #94

Merged
merged 10 commits into from
Sep 3, 2024
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {
minSdk = 24
targetSdk = 34
// 版本号为x.y.z则versionCode为x*1000000+y*10000+z*100+debug版本号(开发需要时迭代, 两位数)
versionCode = 4_04_020
versionCode = 4_04_022
versionName = "0.4.4"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
4 changes: 4 additions & 0 deletions app/src/debug/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="settings_statistics_desc">*Debug 构建不可用</string>
</resources>
1 change: 1 addition & 0 deletions app/src/debug/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name" translatable="false">[DEBUG] LightNovelReader</string>
<string name="settings_statistics_desc">*Unavailable for debug builds</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
var appLocale by mutableStateOf("${Locale.current.platformLocale.language}-${Locale.current.platformLocale.variant}")
var darkMode by mutableStateOf("FollowSystem")
var dynamicColor by mutableStateOf(false)
installSplashScreen()
var statisticsEnabled by mutableStateOf(true)
workManager.enqueueUniquePeriodicWork(
Expand Down Expand Up @@ -98,17 +99,25 @@ class MainActivity : ComponentActivity() {
it?.let { isUsingVolumeKeyFlip = it }
}
}
if (Build.VERSION.SDK_INT >= 33) { /* Android 13 + */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { /* Android 13 + */
if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this, arrayOf(POST_NOTIFICATIONS), 0
)
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
coroutineScope.launch(Dispatchers.IO) {
userDataRepository.booleanUserData(UserDataPath.Settings.Display.DynamicColors.path).getFlow().collect {
dynamicColor = it ?: false
}
}
}
setContent {
LightNovelReaderTheme(
darkMode = darkMode,
appLocale = appLocale
appLocale = appLocale,
isDynamicColor = dynamicColor
) {
LightNovelReaderApp()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sealed class UserDataPath(
}
data object Display: UserDataPath("display", Settings) {
data object DarkMode : UserDataPath("dark_mode", Display)
data object DynamicColors : UserDataPath("dynamic_color", Display)
data object AppLocale : UserDataPath("app_locale", Display)
}
data object Reader : UserDataPath("reader", Settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ import indi.dmzz_yyhyy.lightnovelreader.utils.LocaleUtil

@Composable
fun LightNovelReaderTheme(
isDarkTheme: Boolean = isSystemInDarkTheme(),
darkMode: String,
isDynamicColor: Boolean = false,
isDynamicColor: Boolean = true,
appLocale: String,
content: @Composable () -> Unit
) {
val context = LocalContext.current
val appDarkTheme = when (darkMode) {
"Enabled" -> true
"Disabled" -> false
"FollowSystem" -> isSystemInDarkTheme()
else -> isSystemInDarkTheme()
}
val colorScheme =
if (isDynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
if ((darkMode == "FollowSystem" && isDarkTheme) || (darkMode == "Enabled"))
dynamicDarkColorScheme(context)
else
dynamicLightColorScheme(context)
if (appDarkTheme) dynamicDarkColorScheme(context)
else dynamicLightColorScheme(context)
else
if ((darkMode == "FollowSystem" && isDarkTheme) || (darkMode == "Enabled"))
darkColorScheme()
else
lightColorScheme()
if (appDarkTheme) darkColorScheme()
else lightColorScheme()

if (!LocalInspectionMode.current) {
val view = LocalView.current
Expand All @@ -53,11 +54,11 @@ fun LightNovelReaderTheme(

val controller = WindowCompat.getInsetsController(window, view)
window.statusBarColor = Color.Transparent.toArgb()
controller.isAppearanceLightStatusBars = !isDarkTheme
controller.isAppearanceLightStatusBars = !appDarkTheme

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
window.navigationBarColor = Color.Transparent.toArgb()
controller.isAppearanceLightNavigationBars = !isDarkTheme
controller.isAppearanceLightNavigationBars = !appDarkTheme
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
Expand Down Expand Up @@ -45,6 +46,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.max
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import indi.dmzz_yyhyy.lightnovelreader.R
Expand Down Expand Up @@ -432,9 +434,7 @@ private fun Description(description: String) {
onTextLayout = {
isNeedExpand = it.hasVisualOverflow || isNeedExpand
},
style = MaterialTheme.typography.bodyMedium.copy(
fontWeight = FontWeight.W400
),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurface,
overflow = TextOverflow.Ellipsis
)
Expand All @@ -452,7 +452,7 @@ private fun Description(description: String) {
tint = MaterialTheme.colorScheme.primary
)
Text(
text = "展开",
text = if (expandSummaryText) "收起" else "展开",
color = MaterialTheme.colorScheme.primary
)
}
Expand Down Expand Up @@ -495,19 +495,21 @@ private fun VolumeItem(
Box(
modifier = Modifier
.clickable { onClickChapter(it.id) }
.height(48.dp)
.wrapContentHeight()
.fillMaxWidth()
.padding(horizontal = 4.dp, vertical = 8.dp),
) {
Row(
modifier = Modifier.fillMaxSize(),
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Text(
modifier = Modifier.padding(start = 8.dp),
modifier = Modifier.padding(horizontal = 8.dp),
text = it.title,
style = MaterialTheme.typography.bodyMedium,
fontSize = 16.sp,
maxLines = 2,
overflow = TextOverflow.Ellipsis,
fontSize = 15.sp,
fontWeight =
if (readCompletedChapterIds.contains(it.id))
FontWeight.W400
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package indi.dmzz_yyhyy.lightnovelreader.ui.components

import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
Expand All @@ -22,41 +18,30 @@ import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.RoundRect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.unit.times
import androidx.core.content.ContextCompat.startActivity
import indi.dmzz_yyhyy.lightnovelreader.ui.home.settings.data.MenuOptions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.DecimalFormat
import kotlin.math.roundToInt

/* NOTE
* SettingsSwitchEntry and SettingsSliderEntry renamed and moved from ContentScreen.kt
*/
@Composable
fun SettingsSwitchEntry(
title: String,
description: String,
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
disabled: Boolean = false
) {
FilledCard(
modifier = Modifier
Expand All @@ -66,8 +51,8 @@ fun SettingsSwitchEntry(
Row(
modifier = Modifier
.fillMaxWidth()
.clickable { onCheckedChange(!checked) }
.padding(18.dp, 10.dp, 20.dp, 12.dp),
.then(if (disabled) Modifier.clickable {} else Modifier.clickable { onCheckedChange(!checked) })
.padding(18.dp, 10.dp, 20.dp, 12.dp),
verticalAlignment = Alignment.CenterVertically
) {
Column(
Expand All @@ -89,17 +74,19 @@ fun SettingsSwitchEntry(
fontWeight = FontWeight.W500,
fontSize = 13.sp,
lineHeight = 14.sp,
color = MaterialTheme.colorScheme.secondary
color = MaterialTheme.colorScheme.onSurface
)
}
Switch(
checked = checked,
onCheckedChange = onCheckedChange
enabled = !disabled,
onCheckedChange = if (disabled) null else onCheckedChange
)
}
}
}


@Composable
fun SettingsSliderEntry(
description: String,
Expand Down Expand Up @@ -184,7 +171,7 @@ fun SettingsMenuEntry(
lineHeight = 14.sp,
color = MaterialTheme.colorScheme.onSurface
)
Text(
AnimatedText(
text = options.get(selectedOptionKey).name,
fontSize = 13.sp,
lineHeight = 14.sp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MediumTopAppBar
import androidx.compose.material3.PrimaryTabRow
import androidx.compose.material3.ScrollableTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
Expand Down Expand Up @@ -118,24 +119,23 @@ fun BookshelfHomeScreen(
drawRect(animatedBackgroundColor)
}
) {
if (uiState.bookshelfList.size >= 4) {
if (uiState.bookshelfList.size > 4) {
ScrollableTabRow(
containerColor = animatedBackgroundColor,
selectedTabIndex = uiState.selectedTabIndex,
edgePadding = 16.dp,
indicator = { tabPositions ->
if (tabPositions.isNotEmpty()) {
SecondaryIndicator(
modifier = Modifier
.tabIndicatorOffset(tabPositions[uiState.selectedTabIndex])
.height(4.dp)
.clip(RoundedCornerShape(topStart = 3.dp, topEnd = 3.dp))
.background(MaterialTheme.colorScheme.secondary),
color = MaterialTheme.colorScheme.primary,
)
}
SecondaryIndicator(
modifier = Modifier
.tabIndicatorOffset(tabPositions[uiState.selectedTabIndex])
.height(4.dp)
.clip(RoundedCornerShape(topStart = 3.dp, topEnd = 3.dp))
.background(MaterialTheme.colorScheme.secondary),
color = MaterialTheme.colorScheme.primary,
)
},
) {
uiState.bookshelfList.forEachIndexed { _, bookshelf ->
uiState.bookshelfList.forEach { bookshelf ->
Tab(
selected = uiState.selectedBookshelfId == bookshelf.id,
onClick = { if (!uiState.selectMode) changePage(bookshelf.id) },
Expand All @@ -150,34 +150,15 @@ fun BookshelfHomeScreen(
}
}
else {
TabRow(
PrimaryTabRow(
selectedTabIndex = uiState.selectedTabIndex,
containerColor = Color.Transparent,
contentColor = MaterialTheme.colorScheme.primary,
indicator = { tabPositions ->
if (tabPositions.isNotEmpty())
SecondaryIndicator(
modifier = Modifier
.tabIndicatorOffset(tabPositions[uiState.selectedTabIndex])
.height(4.dp)
.clip(RoundedCornerShape(topStart = 3.dp, topEnd = 3.dp))
.background(MaterialTheme.colorScheme.secondary),
color = MaterialTheme.colorScheme.primary,
)
}
containerColor = animatedBackgroundColor
) {
uiState.bookshelfList.forEachIndexed { _, bookshelf ->
uiState.bookshelfList.forEach { bookshelf ->
Tab(
selected = uiState.selectedBookshelfId == bookshelf.id,
onClick = { if (!uiState.selectMode) changePage(bookshelf.id) },
text = {
Text(
text = bookshelf.name,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
},
modifier = Modifier.padding(horizontal = 8.dp)
text = { Text(text = bookshelf.name, maxLines = 1, overflow = TextOverflow.Ellipsis) },
)
}
}
Expand Down Expand Up @@ -321,7 +302,7 @@ fun CollapseGroupTitle(
)
IconButton(onClickExpand) {
Icon(
modifier = Modifier.rotate(if (expanded) 180f else 0f),
modifier = Modifier.rotate(if (expanded) 0f else 180f),
painter = painterResource(R.drawable.keyboard_arrow_up_24px),
contentDescription = "expand"
)
Expand Down Expand Up @@ -393,7 +374,7 @@ fun BookRow(
overflow = TextOverflow.Ellipsis
)
Text(
text = bookInformation.description,
text = bookInformation.description.trim(),
style = descriptionTextStyle,
maxLines = 1,
overflow = TextOverflow.Ellipsis
Expand Down Expand Up @@ -633,7 +614,13 @@ fun TopBar(
}
},
actions = {
IconButton(if (!selectMode) onClickCreat else onClickSelectAll) {
IconButton(
if (!selectMode) {
scrollBehavior.state.heightOffset = 0f
onClickCreat
}
else onClickSelectAll
) {
Icon(
painter = if (!selectMode) painterResource(R.drawable.library_add_24px) else painterResource(R.drawable.select_all_24px),
contentDescription = if (!selectMode) "create" else "select all"
Expand Down
Loading
Loading