From b11ae055c3903e735992509bc61d20a19e80a56c Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Wed, 24 Apr 2024 17:20:55 +0800 Subject: [PATCH] Refactor(Preview): Change all preview to OxygenPreviews Change all preview to OxygenPreviews. Fix can not preview OxygenTopAppBar bug. --- .../oxygen/toolbox/ui/about/AboutScreen.kt | 8 ++--- .../oxygen/toolbox/ui/component/Background.kt | 29 +++++-------------- .../oxygen/toolbox/ui/component/Navigation.kt | 5 ++-- .../toolbox/ui/component/OxygenTopAppBar.kt | 6 ++-- .../toolbox/ui/component/ToolGroupCard.kt | 9 +++--- .../toolbox/ui/settings/SettingsDialog.kt | 6 ++-- .../fatweb/oxygen/toolbox/ui/theme/Theme.kt | 18 +++++++++++- .../oxygen/toolbox/ui/tool/ToolsScreen.kt | 25 +++++++++------- 8 files changed, 56 insertions(+), 50 deletions(-) diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/about/AboutScreen.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/about/AboutScreen.kt index fe7c2d0..eab9a4a 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/about/AboutScreen.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/about/AboutScreen.kt @@ -23,9 +23,7 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import top.fatweb.oxygen.toolbox.R import top.fatweb.oxygen.toolbox.icon.OxygenIcons -import top.fatweb.oxygen.toolbox.ui.component.ApiLevelPreviews -import top.fatweb.oxygen.toolbox.ui.component.OxygenPreviews -import top.fatweb.oxygen.toolbox.ui.component.ThemePreviews +import top.fatweb.oxygen.toolbox.ui.theme.OxygenPreviews import top.fatweb.oxygen.toolbox.ui.theme.OxygenTheme import top.fatweb.oxygen.toolbox.ui.util.ResourcesUtils @@ -137,7 +135,7 @@ private fun AboutFooter( } } -@ThemePreviews +@OxygenPreviews @Composable fun AboutToolBarPreview() { OxygenTheme { @@ -145,7 +143,7 @@ fun AboutToolBarPreview() { } } -@ApiLevelPreviews +@OxygenPreviews @Composable fun AboutAppInfoPreview() { OxygenTheme { diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/Background.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/Background.kt index df25470..e2364bd 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/Background.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/Background.kt @@ -1,6 +1,5 @@ package top.fatweb.oxygen.toolbox.ui.component -import android.content.res.Configuration import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.size @@ -15,12 +14,12 @@ import androidx.compose.ui.draw.drawWithCache import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import top.fatweb.oxygen.toolbox.ui.theme.GradientColors import top.fatweb.oxygen.toolbox.ui.theme.LocalBackgroundTheme import top.fatweb.oxygen.toolbox.ui.theme.LocalGradientColors +import top.fatweb.oxygen.toolbox.ui.theme.OxygenPreviews import top.fatweb.oxygen.toolbox.ui.theme.OxygenTheme import kotlin.math.tan @@ -92,21 +91,7 @@ fun OxygenGradientBackground( } } - -@Preview(name = "Light theme", group = "ThemePreviews", uiMode = Configuration.UI_MODE_NIGHT_NO) -@Preview(name = "Dark theme", group = "ThemePreviews", uiMode = Configuration.UI_MODE_NIGHT_YES) -@Preview(name = "Api 21", group = "ApiLevelPreviews", apiLevel = 21) -annotation class OxygenPreviews - -@Preview(name = "Light theme", group = "ThemePreviews", uiMode = Configuration.UI_MODE_NIGHT_NO) -@Preview(name = "Dark theme", group = "ThemePreviews", uiMode = Configuration.UI_MODE_NIGHT_YES) -annotation class ThemePreviews - -@Preview(name = "Api 21", group = "ApiLevelPreviews", apiLevel = 21) -@Preview(name = "Api Default", group = "ApiLevelPreviews") -annotation class ApiLevelPreviews - -@ThemePreviews +@OxygenPreviews @Composable fun BackgroundDefault() { OxygenTheme(dynamicColor = false) { @@ -114,7 +99,7 @@ fun BackgroundDefault() { } } -@ThemePreviews +@OxygenPreviews @Composable fun BackgroundDynamic() { OxygenTheme(dynamicColor = true) { @@ -122,7 +107,7 @@ fun BackgroundDynamic() { } } -@ThemePreviews +@OxygenPreviews @Composable fun BackgroundAndroid() { OxygenTheme(androidTheme = true) { @@ -130,7 +115,7 @@ fun BackgroundAndroid() { } } -@ThemePreviews +@OxygenPreviews @Composable fun GradientBackgroundDefault() { OxygenTheme(dynamicColor = false) { @@ -138,7 +123,7 @@ fun GradientBackgroundDefault() { } } -@ThemePreviews +@OxygenPreviews @Composable fun GradientBackgroundDynamic() { OxygenTheme(dynamicColor = true) { @@ -146,7 +131,7 @@ fun GradientBackgroundDynamic() { } } -@ThemePreviews +@OxygenPreviews @Composable fun GradientBackgroundAndroid() { OxygenTheme(androidTheme = true) { diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/Navigation.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/Navigation.kt index 6a6437b..5982bf2 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/Navigation.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/Navigation.kt @@ -17,6 +17,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import top.fatweb.oxygen.toolbox.navigation.TopLevelDestination +import top.fatweb.oxygen.toolbox.ui.theme.OxygenPreviews import top.fatweb.oxygen.toolbox.ui.theme.OxygenTheme @Composable @@ -116,7 +117,7 @@ object OxygenNavigationDefaults { fun navigationIndicatorColor() = MaterialTheme.colorScheme.primaryContainer } -@ThemePreviews +@OxygenPreviews @Composable fun OxygenNavigationBarPreview() { val items = TopLevelDestination.entries @@ -147,7 +148,7 @@ fun OxygenNavigationBarPreview() { } } -@ThemePreviews +@OxygenPreviews @Composable fun OxygenNavigationRailPreview() { val items = TopLevelDestination.entries diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/OxygenTopAppBar.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/OxygenTopAppBar.kt index 0792310..e55ed75 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/OxygenTopAppBar.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/OxygenTopAppBar.kt @@ -18,9 +18,9 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.res.stringResource -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import top.fatweb.oxygen.toolbox.icon.OxygenIcons +import top.fatweb.oxygen.toolbox.ui.theme.OxygenPreviews import top.fatweb.oxygen.toolbox.ui.theme.OxygenTheme import android.R as androidR @@ -39,7 +39,7 @@ fun OxygenTopAppBar( onActionClick: () -> Unit = {} ) { val topInset by animateIntAsState( - if (-scrollBehavior?.state?.heightOffset!! >= with(LocalDensity.current) { 64.0.dp.toPx() }) 0 + if (scrollBehavior != null && -scrollBehavior.state.heightOffset >= with(LocalDensity.current) { 64.0.dp.toPx() }) 0 else TopAppBarDefaults.windowInsets.getTop(LocalDensity.current), label = "" ) CenterAlignedTopAppBar( @@ -70,7 +70,7 @@ fun OxygenTopAppBar( } @OptIn(ExperimentalMaterial3Api::class) -@Preview +@OxygenPreviews @Composable private fun OxygenTopAppBarPreview() { OxygenTheme { diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/ToolGroupCard.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/ToolGroupCard.kt index 5a26587..b636f8d 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/ToolGroupCard.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/component/ToolGroupCard.kt @@ -38,6 +38,7 @@ import top.fatweb.oxygen.toolbox.data.tool.ToolDataSource import top.fatweb.oxygen.toolbox.icon.OxygenIcons import top.fatweb.oxygen.toolbox.model.tool.Tool import top.fatweb.oxygen.toolbox.model.tool.ToolGroup +import top.fatweb.oxygen.toolbox.ui.theme.OxygenPreviews import top.fatweb.oxygen.toolbox.ui.theme.OxygenTheme @Composable @@ -152,7 +153,7 @@ fun SwitchableIcon( ) } -@ThemePreviews +@OxygenPreviews @Composable private fun ToolGroupCardPreview() { val groups = runBlocking { ToolDataSource().tool.first() } @@ -171,7 +172,7 @@ private fun ToolGroupCardPreview() { } } -@ThemePreviews +@OxygenPreviews @Composable fun SwitchableIconPreview() { var switched by remember { mutableStateOf(false) } @@ -185,7 +186,7 @@ fun SwitchableIconPreview() { } } -@ThemePreviews +@OxygenPreviews @Composable fun ToolGroupItemPreview() { OxygenTheme { @@ -193,7 +194,7 @@ fun ToolGroupItemPreview() { } } -@ThemePreviews +@OxygenPreviews @Composable fun ToolGroupContentPreview() { OxygenTheme { diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/settings/SettingsDialog.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/settings/SettingsDialog.kt index b84a45b..fc1ba66 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/settings/SettingsDialog.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/settings/SettingsDialog.kt @@ -39,7 +39,7 @@ import top.fatweb.oxygen.toolbox.model.userdata.LanguageConfig import top.fatweb.oxygen.toolbox.model.userdata.LaunchPageConfig import top.fatweb.oxygen.toolbox.model.userdata.ThemeBrandConfig import top.fatweb.oxygen.toolbox.model.userdata.UserData -import top.fatweb.oxygen.toolbox.ui.component.ThemePreviews +import top.fatweb.oxygen.toolbox.ui.theme.OxygenPreviews import top.fatweb.oxygen.toolbox.ui.theme.OxygenTheme import top.fatweb.oxygen.toolbox.ui.theme.supportsDynamicTheming @@ -316,7 +316,7 @@ private fun SettingsDialogClickerRow( } } -@ThemePreviews +@OxygenPreviews @Composable private fun SettingsDialogLoadingPreview() { OxygenTheme { @@ -334,7 +334,7 @@ private fun SettingsDialogLoadingPreview() { } } -@ThemePreviews +@OxygenPreviews @Composable private fun SettingDialogPreview() { OxygenTheme { diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/theme/Theme.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/theme/Theme.kt index 59274fd..0febfc1 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/theme/Theme.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/theme/Theme.kt @@ -1,5 +1,6 @@ package top.fatweb.oxygen.toolbox.ui.theme +import android.content.res.Configuration import android.os.Build import androidx.annotation.ChecksSdkIntAtLeast import androidx.compose.foundation.isSystemInDarkTheme @@ -13,6 +14,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp val LightDefaultColorScheme = lightColorScheme( @@ -191,4 +193,18 @@ fun OxygenTheme( } @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S) -fun supportsDynamicTheming() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S \ No newline at end of file +fun supportsDynamicTheming() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S + +@Preview( + name = "Light theme", group = "ThemePreviews", + uiMode = Configuration.UI_MODE_NIGHT_NO or Configuration.UI_MODE_TYPE_NORMAL +) +@Preview( + name = "Dark theme", group = "ThemePreviews", + uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL +) +@Preview( + name = "Api 21 Light", group = "ApiLevelPreviews", apiLevel = 21, + uiMode = Configuration.UI_MODE_NIGHT_NO or Configuration.UI_MODE_TYPE_NORMAL +) +annotation class OxygenPreviews \ No newline at end of file diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolsScreen.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolsScreen.kt index 0a1f659..f86e173 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolsScreen.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolsScreen.kt @@ -25,7 +25,6 @@ import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource -import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle @@ -36,6 +35,8 @@ import top.fatweb.oxygen.toolbox.data.tool.ToolDataSource import top.fatweb.oxygen.toolbox.ui.component.scrollbar.DraggableScrollbar import top.fatweb.oxygen.toolbox.ui.component.scrollbar.rememberDraggableScroller import top.fatweb.oxygen.toolbox.ui.component.scrollbar.scrollbarState +import top.fatweb.oxygen.toolbox.ui.theme.OxygenPreviews +import top.fatweb.oxygen.toolbox.ui.theme.OxygenTheme @Composable internal fun ToolsRoute( @@ -110,19 +111,23 @@ fun howManyItems(toolScreenUiState: ToolsScreenUiState) = is ToolsScreenUiState.Success -> toolScreenUiState.toolGroups.size } -@Preview("Loading") +@OxygenPreviews @Composable fun ToolsScreenLoadingPreview() { - ToolsScreen(toolsScreenUiState = ToolsScreenUiState.Loading) + OxygenTheme { + ToolsScreen(toolsScreenUiState = ToolsScreenUiState.Loading) + } } -@Preview("ToolsPage") +@OxygenPreviews @Composable fun ToolsScreenPreview() { - ToolsScreen( - toolsScreenUiState = ToolsScreenUiState.Success( - runBlocking { - ToolDataSource().tool.first() - }) - ) + OxygenTheme { + ToolsScreen( + toolsScreenUiState = ToolsScreenUiState.Success( + runBlocking { + ToolDataSource().tool.first() + }) + ) + } } \ No newline at end of file