@@ -2,7 +2,8 @@ package com.flixclusive.domain.home
22
33import com.flixclusive.core.network.util.Resource
44import com.flixclusive.core.ui.common.util.PagingState
5- import com.flixclusive.core.util.coroutines.AppDispatchers
5+ import com.flixclusive.core.util.coroutines.AppDispatchers.Companion.launchOnIO
6+ import com.flixclusive.core.util.coroutines.AppDispatchers.Companion.withDefaultContext
67import com.flixclusive.core.util.exception.safeCall
78import com.flixclusive.data.configuration.AppConfigurationManager
89import com.flixclusive.data.provider.ProviderManager
@@ -20,11 +21,9 @@ import com.flixclusive.model.provider.ProviderCatalog
2021import kotlinx.coroutines.flow.MutableStateFlow
2122import kotlinx.coroutines.flow.asStateFlow
2223import kotlinx.coroutines.flow.collectLatest
23- import kotlinx.coroutines.flow.combine
2424import kotlinx.coroutines.flow.distinctUntilChanged
2525import kotlinx.coroutines.flow.map
2626import kotlinx.coroutines.flow.update
27- import kotlinx.coroutines.launch
2827import javax.inject.Inject
2928import javax.inject.Singleton
3029import kotlin.random.Random
@@ -55,20 +54,23 @@ class HomeItemsProviderUseCase @Inject constructor(
5554 }
5655
5756 operator fun invoke () {
58- AppDispatchers . Default .scope.launch {
57+ launchOnIO {
5958 _state .update { it.copy(status = Resource .Loading ) }
59+
6060 try {
61- val catalogs = getHomeRecommendations()
61+ val catalogs = withDefaultContext { getHomeRecommendations() }
6262 _state .update {
6363 it.copy(
6464 catalogs = catalogs,
6565 rowItems = List (catalogs.size) { emptyList() },
66- rowItemsPagingState = catalogs.map { catalog ->
67- PaginationStateInfo (
68- canPaginate = catalog.canPaginate,
69- pagingState = if (! catalog.canPaginate) PagingState .PAGINATING_EXHAUST else PagingState .IDLE ,
70- currentPage = 1
71- )
66+ rowItemsPagingState = withDefaultContext {
67+ catalogs.map { catalog ->
68+ PaginationStateInfo (
69+ canPaginate = catalog.canPaginate,
70+ pagingState = if (! catalog.canPaginate) PagingState .PAGINATING_EXHAUST else PagingState .IDLE ,
71+ currentPage = 1
72+ )
73+ }
7274 }
7375 )
7476 }
@@ -110,17 +112,14 @@ class HomeItemsProviderUseCase @Inject constructor(
110112 it.flatMap { api -> api.catalogs }
111113 }.distinctUntilChanged()
112114
113- AppDispatchers .Default .scope.launch {
114- configurationProvider.configurationStatus
115- .combine(catalogs) { configStatus, catalogs ->
116- configStatus to catalogs
117- }
118- .collectLatest { (configStatus, catalogs) ->
119- if (configStatus is Resource .Success ) {
120- _state .update { it.copy(providerCatalogs = catalogs) }
121- invoke()
122- }
115+ launchOnIO {
116+ catalogs.collectLatest {
117+ _state .update { homeState ->
118+ homeState.copy(providerCatalogs = it)
123119 }
120+
121+ invoke()
122+ }
124123 }
125124 }
126125
@@ -166,6 +165,7 @@ class HomeItemsProviderUseCase @Inject constructor(
166165 }
167166 }
168167 }
168+
169169 private suspend fun getHeaderItem (catalogs : List <Catalog >): Film ? {
170170 val traversedCatalogs = mutableSetOf<Int >()
171171 val traversedFilms = mutableSetOf<String >()
0 commit comments