Refactor(ToolView): Optimize preview code logic

This commit is contained in:
2024-08-14 17:18:18 +08:00
parent 6732eb6a22
commit 35a421472d
7 changed files with 28 additions and 16 deletions

View File

@@ -64,7 +64,7 @@ import top.fatweb.oxygen.toolbox.ui.component.scrollbar.scrollbarState
internal fun ToolStoreRoute(
modifier: Modifier = Modifier,
viewModel: ToolStoreViewModel = hiltViewModel(),
onNavigateToToolView: (username: String, toolId: String) -> Unit,
onNavigateToToolView: (username: String, toolId: String, preview: Boolean) -> Unit,
) {
val toolStorePagingItems = viewModel.storeData.collectAsLazyPagingItems()
val installInfo by viewModel.installInfo.collectAsState()
@@ -85,7 +85,7 @@ internal fun ToolStoreRoute(
@Composable
internal fun ToolStoreScreen(
modifier: Modifier = Modifier,
onNavigateToToolView: (username: String, toolId: String) -> Unit,
onNavigateToToolView: (username: String, toolId: String, preview: Boolean) -> Unit,
toolStorePagingItems: LazyPagingItems<ToolEntity>,
onChangeInstallStatus: (status: ToolStoreUiState.InstallInfo.Status) -> Unit,
onChangeInstallType: (type: ToolStoreUiState.InstallInfo.Type) -> Unit,
@@ -125,7 +125,7 @@ internal fun ToolStoreScreen(
onChangeInstallType(installType)
},
onClick = {
onNavigateToToolView(it.authorUsername, it.toolId)
onNavigateToToolView(it.authorUsername, it.toolId, it.upgrade != null)
}
)

View File

@@ -10,6 +10,7 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.stateIn
import top.fatweb.oxygen.toolbox.model.Result
import top.fatweb.oxygen.toolbox.navigation.ToolViewArgs
@@ -32,11 +33,14 @@ class ToolViewScreenViewModel @Inject constructor(
private val toolViewArgs = ToolViewArgs(savedStateHandle)
private val username = toolViewArgs.username
private val toolId = toolViewArgs.toolId
private val preview = toolViewArgs.preview
val toolViewUiState: StateFlow<ToolViewUiState> = toolViewUiState(
savedStateHandle = savedStateHandle,
username = username,
toolId = toolId,
preview = preview,
storeRepository = storeRepository,
toolRepository = toolRepository
)
@@ -51,11 +55,13 @@ private fun toolViewUiState(
savedStateHandle: SavedStateHandle,
username: String,
toolId: String,
preview: Boolean,
storeRepository: StoreRepository,
toolRepository: ToolRepository
): Flow<ToolViewUiState> {
val toolViewTemplate = toolRepository.toolViewTemplate
val entityFlow = toolRepository.getToolByUsernameAndToolId(username, toolId)
val entityFlow =
if (!preview) toolRepository.getToolByUsernameAndToolId(username, toolId) else flowOf(null)
return flow {
combine(entityFlow, toolViewTemplate, ::Pair).collect { (entityFlow, toolViewTemplate) ->

View File

@@ -68,7 +68,7 @@ internal fun ToolsRoute(
modifier: Modifier = Modifier,
viewModel: ToolsScreenViewModel = hiltViewModel(),
onShowSnackbar: suspend (message: String, action: String?) -> Boolean,
onNavigateToToolView: (username: String, toolId: String) -> Unit,
onNavigateToToolView: (username: String, toolId: String, preview: Boolean) -> Unit,
onNavigateToToolStore: () -> Unit
) {
val toolsScreenUiStateState by viewModel.toolsScreenUiState.collectAsStateWithLifecycle()
@@ -88,7 +88,7 @@ internal fun ToolsRoute(
internal fun ToolsScreen(
modifier: Modifier = Modifier,
onShowSnackbar: suspend (message: String, action: String?) -> Boolean,
onNavigateToToolView: (username: String, toolId: String) -> Unit,
onNavigateToToolView: (username: String, toolId: String, preview: Boolean) -> Unit,
onNavigateToToolStore: () -> Unit,
toolsScreenUiState: ToolsScreenUiState,
onUninstall: (ToolEntity) -> Unit,
@@ -208,7 +208,7 @@ internal fun ToolsScreen(
private fun LazyStaggeredGridScope.toolsPanel(
toolItems: List<ToolEntity>,
onClick: (username: String, toolId: String) -> Unit,
onClick: (username: String, toolId: String, preview: Boolean) -> Unit,
onLongClick: (ToolEntity) -> Unit
) {
items(
@@ -216,7 +216,7 @@ private fun LazyStaggeredGridScope.toolsPanel(
key = { it.id },
) {
ToolCard(tool = it,
onClick = { onClick(it.authorUsername, it.toolId) },
onClick = { onClick(it.authorUsername, it.toolId, false) },
onLongClick = { onLongClick(it) })
}
}