Created
April 24, 2021 12:04
-
-
Save VictorKabata/d73a4abe91001ce470bf90c580be2f96 to your computer and use it in GitHub Desktop.
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
//Retrieves movie detail based on id from SQLite. If not available makes a network call to retrieve movie details from API | |
suspend fun getMovieDetails(movieId: Int): Flow<MovieDetails> { | |
val movieDetailsCacheResponse:Flow<MovieDetailsEntity>? = appDatabase.movieDetailsDao().getPopularShows(movieId) | |
return if (movieDetailsCacheResponse != null) { | |
Timber.e("MovieDetailsDataSource: Fetching movie details from cache") | |
return movieDetailsCacheResponse.map { it?.toDomain() } | |
} else { | |
Timber.e("MovieDetailsDataSource: Fetching movie details from network") | |
val movieDetailsNetworkResponse:MovieDetailsDto = safeApiRequest { apiService.fetchMovieDetails(movieId, API_KEY, "en") } | |
_movieDetails.value = movieDetailsNetworkResponse.toEntity() | |
flow { emit(movieDetailsNetworkResponse.toEntity()) }.map { it.toDomain() } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment