Skip to content

Commit

Permalink
Load all similar movies and tv shows from detail screen
Browse files Browse the repository at this point in the history
  • Loading branch information
sLee0306 committed Feb 12, 2024
1 parent 688f5e3 commit adcf53a
Show file tree
Hide file tree
Showing 30 changed files with 241 additions and 24 deletions.
32 changes: 32 additions & 0 deletions app/src/main/java/com/sample/tmdb/ui/TMDbApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import com.sample.tmdb.paging.NowPlayingMovieScreen
import com.sample.tmdb.paging.OnTheAirTVShowScreen
import com.sample.tmdb.paging.PopularMovieScreen
import com.sample.tmdb.paging.PopularTVShowScreen
import com.sample.tmdb.paging.SimilarMovieScreen
import com.sample.tmdb.paging.SimilarTVShowScreen
import com.sample.tmdb.paging.TopRatedMovieScreen
import com.sample.tmdb.paging.TopRatedTVShowScreen
import com.sample.tmdb.paging.TrendingMovieScreen
Expand Down Expand Up @@ -96,6 +98,7 @@ fun TMDbApp() {
onCreditSelected = appState::navigateToPerson,
onImagesSelected = appState::navigateToImages,
onTMDbItemSelected = appState::navigateToMovieDetail,
onAllSimilarSelected = appState::navigateToSimilarMovies,
upPress = appState::upPress
)
tvShowDetailScreens(
Expand All @@ -104,6 +107,7 @@ fun TMDbApp() {
onCreditSelected = appState::navigateToPerson,
onImagesSelected = appState::navigateToImages,
onTMDbItemSelected = appState::navigateToTVShowDetail,
onAllSimilarSelected = appState::navigateToSimilarTVShow,
upPress = appState::upPress
)
moviePagingScreens(
Expand Down Expand Up @@ -207,6 +211,7 @@ private fun NavGraphBuilder.movieDetailScreens(
onCreditSelected: (String) -> Unit,
onImagesSelected: (String, Int) -> Unit,
onTMDbItemSelected: (Int) -> Unit,
onAllSimilarSelected: (Int) -> Unit,
upPress: () -> Unit
) {
composable(
Expand All @@ -231,6 +236,8 @@ private fun NavGraphBuilder.movieDetailScreens(
)
}, onTMDbItemSelected = {
onTMDbItemSelected(it.id)
}, onAllSimilarSelected = {
onAllSimilarSelected(it)
})
}
}
Expand All @@ -241,6 +248,7 @@ private fun NavGraphBuilder.tvShowDetailScreens(
onCreditSelected: (String) -> Unit,
onImagesSelected: (String, Int) -> Unit,
onTMDbItemSelected: (Int) -> Unit,
onAllSimilarSelected: (Int) -> Unit,
upPress: () -> Unit
) {
composable(
Expand All @@ -265,6 +273,8 @@ private fun NavGraphBuilder.tvShowDetailScreens(
)
}, onTMDbItemSelected = {
onTMDbItemSelected(it.id)
}, onAllSimilarSelected = {
onAllSimilarSelected(it)
})
}
}
Expand Down Expand Up @@ -316,6 +326,17 @@ private fun NavGraphBuilder.moviePagingScreens(
onSearchMovie = onSearchMovie
)
}
composable(
route = "${MainDestinations.TMDB_SIMILAR_MOVIES_ROUTE}/{${MainDestinations.TMDB_SIMILAR_ID}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_SIMILAR_ID) { type = NavType.IntType })
) {
SimilarMovieScreen(
onClick = { onMovieSelected(it.id) },
upPress = upPress,
onSearchMovie = onSearchMovie
)
}
}

private fun NavGraphBuilder.tvShowPagingScreens(
Expand Down Expand Up @@ -365,6 +386,17 @@ private fun NavGraphBuilder.tvShowPagingScreens(
onSearchTVShow = onSearchTVShow
)
}
composable(
route = "${MainDestinations.TMDB_SIMILAR_TV_SHOW_ROUTE}/{${MainDestinations.TMDB_SIMILAR_ID}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_SIMILAR_ID) { type = NavType.IntType })
) {
SimilarTVShowScreen(
onClick = { onTVShowSelected(it.id) },
upPress = upPress,
onSearchMovie = onSearchTVShow
)
}
}

private fun NavGraphBuilder.searchScreens(
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/sample/tmdb/ui/TMDbAppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ class TMDbAppState(
navController.navigate("${MainDestinations.TMDB_IMAGES_ROUTE}/$images/$id")
}

fun navigateToSimilarMovies(id: Int) {
navController.navigate("${MainDestinations.TMDB_SIMILAR_MOVIES_ROUTE}/$id")
}

fun navigateToSimilarTVShow(id: Int) {
navController.navigate("${MainDestinations.TMDB_SIMILAR_TV_SHOW_ROUTE}/$id")
}
}

private val NavGraph.startDestination: NavDestination?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ object MainDestinations {
const val TMDB_IMAGES_ROUTE = "images"
const val TMDB_IMAGES_KEY = "imagesKey"
const val TMDB_IMAGE_PAGE = "imagePage"
const val TMDB_SIMILAR_MOVIES_ROUTE = "similar_movies"
const val TMDB_SIMILAR_TV_SHOW_ROUTE = "similar_tv_show"
const val TMDB_SIMILAR_ID = "similarId"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.sample.tmdb.domain.repository.BaseDetailRepository
import com.sample.tmdb.domain.repository.BaseFeedRepository
import com.sample.tmdb.domain.repository.BasePagingRepository
import com.sample.tmdb.domain.repository.BookmarkItemDetailsRepository
import com.sample.tmdb.domain.utils.Similar
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
Expand Down Expand Up @@ -70,6 +71,12 @@ abstract class RepositoryModule {
@Binds
internal abstract fun bindDiscoverMoviesRepository(discoverMoviesPagingRepository: DiscoverMoviesPagingRepository): BasePagingRepository<Movie>

@Singleton
@Similar
@Binds
internal abstract fun bindSimilarMoviesRepository(similarMoviesPagingRepository: SimilarMoviesPagingRepository): BasePagingRepository<Movie>


@Singleton
@Search
@Binds
Expand Down Expand Up @@ -105,6 +112,12 @@ abstract class RepositoryModule {
@Binds
internal abstract fun bindDiscoverTVShowRepository(discoverTvSeriesPagingRepository: DiscoverTvSeriesPagingRepository): BasePagingRepository<TVShow>

@Singleton
@Similar
@Binds
internal abstract fun bindSimilarTVShowRepository(similarTvSeriesPagingRepository: SimilarTvSeriesPagingRepository): BasePagingRepository<TVShow>


@Singleton
@Search
@Binds
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.sample.tmdb.data.paging.movie

import android.content.Context
import com.sample.tmdb.data.network.MovieService
import com.sample.tmdb.data.response.asMovieDomainModel
import com.sample.tmdb.domain.model.Movie
import com.sample.tmdb.domain.paging.BasePagingSource

class SimilarMoviesPagingSource(
context: Context,
private val movieApi: MovieService,
private val movieId: Int
) : BasePagingSource<Movie>(context) {

override suspend fun fetchItems(page: Int): List<Movie> =
movieApi.fetchSimilarMovies(movieId, page).items.asMovieDomainModel()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.sample.tmdb.data.paging.tvshow

import android.content.Context
import com.sample.tmdb.data.network.TVShowService
import com.sample.tmdb.data.response.asTVShowDomainModel
import com.sample.tmdb.domain.model.TVShow
import com.sample.tmdb.domain.paging.BasePagingSource

class SimilarTvSeriesPagingSource(
context: Context,
private val tvShowApi: TVShowService,
private val tvId: Int
) : BasePagingSource<TVShow>(context) {

override suspend fun fetchItems(page: Int): List<TVShow> =
tvShowApi.fetchSimilarMovies(tvId, page).items.asTVShowDomainModel()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class AiringTodayTvSeriesPagingRepository @Inject constructor(
private val tvShowApi: TVShowService
) : BasePagingRepository<TVShow>() {

override fun pagingSource(query: String?): BasePagingSource<TVShow> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<TVShow> =
AiringTodayTvSeriesPagingSource(context, tvShowApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class DiscoverMoviesPagingRepository @Inject constructor(
private val movieApi: MovieService
) : BasePagingRepository<Movie>() {

override fun pagingSource(query: String?): BasePagingSource<Movie> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<Movie> =
DiscoverMoviesPagingSource(context, movieApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class DiscoverTvSeriesPagingRepository @Inject constructor(
private val tvShowApi: TVShowService
) : BasePagingRepository<TVShow>() {

override fun pagingSource(query: String?): BasePagingSource<TVShow> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<TVShow> =
DiscoverTvSeriesPagingSource(context, tvShowApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class NowPlayingMoviesPagingRepository @Inject constructor(
private val movieApi: MovieService
) : BasePagingRepository<Movie>() {

override fun pagingSource(query: String?): BasePagingSource<Movie> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<Movie> =
NowPlayingMoviesPagingSource(context, movieApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class OnTheAirTvSeriesPagingRepository @Inject constructor(
private val tvShowApi: TVShowService
) : BasePagingRepository<TVShow>() {

override fun pagingSource(query: String?): BasePagingSource<TVShow> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<TVShow> =
OnTheAirTvSeriesPagingSource(context, tvShowApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class PopularMoviesPagingRepository @Inject constructor(
private val movieApi: MovieService
) : BasePagingRepository<Movie>() {

override fun pagingSource(query: String?): BasePagingSource<Movie> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<Movie> =
PopularMoviesPagingSource(context, movieApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class PopularTvSeriesPagingRepository @Inject constructor(
private val tvShowApi: TVShowService
) : BasePagingRepository<TVShow>() {

override fun pagingSource(query: String?): BasePagingSource<TVShow> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<TVShow> =
PopularTvSeriesPagingSource(context, tvShowApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class SearchMoviesPagingRepository @Inject constructor(
private val movieApi: MovieService
) : BasePagingRepository<Movie>() {

override fun pagingSource(query: String?): BasePagingSource<Movie> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<Movie> =
SearchMoviesPagingSource(context, movieApi, query!!)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class SearchTvSeriesPagingRepository@Inject constructor(
private val tvShowApi: TVShowService
) : BasePagingRepository<TVShow>() {

override fun pagingSource(query: String?): BasePagingSource<TVShow> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<TVShow> =
SearchTvSeriesPagingSource(context, tvShowApi, query!!)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.sample.tmdb.data.repository

import android.content.Context
import com.sample.tmdb.data.network.MovieService
import com.sample.tmdb.data.paging.movie.SimilarMoviesPagingSource
import com.sample.tmdb.domain.model.Movie
import com.sample.tmdb.domain.paging.BasePagingSource
import com.sample.tmdb.domain.repository.BasePagingRepository
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class SimilarMoviesPagingRepository @Inject constructor(
@ApplicationContext private val context: Context,
private val movieApi: MovieService
) : BasePagingRepository<Movie>() {

override fun pagingSource(query: String?, id: Int?): BasePagingSource<Movie> =
SimilarMoviesPagingSource(context, movieApi, id!!)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.sample.tmdb.data.repository

import android.content.Context
import com.sample.tmdb.data.network.TVShowService
import com.sample.tmdb.data.paging.tvshow.SimilarTvSeriesPagingSource
import com.sample.tmdb.domain.model.TVShow
import com.sample.tmdb.domain.paging.BasePagingSource
import com.sample.tmdb.domain.repository.BasePagingRepository
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class SimilarTvSeriesPagingRepository @Inject constructor(
@ApplicationContext private val context: Context,
private val tvShowApi: TVShowService
) : BasePagingRepository<TVShow>() {

override fun pagingSource(query: String?, id: Int?): BasePagingSource<TVShow> =
SimilarTvSeriesPagingSource(context, tvShowApi, id!!)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class TopRatedMoviesPagingRepository @Inject constructor(
private val movieApi: MovieService
) : BasePagingRepository<Movie>() {

override fun pagingSource(query: String?): BasePagingSource<Movie> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<Movie> =
TopRatedMoviesPagingSource(context, movieApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class TopRatedTvSeriesPagingRepository @Inject constructor(
private val tvShowApi: TVShowService
) : BasePagingRepository<TVShow>() {

override fun pagingSource(query: String?): BasePagingSource<TVShow> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<TVShow> =
TopRatedTvSeriesPagingSource(context, tvShowApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class TrendingMoviesPagingRepository @Inject constructor(
private val movieApi: MovieService
) : BasePagingRepository<Movie>() {

override fun pagingSource(query: String?): BasePagingSource<Movie> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<Movie> =
TrendingMoviesPagingSource(context, movieApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class TrendingTvSeriesPagingRepository @Inject constructor(
private val tvShowApi: TVShowService
) : BasePagingRepository<TVShow>() {

override fun pagingSource(query: String?): BasePagingSource<TVShow> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<TVShow> =
TrendingTvSeriesPagingSource(context, tvShowApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class UpcomingMoviesPagingRepository @Inject constructor(
private val movieApi: MovieService
) : BasePagingRepository<Movie>() {

override fun pagingSource(query: String?): BasePagingSource<Movie> =
override fun pagingSource(query: String?, id: Int?): BasePagingSource<Movie> =
UpcomingMoviesPagingSource(context, movieApi)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import kotlinx.coroutines.flow.Flow

abstract class BasePagingRepository<T : TMDbItem> {

protected abstract fun pagingSource(query: String?): BasePagingSource<T>
protected abstract fun pagingSource(query: String?, id: Int?): BasePagingSource<T>

fun fetchResultStream(query: String?= null): Flow<PagingData<T>> = Pager(
fun fetchResultStream(query: String? = null, id: Int? = null): Flow<PagingData<T>> = Pager(
config = PagingConfig(pageSize = NETWORK_PAGE_SIZE),
pagingSourceFactory = { pagingSource(query) }
pagingSourceFactory = { pagingSource(query, id) }
).flow

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ annotation class Discover

@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class Search
annotation class Search

@Retention(AnnotationRetention.BINARY)
@Qualifier
annotation class Similar
Loading

0 comments on commit adcf53a

Please sign in to comment.