forked from Ronelg/tmdb-android-procamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
437fed3
commit 6ae5d3c
Showing
11 changed files
with
120 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 2 additions & 16 deletions
18
...main/java/com/example/moviestmdb/core/data/tv_shows/datasources/TvShowsLocalDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
domain/src/main/java/com/example/moviestmdb/domain/interactors/UpdatePopularTvShows.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
domain/src/main/java/com/example/moviestmdb/domain/observers/ObservePopularTvShows.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters