Refactor(ToolStore): Save storeDate state when switch navigation

This commit is contained in:
2024-08-26 15:46:04 +08:00
parent e9232631de
commit c9363ee34b
3 changed files with 18 additions and 13 deletions

View File

@@ -6,7 +6,9 @@ import top.fatweb.oxygen.toolbox.model.Result
import top.fatweb.oxygen.toolbox.model.tool.ToolEntity
interface StoreRepository {
suspend fun getStore(searchValue: String, currentPage: Int): Flow<PagingData<ToolEntity>>
suspend fun getStore(
searchValue: String
): Flow<PagingData<ToolEntity>>
fun detail(
username: String,

View File

@@ -23,8 +23,7 @@ internal class NetworkStoreRepository @Inject constructor(
private val toolDao: ToolDao
) : StoreRepository {
override suspend fun getStore(
searchValue: String,
currentPage: Int
searchValue: String
): Flow<PagingData<ToolEntity>> =
Pager(
config = PagingConfig(pageSize = PAGE_SIZE),

View File

@@ -9,8 +9,9 @@ import androidx.paging.cachedIn
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
import top.fatweb.oxygen.toolbox.model.Result
@@ -18,6 +19,7 @@ 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(
@@ -26,17 +28,20 @@ class ToolStoreViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle
) : ViewModel() {
private val searchValue = savedStateHandle.getStateFlow(SEARCH_VALUE, "")
private val currentPage = savedStateHandle.getStateFlow(CURRENT_PAGE, 1)
val installInfo = savedStateHandle.getStateFlow(INSTALL_INFO, ToolStoreUiState.InstallInfo())
@OptIn(ExperimentalCoroutinesApi::class)
val storeData: Flow<PagingData<ToolEntity>> = combine(
searchValue, currentPage, ::Pair
).flatMapLatest { (searchValue, currentPage) ->
storeRepository
.getStore(searchValue, currentPage)
.cachedIn(viewModelScope)
}
val storeData: Flow<PagingData<ToolEntity>> =
searchValue.flatMapLatest { searchValue ->
storeRepository
.getStore(searchValue)
.cachedIn(viewModelScope)
}
.stateIn(
scope = viewModelScope,
initialValue = PagingData.empty(),
started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 5.seconds.inWholeMilliseconds)
)
fun onSearchValueChange(value: String) {
savedStateHandle[SEARCH_VALUE] = value
@@ -103,5 +108,4 @@ data class ToolStoreUiState(
}
private const val SEARCH_VALUE = "searchValue"
private const val CURRENT_PAGE = "currentPage"
private const val INSTALL_INFO = "installInfo"