Skip to content

Commit a45c5d4

Browse files
committed
refactor: move UserReviews into its own dedicated file
1 parent 8546e34 commit a45c5d4

File tree

5 files changed

+99
-73
lines changed

5 files changed

+99
-73
lines changed

dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/moviedetail/MovieDetailScreen.kt

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,22 @@ fun MovieDetailScreen(
8888
listTitle = stringResource(R.string.title_main_actors),
8989
actors = movie?.mainActors?.mapNotNull {
9090
Actor(it.id.toString(), it.name, it.imageUrl)
91-
}.orEmpty(),
91+
},
9292
onActorClicked = { onActorClicked(it) }
9393
)
9494
// Supporting Actors list
9595
ActorsList(
9696
listTitle = stringResource(R.string.title_supporting_actors),
9797
actors = movie?.supportingActors?.mapNotNull {
9898
Actor(it.id.toString(), it.name, it.imageUrl)
99-
}.orEmpty(),
99+
},
100100
onActorClicked = { onActorClicked(it) }
101101
)
102102
UserReviews(
103103
onReviewSubmitted = { rating, text ->
104104
movieDetailViewModel.addRating(rating, text)
105105
},
106-
movie?.reviews ?: emptyList()
106+
movie?.reviews
107107
)
108108
}
109109

@@ -202,61 +202,3 @@ fun MovieInformation(
202202
}
203203
}
204204
}
205-
206-
@Composable
207-
fun UserReviews(
208-
onReviewSubmitted: (rating: Float, text: String) -> Unit,
209-
reviews: List<GetMovieByIdQuery.Data.Movie.ReviewsItem>
210-
) {
211-
var reviewText by remember { mutableStateOf("") }
212-
Text(
213-
text = "User Reviews",
214-
style = MaterialTheme.typography.headlineMedium,
215-
modifier = Modifier.padding(horizontal = 16.dp)
216-
)
217-
Spacer(modifier = Modifier.height(8.dp))
218-
Column(
219-
modifier = Modifier
220-
.fillMaxSize()
221-
.padding(16.dp),
222-
horizontalAlignment = Alignment.CenterHorizontally
223-
) {
224-
var rating by remember { mutableFloatStateOf(3f) }
225-
Text("Rating: ${rating}")
226-
Slider(
227-
value = rating,
228-
// Round the value to the nearest 0.5
229-
onValueChange = { rating = (Math.round(it * 2) / 2.0).toFloat() },
230-
steps = 9,
231-
valueRange = 1f..5f
232-
)
233-
TextField(
234-
value = reviewText,
235-
onValueChange = { reviewText = it },
236-
label = { Text(stringResource(R.string.hint_write_review)) },
237-
modifier = Modifier.fillMaxWidth()
238-
)
239-
240-
Spacer(modifier = Modifier.height(16.dp))
241-
242-
Button(
243-
onClick = {
244-
onReviewSubmitted(rating, reviewText)
245-
reviewText = ""
246-
}
247-
) {
248-
Text(stringResource(R.string.button_submit_review))
249-
}
250-
}
251-
Column {
252-
// TODO(thatfiredev): Handle cases where the list is too long to display
253-
reviews.forEach {
254-
ReviewCard(
255-
userName = it.user.username,
256-
date = it.reviewDate,
257-
rating = it.rating?.toDouble() ?: 0.0,
258-
text = it.reviewText ?: ""
259-
)
260-
}
261-
}
262-
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.google.firebase.example.dataconnect.feature.moviedetail
2+
3+
import androidx.compose.foundation.layout.Column
4+
import androidx.compose.foundation.layout.Spacer
5+
import androidx.compose.foundation.layout.fillMaxSize
6+
import androidx.compose.foundation.layout.fillMaxWidth
7+
import androidx.compose.foundation.layout.height
8+
import androidx.compose.foundation.layout.padding
9+
import androidx.compose.material3.Button
10+
import androidx.compose.material3.MaterialTheme
11+
import androidx.compose.material3.Slider
12+
import androidx.compose.material3.Text
13+
import androidx.compose.material3.TextField
14+
import androidx.compose.runtime.Composable
15+
import androidx.compose.runtime.getValue
16+
import androidx.compose.runtime.mutableFloatStateOf
17+
import androidx.compose.runtime.mutableStateOf
18+
import androidx.compose.runtime.remember
19+
import androidx.compose.runtime.setValue
20+
import androidx.compose.ui.Alignment
21+
import androidx.compose.ui.Modifier
22+
import androidx.compose.ui.res.stringResource
23+
import androidx.compose.ui.unit.dp
24+
import com.google.firebase.dataconnect.movies.GetMovieByIdQuery
25+
import com.google.firebase.example.dataconnect.R
26+
import com.google.firebase.example.dataconnect.ui.components.ReviewCard
27+
28+
@Composable
29+
fun UserReviews(
30+
onReviewSubmitted: (rating: Float, text: String) -> Unit,
31+
reviews: List<GetMovieByIdQuery.Data.Movie.ReviewsItem>? = emptyList()
32+
) {
33+
var reviewText by remember { mutableStateOf("") }
34+
Text(
35+
text = "User Reviews",
36+
style = MaterialTheme.typography.headlineMedium,
37+
modifier = Modifier.padding(horizontal = 16.dp)
38+
)
39+
Spacer(modifier = Modifier.height(8.dp))
40+
Column(
41+
modifier = Modifier
42+
.fillMaxSize()
43+
.padding(16.dp),
44+
horizontalAlignment = Alignment.CenterHorizontally
45+
) {
46+
var rating by remember { mutableFloatStateOf(3f) }
47+
Text("Rating: ${rating}")
48+
Slider(
49+
value = rating,
50+
// Round the value to the nearest 0.5
51+
onValueChange = { rating = (Math.round(it * 2) / 2.0).toFloat() },
52+
steps = 9,
53+
valueRange = 1f..5f
54+
)
55+
TextField(
56+
value = reviewText,
57+
onValueChange = { reviewText = it },
58+
label = { Text(stringResource(R.string.hint_write_review)) },
59+
modifier = Modifier.fillMaxWidth()
60+
)
61+
62+
Spacer(modifier = Modifier.height(16.dp))
63+
64+
Button(
65+
onClick = {
66+
onReviewSubmitted(rating, reviewText)
67+
reviewText = ""
68+
}
69+
) {
70+
Text(stringResource(R.string.button_submit_review))
71+
}
72+
}
73+
Column {
74+
// TODO(thatfiredev): Handle cases where the list is too long to display
75+
reviews.orEmpty().forEach {
76+
ReviewCard(
77+
userName = it.user.username,
78+
date = it.reviewDate,
79+
rating = it.rating?.toDouble() ?: 0.0,
80+
text = it.reviewText ?: ""
81+
)
82+
}
83+
}
84+
}

dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileScreen.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ fun ProfileScreen(
5555
val ui = uiState as ProfileUIState.ProfileState
5656
ProfileScreen(
5757
ui.username ?: "User",
58-
ui.reviews,
59-
ui.watchedMovies,
60-
ui.favoriteMovies,
61-
ui.favoriteActors,
58+
ui.reviews.orEmpty(),
59+
ui.watchedMovies.orEmpty(),
60+
ui.favoriteMovies.orEmpty(),
61+
ui.favoriteActors.orEmpty(),
6262
onSignOut = {
6363
profileViewModel.signOut()
6464
}

dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileUIState.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ sealed class ProfileUIState {
1111

1212
data class ProfileState(
1313
val username: String?,
14-
val reviews: List<GetUserByIdQuery.Data.User.ReviewsItem> = emptyList(),
15-
val watchedMovies: List<GetUserByIdQuery.Data.User.WatchedItem> = emptyList(),
16-
val favoriteMovies: List<GetUserByIdQuery.Data.User.FavoriteMoviesItem> = emptyList(),
17-
val favoriteActors: List<GetUserByIdQuery.Data.User.FavoriteActorsItem> = emptyList()
14+
val reviews: List<GetUserByIdQuery.Data.User.ReviewsItem>? = emptyList(),
15+
val watchedMovies: List<GetUserByIdQuery.Data.User.WatchedItem>? = emptyList(),
16+
val favoriteMovies: List<GetUserByIdQuery.Data.User.FavoriteMoviesItem>? = emptyList(),
17+
val favoriteActors: List<GetUserByIdQuery.Data.User.FavoriteActorsItem>? = emptyList()
1818
) : ProfileUIState()
1919
}

dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/profile/ProfileViewModel.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ class ProfileViewModel(
8383
val user = moviesConnector.getUserById.execute(id = userId).data.user
8484
_uiState.value = ProfileUIState.ProfileState(
8585
user?.username,
86-
favoriteMovies = user?.favoriteMovies ?: emptyList(),
87-
watchedMovies = user?.watched ?: emptyList(),
88-
favoriteActors = user?.favoriteActors ?: emptyList(),
89-
reviews = user?.reviews ?: emptyList()
86+
favoriteMovies = user?.favoriteMovies,
87+
watchedMovies = user?.watched,
88+
favoriteActors = user?.favoriteActors,
89+
reviews = user?.reviews
9090
)
9191
Log.d("DisplayUser", "$user")
9292
} catch (e: Exception) {

0 commit comments

Comments
 (0)