|
|
@@ -43,15 +43,14 @@ class FollowViewModel : BaseViewModel(), IFollowViewModel {
|
|
|
override fun isFollow(uid: String): LiveData<Rlt<FollowStatus>> {
|
|
|
val liveData = OnceMutableLiveData<Rlt<FollowStatus>>()
|
|
|
viewModelScope.launch {
|
|
|
- val rlt = followHttpService.isFollow(FollowStatusReq(listOf(uid)))
|
|
|
+ val rlt = suspendIsFollow(listOf(uid))
|
|
|
when (rlt) {
|
|
|
is Rlt.Failed -> {
|
|
|
liveData.send(rlt)
|
|
|
}
|
|
|
|
|
|
is Rlt.Success -> {
|
|
|
- val followStatus =
|
|
|
- FollowStatus.map(rlt.data.data?.list?.find { it.uid == uid }?.followStatus)
|
|
|
+ val followStatus = rlt.data[uid] ?: FollowStatus.NONE
|
|
|
followStatusMap[uid] = followStatus
|
|
|
isFollowLD.send(followStatusMap)
|
|
|
liveData.send(Rlt.Success(followStatus))
|
|
|
@@ -65,16 +64,14 @@ class FollowViewModel : BaseViewModel(), IFollowViewModel {
|
|
|
override fun isFollow(uids: List<String>): LiveData<Rlt<Map<String, FollowStatus>>> {
|
|
|
val liveData = OnceMutableLiveData<Rlt<Map<String, FollowStatus>>>()
|
|
|
viewModelScope.launch {
|
|
|
- val rlt = followHttpService.isFollow(FollowStatusReq(uids.distinct()))
|
|
|
+ val rlt = suspendIsFollow(uids)
|
|
|
when (rlt) {
|
|
|
is Rlt.Failed -> {
|
|
|
liveData.send(rlt)
|
|
|
}
|
|
|
|
|
|
is Rlt.Success -> {
|
|
|
- rlt.data.data?.list?.forEach {
|
|
|
- followStatusMap[it.uid] = FollowStatus.map(it.followStatus)
|
|
|
- }
|
|
|
+ followStatusMap.putAll(rlt.data)
|
|
|
isFollowLD.send(followStatusMap)
|
|
|
liveData.send(Rlt.Success(followStatusMap))
|
|
|
}
|
|
|
@@ -83,7 +80,24 @@ class FollowViewModel : BaseViewModel(), IFollowViewModel {
|
|
|
return liveData
|
|
|
}
|
|
|
|
|
|
- override fun follow(uid: String): LiveData<Rlt<FollowStatus>> {
|
|
|
+ private suspend fun suspendIsFollow(uids: List<String>): Rlt<Map<String, FollowStatus>> {
|
|
|
+ val rlt = followHttpService.isFollow(FollowStatusReq(uids.distinct()))
|
|
|
+ when (rlt) {
|
|
|
+ is Rlt.Failed -> {
|
|
|
+ return rlt
|
|
|
+ }
|
|
|
+
|
|
|
+ is Rlt.Success -> {
|
|
|
+ val followMap = mutableMapOf<String, FollowStatus>()
|
|
|
+ rlt.data.data?.list?.forEach {
|
|
|
+ followMap[it.uid] = FollowStatus.map(it.followStatus)
|
|
|
+ }
|
|
|
+ return Rlt.Success(followMap)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun follow(uid: String, checkEachFollow: Boolean): LiveData<Rlt<FollowStatus>> {
|
|
|
val liveData = OnceMutableLiveData<Rlt<FollowStatus>>()
|
|
|
viewModelScope.launch {
|
|
|
val rlt = followHttpService.follow(FollowReq(uid, true))
|
|
|
@@ -94,15 +108,31 @@ class FollowViewModel : BaseViewModel(), IFollowViewModel {
|
|
|
|
|
|
is Rlt.Success -> {
|
|
|
showToast(APP_R.string.common_follow_success)
|
|
|
- val nextStatus = onFollowStatusChanged(uid, true)
|
|
|
- liveData.send(Rlt.Success(nextStatus))
|
|
|
+ val finalStatus: FollowStatus = if (checkEachFollow) {
|
|
|
+ //检查互关状态
|
|
|
+ val rlt = suspendIsFollow(listOf(uid))
|
|
|
+ when (rlt) {
|
|
|
+ is Rlt.Failed -> {
|
|
|
+ FollowStatus.FOLLOW
|
|
|
+ }
|
|
|
+
|
|
|
+ is Rlt.Success -> {
|
|
|
+ rlt.data[uid] ?: FollowStatus.FOLLOW
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FollowStatus.FOLLOW
|
|
|
+ }
|
|
|
+ followStatusMap[uid] = finalStatus
|
|
|
+ isFollowLD.send(followStatusMap)
|
|
|
+ liveData.send(Rlt.Success(finalStatus))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return liveData
|
|
|
}
|
|
|
|
|
|
- override fun unFollow(uid: String): LiveData<Rlt<FollowStatus>> {
|
|
|
+ override fun unFollow(uid: String, checkEachFollow: Boolean): LiveData<Rlt<FollowStatus>> {
|
|
|
val liveData = OnceMutableLiveData<Rlt<FollowStatus>>()
|
|
|
viewModelScope.launch {
|
|
|
val activity = AppUtil.currentActivity as? FragmentActivity ?: return@launch
|
|
|
@@ -119,8 +149,24 @@ class FollowViewModel : BaseViewModel(), IFollowViewModel {
|
|
|
}
|
|
|
|
|
|
is Rlt.Success -> {
|
|
|
- val nextStatus = onFollowStatusChanged(uid, false)
|
|
|
- liveData.send(Rlt.Success(nextStatus))
|
|
|
+ val finalStatus: FollowStatus = if (checkEachFollow) {
|
|
|
+ //检查互关状态
|
|
|
+ val rlt = suspendIsFollow(listOf(uid))
|
|
|
+ when (rlt) {
|
|
|
+ is Rlt.Failed -> {
|
|
|
+ FollowStatus.NONE
|
|
|
+ }
|
|
|
+
|
|
|
+ is Rlt.Success -> {
|
|
|
+ rlt.data[uid] ?: FollowStatus.NONE
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ FollowStatus.NONE
|
|
|
+ }
|
|
|
+ followStatusMap[uid] = finalStatus
|
|
|
+ isFollowLD.send(followStatusMap)
|
|
|
+ liveData.send(Rlt.Success(finalStatus))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -186,47 +232,6 @@ class FollowViewModel : BaseViewModel(), IFollowViewModel {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private fun onFollowStatusChanged(uid: String, follow: Boolean): FollowStatus {
|
|
|
- val currentStatus = followStatusMap[uid]
|
|
|
- val nextStatus = when (currentStatus) {
|
|
|
- FollowStatus.NONE, null -> {
|
|
|
- if (follow) {
|
|
|
- FollowStatus.FOLLOW
|
|
|
- } else {
|
|
|
- FollowStatus.NONE
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- FollowStatus.EACH_FOLLOW -> {
|
|
|
- if (follow) {
|
|
|
- FollowStatus.EACH_FOLLOW
|
|
|
- } else {
|
|
|
- FollowStatus.FANS
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- FollowStatus.FANS -> {
|
|
|
- if (follow) {
|
|
|
- FollowStatus.EACH_FOLLOW
|
|
|
- } else {
|
|
|
- FollowStatus.FANS
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- FollowStatus.FOLLOW -> {
|
|
|
- if (follow) {
|
|
|
- FollowStatus.FOLLOW
|
|
|
- } else {
|
|
|
- FollowStatus.NONE
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- followStatusMap[uid] = nextStatus
|
|
|
- isFollowLD.send(followStatusMap)
|
|
|
- return nextStatus
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
private val followList = mutableListOf<FollowItemData>()
|
|
|
private val followUids = mutableSetOf<String>()
|
|
|
private val followPageHandler = PageHandler()
|