Skip to content

Commit

Permalink
try pass serializable data
Browse files Browse the repository at this point in the history
  • Loading branch information
DanilaSidukov committed Jun 16, 2023
1 parent 6c89ddd commit c362587
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 71 deletions.
18 changes: 11 additions & 7 deletions app/src/main/java/com/satyamthakur/silver/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
setContent {
val navController = rememberAnimatedNavController()

val viewModel = koinViewModel<DashboardViewModel>()
val nowShowingMovies by viewModel.nowShowingMovies.collectAsStateWithLifecycle()
val popularMovies by viewModel.popularMovies.collectAsStateWithLifecycle()
val creditsData by viewModel.credits.collectAsStateWithLifecycle()

SilverTheme {
Surface {
Scaffold(
Expand All @@ -63,10 +69,6 @@ class MainActivity : ComponentActivity() {
startDestination = Screen.Dashboard.route
) {
composable(route = Screen.Dashboard.route) {
val viewModel = koinViewModel<DashboardViewModel>()
val nowShowingMovies by viewModel.nowShowingMovies.collectAsStateWithLifecycle()
val popularMovies by viewModel.popularMovies.collectAsStateWithLifecycle()



DashboardScreen(
Expand All @@ -83,20 +85,22 @@ class MainActivity : ComponentActivity() {
navController = navController
)
}
composable(route = Screen.MovieScreen.route + "/{movieItem}",
// + "/{movieItem}",
composable(route = Screen.MovieScreen.route
+ "/{movieItem}",
arguments = listOf(
navArgument("movieItem"){
type = NavType.SerializableType(Movie::class.java)
}
)
){ navBackStackEntry ->

val movieItem = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
navBackStackEntry.arguments?.getSerializable("movieItem", Movie::class.java)
} else {
navBackStackEntry.arguments?.getSerializable("movieItem")
} as Movie
MovieScreen(movie = movieItem)

MovieScreen(movie = movieItem, creditsData )
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ interface MovieEndpoint {
@Query("language") language: String = "en-Us"
): ActorDTO

@GET("movie/{movie_id}")
@GET("movie/{movie_id}/credits")
suspend fun getCreditsMovie(
@Path(value = "movie_id") movieId: Int
@Path(value = "movie_id") movieId: Int,
): CreditsDTO

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.satyamthakur.silver.data.repository

import com.satyamthakur.silver.data.model.MovieDTO
import com.satyamthakur.silver.data.remote.MovieEndpoint
import com.satyamthakur.silver.domain.mapper.asMovie
import com.satyamthakur.silver.domain.mapper.asActor
import com.satyamthakur.silver.domain.mapper.asCredits
import com.satyamthakur.silver.domain.mapper.asMovie
import com.satyamthakur.silver.domain.model.Movie
import com.satyamthakur.silver.domain.model.cast.Actor
import com.satyamthakur.silver.domain.model.cast.Credits
Expand All @@ -13,6 +13,7 @@ import com.satyamthakur.silver.utility.Resource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onStart
Expand Down Expand Up @@ -74,7 +75,7 @@ class MovieRepository(
message = errorParser(t)
)
)
}.onStart {
}.distinctUntilChanged().onStart {
emit(Resource.Loading)
}.flowOn(ioDispatcher)
}
Expand All @@ -95,7 +96,7 @@ class MovieRepository(
message = errorParser(t)
)
)
}.onStart {
}.distinctUntilChanged().onStart {
emit(Resource.Loading)
}.flowOn(ioDispatcher)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ data class Actor(
val name: String,
val place_of_birth: String,
val popularity: Double,
val profile_path: String
val profile_path: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ data class Cast(
val order: Int,
val originalName: String,
val popularity: Double,
val profilePath: String
val profilePath: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ data class Crew(
val name: String,
val originalName: String,
val popularity: Double,
val profilePath: String
val profilePath: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
Expand All @@ -46,6 +48,7 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
Expand All @@ -56,6 +59,7 @@ import com.satyamthakur.silver.R
import com.satyamthakur.silver.domain.model.Movie
import com.satyamthakur.silver.domain.model.PopularMovies
import com.satyamthakur.silver.domain.model.cast.Cast
import com.satyamthakur.silver.domain.model.cast.Credits
import com.satyamthakur.silver.ui.screen.dashboard.viewmodel.DashboardViewModel
import com.satyamthakur.silver.ui.theme.CategoryBackground
import com.satyamthakur.silver.ui.theme.CategoryTextColor
Expand All @@ -71,64 +75,76 @@ import org.koin.androidx.compose.koinViewModel
@Composable
fun MovieScreen(
movie: Movie,
creditsData: Resource<Credits>
) {

val viewModel = koinViewModel<DashboardViewModel>()
val creditsData by viewModel.credits.collectAsStateWithLifecycle()
// val creditsData by viewModel.credits.collectAsStateWithLifecycle()

var cast by remember { mutableStateOf <List<Cast>>(emptyList()) }
var cast by remember { mutableStateOf<List<Cast>?>(emptyList()) }
cast = creditsData.data?.cast

LaunchedEffect(
key1 = creditsData,
block = {
viewModel.getCreditsData(movie.id)
cast = creditsData.data!!.cast
viewModel.getCreditsData(
movie.id
)
cast = creditsData.data?.cast
}
)

Scaffold(
when (cast) {
null -> {
LinearProgressIndicator(modifier = Modifier.fillMaxWidth())
}
else -> {
Scaffold(

topBar = {
TopAppBar(modifier = Modifier
.fillMaxWidth()
.padding(top = 24.dp),
colors = TopAppBarDefaults.smallTopAppBarColors(
containerColor = Color.Transparent,
titleContentColor = Color.Transparent
),
title = {},
navigationIcon = {
IconButton(
onClick = { }
) {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_back),
contentDescription = null,
tint = MaterialTheme.colorScheme.background
)
}
},
actions = {
IconButton(
onClick = { }
) {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_more),
contentDescription = null,
tint = MaterialTheme.colorScheme.background
)
}
topBar = {
TopAppBar(modifier = Modifier
.fillMaxWidth()
.padding(top = 24.dp),
colors = TopAppBarDefaults.smallTopAppBarColors(
containerColor = Color.Transparent,
titleContentColor = Color.Transparent
),
title = {},
navigationIcon = {
IconButton(
onClick = { }
) {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_back),
contentDescription = null,
tint = MaterialTheme.colorScheme.background
)
}
},
actions = {
IconButton(
onClick = { }
) {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_more),
contentDescription = null,
tint = MaterialTheme.colorScheme.background
)
}
}
)
}
)
}

) {
MoviePoster(
castList = cast,
movie = movie
)
) {
MoviePoster(
castList = cast!!,
movie = movie
)
}
}
}


}

@Composable
Expand All @@ -145,7 +161,7 @@ fun MoviePoster(
Modifier
.fillMaxSize(),
lazyListState
){
) {
item {
Image(
painter = painterResource(id = R.drawable.mock_poster),
Expand All @@ -161,7 +177,7 @@ fun MoviePoster(
.fillMaxHeight(0.3f),
)
}
items(1){
items(1) {
Column(
modifier = Modifier
.background(
Expand All @@ -172,20 +188,22 @@ fun MoviePoster(
horizontal = 20.dp,
vertical = 20.dp
)
){
) {
Row(
verticalAlignment = Alignment.CenterVertically
){
) {
Text(
text = movie.title,
maxLines = 2,
style = MaterialTheme.typography.titleMedium,
fontSize = 20.sp
)
Spacer(modifier = Modifier
.weight(1f)
.fillMaxWidth())
IconButton(onClick = { }) {
Spacer(
modifier = Modifier
.weight(1f)
.fillMaxWidth()
)
IconButton(onClick = { }) {
Icon(
painter = painterResource(id = R.drawable.ic_bookmark),
contentDescription = null,
Expand Down Expand Up @@ -221,11 +239,14 @@ fun MoviePoster(
horizontalArrangement = Arrangement.spacedBy(10.dp),
modifier = Modifier.padding(top = 15.dp)
) {
for (genre in movie.genresID){
for (genre in movie.genresID) {
Text(
text = genre.toString(),
modifier = Modifier
.background(color = CategoryBackground, shape = RoundedCornerShape(20)),
.background(
color = CategoryBackground,
shape = RoundedCornerShape(20)
),
style = MaterialTheme.typography.labelMedium.copy(
color = CategoryTextColor
),
Expand Down Expand Up @@ -345,14 +366,14 @@ fun MoviePoster(
@Composable
fun CastMovie(
castList: List<Cast>
){
) {
LazyRow(
modifier = Modifier
.padding(top = 20.dp),
.padding(top = 20.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalAlignment = Alignment.CenterVertically
){
items(castList.size){ index ->
) {
items(castList.size) { index ->
Column(
modifier = Modifier.width(100.dp)
) {
Expand Down

0 comments on commit c362587

Please sign in to comment.