Skip to content

Commit 8286290

Browse files
committed
refactor: ensure a user is logged in before marking as favorite
1 parent dd1c179 commit 8286290

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

dataconnect/app/src/main/java/com/google/firebase/example/dataconnect/feature/actordetail/ActorDetailViewModel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ class ActorDetailViewModel(
6363
}
6464

6565
fun toggleFavorite(newValue: Boolean) {
66+
// TODO(thatfiredev): hide the button if there's no user logged in
67+
val uid = firebaseAuth.currentUser?.uid ?: return
6668
viewModelScope.launch {
6769
try {
6870
if (newValue) {
6971
moviesConnector.addFavoritedActor.execute(UUID.fromString(actorId))
7072
} else {
7173
moviesConnector.deleteFavoriteActor.execute(
72-
userId = firebaseAuth.currentUser?.uid ?: "",
74+
userId = uid,
7375
actorId = UUID.fromString(actorId)
7476
)
7577
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class MovieDetailViewModel(
6969
}
7070

7171
fun toggleFavorite(newValue: Boolean) {
72+
// TODO(thatfiredev): hide the button if there's no user logged in
73+
val uid = firebaseAuth.currentUser?.uid ?: return
7274
viewModelScope.launch {
7375
try {
7476
if (newValue) {
@@ -77,7 +79,7 @@ class MovieDetailViewModel(
7779
// TODO(thatfiredev): investigate whether this is a schema error
7880
// userId probably shouldn't be here.
7981
moviesConnector.deleteFavoritedMovie.execute(
80-
userId = firebaseAuth.currentUser?.uid ?: "",
82+
userId = uid,
8183
movieId = UUID.fromString(movieId)
8284
)
8385
}
@@ -90,6 +92,8 @@ class MovieDetailViewModel(
9092
}
9193

9294
fun toggleWatched(newValue: Boolean) {
95+
// TODO(thatfiredev): hide the button if there's no user logged in
96+
val uid = firebaseAuth.currentUser?.uid ?: return
9397
viewModelScope.launch {
9498
try {
9599
if (newValue) {
@@ -98,7 +102,7 @@ class MovieDetailViewModel(
98102
// TODO(thatfiredev): investigate whether this is a schema error
99103
// userId probably shouldn't be here.
100104
moviesConnector.deleteWatchedMovie.execute(
101-
userId = firebaseAuth.currentUser?.uid ?: "",
105+
userId = uid,
102106
movieId = UUID.fromString(movieId)
103107
)
104108
}
@@ -111,6 +115,8 @@ class MovieDetailViewModel(
111115
}
112116

113117
fun addRating(rating: Float, text: String) {
118+
// TODO(thatfiredev): hide the button if there's no user logged in
119+
if (firebaseAuth.currentUser?.uid == null) return
114120
viewModelScope.launch {
115121
try {
116122
moviesConnector.addReview.execute(

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ fun UserReviews(
6363

6464
Button(
6565
onClick = {
66-
onReviewSubmitted(rating, reviewText)
67-
reviewText = ""
66+
if (!reviewText.isNullOrEmpty()) {
67+
onReviewSubmitted(rating, reviewText)
68+
reviewText = ""
69+
}
6870
}
6971
) {
7072
Text(stringResource(R.string.button_submit_review))

0 commit comments

Comments
 (0)