refactor: use object theme to optimize sub system management, so we will not leak sub-theme system every where

pull/1808/head
CaptainZ 8 months ago
parent a0f2786634
commit 9afd3ce3e3

@ -68,7 +68,7 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaNavig
import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaTopAppBar
import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons
import com.google.samples.apps.nowinandroid.core.designsystem.theme.GradientColors
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalGradientColors
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.feature.settings.SettingsDialog
import com.google.samples.apps.nowinandroid.navigation.NiaNavHost
import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination
@ -88,7 +88,7 @@ fun NiaApp(
NiaBackground(modifier = modifier) {
NiaGradientBackground(
gradientColors = if (shouldShowGradientBackground) {
LocalGradientColors.current
NiaTheme.gradientColors
} else {
GradientColors()
},

@ -36,7 +36,6 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.google.samples.apps.nowinandroid.core.designsystem.theme.GradientColors
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalBackgroundTheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalGradientColors
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import kotlin.math.tan
@ -52,8 +51,8 @@ fun NiaBackground(
modifier: Modifier = Modifier,
content: @Composable () -> Unit,
) {
val color = LocalBackgroundTheme.current.color
val tonalElevation = LocalBackgroundTheme.current.tonalElevation
val color = NiaTheme.backgroundTheme.color
val tonalElevation = NiaTheme.backgroundTheme.tonalElevation
Surface(
color = if (color == Color.Unspecified) Color.Transparent else color,
tonalElevation = if (tonalElevation == Dp.Unspecified) 0.dp else tonalElevation,
@ -76,7 +75,7 @@ fun NiaBackground(
@Composable
fun NiaGradientBackground(
modifier: Modifier = Modifier,
gradientColors: GradientColors = LocalGradientColors.current,
gradientColors: GradientColors = NiaTheme.gradientColors,
content: @Composable () -> Unit,
) {
val currentTopColor by rememberUpdatedState(gradientColors.top)

@ -33,4 +33,4 @@ data class BackgroundTheme(
/**
* A composition local for [BackgroundTheme].
*/
val LocalBackgroundTheme = staticCompositionLocalOf { BackgroundTheme() }
internal val LocalBackgroundTheme = staticCompositionLocalOf { BackgroundTheme() }

@ -37,4 +37,4 @@ data class GradientColors(
/**
* A composition local for [GradientColors].
*/
val LocalGradientColors = staticCompositionLocalOf { GradientColors() }
internal val LocalGradientColors = staticCompositionLocalOf { GradientColors() }

@ -20,7 +20,9 @@ import android.os.Build
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.VisibleForTesting
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Typography
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
@ -28,6 +30,7 @@ import androidx.compose.material3.lightColorScheme
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
@ -246,5 +249,34 @@ fun NiaTheme(
}
}
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S)
fun supportsDynamicTheming() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
object NiaTheme {
val gradientColors: GradientColors
@Composable
@ReadOnlyComposable
get() = LocalGradientColors.current
val backgroundTheme: BackgroundTheme
@Composable
@ReadOnlyComposable
get() = LocalBackgroundTheme.current
val tintTheme: TintTheme
@Composable
@ReadOnlyComposable
get() = LocalTintTheme.current
val colorScheme: ColorScheme
@Composable
@ReadOnlyComposable
get() = MaterialTheme.colorScheme
val typography: Typography
@Composable
@ReadOnlyComposable
get() = MaterialTheme.typography
}

@ -31,4 +31,4 @@ data class TintTheme(
/**
* A composition local for [TintTheme].
*/
val LocalTintTheme = staticCompositionLocalOf { TintTheme() }
internal val LocalTintTheme = staticCompositionLocalOf { TintTheme() }

@ -27,7 +27,6 @@ import androidx.compose.foundation.lazy.staggeredgrid.LazyStaggeredGridScope
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toArgb
@ -61,7 +60,7 @@ fun LazyStaggeredGridScope.newsFeed(
) { userNewsResource ->
val context = LocalContext.current
val analyticsHelper = LocalAnalyticsHelper.current
val backgroundColor = MaterialTheme.colorScheme.background.toArgb()
val backgroundColor = NiaTheme.colorScheme.background.toArgb()
NewsResourceCardExpanded(
userNewsResource = userNewsResource,

@ -64,7 +64,6 @@ import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaLoadi
import com.google.samples.apps.nowinandroid.core.designsystem.component.scrollbar.DraggableScrollbar
import com.google.samples.apps.nowinandroid.core.designsystem.component.scrollbar.rememberDraggableScroller
import com.google.samples.apps.nowinandroid.core.designsystem.component.scrollbar.scrollbarState
import com.google.samples.apps.nowinandroid.core.designsystem.theme.LocalTintTheme
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource
import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
@ -225,7 +224,7 @@ private fun EmptyState(modifier: Modifier = Modifier) {
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
val iconTint = LocalTintTheme.current.iconTint
val iconTint = NiaTheme.tintTheme.iconTint
Image(
modifier = Modifier.fillMaxWidth(),
painter = painterResource(id = R.drawable.feature_bookmarks_img_empty_bookmarks),

Loading…
Cancel
Save