diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/StoreRepository.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/StoreRepository.kt index 374881f..c50169d 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/StoreRepository.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/StoreRepository.kt @@ -6,8 +6,6 @@ import top.fatweb.oxygen.toolbox.model.Result import top.fatweb.oxygen.toolbox.model.tool.ToolEntity interface StoreRepository { - val toolViewTemplate: Flow - suspend fun getStore(searchValue: String, currentPage: Int): Flow> fun detail( diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/ToolRepository.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/ToolRepository.kt index 5a19362..58ad233 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/ToolRepository.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/ToolRepository.kt @@ -4,6 +4,8 @@ import kotlinx.coroutines.flow.Flow import top.fatweb.oxygen.toolbox.model.tool.ToolEntity interface ToolRepository { + val toolViewTemplate: Flow + fun getAllToolsStream(): Flow> fun getToolById(id: Long): Flow diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/impl/NetworkStoreRepository.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/impl/NetworkStoreRepository.kt index 4d356c9..6386d71 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/impl/NetworkStoreRepository.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/impl/NetworkStoreRepository.kt @@ -6,7 +6,6 @@ import androidx.paging.PagingData import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import top.fatweb.oxygen.toolbox.data.network.OxygenNetworkDataSource -import top.fatweb.oxygen.toolbox.data.tool.ToolDataSource import top.fatweb.oxygen.toolbox.model.Result import top.fatweb.oxygen.toolbox.model.asExternalModel import top.fatweb.oxygen.toolbox.model.tool.ToolEntity @@ -20,13 +19,13 @@ import javax.inject.Inject private const val PAGE_SIZE = 20 internal class NetworkStoreRepository @Inject constructor( - private val oxygenNetworkDataSource: OxygenNetworkDataSource, - private val toolDataSource: ToolDataSource + private val oxygenNetworkDataSource: OxygenNetworkDataSource ) : StoreRepository { - override val toolViewTemplate: Flow - get() = toolDataSource.toolViewTemplate - override suspend fun getStore(searchValue: String, currentPage: Int): Flow> = + override suspend fun getStore( + searchValue: String, + currentPage: Int + ): Flow> = Pager( config = PagingConfig(PAGE_SIZE), pagingSourceFactory = { ToolStorePagingSource(oxygenNetworkDataSource, searchValue) } diff --git a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/impl/OfflineToolRepository.kt b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/impl/OfflineToolRepository.kt index 36d1829..06cd6ed 100644 --- a/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/impl/OfflineToolRepository.kt +++ b/app/src/main/kotlin/top/fatweb/oxygen/toolbox/repository/tool/impl/OfflineToolRepository.kt @@ -1,14 +1,19 @@ package top.fatweb.oxygen.toolbox.repository.tool.impl import kotlinx.coroutines.flow.Flow +import top.fatweb.oxygen.toolbox.data.tool.ToolDataSource import top.fatweb.oxygen.toolbox.data.tool.dao.ToolDao import top.fatweb.oxygen.toolbox.model.tool.ToolEntity import top.fatweb.oxygen.toolbox.repository.tool.ToolRepository import javax.inject.Inject class OfflineToolRepository @Inject constructor( + private val toolDataSource: ToolDataSource, private val toolDao: ToolDao ) : ToolRepository { + override val toolViewTemplate: Flow + get() = toolDataSource.toolViewTemplate + override fun getAllToolsStream(): Flow> = toolDao.selectAllTools() 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 deb4387..6ef1552 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 @@ -9,11 +9,12 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.stateIn import top.fatweb.oxygen.toolbox.model.Result import top.fatweb.oxygen.toolbox.navigation.ToolViewArgs import top.fatweb.oxygen.toolbox.repository.tool.StoreRepository +import top.fatweb.oxygen.toolbox.repository.tool.ToolRepository import top.fatweb.oxygen.toolbox.util.decodeToStringWithZip import javax.inject.Inject import kotlin.io.encoding.Base64 @@ -23,6 +24,7 @@ import kotlin.time.Duration.Companion.seconds @HiltViewModel class ToolViewScreenViewModel @Inject constructor( storeRepository: StoreRepository, + toolRepository: ToolRepository, savedStateHandle: SavedStateHandle ) : ViewModel() { private val toolViewArgs = ToolViewArgs(savedStateHandle) @@ -32,7 +34,8 @@ class ToolViewScreenViewModel @Inject constructor( val toolViewUiState: StateFlow = toolViewUiState( username = username, toolId = toolId, - storeRepository = storeRepository + storeRepository = storeRepository, + toolRepository = toolRepository ) .stateIn( scope = viewModelScope, @@ -44,36 +47,52 @@ class ToolViewScreenViewModel @Inject constructor( private fun toolViewUiState( username: String, toolId: String, - storeRepository: StoreRepository + storeRepository: StoreRepository, + toolRepository: ToolRepository ): Flow { - val result = storeRepository.detail( - username = username, - toolId = toolId - ) - val toolViewTemplate = storeRepository.toolViewTemplate + val toolViewTemplate = toolRepository.toolViewTemplate + val entityFlow = toolRepository.getToolByUsernameAndToolId(username, toolId) - return combine(result, toolViewTemplate, ::Pair).map { (result, toolViewTemplate) -> - when (result) { - is Result.Success -> { - val dist = result.data.dist!! - val base = result.data.base!! - ToolViewUiState.Success( - processHtml( - toolViewTemplate = toolViewTemplate, - distBase64 = dist, - baseBase64 = base + return flow { + combine(entityFlow, toolViewTemplate, ::Pair).collect { (entityFlow, toolViewTemplate) -> + if (entityFlow == null) { + storeRepository.detail(username, toolId).collect { + emit( + when (it) { + is Result.Success -> { + val dist = it.data.dist!! + val base = it.data.base!! + ToolViewUiState.Success( + processHtml( + toolViewTemplate = toolViewTemplate, + distBase64 = dist, + baseBase64 = base + ) + ) + } + + is Result.Loading -> ToolViewUiState.Loading + is Result.Error -> { + Log.e("TAG", "toolViewUiState: can not load tool", it.exception) + + ToolViewUiState.Error + } + + is Result.Fail -> ToolViewUiState.Error + } + ) + } + } else { + emit( + ToolViewUiState.Success( + processHtml( + toolViewTemplate = toolViewTemplate, + distBase64 = entityFlow.dist!!, + baseBase64 = entityFlow.base!! + ) ) ) } - - is Result.Loading -> ToolViewUiState.Loading - is Result.Error -> { - Log.e("TAG", "toolViewUiState: can not load tool", result.exception) - - ToolViewUiState.Error - } - - is Result.Fail -> ToolViewUiState.Error } } }