Skip to content

Commit

Permalink
Minor cleanup:
Browse files Browse the repository at this point in the history
- Rename classes.
- Fix lint errors.
  • Loading branch information
thomaskioko committed Jul 11, 2023
1 parent 4845bd1 commit d8c5031
Show file tree
Hide file tree
Showing 20 changed files with 119 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ data class ShowsLoaded(
val list: List<TvShow>,
) : GridState

data class LoadingContentError(val errorMessage: String) : GridState
data class LoadingContentError(val errorMessage: String? = null) : GridState
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<GridState, GridActions>(initialState = LoadingContent) {

init {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,7 +49,7 @@ abstract class ApplicationComponent(
RequestManagerComponent,
SeasonsComponent,
SeasonDetailsComponent,
ShowsComponent,
DiscoverComponent,
ShowImagesComponent,
SimilarShowsComponent,
StatsComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import java.net.UnknownHostException

@Inject
class AndroidNetworkExceptionHandlerUtil(
private val configs: Configs
private val configs: Configs,
) : NetworkExceptionHandler {
val errorMessage = "Something went wrong"

override fun resolveError(throwable: Throwable) =
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!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import me.tatarka.inject.annotations.Inject

@Inject
class IosExceptionHandler(
private val configs: Configs
private val configs: Configs,
) : NetworkExceptionHandler {
val errorMessage = "Something went wrong"

override fun resolveError(throwable: Throwable) =
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ actual interface NetworkPlatformComponent {
@ApplicationScope
@Provides
fun provideExceptionHandler(bind: IosExceptionHandler): NetworkExceptionHandler = bind

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<StoreReadResponse<ShowById>>

Expand All @@ -18,7 +18,7 @@ interface ShowsRepository {

fun observeAnticipatedShows(): Flow<StoreReadResponse<List<ShowsByCategory>>>

fun observeFeaturedShows(): Flow<StoreReadResponse<List<ShowsByCategory>>>
fun observeRecommendedShows(): Flow<StoreReadResponse<List<ShowsByCategory>>>

suspend fun fetchDiscoverShows()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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
fun provideShowsCache(bind: ShowDaoImpl): ShowsDao = bind

@ApplicationScope
@Provides
fun provideShowsRepository(bind: ShowsRepositoryImpl): ShowsRepository = bind
fun provideDiscoverRepository(bind: DiscoverRepositoryImpl): DiscoverRepository = bind
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -98,7 +98,7 @@ class ShowsRepositoryImpl constructor(
)
.flowOn(dispatchers.io)

override fun observeFeaturedShows(): Flow<StoreReadResponse<List<ShowsByCategory>>> =
override fun observeRecommendedShows(): Flow<StoreReadResponse<List<ShowsByCategory>>> =
discoverShowsStore.stream(
StoreReadRequest.cached(
key = RECOMMENDED,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<List<TraktShowResponse>, 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<List<TraktShowResponse>, 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<List<TraktShowsResponse>, 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(),
Expand Down Expand Up @@ -97,46 +112,24 @@ class ShowsResponseMapper(
genres = showById.genres,
)

fun responseToEntityList(response: ApiResponse<List<TraktShowsResponse>, 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<Show>, categoryId: Long) = shows.map {
Show_category(
Expand All @@ -145,3 +138,13 @@ class ShowsResponseMapper(
)
}
}

private fun Category.insertRequest(requestManagerRepository: RequestManagerRepository) {
requestManagerRepository.insert(
LastRequest(
id = id,
entityId = id,
requestType = title,
),
)
}
Loading

0 comments on commit d8c5031

Please sign in to comment.