Refactor(ToolStore): Optimize the way to obtain installation status
This commit is contained in:
@@ -51,7 +51,6 @@ import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.paging.LoadState
|
||||
import androidx.paging.compose.LazyPagingItems
|
||||
import androidx.paging.compose.collectAsLazyPagingItems
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import top.fatweb.oxygen.toolbox.R
|
||||
import top.fatweb.oxygen.toolbox.icon.Loading
|
||||
import top.fatweb.oxygen.toolbox.icon.OxygenIcons
|
||||
@@ -74,7 +73,6 @@ internal fun ToolStoreRoute(
|
||||
modifier = modifier,
|
||||
onNavigateToToolView = onNavigateToToolView,
|
||||
toolStorePagingItems = toolStorePagingItems,
|
||||
hasInstalled = { viewModel.hasInstalled(it) },
|
||||
onChangeInstallStatus = viewModel::changeInstallStatus,
|
||||
onInstallTool = viewModel::installTool,
|
||||
installInfo = installInfo
|
||||
@@ -86,7 +84,6 @@ internal fun ToolStoreScreen(
|
||||
modifier: Modifier = Modifier,
|
||||
onNavigateToToolView: (username: String, toolId: String) -> Unit,
|
||||
toolStorePagingItems: LazyPagingItems<ToolEntity>,
|
||||
hasInstalled: (ToolEntity) -> StateFlow<Boolean>,
|
||||
onChangeInstallStatus: (installStatus: ToolStoreUiState.Status) -> Unit,
|
||||
onInstallTool: (username: String, toolId: String) -> Unit,
|
||||
installInfo: ToolStoreUiState.InstallInfo
|
||||
@@ -119,7 +116,6 @@ internal fun ToolStoreScreen(
|
||||
) {
|
||||
toolsPanel(
|
||||
toolStorePagingItems = toolStorePagingItems,
|
||||
hasInstalled = hasInstalled,
|
||||
onAction = { username, toolId ->
|
||||
installToolUsername = username
|
||||
installToolId = toolId
|
||||
@@ -179,7 +175,6 @@ internal fun ToolStoreScreen(
|
||||
|
||||
private fun LazyStaggeredGridScope.toolsPanel(
|
||||
toolStorePagingItems: LazyPagingItems<ToolEntity>,
|
||||
hasInstalled: (ToolEntity) -> StateFlow<Boolean>,
|
||||
onAction: (username: String, toolId: String) -> Unit,
|
||||
onClick: (username: String, toolId: String) -> Unit
|
||||
) {
|
||||
@@ -187,10 +182,9 @@ private fun LazyStaggeredGridScope.toolsPanel(
|
||||
items = toolStorePagingItems.itemSnapshotList,
|
||||
key = { it!!.id },
|
||||
) {
|
||||
val installed by hasInstalled(it!!).collectAsState()
|
||||
ToolCard(
|
||||
tool = it!!,
|
||||
actionIcon = if (installed) null else OxygenIcons.Download,
|
||||
actionIcon = if (!it.isInstalled) OxygenIcons.Download else null,
|
||||
actionIconContentDescription = stringResource(R.string.core_install),
|
||||
onAction = { onAction(it.authorUsername, it.toolId) },
|
||||
onClick = { onClick(it.authorUsername, it.toolId) },
|
||||
|
||||
@@ -9,13 +9,8 @@ import androidx.paging.cachedIn
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import top.fatweb.oxygen.toolbox.model.Result
|
||||
@@ -23,7 +18,6 @@ import top.fatweb.oxygen.toolbox.model.tool.ToolEntity
|
||||
import top.fatweb.oxygen.toolbox.repository.tool.StoreRepository
|
||||
import top.fatweb.oxygen.toolbox.repository.tool.ToolRepository
|
||||
import javax.inject.Inject
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
@HiltViewModel
|
||||
class ToolStoreViewModel @Inject constructor(
|
||||
@@ -33,10 +27,6 @@ class ToolStoreViewModel @Inject constructor(
|
||||
) : ViewModel() {
|
||||
private val searchValue = savedStateHandle.getStateFlow(SEARCH_VALUE, "")
|
||||
private val currentPage = savedStateHandle.getStateFlow(CURRENT_PAGE, 1)
|
||||
private val installedStatus =
|
||||
savedStateHandle.getStateFlow<MutableMap<String, MutableMap<String, Boolean>>>(
|
||||
INSTALLED_STATUS, mutableMapOf()
|
||||
)
|
||||
val installInfo = savedStateHandle.getStateFlow(INSTALL_INFO, ToolStoreUiState.InstallInfo())
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
@@ -48,26 +38,6 @@ class ToolStoreViewModel @Inject constructor(
|
||||
)
|
||||
}
|
||||
|
||||
fun hasInstalled(toolEntity: ToolEntity): StateFlow<Boolean> =
|
||||
installedStatus.value[toolEntity.authorUsername]?.get(toolEntity.toolId)
|
||||
?.let { MutableStateFlow(it) } ?: toolRepository.getToolByUsernameAndToolId(
|
||||
toolEntity.authorUsername, toolEntity.toolId
|
||||
).map {
|
||||
if (installedStatus.value[toolEntity.authorUsername] == null) {
|
||||
installedStatus.value[toolEntity.authorUsername] =
|
||||
mutableMapOf(toolEntity.toolId to (it != null))
|
||||
} else {
|
||||
installedStatus.value[toolEntity.authorUsername]?.set(
|
||||
toolEntity.toolId, it != null
|
||||
)
|
||||
}
|
||||
it != null
|
||||
}.stateIn(
|
||||
scope = viewModelScope,
|
||||
initialValue = true,
|
||||
started = SharingStarted.WhileSubscribed(5.seconds.inWholeMilliseconds)
|
||||
)
|
||||
|
||||
fun changeInstallStatus(installStatus: ToolStoreUiState.Status) {
|
||||
savedStateHandle[INSTALL_INFO] = ToolStoreUiState.InstallInfo(installStatus)
|
||||
}
|
||||
@@ -86,11 +56,6 @@ class ToolStoreViewModel @Inject constructor(
|
||||
toolRepository.saveTool(it.data)
|
||||
savedStateHandle[INSTALL_INFO] =
|
||||
ToolStoreUiState.InstallInfo(ToolStoreUiState.Status.Success)
|
||||
if (installedStatus.value[username] == null) {
|
||||
installedStatus.value[username] = mutableMapOf(toolId to true)
|
||||
} else {
|
||||
installedStatus.value[username]?.set(toolId, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,5 +79,4 @@ data class ToolStoreUiState(
|
||||
|
||||
private const val SEARCH_VALUE = "searchValue"
|
||||
private const val CURRENT_PAGE = "currentPage"
|
||||
private const val INSTALLED_STATUS = "installedStatus"
|
||||
private const val INSTALL_INFO = "installInfo"
|
||||
|
||||
Reference in New Issue
Block a user