Skip to content

Instantly share code, notes, and snippets.

@VictorKabata
Created April 24, 2021 12:04
Show Gist options
  • Save VictorKabata/d73a4abe91001ce470bf90c580be2f96 to your computer and use it in GitHub Desktop.
Save VictorKabata/d73a4abe91001ce470bf90c580be2f96 to your computer and use it in GitHub Desktop.
//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