优化会话列表的数据加载和合并逻辑

- 在 `AndroidManifest.xml` 中添加 `com.android.vending.BILLING` 权限。
- 重构 `AllChatListScreen.kt` 的数据加载机制,通过 `LaunchedEffect` 监听各个 `ViewModel` 的列表变化,并自动合并数据。
- 将原有的数据刷新和合并逻辑拆分为 `refreshAllData` 和 `combineAllData` 两个函数,提高了代码的清晰度和复用性。
- 优化了下拉刷新和初次加载的逻辑,确保在所有数据源加载完成后才更新UI状态,提升了用户体验。
This commit is contained in:
2025-11-14 16:47:15 +08:00
parent 8901792561
commit b12f359da1
2 changed files with 55 additions and 6 deletions

View File

@@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-feature android:name="android.hardware.camera.any" android:required="false" />
<application

View File

@@ -21,6 +21,9 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import kotlinx.coroutines.delay
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@@ -127,8 +130,8 @@ fun AllChatListScreen() {
refreshing = true
refreshAllData(context,
onSuccess = { conversations ->
allConversations = conversations
refreshing = false
// 数据合并会在 LaunchedEffect 中自动完成
// refreshing 状态会在 LaunchedEffect 中更新
},
onError = { errorMsg ->
error = errorMsg
@@ -138,12 +141,44 @@ fun AllChatListScreen() {
}
)
// 监听各个 ViewModel 的列表变化,自动合并数据
LaunchedEffect(
AgentChatListViewModel.agentChatList,
GroupChatListViewModel.groupChatList,
FriendChatListViewModel.friendChatList,
AgentChatListViewModel.isLoading,
GroupChatListViewModel.isLoading,
FriendChatListViewModel.isLoading
) {
// 只有当所有 ViewModel 都完成加载后,才合并数据
val allLoaded = !AgentChatListViewModel.isLoading &&
!GroupChatListViewModel.isLoading &&
!FriendChatListViewModel.isLoading
// 如果所有 ViewModel 都已完成加载,合并数据
// 或者如果至少有一个列表有数据,也合并(处理部分数据已加载的情况)
val hasAnyData = AgentChatListViewModel.agentChatList.isNotEmpty() ||
GroupChatListViewModel.groupChatList.isNotEmpty() ||
FriendChatListViewModel.friendChatList.isNotEmpty()
if (allLoaded || hasAnyData) {
combineAllData(onSuccess = { conversations ->
allConversations = conversations
if (allLoaded) {
isLoading = false
refreshing = false
}
})
}
}
// 初始化时触发数据加载
LaunchedEffect(Unit) {
isLoading = true
refreshAllData(context,
onSuccess = { conversations ->
allConversations = conversations
isLoading = false
// 数据合并会在 LaunchedEffect 中自动完成
// 这里只处理错误情况
},
onError = { errorMsg ->
error = errorMsg
@@ -369,11 +404,24 @@ fun refreshAllData(
onError: (String) -> Unit
) {
try {
// 同时刷新所有类型的数据
// 同时刷新所有类型的数据(异步操作)
AgentChatListViewModel.refreshPager(context = context)
GroupChatListViewModel.refreshPager(context = context)
FriendChatListViewModel.refreshPager(context = context)
// 注意:由于 refreshPager 是异步的,数据合并会在 LaunchedEffect 中通过监听列表变化来完成
// 这里先立即合并一次当前已有的数据(如果有的话)
combineAllData(onSuccess)
} catch (e: Exception) {
onError("刷新数据失败: ${e.message}")
}
}
// 合并所有类型的数据
fun combineAllData(
onSuccess: (List<CombinedConversation>) -> Unit
) {
try {
val combinedList = mutableListOf<CombinedConversation>()
AgentChatListViewModel.agentChatList.forEach { agent ->
@@ -401,6 +449,6 @@ fun refreshAllData(
onSuccess(sortedList)
} catch (e: Exception) {
onError("刷新数据失败: ${e.message}")
// 静默失败,让 LaunchedEffect 继续监听变化
}
}