Skip to content

Commit 3b28c93

Browse files
committed
refactor(friend): 重构好友相关功能
- 将 FriendListPagerModel 和 FriendLocationPagerModel 中的 friendsApi 调用替换为 friendService - 更新 UserProfileScreenModel 中的好友操作逻辑,使用 friendService 替代 friendsApi- 修改 FriendService 中的 refreshFriendList 方法,支持离线模式和回调函数 - 优化好友列表获取逻辑,提高数据同步效率 Signed-off-by: KAMOsama <kamosama.dev@gmail.com>
1 parent 3465300 commit 3b28c93

File tree

4 files changed

+30
-39
lines changed

4 files changed

+30
-39
lines changed

composeApp/src/commonMain/kotlin/presentation/screens/home/pager/FriendListPagerModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class FriendListPagerModel(
6060
private val _worldGroupOptions = MutableStateFlow(WorldGroupOptions())
6161
var worldGroupOptions = _worldGroupOptions.asStateFlow()
6262

63-
private val _friendList = MutableStateFlow(emptyList<FriendData>())
63+
private val _friendList = MutableStateFlow(friendService.friendMap.values.toList().sortedUserByStatus())
6464
val friendList: StateFlow<List<FriendData>> = _friendList.asStateFlow()
6565

6666
private val _friendTotal = MutableStateFlow(0)

composeApp/src/commonMain/kotlin/presentation/screens/home/pager/FriendLocationPagerModel.kt

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import cafe.adriel.voyager.core.model.screenModelScope
66
import io.github.vrcmteam.vrcm.core.shared.SharedFlowCentre
77
import io.github.vrcmteam.vrcm.network.api.attributes.BlueprintType
88
import io.github.vrcmteam.vrcm.network.api.attributes.LocationType
9-
import io.github.vrcmteam.vrcm.network.api.friends.FriendsApi
109
import io.github.vrcmteam.vrcm.network.api.friends.date.FriendData
1110
import io.github.vrcmteam.vrcm.network.api.groups.GroupsApi
1211
import io.github.vrcmteam.vrcm.network.api.instances.InstancesApi
1312
import io.github.vrcmteam.vrcm.network.api.instances.data.InstanceData
1413
import io.github.vrcmteam.vrcm.network.api.users.UsersApi
15-
import io.github.vrcmteam.vrcm.network.supports.VRCApiException
1614
import io.github.vrcmteam.vrcm.network.websocket.data.WebSocketEvent
1715
import io.github.vrcmteam.vrcm.network.websocket.data.content.FriendActiveContent
1816
import io.github.vrcmteam.vrcm.network.websocket.data.content.FriendLocationContent
@@ -23,17 +21,16 @@ import io.github.vrcmteam.vrcm.presentation.extensions.onApiFailure
2321
import io.github.vrcmteam.vrcm.presentation.screens.home.data.FriendLocation
2422
import io.github.vrcmteam.vrcm.presentation.screens.home.data.HomeInstanceVo
2523
import io.github.vrcmteam.vrcm.service.AuthService
24+
import io.github.vrcmteam.vrcm.service.FriendService
2625
import kotlinx.coroutines.Dispatchers
2726
import kotlinx.coroutines.IO
28-
import kotlinx.coroutines.flow.catch
29-
import kotlinx.coroutines.flow.retry
3027
import kotlinx.coroutines.launch
3128
import kotlinx.coroutines.sync.Mutex
3229
import kotlinx.coroutines.sync.withLock
3330
import kotlinx.serialization.json.Json
3431

3532
class FriendLocationPagerModel(
36-
private val friendsApi: FriendsApi,
33+
private val friendService: FriendService,
3734
private val usersApi: UsersApi,
3835
private val groupsApi: GroupsApi,
3936
private val instancesApi: InstancesApi,
@@ -120,18 +117,12 @@ class FriendLocationPagerModel(
120117
// 防止再次更新时拉取到的与上次相同的instanceId导致item的key冲突
121118
val includedIdList: MutableList<String> = mutableListOf()
122119
screenModelScope.launch(Dispatchers.IO) {
123-
friendsApi.friendsFlow()
124-
.retry(1) {
125-
// 如果是登录失效了就会重新登录并重试一次
126-
if (it is VRCApiException) authService.doReTryAuth() else false
127-
}.catch {
128-
SharedFlowCentre.toastText.emit(ToastText.Error(it.message.toString()))
129-
}.collect { friends ->
130-
update(friends)
131-
if (removeNotIncluded) {
132-
includedIdList.addAll(friends.map { it.id })
133-
}
120+
friendService.refreshFriendList(offline = false) { friends ->
121+
update(friends)
122+
if (removeNotIncluded) {
123+
includedIdList.addAll(friends.map { it.id })
134124
}
125+
}
135126
}.join()
136127
if (removeNotIncluded) {
137128
friendMap.keys

composeApp/src/commonMain/kotlin/presentation/screens/user/UserProfileScreenModel.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import cafe.adriel.voyager.core.model.screenModelScope
77
import io.github.vrcmteam.vrcm.core.extensions.pretty
88
import io.github.vrcmteam.vrcm.core.shared.SharedFlowCentre
99
import io.github.vrcmteam.vrcm.network.api.attributes.NotificationType
10-
import io.github.vrcmteam.vrcm.network.api.friends.FriendsApi
1110
import io.github.vrcmteam.vrcm.network.api.notification.NotificationApi
1211
import io.github.vrcmteam.vrcm.network.api.users.UsersApi
1312
import io.github.vrcmteam.vrcm.network.api.users.data.UserData
1413
import io.github.vrcmteam.vrcm.presentation.compoments.ToastText
1514
import io.github.vrcmteam.vrcm.presentation.screens.user.data.UserProfileVo
1615
import io.github.vrcmteam.vrcm.service.AuthService
16+
import io.github.vrcmteam.vrcm.service.FriendService
1717
import io.ktor.client.call.*
1818
import io.ktor.client.statement.*
1919
import kotlinx.coroutines.Dispatchers
@@ -25,7 +25,7 @@ import org.koin.core.logger.Logger
2525
class UserProfileScreenModel(
2626
private val authService: AuthService,
2727
private val usersApi: UsersApi,
28-
private val friendsApi: FriendsApi,
28+
private val friendService: FriendService,
2929
private val notificationApi: NotificationApi,
3030
private val logger: Logger,
3131
) : ScreenModel {
@@ -58,38 +58,38 @@ class UserProfileScreenModel(
5858

5959
suspend fun sendFriendRequest(userId: String, message: String): Boolean =
6060
friendAction(message) {
61-
friendsApi.sendFriendRequest(userId)
61+
friendService.sendFriendRequest(userId)
6262
}
6363

6464
suspend fun deleteFriendRequest(userId: String, message: String): Boolean = friendAction(message) {
65-
friendsApi.deleteFriendRequest(userId)
65+
friendService.deleteFriendRequest(userId)
6666
}
6767

6868
suspend fun unfriend(userId: String, message: String): Boolean = friendAction(message) {
69-
friendsApi.unfriend(userId)
69+
friendService.unfriend(userId)
7070
}
7171

7272
suspend fun acceptFriendRequest(userId: String, message: String) = friendAction(message) {
7373
// 看看要不要加载大于 100 条的通知
7474
// 先看没有hidden的, 如果没有再看hidden的 TODO:是不是要单独抽成一个独立方法
75-
return@friendAction (notificationApi.fetchNotificationsV2(
76-
type = NotificationType.FriendRequest.value,
77-
).firstOrNull { it.senderUserId == userId }
78-
?: notificationApi.fetchNotificationsV2(
75+
return@friendAction authService.reTryAuthCatching {
76+
(notificationApi.fetchNotificationsV2(
7977
type = NotificationType.FriendRequest.value,
80-
hidden = true
81-
).firstOrNull { it.senderUserId == userId })
82-
?.let {
83-
notificationApi.acceptFriendRequest(it.id).isSuccess
84-
} ?: error("Not found notification")
85-
78+
).firstOrNull { it.senderUserId == userId }
79+
?: notificationApi.fetchNotificationsV2(
80+
type = NotificationType.FriendRequest.value,
81+
hidden = true
82+
).firstOrNull { it.senderUserId == userId })
83+
?.let {
84+
notificationApi.acceptFriendRequest(it.id).isSuccess
85+
} ?: error("Not found notification")
86+
}
8687
}
8788

88-
private suspend fun friendAction(message: String, action: suspend () -> Any): Boolean =
89+
private suspend fun friendAction(message: String, action: suspend () -> Result<*>): Boolean =
8990
screenModelScope.async(Dispatchers.IO) {
90-
authService.reTryAuthCatching {
91-
action()
92-
}.onFailure {
91+
action()
92+
.onFailure {
9393
handleError(it)
9494
}.onSuccess {
9595
SharedFlowCentre.toastText.emit(ToastText.Success(message))

composeApp/src/commonMain/kotlin/service/FriendService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ class FriendService(
5555
* 刷新好友列表
5656
* @return 是否成功获取到数据
5757
*/
58-
suspend fun refreshFriendList(onUpdater: () -> Unit = {}) {
58+
suspend fun refreshFriendList(offline: Boolean? = null, onUpdater: (List<FriendData>) -> Unit = {}) {
5959

6060
try {
61-
val count = friendsApi.allFriendsFlow()
61+
val count = (offline?.run(friendsApi::friendsFlow) ?: friendsApi.allFriendsFlow())
6262
.retry(1) {
6363
// 如果是登录失效了就会重新登录并重试一次
6464
if (it is VRCApiException) authService.doReTryAuth() else false
@@ -68,7 +68,7 @@ class FriendService(
6868
}
6969
.onEach { friends ->
7070
updateFriendMap(friends)
71-
onUpdater()
71+
onUpdater(friends)
7272
}
7373
.count()
7474

0 commit comments

Comments
 (0)