From d8c5031c9b3186e02797e8c509b302696f1572c3 Mon Sep 17 00:00:00 2001 From: kioko Date: Tue, 11 Jul 2023 14:36:59 +0200 Subject: [PATCH] Minor cleanup: - Rename classes. - Fix lint errors. --- .../DiscoverPreviewParameterProvider.kt | 10 +- .../tvmaniac/showsgrid/GridState.kt | 2 +- .../tvmaniac/showsgrid/GridStateMachine.kt | 8 +- .../tvmaniac/inject/ApplicationComponent.kt | 4 +- .../AndroidNetworkExceptionHandlerUtil.kt | 8 +- .../inject/NetworkPlatformComponent.kt | 5 +- .../core/networkutil/IosExceptionHandler.kt | 8 +- .../inject/NetworkPlatformComponent.kt | 1 - ...owsRepository.kt => DiscoverRepository.kt} | 4 +- ...ShowsComponent.kt => DiscoverComponent.kt} | 6 +- ...itoryImpl.kt => DiscoverRepositoryImpl.kt} | 8 +- ...nseMapper.kt => DiscoverResponseMapper.kt} | 123 +++++++++--------- .../implementation/DiscoverShowsStore.kt | 27 +--- .../shows/implementation/ShowStore.kt | 2 +- ...epository.kt => FakeDiscoverRepository.kt} | 6 +- .../discover/DiscoverShowsState.kt | 2 +- .../discover/DiscoverStateMachineTest.kt | 4 +- .../showdetails/ShowDetailsStateMachine.kt | 14 +- .../ShowDetailsStateMachineTest.kt | 14 +- .../base/ApplicationComponent.kt | 4 +- 20 files changed, 119 insertions(+), 141 deletions(-) rename data/shows/api/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/api/{ShowsRepository.kt => DiscoverRepository.kt} (88%) rename data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/{ShowsComponent.kt => DiscoverComponent.kt} (65%) rename data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/{ShowsRepositoryImpl.kt => DiscoverRepositoryImpl.kt} (95%) rename data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/{ShowsResponseMapper.kt => DiscoverResponseMapper.kt} (52%) rename data/shows/testing/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/testing/{FakeShowsRepository.kt => FakeDiscoverRepository.kt} (94%) diff --git a/android-features/discover/src/main/java/com/thomaskioko/tvmaniac/discover/DiscoverPreviewParameterProvider.kt b/android-features/discover/src/main/java/com/thomaskioko/tvmaniac/discover/DiscoverPreviewParameterProvider.kt index 393e17994..b0d86f32d 100644 --- a/android-features/discover/src/main/java/com/thomaskioko/tvmaniac/discover/DiscoverPreviewParameterProvider.kt +++ b/android-features/discover/src/main/java/com/thomaskioko/tvmaniac/discover/DiscoverPreviewParameterProvider.kt @@ -10,11 +10,11 @@ val shows = TvShow( traktId = 84958, title = "Loki", overview = "After stealing the Tesseract during the events of “Avengers: Endgame,” " + - "an alternate version of Loki is brought to the mysterious Time Variance " + - "Authority, a bureaucratic organization that exists outside of time and " + - "space and monitors the timeline. They give Loki a choice: face being " + - "erased from existence due to being a “time variant”or help fix " + - "the timeline and stop a greater threat.", + "an alternate version of Loki is brought to the mysterious Time Variance " + + "Authority, a bureaucratic organization that exists outside of time and " + + "space and monitors the timeline. They give Loki a choice: face being " + + "erased from existence due to being a “time variant”or help fix " + + "the timeline and stop a greater threat.", posterImageUrl = "/kEl2t3OhXc3Zb9FBh1AuYzRTgZp.jpg", backdropImageUrl = "/kEl2t3OhXc3Zb9FBh1AuYzRTgZp.jpg", language = "en", diff --git a/android-features/shows-grid/src/main/kotlin/com/thomaskioko/tvmaniac/showsgrid/GridState.kt b/android-features/shows-grid/src/main/kotlin/com/thomaskioko/tvmaniac/showsgrid/GridState.kt index dc4963b27..db4097a12 100644 --- a/android-features/shows-grid/src/main/kotlin/com/thomaskioko/tvmaniac/showsgrid/GridState.kt +++ b/android-features/shows-grid/src/main/kotlin/com/thomaskioko/tvmaniac/showsgrid/GridState.kt @@ -10,4 +10,4 @@ data class ShowsLoaded( val list: List, ) : GridState -data class LoadingContentError(val errorMessage: String) : GridState +data class LoadingContentError(val errorMessage: String? = null) : GridState diff --git a/android-features/shows-grid/src/main/kotlin/com/thomaskioko/tvmaniac/showsgrid/GridStateMachine.kt b/android-features/shows-grid/src/main/kotlin/com/thomaskioko/tvmaniac/showsgrid/GridStateMachine.kt index 6d73cc09a..2a42501ed 100644 --- a/android-features/shows-grid/src/main/kotlin/com/thomaskioko/tvmaniac/showsgrid/GridStateMachine.kt +++ b/android-features/shows-grid/src/main/kotlin/com/thomaskioko/tvmaniac/showsgrid/GridStateMachine.kt @@ -3,8 +3,7 @@ package com.thomaskioko.tvmaniac.showsgrid import com.freeletics.flowredux.dsl.ChangedState import com.freeletics.flowredux.dsl.FlowReduxStateMachine import com.freeletics.flowredux.dsl.State -import com.thomaskioko.tvmaniac.shows.api.ShowsRepository -import com.thomaskioko.tvmaniac.util.ExceptionHandler +import com.thomaskioko.tvmaniac.shows.api.DiscoverRepository import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.FlowPreview import me.tatarka.inject.annotations.Inject @@ -13,8 +12,7 @@ import org.mobilenativefoundation.store.store5.StoreReadResponse @OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class) @Inject class GridStateMachine( - private val repository: ShowsRepository, - private val exceptionHandler: ExceptionHandler, + private val repository: DiscoverRepository, ) : FlowReduxStateMachine(initialState = LoadingContent) { init { @@ -52,7 +50,7 @@ class GridStateMachine( ) } is StoreReadResponse.Error.Exception -> state.override { - LoadingContentError(exceptionHandler.resolveError(result.error)) + LoadingContentError(result.error.message) } is StoreReadResponse.Error.Message -> state.override { LoadingContentError(result.message) diff --git a/app/src/main/kotlin/com/thomaskioko/tvmaniac/inject/ApplicationComponent.kt b/app/src/main/kotlin/com/thomaskioko/tvmaniac/inject/ApplicationComponent.kt index 2ce0c2480..36f2d84a5 100644 --- a/app/src/main/kotlin/com/thomaskioko/tvmaniac/inject/ApplicationComponent.kt +++ b/app/src/main/kotlin/com/thomaskioko/tvmaniac/inject/ApplicationComponent.kt @@ -19,7 +19,7 @@ import com.thomaskioko.tvmaniac.resourcemanager.implementation.RequestManagerCom import com.thomaskioko.tvmaniac.seasondetails.implementation.SeasonDetailsComponent import com.thomaskioko.tvmaniac.seasons.implementation.SeasonsComponent import com.thomaskioko.tvmaniac.showimages.implementation.ShowImagesComponent -import com.thomaskioko.tvmaniac.shows.implementation.ShowsComponent +import com.thomaskioko.tvmaniac.shows.implementation.DiscoverComponent import com.thomaskioko.tvmaniac.similar.implementation.SimilarShowsComponent import com.thomaskioko.tvmaniac.tmdb.implementation.TmdbPlatformComponent import com.thomaskioko.tvmaniac.traktauth.implementation.TraktAuthComponent @@ -49,7 +49,7 @@ abstract class ApplicationComponent( RequestManagerComponent, SeasonsComponent, SeasonDetailsComponent, - ShowsComponent, + DiscoverComponent, ShowImagesComponent, SimilarShowsComponent, StatsComponent, diff --git a/core/networkutil/src/androidMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/AndroidNetworkExceptionHandlerUtil.kt b/core/networkutil/src/androidMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/AndroidNetworkExceptionHandlerUtil.kt index 41b83cf03..b705b607b 100644 --- a/core/networkutil/src/androidMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/AndroidNetworkExceptionHandlerUtil.kt +++ b/core/networkutil/src/androidMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/AndroidNetworkExceptionHandlerUtil.kt @@ -9,7 +9,7 @@ import java.net.UnknownHostException @Inject class AndroidNetworkExceptionHandlerUtil( - private val configs: Configs + private val configs: Configs, ) : NetworkExceptionHandler { val errorMessage = "Something went wrong" @@ -17,9 +17,11 @@ class AndroidNetworkExceptionHandlerUtil( when (throwable) { is HttpExceptions -> if (configs.isDebug) throwable.message else errorMessage is ClientRequestException -> { - if (configs.isDebug) + if (configs.isDebug) { "${throwable.response.status.value} Missing Api Key" - else errorMessage + } else { + errorMessage + } } is UnknownHostException -> "No Internet Connection!" diff --git a/core/networkutil/src/androidMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/inject/NetworkPlatformComponent.kt b/core/networkutil/src/androidMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/inject/NetworkPlatformComponent.kt index ff8b8734d..6c16a51cb 100644 --- a/core/networkutil/src/androidMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/inject/NetworkPlatformComponent.kt +++ b/core/networkutil/src/androidMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/inject/NetworkPlatformComponent.kt @@ -5,12 +5,11 @@ import com.thomaskioko.tvmaniac.core.networkutil.NetworkExceptionHandler import com.thomaskioko.tvmaniac.util.scope.ApplicationScope import me.tatarka.inject.annotations.Provides -interface NetworkPlatformComponent { +actual interface NetworkPlatformComponent { @ApplicationScope @Provides fun provideNetworkExceptionHandler( - bind: AndroidNetworkExceptionHandlerUtil + bind: AndroidNetworkExceptionHandlerUtil, ): NetworkExceptionHandler = bind - } diff --git a/core/networkutil/src/iosMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/IosExceptionHandler.kt b/core/networkutil/src/iosMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/IosExceptionHandler.kt index 1db3642e9..e7fd9ea5b 100644 --- a/core/networkutil/src/iosMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/IosExceptionHandler.kt +++ b/core/networkutil/src/iosMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/IosExceptionHandler.kt @@ -8,7 +8,7 @@ import me.tatarka.inject.annotations.Inject @Inject class IosExceptionHandler( - private val configs: Configs + private val configs: Configs, ) : NetworkExceptionHandler { val errorMessage = "Something went wrong" @@ -16,9 +16,11 @@ class IosExceptionHandler( when (throwable) { is HttpExceptions -> if (configs.isDebug) throwable.message else errorMessage is ClientRequestException -> { - if (configs.isDebug) + if (configs.isDebug) { "${throwable.response.status.value} Missing Api Key" - else errorMessage + } else { + errorMessage + } } is JsonConvertException -> "Error Parsing Response" diff --git a/core/networkutil/src/iosMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/inject/NetworkPlatformComponent.kt b/core/networkutil/src/iosMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/inject/NetworkPlatformComponent.kt index 6ec92361a..0f8f5b4eb 100644 --- a/core/networkutil/src/iosMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/inject/NetworkPlatformComponent.kt +++ b/core/networkutil/src/iosMain/kotlin/com/thomaskioko/tvmaniac/core/networkutil/inject/NetworkPlatformComponent.kt @@ -9,5 +9,4 @@ actual interface NetworkPlatformComponent { @ApplicationScope @Provides fun provideExceptionHandler(bind: IosExceptionHandler): NetworkExceptionHandler = bind - } diff --git a/data/shows/api/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/api/ShowsRepository.kt b/data/shows/api/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/api/DiscoverRepository.kt similarity index 88% rename from data/shows/api/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/api/ShowsRepository.kt rename to data/shows/api/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/api/DiscoverRepository.kt index 99dc642cc..928891f5d 100644 --- a/data/shows/api/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/api/ShowsRepository.kt +++ b/data/shows/api/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/api/DiscoverRepository.kt @@ -6,7 +6,7 @@ import com.thomaskioko.tvmaniac.core.db.ShowsByCategory import kotlinx.coroutines.flow.Flow import org.mobilenativefoundation.store.store5.StoreReadResponse -interface ShowsRepository { +interface DiscoverRepository { fun observeShow(traktId: Long): Flow> @@ -18,7 +18,7 @@ interface ShowsRepository { fun observeAnticipatedShows(): Flow>> - fun observeFeaturedShows(): Flow>> + fun observeRecommendedShows(): Flow>> suspend fun fetchDiscoverShows() diff --git a/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowsComponent.kt b/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverComponent.kt similarity index 65% rename from data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowsComponent.kt rename to data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverComponent.kt index ee5966c8f..5d0d2322c 100644 --- a/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowsComponent.kt +++ b/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverComponent.kt @@ -1,11 +1,11 @@ package com.thomaskioko.tvmaniac.shows.implementation +import com.thomaskioko.tvmaniac.shows.api.DiscoverRepository import com.thomaskioko.tvmaniac.shows.api.ShowsDao -import com.thomaskioko.tvmaniac.shows.api.ShowsRepository import com.thomaskioko.tvmaniac.util.scope.ApplicationScope import me.tatarka.inject.annotations.Provides -interface ShowsComponent { +interface DiscoverComponent { @ApplicationScope @Provides @@ -13,5 +13,5 @@ interface ShowsComponent { @ApplicationScope @Provides - fun provideShowsRepository(bind: ShowsRepositoryImpl): ShowsRepository = bind + fun provideDiscoverRepository(bind: DiscoverRepositoryImpl): DiscoverRepository = bind } diff --git a/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowsRepositoryImpl.kt b/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverRepositoryImpl.kt similarity index 95% rename from data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowsRepositoryImpl.kt rename to data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverRepositoryImpl.kt index 95591dc73..b28e4c47c 100644 --- a/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowsRepositoryImpl.kt +++ b/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverRepositoryImpl.kt @@ -9,7 +9,7 @@ import com.thomaskioko.tvmaniac.category.api.model.getCategory import com.thomaskioko.tvmaniac.core.db.ShowById import com.thomaskioko.tvmaniac.core.db.ShowsByCategory import com.thomaskioko.tvmaniac.resourcemanager.api.RequestManagerRepository -import com.thomaskioko.tvmaniac.shows.api.ShowsRepository +import com.thomaskioko.tvmaniac.shows.api.DiscoverRepository import com.thomaskioko.tvmaniac.util.model.AppCoroutineDispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flowOn @@ -20,12 +20,12 @@ import org.mobilenativefoundation.store.store5.impl.extensions.get import kotlin.time.Duration.Companion.days @Inject -class ShowsRepositoryImpl constructor( +class DiscoverRepositoryImpl constructor( private val showStore: ShowStore, private val discoverShowsStore: DiscoverShowsStore, private val requestManagerRepository: RequestManagerRepository, private val dispatchers: AppCoroutineDispatchers, -) : ShowsRepository { +) : DiscoverRepository { override suspend fun getShowById(traktId: Long): ShowById = showStore.get(key = traktId) @@ -98,7 +98,7 @@ class ShowsRepositoryImpl constructor( ) .flowOn(dispatchers.io) - override fun observeFeaturedShows(): Flow>> = + override fun observeRecommendedShows(): Flow>> = discoverShowsStore.stream( StoreReadRequest.cached( key = RECOMMENDED, diff --git a/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowsResponseMapper.kt b/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverResponseMapper.kt similarity index 52% rename from data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowsResponseMapper.kt rename to data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverResponseMapper.kt index 4e1d79428..b33275136 100644 --- a/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowsResponseMapper.kt +++ b/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverResponseMapper.kt @@ -1,10 +1,13 @@ package com.thomaskioko.tvmaniac.shows.implementation +import com.thomaskioko.tvmaniac.category.api.model.Category import com.thomaskioko.tvmaniac.core.db.Show import com.thomaskioko.tvmaniac.core.db.ShowById import com.thomaskioko.tvmaniac.core.db.Show_category import com.thomaskioko.tvmaniac.core.db.ShowsByCategory import com.thomaskioko.tvmaniac.core.networkutil.ApiResponse +import com.thomaskioko.tvmaniac.resourcemanager.api.LastRequest +import com.thomaskioko.tvmaniac.resourcemanager.api.RequestManagerRepository import com.thomaskioko.tvmaniac.trakt.api.model.ErrorResponse import com.thomaskioko.tvmaniac.trakt.api.model.TraktShowResponse import com.thomaskioko.tvmaniac.trakt.api.model.TraktShowsResponse @@ -13,34 +16,46 @@ import com.thomaskioko.tvmaniac.util.KermitLogger import me.tatarka.inject.annotations.Inject @Inject -class ShowsResponseMapper( +class DiscoverResponseMapper( + private val requestManagerRepository: RequestManagerRepository, private val formatterUtil: FormatterUtil, private val logger: KermitLogger, ) { - fun showResponseToCacheList(response: ApiResponse, ErrorResponse>) = - when (response) { - is ApiResponse.Success -> response.body.map { responseToEntity(it) } - is ApiResponse.Error.GenericError -> { - logger.error("ShowsResponse GenericError", "${response.errorMessage}") - throw Throwable("${response.errorMessage}") - } + fun showResponseToCacheList( + category: Category, + response: ApiResponse, ErrorResponse>, + ) = when (response) { + is ApiResponse.Success -> { + category.insertRequest(requestManagerRepository) + response.body.map { responseToEntity(it) } + } + + is ApiResponse.Error.HttpError -> + throw Throwable("${response.code} - ${response.errorBody?.message}") - is ApiResponse.Error.HttpError -> { - logger.error("ShowsResponse HttpError", "${response.code} - ${response.errorBody?.message}") - throw Throwable("${response.code} - ${response.errorBody?.message}") - } + is ApiResponse.Error.GenericError -> throw Throwable("${response.errorMessage}") + is ApiResponse.Error.SerializationError -> throw Throwable("$response") + is ApiResponse.Error.JsonConvertException -> throw Throwable("$response") + } - is ApiResponse.Error.SerializationError -> { - logger.error("ShowsResponse SerializationError", "$response") - throw Throwable("$response") - } - is ApiResponse.Error.JsonConvertException -> { - logger.error("ShowsResponse JsonConvertException", "$response") - throw Throwable("$response") - } + fun responseToEntityList( + category: Category, + response: ApiResponse, ErrorResponse>, + ) = when (response) { + is ApiResponse.Success -> { + category.insertRequest(requestManagerRepository) + response.body.map { showResponseToCacheList(it) } } + is ApiResponse.Error.HttpError -> + throw Throwable("${response.code} - ${response.errorBody?.message}") + + is ApiResponse.Error.SerializationError -> throw Throwable("$response") + is ApiResponse.Error.JsonConvertException -> throw Throwable("$response") + is ApiResponse.Error.GenericError -> throw Throwable("${response.errorMessage}") + } + private fun responseToEntity(response: TraktShowResponse) = ShowsByCategory( trakt_id = response.ids.trakt.toLong(), tmdb_id = response.ids.tmdb?.toLong(), @@ -97,46 +112,24 @@ class ShowsResponseMapper( genres = showById.genres, ) - fun responseToEntityList(response: ApiResponse, ErrorResponse>) = - when (response) { - is ApiResponse.Success -> response.body.map { showResponseToCacheList(it) } - is ApiResponse.Error.GenericError -> { - logger.error("ShowsResponse GenericError", "${response.errorMessage}") - throw Throwable("${response.errorMessage}") - } - - is ApiResponse.Error.HttpError -> { - logger.error("ShowsResponse HttpError", "${response.code} - ${response.errorBody?.message}") - throw Throwable("${response.code} - ${response.errorBody?.message}") - } - - is ApiResponse.Error.SerializationError -> { - logger.error("ShowsResponse SerializationError", "$response") - throw Throwable("$response") - } - is ApiResponse.Error.JsonConvertException -> { - logger.error("ShowsResponse JsonConvertException", "$response") - throw Throwable("$response") - } - } - - private fun showResponseToCacheList(response: TraktShowsResponse): ShowsByCategory = ShowsByCategory( - trakt_id = response.show.ids.trakt.toLong(), - tmdb_id = response.show.ids.tmdb?.toLong(), - title = response.show.title, - overview = response.show.overview ?: "", - votes = response.show.votes.toLong(), - year = response.show.year?.toString() ?: "--", - runtime = response.show.runtime.toLong(), - aired_episodes = response.show.airedEpisodes.toLong(), - language = response.show.language?.uppercase(), - rating = formatterUtil.formatDouble(response.show.rating, 1), - genres = response.show.genres.map { it.replaceFirstChar { it.uppercase() } }, - status = response.show.status.replaceFirstChar { it.uppercase() }, - poster_url = null, - backdrop_url = null, - category_id = null, - ) + private fun showResponseToCacheList(response: TraktShowsResponse): ShowsByCategory = + ShowsByCategory( + trakt_id = response.show.ids.trakt.toLong(), + tmdb_id = response.show.ids.tmdb?.toLong(), + title = response.show.title, + overview = response.show.overview ?: "", + votes = response.show.votes.toLong(), + year = response.show.year?.toString() ?: "--", + runtime = response.show.runtime.toLong(), + aired_episodes = response.show.airedEpisodes.toLong(), + language = response.show.language?.uppercase(), + rating = formatterUtil.formatDouble(response.show.rating, 1), + genres = response.show.genres.map { it.replaceFirstChar { it.uppercase() } }, + status = response.show.status.replaceFirstChar { it.uppercase() }, + poster_url = null, + backdrop_url = null, + category_id = null, + ) fun toCategoryCache(shows: List, categoryId: Long) = shows.map { Show_category( @@ -145,3 +138,13 @@ class ShowsResponseMapper( ) } } + +private fun Category.insertRequest(requestManagerRepository: RequestManagerRepository) { + requestManagerRepository.insert( + LastRequest( + id = id, + entityId = id, + requestType = title, + ), + ) +} diff --git a/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverShowsStore.kt b/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverShowsStore.kt index 71ed71846..91c625876 100644 --- a/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverShowsStore.kt +++ b/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/DiscoverShowsStore.kt @@ -4,8 +4,6 @@ import com.thomaskioko.tvmaniac.category.api.cache.CategoryCache import com.thomaskioko.tvmaniac.category.api.model.Category import com.thomaskioko.tvmaniac.core.db.Show import com.thomaskioko.tvmaniac.core.db.ShowsByCategory -import com.thomaskioko.tvmaniac.resourcemanager.api.LastRequest -import com.thomaskioko.tvmaniac.resourcemanager.api.RequestManagerRepository import com.thomaskioko.tvmaniac.shows.api.ShowsDao import com.thomaskioko.tvmaniac.trakt.api.TraktRemoteDataSource import com.thomaskioko.tvmaniac.util.model.AppCoroutineScope @@ -20,31 +18,26 @@ class DiscoverShowsStore( private val showsDao: ShowsDao, private val categoryCache: CategoryCache, private val traktRemoteDataSource: TraktRemoteDataSource, - private val requestManagerRepository: RequestManagerRepository, - private val mapper: ShowsResponseMapper, + private val mapper: DiscoverResponseMapper, private val scope: AppCoroutineScope, ) : Store> by StoreBuilder.from, List>( fetcher = Fetcher.of { category -> when (category) { Category.POPULAR -> { - category.insertRequest(requestManagerRepository) val apiResponse = traktRemoteDataSource.getPopularShows() - mapper.showResponseToCacheList(apiResponse) + mapper.showResponseToCacheList(category, apiResponse) } Category.TRENDING -> { - category.insertRequest(requestManagerRepository) val apiResponse = traktRemoteDataSource.getTrendingShows() - mapper.responseToEntityList(apiResponse) + mapper.responseToEntityList(category, apiResponse) } Category.ANTICIPATED -> { - category.insertRequest(requestManagerRepository) val apiResponse = traktRemoteDataSource.getAnticipatedShows() - mapper.responseToEntityList(apiResponse) + mapper.responseToEntityList(category, apiResponse) } Category.RECOMMENDED -> { - category.insertRequest(requestManagerRepository) val apiResponse = traktRemoteDataSource.getRecommendedShows() - mapper.responseToEntityList(apiResponse) + mapper.responseToEntityList(category, apiResponse) } } }, @@ -75,13 +68,3 @@ class DiscoverShowsStore( ) .scope(scope.io) .build() - -private fun Category.insertRequest(requestManagerRepository: RequestManagerRepository) { - requestManagerRepository.insert( - LastRequest( - id = id, - entityId = id, - requestType = title, - ), - ) -} diff --git a/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowStore.kt b/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowStore.kt index a0ade83b9..95bbca9e2 100644 --- a/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowStore.kt +++ b/data/shows/implementation/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/implementation/ShowStore.kt @@ -19,7 +19,7 @@ class ShowStore( private val showsDao: ShowsDao, private val traktRemoteDataSource: TraktRemoteDataSource, private val requestManagerRepository: RequestManagerRepository, - private val mapper: ShowsResponseMapper, + private val mapper: DiscoverResponseMapper, private val scope: AppCoroutineScope, private val logger: KermitLogger, ) : Store by StoreBuilder.from( diff --git a/data/shows/testing/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/testing/FakeShowsRepository.kt b/data/shows/testing/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/testing/FakeDiscoverRepository.kt similarity index 94% rename from data/shows/testing/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/testing/FakeShowsRepository.kt rename to data/shows/testing/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/testing/FakeDiscoverRepository.kt index ec394d07d..dafc7c483 100644 --- a/data/shows/testing/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/testing/FakeShowsRepository.kt +++ b/data/shows/testing/src/commonMain/kotlin/com/thomaskioko/tvmaniac/shows/testing/FakeDiscoverRepository.kt @@ -3,13 +3,13 @@ package com.thomaskioko.tvmaniac.shows.testing import com.thomaskioko.tvmaniac.category.api.model.Category import com.thomaskioko.tvmaniac.core.db.ShowById import com.thomaskioko.tvmaniac.core.db.ShowsByCategory -import com.thomaskioko.tvmaniac.shows.api.ShowsRepository +import com.thomaskioko.tvmaniac.shows.api.DiscoverRepository import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOf import org.mobilenativefoundation.store.store5.StoreReadResponse -class FakeShowsRepository : ShowsRepository { +class FakeDiscoverRepository : DiscoverRepository { private var featuredResult = flowOf>>() @@ -59,7 +59,7 @@ class FakeShowsRepository : ShowsRepository { return anticipatedResult } - override fun observeFeaturedShows(): Flow>> { + override fun observeRecommendedShows(): Flow>> { return featuredResult } diff --git a/presentation/discover/src/commonMain/kotlin/com/thomaskioko/tvmaniac/presentation/discover/DiscoverShowsState.kt b/presentation/discover/src/commonMain/kotlin/com/thomaskioko/tvmaniac/presentation/discover/DiscoverShowsState.kt index 6d94fa5e9..950f833d7 100644 --- a/presentation/discover/src/commonMain/kotlin/com/thomaskioko/tvmaniac/presentation/discover/DiscoverShowsState.kt +++ b/presentation/discover/src/commonMain/kotlin/com/thomaskioko/tvmaniac/presentation/discover/DiscoverShowsState.kt @@ -19,5 +19,5 @@ data class DataLoaded( val errorMessage: String? = null, ) : DiscoverState { override val isContentEmpty: Boolean = recommendedShows.isNullOrEmpty() && - trendingShows.isNullOrEmpty() && popularShows.isNullOrEmpty() && anticipatedShows.isNullOrEmpty() + trendingShows.isNullOrEmpty() && popularShows.isNullOrEmpty() && anticipatedShows.isNullOrEmpty() } diff --git a/presentation/discover/src/commonTest/kotlin/com/thomaskioko/tvmaniac/presentation/discover/DiscoverStateMachineTest.kt b/presentation/discover/src/commonTest/kotlin/com/thomaskioko/tvmaniac/presentation/discover/DiscoverStateMachineTest.kt index 972c76fd8..cfea3517f 100644 --- a/presentation/discover/src/commonTest/kotlin/com/thomaskioko/tvmaniac/presentation/discover/DiscoverStateMachineTest.kt +++ b/presentation/discover/src/commonTest/kotlin/com/thomaskioko/tvmaniac/presentation/discover/DiscoverStateMachineTest.kt @@ -1,7 +1,7 @@ package com.thomaskioko.tvmaniac.presentation.discover import app.cash.turbine.test -import com.thomaskioko.tvmaniac.shows.testing.FakeShowsRepository +import com.thomaskioko.tvmaniac.shows.testing.FakeDiscoverRepository import com.thomaskioko.tvmaniac.tmdb.testing.FakeShowImagesRepository import io.kotest.matchers.shouldBe import kotlinx.coroutines.test.runTest @@ -13,7 +13,7 @@ import kotlin.test.Test @Ignore internal class DiscoverStateMachineTest { - private val traktRepository = FakeShowsRepository() + private val traktRepository = FakeDiscoverRepository() private val imagesRepository = FakeShowImagesRepository() private val stateMachine = DiscoverStateMachine( traktRepository, diff --git a/presentation/show-details/src/commonMain/kotlin/com/thomaskioko/tvmaniac/presentation/showdetails/ShowDetailsStateMachine.kt b/presentation/show-details/src/commonMain/kotlin/com/thomaskioko/tvmaniac/presentation/showdetails/ShowDetailsStateMachine.kt index 6ee7c1055..58c5b7657 100644 --- a/presentation/show-details/src/commonMain/kotlin/com/thomaskioko/tvmaniac/presentation/showdetails/ShowDetailsStateMachine.kt +++ b/presentation/show-details/src/commonMain/kotlin/com/thomaskioko/tvmaniac/presentation/showdetails/ShowDetailsStateMachine.kt @@ -9,10 +9,9 @@ import com.thomaskioko.tvmaniac.core.db.Trailers import com.thomaskioko.tvmaniac.data.trailers.implementation.TrailerRepository import com.thomaskioko.tvmaniac.presentation.showdetails.ShowDetailsLoaded.TrailersContent.Companion.playerErrorMessage import com.thomaskioko.tvmaniac.seasons.api.SeasonsRepository -import com.thomaskioko.tvmaniac.shows.api.ShowsRepository +import com.thomaskioko.tvmaniac.shows.api.DiscoverRepository import com.thomaskioko.tvmaniac.shows.api.WatchlistRepository import com.thomaskioko.tvmaniac.similar.api.SimilarShowsRepository -import com.thomaskioko.tvmaniac.util.ExceptionHandler import kotlinx.coroutines.ExperimentalCoroutinesApi import me.tatarka.inject.annotations.Assisted import me.tatarka.inject.annotations.Inject @@ -22,12 +21,11 @@ import org.mobilenativefoundation.store.store5.StoreReadResponse @Inject class ShowDetailsStateMachine constructor( @Assisted private val traktShowId: Long, - private val showsRepository: ShowsRepository, + private val discoverRepository: DiscoverRepository, private val similarShowsRepository: SimilarShowsRepository, private val seasonsRepository: SeasonsRepository, private val trailerRepository: TrailerRepository, private val watchlistRepository: WatchlistRepository, - private val exceptionHandler: ExceptionHandler, ) : FlowReduxStateMachine( initialState = ShowDetailsLoaded.EMPTY_DETAIL_STATE, ) { @@ -41,7 +39,7 @@ class ShowDetailsStateMachine constructor( fetchShowDetails(state) } - collectWhileInState(showsRepository.observeShow(traktShowId)) { response, state -> + collectWhileInState(discoverRepository.observeShow(traktShowId)) { response, state -> when (response) { is StoreReadResponse.NoNewData -> state.noChange() is StoreReadResponse.Loading -> state.mutate { @@ -150,7 +148,7 @@ class ShowDetailsStateMachine constructor( state.mutate { copy( similarShowsContent = similarShowsContent.copy( - errorMessage = exceptionHandler.resolveError(response.error), + errorMessage = response.error.message, ), ) } @@ -195,7 +193,7 @@ class ShowDetailsStateMachine constructor( state.mutate { copy( trailersContent = trailersContent.copy( - errorMessage = exceptionHandler.resolveError(response.error), + errorMessage = response.error.message, ), ) } @@ -258,7 +256,7 @@ class ShowDetailsStateMachine constructor( } private suspend fun fetchShowDetails(state: State): ChangedState { - val show = showsRepository.getShowById(traktShowId) + val show = discoverRepository.getShowById(traktShowId) val similar = similarShowsRepository.fetchSimilarShows(traktShowId) val season = seasonsRepository.getSeasons(traktShowId) val trailers = trailerRepository.fetchTrailersByShowId(traktShowId) diff --git a/presentation/show-details/src/commonTest/kotlin/com/thomaskioko/tvmaniac/presentation/showdetails/ShowDetailsStateMachineTest.kt b/presentation/show-details/src/commonTest/kotlin/com/thomaskioko/tvmaniac/presentation/showdetails/ShowDetailsStateMachineTest.kt index b1481a422..1a819bc67 100644 --- a/presentation/show-details/src/commonTest/kotlin/com/thomaskioko/tvmaniac/presentation/showdetails/ShowDetailsStateMachineTest.kt +++ b/presentation/show-details/src/commonTest/kotlin/com/thomaskioko/tvmaniac/presentation/showdetails/ShowDetailsStateMachineTest.kt @@ -5,11 +5,10 @@ import com.thomaskioko.tvmaniac.presentation.showdetails.ShowDetailsLoaded.Seaso import com.thomaskioko.tvmaniac.presentation.showdetails.ShowDetailsLoaded.SimilarShowsContent.Companion.EMPTY_SIMILAR_SHOWS import com.thomaskioko.tvmaniac.presentation.showdetails.ShowDetailsLoaded.TrailersContent.Companion.EMPTY_TRAILERS import com.thomaskioko.tvmaniac.seasons.testing.FakeSeasonsRepository -import com.thomaskioko.tvmaniac.shows.testing.FakeShowsRepository +import com.thomaskioko.tvmaniac.shows.testing.FakeDiscoverRepository import com.thomaskioko.tvmaniac.similar.testing.FakeSimilarShowsRepository import com.thomaskioko.tvmaniac.trailers.testing.FakeTrailerRepository import com.thomaskioko.tvmaniac.trailers.testing.trailers -import com.thomaskioko.tvmaniac.util.ExceptionHandler import com.thomaskioko.tvmaniac.watchlist.testing.FakeWatchlistRepository import io.kotest.matchers.shouldBe import kotlinx.coroutines.test.runTest @@ -18,27 +17,22 @@ import org.mobilenativefoundation.store.store5.StoreReadResponseOrigin import kotlin.test.Ignore import kotlin.test.Test -@Ignore // TODO:: Fix test +@Ignore internal class ShowDetailsStateMachineTest { - private val exceptionHandler = object : ExceptionHandler { - override fun resolveError(throwable: Throwable): String = "Something went wrong" - } - private val seasonsRepository = FakeSeasonsRepository() private val trailerRepository = FakeTrailerRepository() - private val traktRepository = FakeShowsRepository() + private val traktRepository = FakeDiscoverRepository() private val similarShowsRepository = FakeSimilarShowsRepository() private val watchlistRepository = FakeWatchlistRepository() private val stateMachine = ShowDetailsStateMachine( traktShowId = 84958, - showsRepository = traktRepository, + discoverRepository = traktRepository, trailerRepository = trailerRepository, seasonsRepository = seasonsRepository, similarShowsRepository = similarShowsRepository, watchlistRepository = watchlistRepository, - exceptionHandler = exceptionHandler, ) @Test diff --git a/shared/src/iosMain/kotlin/com.thomaskioko.tvmaniac.shared/base/ApplicationComponent.kt b/shared/src/iosMain/kotlin/com.thomaskioko.tvmaniac.shared/base/ApplicationComponent.kt index 8513e64a9..95eddefb6 100644 --- a/shared/src/iosMain/kotlin/com.thomaskioko.tvmaniac.shared/base/ApplicationComponent.kt +++ b/shared/src/iosMain/kotlin/com.thomaskioko.tvmaniac.shared/base/ApplicationComponent.kt @@ -22,7 +22,7 @@ import com.thomaskioko.tvmaniac.shared.base.wrappers.ShowDetailsStateMachineWrap import com.thomaskioko.tvmaniac.shared.base.wrappers.TrailersStateMachineWrapper import com.thomaskioko.tvmaniac.shared.base.wrappers.WatchlistStateMachineWrapper import com.thomaskioko.tvmaniac.showimages.implementation.ShowImagesComponent -import com.thomaskioko.tvmaniac.shows.implementation.ShowsComponent +import com.thomaskioko.tvmaniac.shows.implementation.DiscoverComponent import com.thomaskioko.tvmaniac.similar.implementation.SimilarShowsComponent import com.thomaskioko.tvmaniac.tmdb.implementation.TmdbPlatformComponent import com.thomaskioko.tvmaniac.traktauth.implementation.TraktAuthenticationComponent @@ -43,7 +43,7 @@ abstract class ApplicationComponent : RequestManagerComponent, SeasonsComponent, SeasonDetailsComponent, - ShowsComponent, + DiscoverComponent, ShowImagesComponent, SimilarShowsComponent, StatsComponent,