更新关注逻辑

This commit is contained in:
2024-12-01 15:13:47 +08:00
parent 79fccda1aa
commit c54d5c914a
14 changed files with 199 additions and 156 deletions

View File

@@ -12,7 +12,10 @@ import com.aiosman.ravenow.entity.AccountProfileEntity
import com.aiosman.ravenow.entity.MomentEntity
import com.aiosman.ravenow.entity.MomentLoader
import com.aiosman.ravenow.entity.MomentLoaderExtraArgs
import com.aiosman.ravenow.event.FollowChangeEvent
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
class AccountProfileViewModel : ViewModel() {
@@ -27,6 +30,9 @@ class AccountProfileViewModel : ViewModel() {
}
}
var moments by mutableStateOf<List<MomentEntity>>(listOf())
init {
EventBus.getDefault().register(this)
}
fun loadProfile(id: String, pullRefresh: Boolean = false) {
viewModelScope.launch {
@@ -47,19 +53,23 @@ class AccountProfileViewModel : ViewModel() {
}
}
}
@Subscribe
fun onFollowChangeEvent(event: FollowChangeEvent) {
if (event.userId == profile?.id) {
profile = profile?.copy(followerCount = profile!!.followerCount + if (event.isFollow) 1 else -1, isFollowing = event.isFollow)
}
}
fun followUser(userId: String) {
viewModelScope.launch {
userService.followUser(userId)
profile = profile?.copy(followerCount = profile!!.followerCount + 1, isFollowing = true)
EventBus.getDefault().post(FollowChangeEvent(userId.toInt(), true))
}
}
fun unFollowUser(userId: String) {
viewModelScope.launch {
userService.unFollowUser(userId)
profile =
profile?.copy(followerCount = profile!!.followerCount - 1, isFollowing = false)
EventBus.getDefault().post(FollowChangeEvent(userId.toInt(), false))
}
}
@@ -67,4 +77,9 @@ class AccountProfileViewModel : ViewModel() {
val nickName get() = profile?.nickName ?: ""
val avatar get() = profile?.avatar
override fun onCleared() {
super.onCleared()
EventBus.getDefault().unregister(this)
}
}