diff --git a/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/moviedetail/MovieDetailScreen.kt b/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/moviedetail/MovieDetailScreen.kt index 248ff07ae..d0b8729b9 100644 --- a/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/moviedetail/MovieDetailScreen.kt +++ b/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/moviedetail/MovieDetailScreen.kt @@ -88,7 +88,7 @@ fun MovieDetailScreen( listTitle = stringResource(R.string.title_main_actors), actors = movie?.mainActors?.mapNotNull { Actor(it.id.toString(), it.name, it.imageUrl) - }.orEmpty(), + }, onActorClicked = { onActorClicked(it) } ) // Supporting Actors list @@ -96,14 +96,14 @@ fun MovieDetailScreen( listTitle = stringResource(R.string.title_supporting_actors), actors = movie?.supportingActors?.mapNotNull { Actor(it.id.toString(), it.name, it.imageUrl) - }.orEmpty(), + }, onActorClicked = { onActorClicked(it) } ) UserReviews( onReviewSubmitted = { rating, text -> movieDetailViewModel.addRating(rating, text) }, - movie?.reviews ?: emptyList() + movie?.reviews ) } @@ -202,61 +202,3 @@ fun MovieInformation( } } } - -@Composable -fun UserReviews( - onReviewSubmitted: (rating: Float, text: String) -> Unit, - reviews: List -) { - var reviewText by remember { mutableStateOf("") } - Text( - text = "User Reviews", - style = MaterialTheme.typography.headlineMedium, - modifier = Modifier.padding(horizontal = 16.dp) - ) - Spacer(modifier = Modifier.height(8.dp)) - Column( - modifier = Modifier - .fillMaxSize() - .padding(16.dp), - horizontalAlignment = Alignment.CenterHorizontally - ) { - var rating by remember { mutableFloatStateOf(3f) } - Text("Rating: ${rating}") - Slider( - value = rating, - // Round the value to the nearest 0.5 - onValueChange = { rating = (Math.round(it * 2) / 2.0).toFloat() }, - steps = 9, - valueRange = 1f..5f - ) - TextField( - value = reviewText, - onValueChange = { reviewText = it }, - label = { Text(stringResource(R.string.hint_write_review)) }, - modifier = Modifier.fillMaxWidth() - ) - - Spacer(modifier = Modifier.height(16.dp)) - - Button( - onClick = { - onReviewSubmitted(rating, reviewText) - reviewText = "" - } - ) { - Text(stringResource(R.string.button_submit_review)) - } - } - Column { - // TODO(thatfiredev): Handle cases where the list is too long to display - reviews.forEach { - ReviewCard( - userName = it.user.username, - date = it.reviewDate, - rating = it.rating?.toDouble() ?: 0.0, - text = it.reviewText ?: "" - ) - } - } -} diff --git a/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/moviedetail/UserReviews.kt b/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/moviedetail/UserReviews.kt new file mode 100644 index 000000000..0b677d48d --- /dev/null +++ b/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/moviedetail/UserReviews.kt @@ -0,0 +1,84 @@ +package com.google.firebase.example.dataconnect.feature.moviedetail + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Button +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Slider +import androidx.compose.material3.Text +import androidx.compose.material3.TextField +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableFloatStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import com.google.firebase.dataconnect.movies.GetMovieByIdQuery +import com.google.firebase.example.dataconnect.R +import com.google.firebase.example.dataconnect.ui.components.ReviewCard + +@Composable +fun UserReviews( + onReviewSubmitted: (rating: Float, text: String) -> Unit, + reviews: List? = emptyList() +) { + var reviewText by remember { mutableStateOf("") } + Text( + text = "User Reviews", + style = MaterialTheme.typography.headlineMedium, + modifier = Modifier.padding(horizontal = 16.dp) + ) + Spacer(modifier = Modifier.height(8.dp)) + Column( + modifier = Modifier + .fillMaxSize() + .padding(16.dp), + horizontalAlignment = Alignment.CenterHorizontally + ) { + var rating by remember { mutableFloatStateOf(3f) } + Text("Rating: ${rating}") + Slider( + value = rating, + // Round the value to the nearest 0.5 + onValueChange = { rating = (Math.round(it * 2) / 2.0).toFloat() }, + steps = 9, + valueRange = 1f..5f + ) + TextField( + value = reviewText, + onValueChange = { reviewText = it }, + label = { Text(stringResource(R.string.hint_write_review)) }, + modifier = Modifier.fillMaxWidth() + ) + + Spacer(modifier = Modifier.height(16.dp)) + + Button( + onClick = { + onReviewSubmitted(rating, reviewText) + reviewText = "" + } + ) { + Text(stringResource(R.string.button_submit_review)) + } + } + Column { + // TODO(thatfiredev): Handle cases where the list is too long to display + reviews.orEmpty().forEach { + ReviewCard( + userName = it.user.username, + date = it.reviewDate, + rating = it.rating?.toDouble() ?: 0.0, + text = it.reviewText ?: "" + ) + } + } +} diff --git a/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileScreen.kt b/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileScreen.kt index 027c471af..7b7ee77d2 100644 --- a/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileScreen.kt +++ b/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileScreen.kt @@ -55,10 +55,10 @@ fun ProfileScreen( val ui = uiState as ProfileUIState.ProfileState ProfileScreen( ui.username ?: "User", - ui.reviews, - ui.watchedMovies, - ui.favoriteMovies, - ui.favoriteActors, + ui.reviews.orEmpty(), + ui.watchedMovies.orEmpty(), + ui.favoriteMovies.orEmpty(), + ui.favoriteActors.orEmpty(), onSignOut = { profileViewModel.signOut() } diff --git a/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileUIState.kt b/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileUIState.kt index a86ce34b7..9977d7b77 100644 --- a/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileUIState.kt +++ b/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileUIState.kt @@ -11,9 +11,9 @@ sealed class ProfileUIState { data class ProfileState( val username: String?, - val reviews: List = emptyList(), - val watchedMovies: List = emptyList(), - val favoriteMovies: List = emptyList(), - val favoriteActors: List = emptyList() + val reviews: List? = emptyList(), + val watchedMovies: List? = emptyList(), + val favoriteMovies: List? = emptyList(), + val favoriteActors: List? = emptyList() ) : ProfileUIState() } diff --git a/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileViewModel.kt b/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileViewModel.kt index 1fe8c75e4..9430d067e 100644 --- a/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileViewModel.kt +++ b/dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileViewModel.kt @@ -83,10 +83,10 @@ class ProfileViewModel( val user = moviesConnector.getUserById.execute(id = userId).data.user _uiState.value = ProfileUIState.ProfileState( user?.username, - favoriteMovies = user?.favoriteMovies ?: emptyList(), - watchedMovies = user?.watched ?: emptyList(), - favoriteActors = user?.favoriteActors ?: emptyList(), - reviews = user?.reviews ?: emptyList() + favoriteMovies = user?.favoriteMovies, + watchedMovies = user?.watched, + favoriteActors = user?.favoriteActors, + reviews = user?.reviews ) Log.d("DisplayUser", "$user") } catch (e: Exception) {