Skip to content

Commit 2966e2d

Browse files
Merge pull request #275 from LookUpGroup27/fix-remove-zero-star-rating
fix: Remove zero star rating
2 parents 523c8e2 + 1b5fef4 commit 2966e2d

File tree

6 files changed

+81
-185
lines changed

6 files changed

+81
-185
lines changed

app/src/androidTest/java/com/github/lookupgroup27/lookup/ui/image/ImagePreviewDialogTest.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import org.junit.*
1010
import org.mockito.Mock
1111
import org.mockito.Mockito.*
1212
import org.mockito.kotlin.any
13-
import org.mockito.kotlin.eq
14-
import org.mockito.kotlin.verify
1513

1614
class ImagePreviewDialogTest {
1715

@@ -111,7 +109,7 @@ class ImagePreviewDialogTest {
111109
assert(dialogDismissed)
112110
}
113111

114-
@Test
112+
/*@Test
115113
fun testStarClickCallsUpdatePost() {
116114
// Set the Compose content to ImagePreviewDialog
117115
composeTestRule.setContent {
@@ -124,7 +122,7 @@ class ImagePreviewDialogTest {
124122
125123
// Verify that updatePost was called in the postsViewModel
126124
verify(postsRepository).updatePost(eq(testPost), any(), any())
127-
}
125+
}*/
128126

129127
/*@Test
130128
fun testStarClickCallsUpdateUserProfile() {
@@ -141,15 +139,12 @@ class ImagePreviewDialogTest {
141139
}*/
142140

143141
@Test
144-
fun testStarIsDisplayed() {
142+
fun testStarIsNotDisplayed() {
145143
composeTestRule.setContent {
146144
ImagePreviewDialog(
147145
post = testPost, username = "User1", onDismiss = {}, testStarStates, onRatingChanged = {})
148146
}
149147
// Perform click on the first star icon of a post with uid "1"
150-
composeTestRule
151-
.onNodeWithTag("Star_1_1")
152-
.assertIsDisplayed()
153-
.performClick() // Click on the first star
148+
composeTestRule.onNodeWithTag("Star_1_1").assertIsNotDisplayed() // Click on the first star
154149
}
155150
}

app/src/main/java/com/github/lookupgroup27/lookup/ui/feed/Feed.kt

Lines changed: 39 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,9 @@ fun FeedScreen(
7676
onDispose { activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED }
7777
}
7878

79-
// Fetch user profile
80-
LaunchedEffect(Unit) {
81-
Log.d("FeedScreen", "Fetching user profile")
82-
profileViewModel.fetchUserProfile()
83-
}
84-
8579
// User-related state
80+
81+
profileViewModel.fetchUserProfile()
8682
val profile by profileViewModel.userProfile.collectAsState()
8783
val user = FirebaseAuth.getInstance().currentUser
8884
val isUserLoggedIn = user != null
@@ -256,30 +252,50 @@ fun FeedScreen(
256252
postRatings[post.uid] ?: List(NUMBER_OF_STARS) { false },
257253
onRatingChanged = { newRating ->
258254
val oldPostRatings =
259-
postRatings[post.uid] ?: List(NUMBER_OF_STARS) { false }
255+
postRatings[post.uid] ?: mutableListOf(false, false, false)
260256
val oldStarCounts = oldPostRatings.count { it }
257+
// Directly modify the existing starStates list to avoid
258+
// creating a new list
261259
postRatings[post.uid] = newRating.toList()
260+
// Update the stars count based on the new rating
262261
val starsCount = newRating.count { it }
263-
264-
// Update user profile ratings
265-
val newProfile =
266-
updateProfileRatings(
267-
currentProfile = profile,
268-
postUid = post.uid,
269-
starsCount = starsCount,
262+
// Update user profile with the new rating count
263+
val updatedRatings = profile?.ratings?.toMutableMap()
264+
updatedRatings?.set(post.uid, starsCount)
265+
val newProfile: UserProfile =
266+
profile?.copy(
270267
username = username,
271268
bio = bio,
272-
email = email)
269+
email = email,
270+
ratings = updatedRatings ?: emptyMap())
271+
?: UserProfile(
272+
username = username,
273+
bio = bio,
274+
email = email,
275+
ratings = updatedRatings ?: emptyMap())
273276
profileViewModel.updateUserProfile(newProfile)
274277

275-
// Update post details
276-
val updatedPost =
277-
calculatePostUpdates(
278-
post = post,
279-
userEmail = userEmail,
280-
starsCount = starsCount,
281-
oldStarCounts = oldStarCounts)
282-
postsViewModel.updatePost(updatedPost)
278+
val isReturningUser = post.ratedBy.contains(userEmail)
279+
val newStarsCount =
280+
if (isReturningUser)
281+
post.starsCount - oldStarCounts + starsCount
282+
else post.starsCount + starsCount
283+
val newUsersNumber =
284+
if (isReturningUser) post.usersNumber
285+
else post.usersNumber + 1
286+
val newAvg = newStarsCount.toDouble() / newUsersNumber
287+
288+
postsViewModel.updatePost(
289+
post.copy(
290+
averageStars = newAvg,
291+
starsCount = newStarsCount,
292+
usersNumber = newUsersNumber,
293+
ratedBy =
294+
if (!isReturningUser) {
295+
post.ratedBy + userEmail
296+
} else {
297+
post.ratedBy
298+
}))
283299
},
284300
onAddressClick = { clickedPost ->
285301
val selectedMarker =
@@ -301,57 +317,3 @@ fun FeedScreen(
301317
}
302318
}
303319
}
304-
/**
305-
* Updates the user's profile ratings.
306-
*
307-
* @param currentProfile The current user profile.
308-
* @param postUid The unique identifier of the post being rated.
309-
* @param starsCount The number of stars given to the post.
310-
* @param username The user's username.
311-
* @param bio The user's bio.
312-
* @param email The user's email.
313-
* @return An updated [UserProfile] with the new rating.
314-
*/
315-
fun updateProfileRatings(
316-
currentProfile: UserProfile?,
317-
postUid: String,
318-
starsCount: Int,
319-
username: String,
320-
bio: String,
321-
email: String
322-
): UserProfile {
323-
val updatedRatings =
324-
currentProfile?.ratings?.toMutableMap()?.apply { this[postUid] = starsCount }
325-
?: mutableMapOf(postUid to starsCount)
326-
327-
return currentProfile?.copy(
328-
username = username, bio = bio, email = email, ratings = updatedRatings)
329-
?: UserProfile(username = username, bio = bio, email = email, ratings = updatedRatings)
330-
}
331-
332-
/**
333-
* Calculates the updated state of a post after a user rates it.
334-
*
335-
* @param post The original post.
336-
* @param userEmail The email of the user rating the post.
337-
* @param starsCount The number of stars the user has given.
338-
* @param oldStarCounts The previous number of stars the user had given.
339-
* @return An updated [Post] with recalculated ratings and user counts.
340-
*/
341-
fun calculatePostUpdates(post: Post, userEmail: String, starsCount: Int, oldStarCounts: Int): Post {
342-
val isReturningUser = post.ratedBy.contains(userEmail)
343-
val newStarsCount =
344-
if (isReturningUser) {
345-
post.starsCount - oldStarCounts + starsCount
346-
} else {
347-
post.starsCount + starsCount
348-
}
349-
val newUsersNumber = if (isReturningUser) post.usersNumber else post.usersNumber + 1
350-
val newAvg = newStarsCount.toDouble() / newUsersNumber
351-
352-
return post.copy(
353-
averageStars = newAvg,
354-
starsCount = newStarsCount,
355-
usersNumber = newUsersNumber,
356-
ratedBy = if (!isReturningUser) post.ratedBy + userEmail else post.ratedBy)
357-
}

app/src/main/java/com/github/lookupgroup27/lookup/ui/feed/components/PostItem.kt

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fun PostItem(
4949
onImageClick: (imageUrl: String, username: String, description: String) -> Unit,
5050
color: Color = Color.White,
5151
textForUsername: String = post.username,
52+
showStars: Boolean = true,
5253
showAverage: Boolean = true,
5354
showAddress: Boolean = true
5455
) {
@@ -100,39 +101,40 @@ fun PostItem(
100101
style = MaterialTheme.typography.bodyMedium.copy(color = Color.White),
101102
modifier = Modifier.testTag("DescriptionTag_${post.uid}"))
102103
}
103-
104-
// Rating Row
105-
Row(
106-
verticalAlignment = androidx.compose.ui.Alignment.CenterVertically,
107-
horizontalArrangement = Arrangement.spacedBy(4.dp),
108-
modifier = Modifier.fillMaxWidth()) {
109-
starStates.forEachIndexed { index, isFilled ->
110-
IconButton(
111-
onClick = {
112-
val newRating = starStates.mapIndexed { i, _ -> i <= index }
113-
onRatingChanged(newRating)
114-
},
115-
modifier =
116-
Modifier.size(36.dp).testTag("Star_${index + 1}_${post.uid}")) {
117-
Image(
118-
painter =
119-
painterResource(
120-
id =
121-
if (isFilled) R.drawable.full_star2
122-
else R.drawable.empty_star2),
123-
contentDescription = "Star")
124-
}
125-
}
126-
Spacer(modifier = Modifier.weight(1f))
127-
if (showAverage) {
128-
Text(
129-
text = "Avg: ${"%.1f".format(post.averageStars)}",
130-
style =
131-
MaterialTheme.typography.bodyMedium.copy(
132-
fontWeight = FontWeight.Medium, color = color),
133-
modifier = Modifier.testTag("AverageRatingTag_${post.uid}"))
104+
if (showStars) {
105+
// Rating Row
106+
Row(
107+
verticalAlignment = androidx.compose.ui.Alignment.CenterVertically,
108+
horizontalArrangement = Arrangement.spacedBy(4.dp),
109+
modifier = Modifier.fillMaxWidth()) {
110+
starStates.forEachIndexed { index, isFilled ->
111+
IconButton(
112+
onClick = {
113+
val newRating = starStates.mapIndexed { i, _ -> i <= index }
114+
onRatingChanged(newRating)
115+
},
116+
modifier =
117+
Modifier.size(36.dp).testTag("Star_${index + 1}_${post.uid}")) {
118+
Image(
119+
painter =
120+
painterResource(
121+
id =
122+
if (isFilled) R.drawable.full_star2
123+
else R.drawable.empty_star2),
124+
contentDescription = "Star")
125+
}
126+
}
127+
Spacer(modifier = Modifier.weight(1f))
128+
if (showAverage) {
129+
Text(
130+
text = "Avg: ${"%.1f".format(post.averageStars)}",
131+
style =
132+
MaterialTheme.typography.bodyMedium.copy(
133+
fontWeight = FontWeight.Medium, color = color),
134+
modifier = Modifier.testTag("AverageRatingTag_${post.uid}"))
135+
}
134136
}
135-
}
137+
}
136138
}
137139
}
138140
}

app/src/main/java/com/github/lookupgroup27/lookup/ui/googlemap/GoogleMap.kt

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import androidx.core.content.ContextCompat
2727
import androidx.lifecycle.viewmodel.compose.viewModel
2828
import com.github.lookupgroup27.lookup.R
2929
import com.github.lookupgroup27.lookup.model.location.LocationProviderSingleton
30-
import com.github.lookupgroup27.lookup.model.profile.UserProfile
3130
import com.github.lookupgroup27.lookup.ui.googlemap.components.*
3231
import com.github.lookupgroup27.lookup.ui.navigation.*
3332
import com.github.lookupgroup27.lookup.ui.post.PostsViewModel
@@ -188,31 +187,6 @@ fun GoogleMapScreen(
188187
locationProvider.currentLocation.value,
189188
autoCenteringEnabled,
190189
allPosts,
191-
userEmail,
192-
updateProfile = { profile, updatedRatings ->
193-
val newProfile: UserProfile =
194-
profile?.copy(
195-
username = username,
196-
bio = bio,
197-
email = email,
198-
ratings = updatedRatings ?: emptyMap())
199-
?: UserProfile(
200-
username = username,
201-
bio = bio,
202-
email = email,
203-
ratings = updatedRatings ?: emptyMap())
204-
profileViewModel.updateUserProfile(newProfile)
205-
},
206-
profile = profile,
207-
updatePost = { post, newAvg, newStarsCount, newUsersNumber, newRatedBy ->
208-
postsViewModel.updatePost(
209-
post.copy(
210-
averageStars = newAvg,
211-
starsCount = newStarsCount,
212-
usersNumber = newUsersNumber,
213-
ratedBy = newRatedBy))
214-
},
215-
postRatings = postRatings,
216190
highlightedPost = highlightedPost)
217191
}
218192
}

app/src/main/java/com/github/lookupgroup27/lookup/ui/googlemap/components/MapView.kt

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import androidx.compose.foundation.layout.*
66
import androidx.compose.runtime.*
77
import androidx.compose.ui.Modifier
88
import com.github.lookupgroup27.lookup.model.post.Post
9-
import com.github.lookupgroup27.lookup.model.profile.UserProfile
109
import com.github.lookupgroup27.lookup.ui.image.ImagePreviewDialog
1110
import com.google.android.gms.maps.CameraUpdateFactory
1211
import com.google.android.gms.maps.model.LatLng
@@ -34,11 +33,6 @@ fun MapView(
3433
location: Location?,
3534
autoCenteringEnabled: Boolean,
3635
posts: List<Post>,
37-
userEmail: String,
38-
updateProfile: (UserProfile?, MutableMap<String, Int>?) -> Unit,
39-
profile: UserProfile?,
40-
updatePost: (Post, Double, Int, Int, List<String>) -> Unit,
41-
postRatings: MutableMap<String, List<Boolean>>,
4236
highlightedPost: SelectedPostMarker?
4337
) {
4438

@@ -70,14 +64,6 @@ fun MapView(
7064
}
7165
}
7266

73-
LaunchedEffect(profile, posts) {
74-
posts.forEach { post ->
75-
val starsCount = postRatings[post.uid]?.count { it } ?: 0
76-
val avg = if (post.usersNumber == 0) 0.0 else starsCount.toDouble() / post.usersNumber
77-
updatePost(post, avg, starsCount, post.usersNumber, post.ratedBy)
78-
}
79-
}
80-
8167
LaunchedEffect(highlightedPost) {
8268
highlightedPost?.let { post ->
8369
// Zoom to highlighted marker position with zoom level 15f
@@ -110,33 +96,8 @@ fun MapView(
11096
post = it,
11197
username = it.username,
11298
onDismiss = { selectedPost = null },
113-
starStates = postRatings[it.uid] ?: mutableListOf(false, false, false),
114-
onRatingChanged = { newRating ->
115-
val oldPostRatings = postRatings[it.uid] ?: mutableListOf(false, false, false)
116-
val oldStarCounts = oldPostRatings.count { it }
117-
// Directly modify the existing starStates list to avoid creating a new list
118-
postRatings[it.uid] = newRating.toList()
119-
// Update the stars count based on the new rating
120-
val starsCount = newRating.count { it }
121-
// Update user profile with the new rating count
122-
val updatedRatings = profile?.ratings?.toMutableMap()
123-
updatedRatings?.set(it.uid, starsCount)
124-
updateProfile(profile, updatedRatings)
125-
126-
val isReturningUser = it.ratedBy.contains(userEmail)
127-
val newStarsCount =
128-
if (isReturningUser) it.starsCount - oldStarCounts + starsCount
129-
else it.starsCount + starsCount
130-
val newUsersNumber = if (isReturningUser) it.usersNumber else it.usersNumber + 1
131-
val newAvg = newStarsCount.toDouble() / newUsersNumber
132-
val newRatedBy =
133-
if (!isReturningUser) {
134-
it.ratedBy + userEmail
135-
} else {
136-
it.ratedBy
137-
}
138-
updatePost(it, newAvg, newStarsCount, newUsersNumber, newRatedBy)
139-
})
99+
starStates = mutableListOf(false, false, false),
100+
onRatingChanged = {})
140101
}
141102
}
142103
}

app/src/main/java/com/github/lookupgroup27/lookup/ui/image/ImagePreviewDialog.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fun ImagePreviewDialog(
3434
onRatingChanged: (List<Boolean>) -> Unit
3535
) {
3636
if (post != null) {
37+
3738
Dialog(onDismissRequest = onDismiss) {
3839
Surface(
3940
modifier = Modifier.padding(16.dp).testTag("imagePreviewDialog"),
@@ -49,6 +50,7 @@ fun ImagePreviewDialog(
4950
onImageClick = { _, _, _ -> }, // No-op for dialog preview
5051
color = Color.White,
5152
textForUsername = "Posted by: $username",
53+
showStars = false,
5254
showAverage = false,
5355
showAddress = false)
5456
Spacer(modifier = Modifier.height(16.dp))

0 commit comments

Comments
 (0)