Skip to content

Commit

Permalink
Add similar items to detail screen
Browse files Browse the repository at this point in the history
  • Loading branch information
sLee0306 committed Feb 11, 2024
1 parent 7866462 commit 46ca466
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 60 deletions.
34 changes: 29 additions & 5 deletions app/src/main/java/com/sample/tmdb/ui/TMDbApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,20 @@ fun TMDbApp() {
onSearchTVShow = appState::navigateToSearchTVShow,
navController = appState.navController
)
detailScreens(
movieDetailScreens(
onAllCastSelected = appState::navigateToCastList,
onAllCrewSelected = appState::navigateToCrewList,
onCreditSelected = appState::navigateToPerson,
onImagesSelected = appState::navigateToImages,
onTMDbItemSelected = appState::navigateToMovieDetail,
upPress = appState::upPress
)
tvShowDetailScreens(
onAllCastSelected = appState::navigateToCastList,
onAllCrewSelected = appState::navigateToCrewList,
onCreditSelected = appState::navigateToPerson,
onImagesSelected = appState::navigateToImages,
onTMDbItemSelected = appState::navigateToTVShowDetail,
upPress = appState::upPress
)
moviePagingScreens(
Expand Down Expand Up @@ -192,11 +201,12 @@ private fun NavGraphBuilder.navigationScreens(
}
}

private fun NavGraphBuilder.detailScreens(
private fun NavGraphBuilder.movieDetailScreens(
onAllCastSelected: (String) -> Unit,
onAllCrewSelected: (String) -> Unit,
onCreditSelected: (String) -> Unit,
onImagesSelected: (String, Int) -> Unit,
onTMDbItemSelected: (Int) -> Unit,
upPress: () -> Unit
) {
composable(
Expand All @@ -219,8 +229,20 @@ private fun NavGraphBuilder.detailScreens(
Uri.encode(gson.toJson(images, object : TypeToken<List<TMDbImage>>() {}.type)),
index
)
}, onTMDbItemSelected = {
onTMDbItemSelected(it.id)
})
}
}

private fun NavGraphBuilder.tvShowDetailScreens(
onAllCastSelected: (String) -> Unit,
onAllCrewSelected: (String) -> Unit,
onCreditSelected: (String) -> Unit,
onImagesSelected: (String, Int) -> Unit,
onTMDbItemSelected: (Int) -> Unit,
upPress: () -> Unit
) {
composable(
route = "${MainDestinations.TMDB_TV_SHOW_DETAIL_ROUTE}/{${MainDestinations.TMDB_ID_KEY}}",
arguments = listOf(
Expand All @@ -241,6 +263,8 @@ private fun NavGraphBuilder.detailScreens(
Uri.encode(gson.toJson(images, object : TypeToken<List<TMDbImage>>() {}.type)),
index
)
}, onTMDbItemSelected = {
onTMDbItemSelected(it.id)
})
}
}
Expand Down Expand Up @@ -410,18 +434,18 @@ private fun NavGraphBuilder.personScreen(

private fun NavGraphBuilder.imagesScreen() {
composable(
route = "${MainDestinations.TMDB_IMAGES_ROUTE}/{${MainDestinations.TMDB_IMAGES_KEY}}/{${MainDestinations.TMDB_IMAGE_ID}}",
route = "${MainDestinations.TMDB_IMAGES_ROUTE}/{${MainDestinations.TMDB_IMAGES_KEY}}/{${MainDestinations.TMDB_IMAGE_PAGE}}",
arguments = listOf(
navArgument(MainDestinations.TMDB_IMAGES_KEY) { type = NavType.StringType },
navArgument(MainDestinations.TMDB_IMAGE_ID) { type = NavType.IntType }
navArgument(MainDestinations.TMDB_IMAGE_PAGE) { type = NavType.IntType }
)
) { from ->
ImagesScreen(
images = gson.fromJson(
from.arguments?.getString(MainDestinations.TMDB_IMAGES_KEY),
object : TypeToken<List<TMDbImage>>() {}.type
),
initialPage = from.arguments?.getInt(MainDestinations.TMDB_IMAGE_ID)!!
initialPage = from.arguments?.getInt(MainDestinations.TMDB_IMAGE_PAGE)!!
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object MainDestinations {
const val TMDB_CREDIT_KEY = "credit_list"
const val TMDB_PERSON_ROUTE = "person"
const val TMDB_PERSON_KEY = "personId"
const val TMDB_IMAGES_ROUTE = "image"
const val TMDB_IMAGES_ROUTE = "images"
const val TMDB_IMAGES_KEY = "imagesKey"
const val TMDB_IMAGE_ID = "imageId"
const val TMDB_IMAGE_PAGE = "imagePage"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.sample.tmdb.common.ui.component

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.sample.tmdb.common.model.TMDbItem
import com.sample.tmdb.common.ui.Dimens
import com.sample.tmdb.common.ui.theme.TmdbPagingComposeTheme

@Composable
fun TMDbCard(
tmdbItem: TMDbItem,
onFeedClick: (TMDbItem) -> Unit,
imageUrl: String? = tmdbItem.posterUrl,
itemWidth: Dp = 120.dp
) {
Card(
modifier = Modifier
.padding(Dimens.PaddingSmall)
.clickable(onClick = { onFeedClick(tmdbItem) }),
shape = RoundedCornerShape(10.dp)
) {
Column {
AsyncImage(
model = imageUrl,
contentDescription = null,
modifier = Modifier
.size(width = itemWidth, height = 180.dp),
contentScale = ContentScale.Crop
)
Text(
text = tmdbItem.name,
fontSize = TmdbPagingComposeTheme.fontSizes.sp_11,
color = MaterialTheme.colors.onSurface,
textAlign = TextAlign.Center,
maxLines = 2,
overflow = TextOverflow.Ellipsis,
modifier = Modifier
.size(width = itemWidth, height = 36.dp)
.wrapContentHeight()
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ interface MovieService {

@GET("3/movie/{movie_id}/images")
suspend fun fetchImages(@Path("movie_id") movieId: Int): ImagesResponse

@GET("3/movie/{movie_id}/similar")
suspend fun fetchSimilarMovies(@Path("movie_id") movieId: Int): TMDbWrapper<NetworkMovie>

@GET("3/movie/{movie_id}/similar")
suspend fun fetchSimilarMovies(
@Path("movie_id") movieId: Int,
@Query("page") page: Int
): TMDbWrapper<NetworkMovie>
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ interface TVShowService {

@GET("3/tv/{tvId}/images")
suspend fun fetchImages(@Path("tvId") tvId: Int): ImagesResponse

@GET("3/tv/{tvId}/similar")
suspend fun fetchSimilarMovies(@Path("tvId") tvId: Int): TMDbWrapper<NetworkTVShow>

@GET("3/tv/{TvId}/similar")
suspend fun fetchSimilarMovies(
@Path("TvId") tvId: Int,
@Query("page") page: Int
): TMDbWrapper<NetworkTVShow>
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.sample.tmdb.data.repository

import android.content.Context
import com.sample.tmdb.common.model.TMDbItem
import com.sample.tmdb.data.di.IoDispatcher
import com.sample.tmdb.data.network.MovieService
import com.sample.tmdb.data.response.asCastDomainModel
import com.sample.tmdb.data.response.asCrewDomainModel
import com.sample.tmdb.data.response.asDomainModel
import com.sample.tmdb.data.response.asMovieDomainModel
import com.sample.tmdb.domain.model.Cast
import com.sample.tmdb.domain.model.Crew
import com.sample.tmdb.domain.model.MovieDetails
Expand Down Expand Up @@ -36,4 +38,7 @@ class MovieDetailRepository @Inject constructor(

override suspend fun getImages(id: Int): List<TMDbImage> =
movieApi.fetchImages(id).asDomainModel()

override suspend fun getSimilarItems(id: Int): List<TMDbItem> =
movieApi.fetchSimilarMovies(id).items.asMovieDomainModel()
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.sample.tmdb.data.repository

import android.content.Context
import com.sample.tmdb.common.model.TMDbItem
import com.sample.tmdb.data.di.IoDispatcher
import com.sample.tmdb.data.network.TVShowService
import com.sample.tmdb.data.response.asCastDomainModel
import com.sample.tmdb.data.response.asCrewDomainModel
import com.sample.tmdb.data.response.asDomainModel
import com.sample.tmdb.data.response.asTVShowDomainModel
import com.sample.tmdb.domain.model.Cast
import com.sample.tmdb.domain.model.Crew
import com.sample.tmdb.domain.model.TMDbImage
Expand Down Expand Up @@ -36,4 +38,7 @@ class TVShowDetailRepository @Inject constructor(

override suspend fun getImages(id: Int): List<TMDbImage> =
tvShowApi.fetchImages(id).asDomainModel()

override suspend fun getSimilarItems(id: Int): List<TMDbItem> =
tvShowApi.fetchSimilarMovies(id).items.asTVShowDomainModel()
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.sample.tmdb.domain.model

import com.sample.tmdb.common.model.TMDbItem

class DetailWrapper<T : TMDbItemDetails>(
val cast: List<Cast>,
val crew: List<Crew>,
val details: T,
val images: List<TMDbImage>
val images: List<TMDbImage>,
val similarItems: List<TMDbItem>
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.sample.tmdb.domain.repository

import android.content.Context
import com.sample.tmdb.common.base.BaseRepository
import com.sample.tmdb.common.model.TMDbItem
import com.sample.tmdb.domain.model.Cast
import com.sample.tmdb.domain.model.Crew
import com.sample.tmdb.domain.model.DetailWrapper
Expand All @@ -23,19 +24,30 @@ abstract class BaseDetailRepository<T : TMDbItemDetails>(

protected abstract suspend fun getImages(id: Int): List<TMDbImage>

protected abstract suspend fun getSimilarItems(id: Int): List<TMDbItem>

override suspend fun getSuccessResult(id: Any?): DetailWrapper<T> {
val detailsDeferred: Deferred<T>
val creditDeferred: Deferred<Pair<List<Cast>, List<Crew>>>
val imageDeferred: Deferred<List<TMDbImage>>
val similarDeferred: Deferred<List<TMDbItem>>
coroutineScope {
detailsDeferred = async { getDetails(id as Int) }
creditDeferred = async { getCredit(id as Int) }
imageDeferred= async { getImages(id as Int) }
imageDeferred = async { getImages(id as Int) }
similarDeferred = async { getSimilarItems(id as Int) }
}
val details = detailsDeferred.await()
val creditWrapper = creditDeferred.await()
val images = imageDeferred.await()
val similarItems = similarDeferred.await()

return DetailWrapper(creditWrapper.first, creditWrapper.second, details, images)
return DetailWrapper(
creditWrapper.first,
creditWrapper.second,
details,
images,
similarItems
)
}
}
Loading

0 comments on commit 46ca466

Please sign in to comment.