Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: FollowButton state management and minor ui bugs #506

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.text,
07jasjeet marked this conversation as resolved.
Show resolved Hide resolved
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
Loading