Skip to content

Commit

Permalink
add popular tv shows
Browse files Browse the repository at this point in the history
  • Loading branch information
ElnatanDigiBank committed Dec 23, 2022
1 parent 437fed3 commit 6ae5d3c
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ class TvShowsRepository @Inject constructor(
}

fun observeTopRatedTvShows() = local.topRatedTvShowsStore.observeEnteries()


suspend fun getPopularTvShows(page: Int) =
flow {
emit(remote.getPopular(page))
}

fun savePopularTvShows(page: Int, tvShows: List<TvShow>) {
local.popularTvShowsStore.insert(page, tvShows)
}

fun observePopularTvShows() = local.popularTvShowsStore.observeEnteries()
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
package com.example.moviestmdb.core.data.tv_shows.datasources

import com.example.moviestmdb.Genere
import com.example.moviestmdb.core.data.tv_shows.TvShowsStore
import com.example.moviestmdb.core.di.PopularsTvShows
import com.example.moviestmdb.core.di.TopRatedTvShows
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import javax.inject.Inject

class TvShowsLocalDataSource @Inject constructor(
@TopRatedTvShows val topRatedTvShowsStore: TvShowsStore,
@PopularsTvShows val popularTvShowsStore: TvShowsStore,
) {

private val _generes = MutableSharedFlow<List<Genere>>(replay = 1).apply {
tryEmit(emptyList())
}

private val generes = _generes.asSharedFlow()

fun saveGeneres(generes: List<Genere>) {
_generes.tryEmit(generes)
}

// fun observeGeneres(): SharedFlow<List<Genere>> = _generes.asSharedFlow()
fun observeGeneres(): SharedFlow<List<Genere>> = generes
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ class TvShowsRemoteDataSource @Inject constructor(
}
}

suspend fun getPopular(page: Int): Result<TvShowResponse> {
return safeApiCall {
tvShowsService
.getPopular(page)
.executeWithRetry()
.toResult()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ annotation class TopRatedMovies
@MustBeDocumented
annotation class TopRatedTvShows

@Retention(AnnotationRetention.RUNTIME)
@Qualifier
@MustBeDocumented
annotation class PopularsTvShows

@Retention(AnnotationRetention.RUNTIME)
@Qualifier
@MustBeDocumented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ class StoreModule {
@Provides
@TopRatedTvShows
fun provideTopRatedTvShowsStore(): TvShowsStore = TvShowsStore()

@Singleton
@Provides
@PopularsTvShows
fun providePopularTvShowsStore(): TvShowsStore = TvShowsStore()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ interface TvShowsService {
fun getTopRated(
@Query("page") page: Int
): Call<TvShowResponse>

@GET("tv/popular")
fun getPopular(
@Query("page") page: Int
): Call<TvShowResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.moviestmdb.domain.interactors

import com.example.moviestmdb.TvShowResponse
import com.example.moviestmdb.core.data.tv_shows.TvShowsRepository
import com.example.moviestmdb.core.data.tv_shows.TvShowsStore
import com.example.moviestmdb.core.di.PopularsTvShows
import com.example.moviestmdb.core.result.Result
import com.example.moviestmdb.core.util.AppCoroutineDispatchers
import com.example.moviestmdb.domain.FlowInteractor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
import javax.inject.Inject

class UpdatePopularTvShows @Inject constructor(
private val tvShowsRepository: TvShowsRepository,
@PopularsTvShows val popularTvShowsStore: TvShowsStore,
private val dispatchers: AppCoroutineDispatchers,
) : FlowInteractor<UpdatePopularTvShows.Params, TvShowResponse>(dispatchers.io) {

override suspend fun doWork(params: Params): Flow<Result<TvShowResponse>> {
val page = when {
params.page >= 1 -> params.page
params.page == Page.NEXT_PAGE -> {
val lastPage = popularTvShowsStore.getLastPage()
lastPage + 1
}
else -> 1
}

return tvShowsRepository.getPopularTvShows(page)
.onEach { result ->
when (result) {
is Result.Error -> Timber.e(result.exception)
is Result.Success -> tvShowsRepository.savePopularTvShows(
result.data.page,
result.data.tvShowList
)
}
}
}

data class Params(val page: Int)

object Page {
const val NEXT_PAGE = -1
const val REFRESH = -2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.moviestmdb.domain.observers

import com.example.moviestmdb.TvShow
import com.example.moviestmdb.core.data.tv_shows.TvShowsRepository
import com.example.moviestmdb.domain.SubjectInteractor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class ObservePopularTvShows @Inject constructor(
private val tvShowsRepository: TvShowsRepository
) : SubjectInteractor<ObservePopularTvShows.Params, List<TvShow>>() {

override fun createObservable(params: Params): Flow<List<TvShow>> {
return tvShowsRepository.observePopularTvShows()
.map { list -> list.flatMap { it.value } }
}

data class Params(val page: Int)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import com.example.moviestmdb.TvShow
import com.example.moviestmdb.core.util.UiMessage

data class LobbyViewState(
val popularTvShows: List<TvShow> = emptyList(),
val popularRefreshing: Boolean = false,
val topRatedTvShows: List<TvShow> = emptyList(),
val topRatedRefreshing: Boolean = false,
val message: UiMessage? = null
) {
val refreshing: Boolean
get() = topRatedRefreshing
get() = popularRefreshing || topRatedRefreshing

companion object {
val Empty = LobbyViewState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class TvShowsLobbyFragment : Fragment() {
@Inject
lateinit var tmdbImageManager: TmdbImageManager


lateinit var binding: FragmentTvShowsLobbyBinding

override fun onCreateView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,33 @@ import javax.inject.Inject
class TvShowsLobbyViewModel @Inject constructor(

private val updateTopRatedTvShows: UpdateTopRatedTvShows,
private val updatePopularTvShows: UpdatePopularTvShows,
observeTopRatedTvShows: ObserveTopRatedTvShows,
observePopularTvShows: ObservePopularTvShows,
private val dispatchers: AppCoroutineDispatchers,
) : ViewModel() {

private val popularLoadingState = ObservableLoadingCounter()
private val topRatedLoadingState = ObservableLoadingCounter()
private val uiMessageManager = UiMessageManager()

init {
observePopularTvShows(ObservePopularTvShows.Params(1))
observeTopRatedTvShows(ObserveTopRatedTvShows.Params(1))
refresh()
}

val state: StateFlow<LobbyViewState> = combine(
popularLoadingState.observable,
topRatedLoadingState.observable,
observePopularTvShows.flow,
observeTopRatedTvShows.flow,
uiMessageManager.message,
) { topRatedRefreshing, topRatedTvShows, message ->
) { popularRefreshing, topRatedRefreshing, popularTvShows, topRatedTvShows, message ->
LobbyViewState(
popularRefreshing = popularRefreshing,
topRatedRefreshing = topRatedRefreshing,
popularTvShows = popularTvShows,
topRatedTvShows = topRatedTvShows,
message = message
)
Expand All @@ -50,7 +58,7 @@ class TvShowsLobbyViewModel @Inject constructor(
)


private fun refresh() {
private fun refresh() {
viewModelScope.launch(dispatchers.io) {
updateTopRatedTvShows(UpdateTopRatedTvShows.Params(UpdateTopRatedTvShows.Page.REFRESH))
.collectStatus(
Expand Down

0 comments on commit 6ae5d3c

Please sign in to comment.