Skip to content

Commit

Permalink
Change hard-coded colors to adjust according to schemes. Support Dark…
Browse files Browse the repository at this point in the history
… theme on Android (follow system). #24
  • Loading branch information
Him188 committed Jan 6, 2023
1 parent 67f278f commit b8c88a2
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 18 deletions.
14 changes: 11 additions & 3 deletions android/src/main/kotlin/activity/BaseComponentActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package me.him188.animationgarden.android.activity

import androidx.activity.ComponentActivity
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -30,6 +30,14 @@ import kotlinx.coroutines.launch
abstract class BaseComponentActivity : ComponentActivity() {
@Stable
val snackbarHostState = SnackbarHostState()

val colorScheme
@Composable
get() = if (isSystemInDarkTheme()) {
darkColorScheme()
} else {
lightColorScheme()
}
}

suspend fun BaseComponentActivity.showSnackbar(
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/kotlin/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class MainActivity : BaseComponentActivity() {
ObserveSettingsChanges(app)


MaterialTheme {
MaterialTheme(colorScheme) {
ImmerseStatusBar(AppTheme.colorScheme.primary)

MainPage(app)
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/kotlin/activity/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SettingsActivity : BaseComponentActivity() {
requestWindowFeature(Window.FEATURE_NO_TITLE)

setContent {
MaterialTheme {
MaterialTheme(colorScheme) {
ImmerseStatusBar(AppTheme.colorScheme.primary)

CommonAppScaffold(
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/kotlin/activity/StarredListActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class StarredListActivity : BaseComponentActivity() {
requestWindowFeature(Window.FEATURE_NO_TITLE)

setContent {
MaterialTheme {
MaterialTheme(colorScheme) {
ImmerseStatusBar(AppTheme.colorScheme.primary)

CommonAppScaffold(
Expand Down
10 changes: 7 additions & 3 deletions common/src/commonMain/kotlin/ui/MainPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import me.him188.animationgarden.app.platform.LocalContext
import me.him188.animationgarden.app.platform.Res
import me.him188.animationgarden.app.platform.browse
import me.him188.animationgarden.app.ui.interaction.onEnterKeyEvent
import me.him188.animationgarden.app.ui.theme.darken
import me.him188.animationgarden.app.ui.theme.weaken
import me.him188.animationgarden.app.ui.widgets.OutlinedTextFieldEx
import java.time.LocalDateTime
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -262,7 +264,7 @@ fun SearchTextField(
Text(
LocalI18n.current.getString("search.keywords"),
style = AppTheme.typography.bodyMedium.copy(
color = AppTheme.typography.bodyMedium.color.copy(0.3f),
color = AppTheme.typography.bodyMedium.color.darken(),
lineHeight = 16.sp
)
)
Expand Down Expand Up @@ -609,13 +611,15 @@ private fun SearchResultField(
fetchingState.render(resources)
)
},
style = AppTheme.typography.bodyMedium.run { copy(color = color.copy(alpha = 0.5f)) }
style = AppTheme.typography.bodyMedium.run {
copy(color = color.weaken())
}
)
ClickableText(
AnnotatedString(LocalI18n.current.getString("search.failed.check.proxy")),
style = AppTheme.typography.bodyMedium.run {
copy(
color = Color.Blue.copy(alpha = 0.5f),
color = Color.Blue.weaken(),
textDecoration = TextDecoration.Underline
)
},
Expand Down
3 changes: 2 additions & 1 deletion common/src/commonMain/kotlin/ui/OrganizedViewCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import me.him188.animationgarden.app.i18n.LocalI18n
import me.him188.animationgarden.app.i18n.loadResourceBundle
import me.him188.animationgarden.app.platform.LocalContext
import me.him188.animationgarden.app.platform.Res
import me.him188.animationgarden.app.ui.theme.darken
import me.him188.animationgarden.app.ui.widgets.ToggleStarButton
import java.time.LocalDateTime

Expand Down Expand Up @@ -155,7 +156,7 @@ private fun OrganizedViewContent(
Text(
it.raw,
color = if (isEpisodeWatched(it)) {
LocalTextStyle.current.color.copy(alpha = 0.3f)
LocalTextStyle.current.color.darken()
} else {
LocalTextStyle.current.color
}
Expand Down
3 changes: 2 additions & 1 deletion common/src/commonMain/kotlin/ui/StarredAnimeCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import me.him188.animationgarden.app.platform.LocalContext
import me.him188.animationgarden.app.ui.interaction.VibrationStrength
import me.him188.animationgarden.app.ui.interaction.onClickEx
import me.him188.animationgarden.app.ui.interaction.vibrateIfSupported
import me.him188.animationgarden.app.ui.theme.darken
import me.him188.animationgarden.app.ui.widgets.ToggleStarButton

@Composable
Expand Down Expand Up @@ -112,7 +113,7 @@ fun StarredAnimeCard(
Text(
it.raw,
color = if (currentAnime.watchedEpisodes.contains(it)) {
LocalTextStyle.current.color.copy(alpha = 0.3f)
LocalTextStyle.current.color.darken()
} else {
LocalTextStyle.current.color
}
Expand Down
9 changes: 6 additions & 3 deletions common/src/commonMain/kotlin/ui/TopicItemCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import me.him188.animationgarden.api.model.DATE_FORMAT
import me.him188.animationgarden.api.model.Topic
import me.him188.animationgarden.api.tags.Episode
import me.him188.animationgarden.app.AppTheme
import me.him188.animationgarden.app.ui.theme.weaken

@Immutable
data class Tag(
Expand Down Expand Up @@ -137,7 +138,7 @@ fun TopicItemCard(topic: Topic, onClick: () -> Unit) {
Text(
dateFormatted,
style = AppTheme.typography.bodyMedium,
color = AppTheme.typography.bodyMedium.color.copy(alpha = 0.5f),
color = AppTheme.typography.bodyMedium.color.weaken(),
modifier = Modifier.padding(start = 4.dp),
fontWeight = FontWeight.W400,
maxLines = 1,
Expand Down Expand Up @@ -270,7 +271,9 @@ private fun Subtitle(
Text(
text,
modifier,
style = AppTheme.typography.titleMedium.run { copy(color = color.copy(alpha = 0.5f)) },
style = AppTheme.typography.titleMedium.run {
copy(color = color.weaken())
},
fontWeight = FontWeight.W400,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
Expand Down Expand Up @@ -306,7 +309,7 @@ private fun TagButton(
val containerColor = if (containerColorEffect == Color.Unspecified) {
defaultContainerColor
} else {
containerColorEffect.compositeOver(defaultContainerColor.copy(alpha = 0.5f))
containerColorEffect.compositeOver(defaultContainerColor.weaken())
}
val shape = AppTheme.shapes.small
ElevatedButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import me.him188.animationgarden.app.ui.theme.darken

@Composable
fun LabelledRadioButton(
Expand Down Expand Up @@ -95,8 +96,8 @@ object LabelDefaults {
fun labelColors(
checkedTextColor: Color = MaterialTheme.colorScheme.onSurface,
uncheckedTextColor: Color = MaterialTheme.colorScheme.onSurface,
disabledCheckedTextColor: Color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f),
disabledUncheckedTextColor: Color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f),
disabledCheckedTextColor: Color = MaterialTheme.colorScheme.onSurface.darken(),
disabledUncheckedTextColor: Color = MaterialTheme.colorScheme.onSurface.darken(),
): LabelColors {
return DefaultLabelColors(
checkedTextColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import me.him188.animationgarden.app.AppTheme
import me.him188.animationgarden.app.ui.interaction.onEnterKeyEvent
import me.him188.animationgarden.app.ui.theme.darken
import me.him188.animationgarden.app.ui.widgets.OutlinedTextFieldEx

@Composable
Expand Down Expand Up @@ -128,7 +129,7 @@ fun SettingsOutlinedTextField(
placeholder = {
ProvideTextStyle(
AppTheme.typography.bodyMedium.copy(
color = AppTheme.typography.bodyMedium.color.copy(0.3f),
color = AppTheme.typography.bodyMedium.color.darken(),
lineHeight = 20.sp
)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import me.him188.animationgarden.app.app.AppSettings
import me.him188.animationgarden.app.app.AppSettingsManager
import me.him188.animationgarden.app.app.settings.ProxyMode
import me.him188.animationgarden.app.i18n.LocalI18n
import me.him188.animationgarden.app.ui.theme.darken

@Composable
fun ColumnScope.ProxySettingsGroup(
Expand Down Expand Up @@ -80,7 +81,7 @@ fun ColumnScope.ProxySettingsGroup(
exit = exit,
) {
Row(Modifier.padding(vertical = 8.dp, horizontal = 16.dp)) {
ProvideTextStyle(AppTheme.typography.bodyMedium.run { copy(color = color.copy(alpha = 0.38f)) }) {
ProvideTextStyle(AppTheme.typography.bodyMedium.run { copy(color = color.darken()) }) {
disabledContent()
}
}
Expand Down
43 changes: 43 additions & 0 deletions common/src/commonMain/kotlin/ui/theme/Colors.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Animation Garden App
* Copyright (C) 2022 Him188
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package me.him188.animationgarden.app.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.compositeOver


@Composable
fun Color.weaken(): Color {
return if (isSystemInDarkTheme()) {
Color.Gray.compositeOver(this)
} else {
copy(alpha = 0.5f)
}
}

@Composable
fun Color.darken(): Color {
return if (isSystemInDarkTheme()) {
Color.DarkGray.compositeOver(this)
} else {
copy(alpha = 0.38f)
}
}

0 comments on commit b8c88a2

Please sign in to comment.