Skip to content

Commit

Permalink
Merge pull request #506 from Shreyassp002/Fix-FollowButton-Compatibilty
Browse files Browse the repository at this point in the history
Fix: FollowButton state management and minor ui bugs
  • Loading branch information
07jasjeet authored Dec 27, 2024
2 parents 63f5aba + 63c6048 commit 6ec7c05
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ fun BaseProfileScreen(
modifier = Modifier,
isFollowedState = uiState.listensTabUiState.isFollowing,
buttonColor = lb_purple,
followedStateTextColor = new_app_bg_light,
unfollowedStateTextColor = ListenBrainzTheme.colorScheme.text,
followedStateTextColor = ListenBrainzTheme.colorScheme.text,
unfollowedStateTextColor = new_app_bg_light,
onClick = {
if (uiState.listensTabUiState.isFollowing) {
onUnfollowClick(username ?: "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import org.listenbrainz.android.model.TrackMetadata
import org.listenbrainz.android.model.feed.ReviewEntityType
import org.listenbrainz.android.model.user.Artist
import org.listenbrainz.android.ui.components.ErrorBar
import org.listenbrainz.android.ui.components.FollowButton
import org.listenbrainz.android.ui.components.ListenCardSmallDefault
import org.listenbrainz.android.ui.components.SimilarUserCard
import org.listenbrainz.android.ui.components.SuccessBar
Expand All @@ -84,6 +85,7 @@ import org.listenbrainz.android.ui.theme.app_bg_mid
import org.listenbrainz.android.ui.theme.compatibilityMeterColor
import org.listenbrainz.android.ui.theme.lb_purple
import org.listenbrainz.android.ui.theme.lb_purple_night
import org.listenbrainz.android.ui.theme.new_app_bg_light
import org.listenbrainz.android.util.Utils.getCoverArtUrl
import org.listenbrainz.android.viewmodel.FeedViewModel
import org.listenbrainz.android.viewmodel.ListensViewModel
Expand Down Expand Up @@ -389,7 +391,7 @@ private fun BuildSimilarArtists(similarArtists: List<Artist>, onArtistClick: (St
similarArtists.size > 5 -> {
val topSimilarArtists = similarArtists.take(5)
val text = buildAnnotatedString {
withStyle(style = SpanStyle(color = white)) {
withStyle(style = SpanStyle(color = lb_purple_night)) {
append("You both listen to ")
}
topSimilarArtists.forEachIndexed { index, artist ->
Expand All @@ -401,10 +403,13 @@ private fun BuildSimilarArtists(similarArtists: List<Artist>, onArtistClick: (St
}
pop()
if (index < topSimilarArtists.size - 1) {
append(", ")
withStyle(style = SpanStyle(color = lb_purple_night)) {
append(", ")
}

}
}
withStyle(style = SpanStyle(color = white)) {
withStyle(style = SpanStyle(color = lb_purple_night)) {
append(" and more.")
}
}
Expand Down Expand Up @@ -734,29 +739,19 @@ private fun FollowCard(username: String?, onFollowButtonClick: (String?, Boolean
goToUserPage(username)
}
)
TextButton(
onClick = {
onFollowButtonClick(username, followStatus)
}, colors = ButtonDefaults.buttonColors(
containerColor = when (followStatus) {
true -> ListenBrainzTheme.colorScheme.followingButtonColor
false -> lb_purple
}
), modifier = Modifier
.width(90.dp)
.height(40.dp), shape = RoundedCornerShape(10.dp),
border = ListenBrainzTheme.colorScheme.followingButtonBorder
) {
Text(
when (followStatus) {
true -> "Following"
false -> "Follow"
}, color = when(followStatus){
true -> ListenBrainzTheme.colorScheme.followerCardTextColor
false -> Color.White
Box(){
FollowButton(
modifier = Modifier,
isFollowedState = followStatus,
buttonColor = lb_purple,
followedStateTextColor = ListenBrainzTheme.colorScheme.lbSignature,
unfollowedStateTextColor = new_app_bg_light,
onClick = {
onFollowButtonClick(username, followStatus)
}
)
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,42 @@ class UserViewModel @Inject constructor(
return similarArtists.distinct()
}

fun followUser(username: String?){
if(username.isNullOrEmpty()) return
viewModelScope.launch (ioDispatcher) {
listenStateFlow.value = listenStateFlow.value.copy(isFollowing = true)
fun followUser(username: String?) {
if (username.isNullOrEmpty()) return
updateFollowState(username, true)

viewModelScope.launch(ioDispatcher) {
val result = socialRepository.followUser(username)
if (result.status == Resource.Status.FAILED) {
listenStateFlow.value = listenStateFlow.value.copy(isFollowing = false)
updateFollowState(username, false)
}
}
}

fun unfollowUser(username: String?){
if(username.isNullOrEmpty()) return
fun unfollowUser(username: String?) {
if (username.isNullOrEmpty()) return
updateFollowState(username, false)

viewModelScope.launch(ioDispatcher) {
listenStateFlow.value = listenStateFlow.value.copy(isFollowing = false)
val result = socialRepository.unfollowUser(username)
if (result.status == Resource.Status.FAILED) {
listenStateFlow.value = listenStateFlow.value.copy(isFollowing = true)
updateFollowState(username, true)
}
}
}

private fun updateFollowState(username: String, isFollowing: Boolean) {
val updatedFollowers = listenStateFlow.value.followers?.map { (user, status) ->
if (user == username) user to isFollowing else user to status
}
val updatedFollowing = listenStateFlow.value.following?.map { (user, status) ->
if (user == username) user to isFollowing else user to status
}
listenStateFlow.value = listenStateFlow.value.copy(
followers = updatedFollowers,
following = updatedFollowing,
isFollowing = isFollowing
)
}

suspend fun getUserDataFromRemote(
Expand Down

0 comments on commit 6ec7c05

Please sign in to comment.