Refactor(ToolStore): Save storeDate state when switch navigation
This commit is contained in:
@@ -6,7 +6,9 @@ import top.fatweb.oxygen.toolbox.model.Result
|
|||||||
import top.fatweb.oxygen.toolbox.model.tool.ToolEntity
|
import top.fatweb.oxygen.toolbox.model.tool.ToolEntity
|
||||||
|
|
||||||
interface StoreRepository {
|
interface StoreRepository {
|
||||||
suspend fun getStore(searchValue: String, currentPage: Int): Flow<PagingData<ToolEntity>>
|
suspend fun getStore(
|
||||||
|
searchValue: String
|
||||||
|
): Flow<PagingData<ToolEntity>>
|
||||||
|
|
||||||
fun detail(
|
fun detail(
|
||||||
username: String,
|
username: String,
|
||||||
|
|||||||
@@ -23,8 +23,7 @@ internal class NetworkStoreRepository @Inject constructor(
|
|||||||
private val toolDao: ToolDao
|
private val toolDao: ToolDao
|
||||||
) : StoreRepository {
|
) : StoreRepository {
|
||||||
override suspend fun getStore(
|
override suspend fun getStore(
|
||||||
searchValue: String,
|
searchValue: String
|
||||||
currentPage: Int
|
|
||||||
): Flow<PagingData<ToolEntity>> =
|
): Flow<PagingData<ToolEntity>> =
|
||||||
Pager(
|
Pager(
|
||||||
config = PagingConfig(pageSize = PAGE_SIZE),
|
config = PagingConfig(pageSize = PAGE_SIZE),
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ import androidx.paging.cachedIn
|
|||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.combine
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.flatMapLatest
|
import kotlinx.coroutines.flow.flatMapLatest
|
||||||
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.parcelize.Parcelize
|
import kotlinx.parcelize.Parcelize
|
||||||
import top.fatweb.oxygen.toolbox.model.Result
|
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.StoreRepository
|
||||||
import top.fatweb.oxygen.toolbox.repository.tool.ToolRepository
|
import top.fatweb.oxygen.toolbox.repository.tool.ToolRepository
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class ToolStoreViewModel @Inject constructor(
|
class ToolStoreViewModel @Inject constructor(
|
||||||
@@ -26,17 +28,20 @@ class ToolStoreViewModel @Inject constructor(
|
|||||||
private val savedStateHandle: SavedStateHandle
|
private val savedStateHandle: SavedStateHandle
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
private val searchValue = savedStateHandle.getStateFlow(SEARCH_VALUE, "")
|
private val searchValue = savedStateHandle.getStateFlow(SEARCH_VALUE, "")
|
||||||
private val currentPage = savedStateHandle.getStateFlow(CURRENT_PAGE, 1)
|
|
||||||
val installInfo = savedStateHandle.getStateFlow(INSTALL_INFO, ToolStoreUiState.InstallInfo())
|
val installInfo = savedStateHandle.getStateFlow(INSTALL_INFO, ToolStoreUiState.InstallInfo())
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
val storeData: Flow<PagingData<ToolEntity>> = combine(
|
val storeData: Flow<PagingData<ToolEntity>> =
|
||||||
searchValue, currentPage, ::Pair
|
searchValue.flatMapLatest { searchValue ->
|
||||||
).flatMapLatest { (searchValue, currentPage) ->
|
storeRepository
|
||||||
storeRepository
|
.getStore(searchValue)
|
||||||
.getStore(searchValue, currentPage)
|
.cachedIn(viewModelScope)
|
||||||
.cachedIn(viewModelScope)
|
}
|
||||||
}
|
.stateIn(
|
||||||
|
scope = viewModelScope,
|
||||||
|
initialValue = PagingData.empty(),
|
||||||
|
started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 5.seconds.inWholeMilliseconds)
|
||||||
|
)
|
||||||
|
|
||||||
fun onSearchValueChange(value: String) {
|
fun onSearchValueChange(value: String) {
|
||||||
savedStateHandle[SEARCH_VALUE] = value
|
savedStateHandle[SEARCH_VALUE] = value
|
||||||
@@ -103,5 +108,4 @@ data class ToolStoreUiState(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private const val SEARCH_VALUE = "searchValue"
|
private const val SEARCH_VALUE = "searchValue"
|
||||||
private const val CURRENT_PAGE = "currentPage"
|
|
||||||
private const val INSTALL_INFO = "installInfo"
|
private const val INSTALL_INFO = "installInfo"
|
||||||
|
|||||||
Reference in New Issue
Block a user