From 60ffc569a5be446c1591cfcff13436a22a934a3b Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Mon, 12 Aug 2024 17:39:32 +0800 Subject: [PATCH] Feat(ToolView): Add preview tooltip --- .../top/fatweb/oxygen/toolbox/ui/OxygenApp.kt | 4 +++- .../oxygen/toolbox/ui/about/AboutScreen.kt | 4 +++- .../toolbox/ui/about/LibrariesScreen.kt | 4 +++- .../toolbox/ui/component/OxygenTopAppBar.kt | 7 +++---- .../oxygen/toolbox/ui/tool/ToolViewScreen.kt | 17 ++++++++++++++++- .../ui/tool/ToolViewScreenViewModel.kt | 19 ++++++++++++++++--- app/src/main/res/values-zh/strings.xml | 2 ++ app/src/main/res/values/strings.xml | 2 ++ 8 files changed, 48 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/OxygenApp.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/OxygenApp.kt index 020ed3c..014a431 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/OxygenApp.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/OxygenApp.kt @@ -145,7 +145,9 @@ fun OxygenApp(appState: OxygenAppState) { if (destination != null) { OxygenTopAppBar( scrollBehavior = topAppBarScrollBehavior, - titleRes = destination.titleTextId, + title = { + Text(text = stringResource(id = destination.titleTextId)) + }, navigationIcon = OxygenIcons.Search, navigationIconContentDescription = stringResource(R.string.feature_settings_top_app_bar_navigation_icon_description), actionIcon = OxygenIcons.MoreVert, 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 0d15c1c..eafb509 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 @@ -84,7 +84,9 @@ internal fun AboutScreen( ) { OxygenTopAppBar( scrollBehavior = topAppBarScrollBehavior, - titleRes = R.string.feature_settings_more_about, + title = { + Text(text = stringResource(id = R.string.feature_settings_more_about)) + }, navigationIcon = OxygenIcons.Back, navigationIconContentDescription = stringResource(R.string.core_back), colors = TopAppBarDefaults.centerAlignedTopAppBarColors( diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/about/LibrariesScreen.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/about/LibrariesScreen.kt index 267cab8..8aa26bd 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/about/LibrariesScreen.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/about/LibrariesScreen.kt @@ -134,7 +134,9 @@ internal fun LibrariesScreen( ) { OxygenTopAppBar( scrollBehavior = topAppBarScrollBehavior, - titleRes = R.string.feature_settings_open_source_license, + title = { + Text(text = stringResource(id = R.string.feature_settings_open_source_license)) + }, navigationIcon = OxygenIcons.Back, navigationIconContentDescription = stringResource(R.string.core_back), actionIcon = OxygenIcons.Search, 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 ebc516d..e937a25 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 @@ -1,6 +1,5 @@ package top.fatweb.oxygen.toolbox.ui.component -import androidx.annotation.StringRes import androidx.compose.animation.core.animateIntAsState import androidx.compose.foundation.background import androidx.compose.foundation.layout.WindowInsets @@ -46,7 +45,7 @@ import android.R as androidR fun OxygenTopAppBar( modifier: Modifier = Modifier, scrollBehavior: TopAppBarScrollBehavior? = null, - @StringRes titleRes: Int? = null, + title: @Composable () -> Unit = {}, navigationIcon: ImageVector? = null, navigationIconContentDescription: String? = null, actionIcon: ImageVector? = null, @@ -120,7 +119,7 @@ fun OxygenTopAppBar( if ("\n" !in it) onQueryChange(it) } ) - else if (titleRes != null) Text(stringResource(titleRes)) + else title() }, navigationIcon = { navigationIcon?.let { @@ -161,7 +160,7 @@ fun OxygenTopAppBar( private fun OxygenTopAppBarPreview() { OxygenTheme { OxygenTopAppBar( - titleRes = androidR.string.untitled, + title = { Text(text = stringResource(androidR.string.untitled)) }, navigationIcon = OxygenIcons.Search, navigationIconContentDescription = "Navigation icon", actionIcon = OxygenIcons.MoreVert, diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolViewScreen.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolViewScreen.kt index 80aa495..22253a0 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolViewScreen.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolViewScreen.kt @@ -47,10 +47,12 @@ internal fun ToolViewRoute( viewModel: ToolViewScreenViewModel = hiltViewModel(), onBackClick: () -> Unit ) { + val isPreview by viewModel.isPreview.collectAsStateWithLifecycle() val toolViewUiState by viewModel.toolViewUiState.collectAsStateWithLifecycle() ToolViewScreen( modifier = modifier, + isPreview = isPreview, onBackClick = onBackClick, toolViewUiState = toolViewUiState ) @@ -61,6 +63,7 @@ internal fun ToolViewRoute( @Composable internal fun ToolViewScreen( modifier: Modifier = Modifier, + isPreview: Boolean, onBackClick: () -> Unit, toolViewUiState: ToolViewUiState ) { @@ -70,7 +73,7 @@ internal fun ToolViewScreen( Scaffold( modifier = Modifier, containerColor = Color.Transparent, - contentWindowInsets = WindowInsets(0, 0, 0, 0), + contentWindowInsets = WindowInsets(0, 0, 0, 0) ) { padding -> Column( modifier @@ -85,6 +88,18 @@ internal fun ToolViewScreen( ) { OxygenTopAppBar( modifier = Modifier.zIndex(100f), + title = { + Text( + text = when (toolViewUiState) { + ToolViewUiState.Loading -> stringResource(R.string.core_loading) + ToolViewUiState.Error -> stringResource(R.string.feature_tools_can_not_open) + is ToolViewUiState.Success -> if (isPreview) stringResource( + R.string.feature_tool_view_preview_suffix, + toolViewUiState.toolName + ) else toolViewUiState.toolName + } + ) + }, navigationIcon = OxygenIcons.Back, navigationIconContentDescription = stringResource(R.string.core_back), colors = TopAppBarDefaults.centerAlignedTopAppBarColors( diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolViewScreenViewModel.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolViewScreenViewModel.kt index 6ef1552..eefefd3 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolViewScreenViewModel.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/ui/tool/ToolViewScreenViewModel.kt @@ -27,11 +27,14 @@ class ToolViewScreenViewModel @Inject constructor( toolRepository: ToolRepository, savedStateHandle: SavedStateHandle ) : ViewModel() { + val isPreview = savedStateHandle.getStateFlow(IS_PREVIEW, false) + private val toolViewArgs = ToolViewArgs(savedStateHandle) - val username = toolViewArgs.username - val toolId = toolViewArgs.toolId + private val username = toolViewArgs.username + private val toolId = toolViewArgs.toolId val toolViewUiState: StateFlow = toolViewUiState( + savedStateHandle = savedStateHandle, username = username, toolId = toolId, storeRepository = storeRepository, @@ -45,6 +48,7 @@ class ToolViewScreenViewModel @Inject constructor( } private fun toolViewUiState( + savedStateHandle: SavedStateHandle, username: String, toolId: String, storeRepository: StoreRepository, @@ -56,6 +60,7 @@ private fun toolViewUiState( return flow { combine(entityFlow, toolViewTemplate, ::Pair).collect { (entityFlow, toolViewTemplate) -> if (entityFlow == null) { + savedStateHandle[IS_PREVIEW] = true storeRepository.detail(username, toolId).collect { emit( when (it) { @@ -63,6 +68,7 @@ private fun toolViewUiState( val dist = it.data.dist!! val base = it.data.base!! ToolViewUiState.Success( + it.data.name, processHtml( toolViewTemplate = toolViewTemplate, distBase64 = dist, @@ -83,8 +89,10 @@ private fun toolViewUiState( ) } } else { + savedStateHandle[IS_PREVIEW] = false emit( ToolViewUiState.Success( + entityFlow.name, processHtml( toolViewTemplate = toolViewTemplate, distBase64 = entityFlow.dist!!, @@ -98,7 +106,10 @@ private fun toolViewUiState( } sealed interface ToolViewUiState { - data class Success(val htmlData: String) : ToolViewUiState + data class Success( + val toolName: String, + val htmlData: String + ) : ToolViewUiState data object Error : ToolViewUiState @@ -112,3 +123,5 @@ private fun processHtml(toolViewTemplate: String, distBase64: String, baseBase64 return toolViewTemplate.replace("{{replace_code}}", "$dist\n$base") } + +private const val IS_PREVIEW = "IS_PREVIEW" diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index ffac717..2f1c3e0 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -33,6 +33,8 @@ 暂无工具已安装 前往商店… + %1$s (预览) + 收藏 设置 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 956f21b..81f668f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -32,6 +32,8 @@ No tools installed yet Go to store… + %1$s (Preview) + Star Settings