Skip to content

Commit

Permalink
refactor(home): optimize home catalogs loader
Browse files Browse the repository at this point in the history
  • Loading branch information
rhenwinch committed Sep 30, 2024
1 parent ab437cc commit b1be7a3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
// Version
val versionMajor = 2
val versionMinor = 1
val versionPatch = 1
val versionPatch = 2
val versionBuild = 0
val applicationName: String = libs.versions.applicationName.get()
val _applicationId: String = libs.versions.applicationId.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package com.flixclusive.domain.home

import com.flixclusive.core.network.util.Resource
import com.flixclusive.core.ui.common.util.PagingState
import com.flixclusive.core.util.coroutines.AppDispatchers
import com.flixclusive.core.util.coroutines.AppDispatchers.Companion.launchOnIO
import com.flixclusive.core.util.coroutines.AppDispatchers.Companion.withDefaultContext
import com.flixclusive.core.util.exception.safeCall
import com.flixclusive.data.configuration.AppConfigurationManager
import com.flixclusive.data.provider.ProviderManager
Expand All @@ -20,11 +21,9 @@ import com.flixclusive.model.provider.ProviderCatalog
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.random.Random
Expand Down Expand Up @@ -55,20 +54,23 @@ class HomeItemsProviderUseCase @Inject constructor(
}

operator fun invoke() {
AppDispatchers.Default.scope.launch {
launchOnIO {
_state.update { it.copy(status = Resource.Loading) }

try {
val catalogs = getHomeRecommendations()
val catalogs = withDefaultContext { getHomeRecommendations() }
_state.update {
it.copy(
catalogs = catalogs,
rowItems = List(catalogs.size) { emptyList() },
rowItemsPagingState = catalogs.map { catalog ->
PaginationStateInfo(
canPaginate = catalog.canPaginate,
pagingState = if (!catalog.canPaginate) PagingState.PAGINATING_EXHAUST else PagingState.IDLE,
currentPage = 1
)
rowItemsPagingState = withDefaultContext {
catalogs.map { catalog ->
PaginationStateInfo(
canPaginate = catalog.canPaginate,
pagingState = if (!catalog.canPaginate) PagingState.PAGINATING_EXHAUST else PagingState.IDLE,
currentPage = 1
)
}
}
)
}
Expand Down Expand Up @@ -110,17 +112,14 @@ class HomeItemsProviderUseCase @Inject constructor(
it.flatMap { api -> api.catalogs }
}.distinctUntilChanged()

AppDispatchers.Default.scope.launch {
configurationProvider.configurationStatus
.combine(catalogs) { configStatus, catalogs ->
configStatus to catalogs
}
.collectLatest { (configStatus, catalogs) ->
if (configStatus is Resource.Success) {
_state.update { it.copy(providerCatalogs = catalogs) }
invoke()
}
launchOnIO {
catalogs.collectLatest {
_state.update { homeState ->
homeState.copy(providerCatalogs = it)
}

invoke()
}
}
}

Expand Down Expand Up @@ -166,6 +165,7 @@ class HomeItemsProviderUseCase @Inject constructor(
}
}
}

private suspend fun getHeaderItem(catalogs: List<Catalog>): Film? {
val traversedCatalogs = mutableSetOf<Int>()
val traversedFilms = mutableSetOf<String>()
Expand Down

0 comments on commit b1be7a3

Please sign in to comment.