Refactor: 优化关注列表和代码逻辑
本次提交主要包含以下更改:
- **关注/粉丝列表逻辑修正**:修复了在 `FollowerListViewModel` 和 `FollowingListViewModel` 中 `followerId` 和 `followingId` 属性赋值错误的问题,确保正确加载关注和粉丝列表。
- **分页大小调整**:将 `BaseFollowModel` 中分页加载的 `pageSize` 从 5 调整为 20,以提高用户体验。
- **个人资料页智能体展示**:
- 在 `AccountProfileV2` 和 `AccountProfileViewModel` 中增加了对用户智能体列表的加载和展示逻辑。
- 修复了 `MyProfileViewModel` 中加载当前用户智能体时 `authorId` 传递错误的问题。
- 优化了 `UserAgentsRow` 和 `UserAgentsViewModel` 的逻辑,确保正确加载和显示用户智能体,并在用户切换时清理旧数据。
- **代码清理**:移除了部分冗余的 `println` 调试信息。
- **Agent创建流程优化**:在 `AddAgentViewModel` 和 `AgentImageCropScreen` 中,确保在已有裁剪图片时直接使用,避免重复裁剪。
This commit is contained in:
@@ -138,10 +138,7 @@ fun AddAgentScreen() {
|
||||
contentScale = ContentScale.Crop,
|
||||
placeholderRes = R.mipmap.rider_pro_agent_avatar
|
||||
)
|
||||
// 调试信息
|
||||
LaunchedEffect(model.croppedBitmap) {
|
||||
println("AddAgent: croppedBitmap changed: ${model.croppedBitmap != null}")
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.size(32.dp)
|
||||
@@ -149,7 +146,6 @@ fun AddAgentScreen() {
|
||||
.background(appColors.main)
|
||||
.align(Alignment.BottomEnd)
|
||||
.noRippleClickable {
|
||||
println("AddAgent: Navigating to AgentImageCrop")
|
||||
navController.navigate(NavigationRoute.AgentImageCrop.route)
|
||||
},
|
||||
contentAlignment = Alignment.Center
|
||||
@@ -226,7 +222,6 @@ fun AddAgentScreen() {
|
||||
if (validationError != null) {
|
||||
// 显示验证错误
|
||||
errorMessage = validationError
|
||||
println("AddAgent: Validation error: $validationError")
|
||||
return@ActionButton
|
||||
}
|
||||
|
||||
@@ -236,16 +231,13 @@ fun AddAgentScreen() {
|
||||
// 调用创建智能体API
|
||||
model.viewModelScope.launch {
|
||||
try {
|
||||
println("AddAgent: Starting to create agent...")
|
||||
val result = model.createAgent(context)
|
||||
if (result != null) {
|
||||
println("AddAgent: Agent created successfully, closing page")
|
||||
// 创建成功,清空数据并关闭页面
|
||||
model.clearData()
|
||||
navController.popBackStack()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("AddAgent: Error creating agent: ${e.message}")
|
||||
// 显示错误信息
|
||||
errorMessage = "创建智能体失败: ${e.message}"
|
||||
e.printStackTrace()
|
||||
|
||||
@@ -26,11 +26,9 @@ object AddAgentViewModel : ViewModel() {
|
||||
var isFromAddAgent by mutableStateOf(false)
|
||||
|
||||
suspend fun updateAgentAvatar(context: Context) {
|
||||
println("AddAgentViewModel: updateAgentAvatar called, croppedBitmap: ${croppedBitmap != null}")
|
||||
croppedBitmap?.let {
|
||||
val file = File(context.cacheDir, "agent_avatar.jpg")
|
||||
it.compress(Bitmap.CompressFormat.JPEG, 100, file.outputStream())
|
||||
println("AddAgentViewModel: Avatar saved to ${file.absolutePath}")
|
||||
// 这里可以上传图片到服务器,暂时先保存到本地
|
||||
// UploadImage(file, "agent_avatar.jpg", "", "jpg")
|
||||
}
|
||||
@@ -39,7 +37,6 @@ object AddAgentViewModel : ViewModel() {
|
||||
suspend fun createAgent(context: Context): AgentEntity? {
|
||||
try {
|
||||
isUpdating = true
|
||||
println("AddAgentViewModel: Creating agent with name: $name, desc: $desc")
|
||||
|
||||
// 准备头像文件
|
||||
val avatarFile = if (croppedBitmap != null) {
|
||||
@@ -57,12 +54,8 @@ object AddAgentViewModel : ViewModel() {
|
||||
avatar = avatarFile
|
||||
)
|
||||
|
||||
println("AddAgentViewModel: Agent created successfully with ID: ${result.id}")
|
||||
|
||||
|
||||
return result
|
||||
} catch (e: Exception) {
|
||||
println("AddAgentViewModel: Error creating agent: ${e.message}")
|
||||
} catch (e: Exception)
|
||||
throw e
|
||||
} finally {
|
||||
isUpdating = false
|
||||
|
||||
@@ -128,7 +128,6 @@ fun AgentImageCropScreen() {
|
||||
modifier = Modifier.clickable {
|
||||
if (croppedBitmap != null) {
|
||||
// 如果已经有裁剪结果,直接返回
|
||||
println("AgentImageCrop: Using existing cropped bitmap")
|
||||
AddAgentViewModel.croppedBitmap = croppedBitmap
|
||||
AddAgentViewModel.viewModelScope.launch {
|
||||
AddAgentViewModel.updateAgentAvatar(context)
|
||||
@@ -139,7 +138,6 @@ fun AgentImageCropScreen() {
|
||||
imageCrop?.let {
|
||||
val bitmap = it.onCrop()
|
||||
croppedBitmap = bitmap
|
||||
println("AgentImageCrop: Cropped bitmap created: ${bitmap != null}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,10 +113,8 @@ fun ImageCropScreen() {
|
||||
modifier = Modifier.clickable {
|
||||
imageCrop?.let {
|
||||
val bitmap = it.onCrop()
|
||||
println("ImageCrop: Cropped bitmap created: ${bitmap != null}")
|
||||
|
||||
// 专门处理个人资料头像
|
||||
println("ImageCrop: Setting bitmap to AccountEditViewModel for user profile")
|
||||
AccountEditViewModel.croppedBitmap = bitmap
|
||||
AccountEditViewModel.viewModelScope.launch {
|
||||
AccountEditViewModel.updateUserProfile(context)
|
||||
|
||||
@@ -36,12 +36,12 @@ open class BaseFollowModel:ViewModel() {
|
||||
isLoading = true
|
||||
viewModelScope.launch {
|
||||
Pager(
|
||||
config = PagingConfig(pageSize = 5, enablePlaceholders = false),
|
||||
config = PagingConfig(pageSize = 20, enablePlaceholders = false),
|
||||
pagingSourceFactory = {
|
||||
AccountPagingSource(
|
||||
userService,
|
||||
followerId = followingId,
|
||||
followingId = followerId
|
||||
followerId = followerId,
|
||||
followingId = followingId
|
||||
)
|
||||
}
|
||||
).flow.cachedIn(viewModelScope).collectLatest {
|
||||
|
||||
@@ -3,5 +3,5 @@ package com.aiosman.ravenow.ui.follower
|
||||
class FollowerListViewModel(
|
||||
val userId: Int
|
||||
) : BaseFollowModel() {
|
||||
override var followingId: Int? = userId
|
||||
override var followerId: Int? = userId
|
||||
}
|
||||
@@ -21,5 +21,5 @@ import kotlinx.coroutines.launch
|
||||
class FollowingListViewModel(
|
||||
val userId: Int
|
||||
) : BaseFollowModel() {
|
||||
override var followerId: Int? = userId
|
||||
override var followingId: Int? = userId
|
||||
}
|
||||
@@ -157,9 +157,7 @@ object AgentChatListViewModel : ViewModel() {
|
||||
}
|
||||
|
||||
agentChatList = result?.conversationList?.map { msg: V2TIMConversation ->
|
||||
val conversation = AgentConversation.convertToAgentConversation(msg, context)
|
||||
println("AgentChatList: Conversation ${conversation.nickname} has ${conversation.unreadCount} unread messages")
|
||||
conversation
|
||||
AgentConversation.convertToAgentConversation(msg, context)
|
||||
} ?: emptyList()
|
||||
}
|
||||
|
||||
|
||||
@@ -157,9 +157,7 @@ object FriendChatListViewModel : ViewModel() {
|
||||
} ?: emptyList()
|
||||
|
||||
friendChatList = filteredConversations.map { msg: V2TIMConversation ->
|
||||
val conversation = FriendConversation.convertToFriendConversation(msg, context)
|
||||
println("FriendChatList: Conversation ${conversation.nickname} has ${conversation.unreadCount} unread messages")
|
||||
conversation
|
||||
FriendConversation.convertToFriendConversation(msg, context)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -178,9 +178,7 @@ object GroupChatListViewModel : ViewModel() {
|
||||
} ?: emptyList()
|
||||
|
||||
groupChatList = filteredConversations.map { msg: V2TIMConversation ->
|
||||
val conversation = GroupConversation.convertToGroupConversation(msg, context)
|
||||
println("GroupChatList: Conversation ${conversation.groupName} has ${conversation.unreadCount} unread messages")
|
||||
conversation
|
||||
GroupConversation.convertToGroupConversation(msg, context)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,8 @@ object MyProfileViewModel : ViewModel() {
|
||||
profile?.let {
|
||||
try {
|
||||
momentLoader.loadData(extra = MomentLoaderExtraArgs(authorId = it.id))
|
||||
agentLoader.loadData(extra = AgentLoaderExtraArgs(authorId = it.id))
|
||||
// MyProfileViewModel 总是加载当前用户的数据,所以传递null以调用getMyAgent()
|
||||
agentLoader.loadData(extra = AgentLoaderExtraArgs(authorId = null))
|
||||
} catch (e: Exception) {
|
||||
Log.e("MyProfileViewModel", "loadProfile: ", e)
|
||||
}
|
||||
|
||||
@@ -48,17 +48,14 @@ fun UserAgentsRow(
|
||||
onAgentClick: (AgentEntity) -> Unit = {}
|
||||
) {
|
||||
val AppColors = LocalAppTheme.current
|
||||
val viewModel: UserAgentsViewModel = viewModel()
|
||||
val viewModel: UserAgentsViewModel = viewModel(key = "UserAgentsViewModel_${userId ?: "self"}")
|
||||
val isSelf = userId == null
|
||||
|
||||
// 加载用户的智能体数据
|
||||
LaunchedEffect(userId) {
|
||||
// 无论userId是否为null都加载数据
|
||||
// null表示加载当前用户自己的智能体
|
||||
println("UserAgentsRow: LaunchedEffect 触发, userId = $userId, isSelf = $isSelf")
|
||||
println("UserAgentsRow: 准备调用 viewModel.loadUserAgents")
|
||||
viewModel.loadUserAgents(userId)
|
||||
println("UserAgentsRow: 已调用 viewModel.loadUserAgents")
|
||||
}
|
||||
|
||||
// 总是显示智能体区域,即使没有数据也显示标题和状态
|
||||
@@ -78,7 +75,6 @@ fun UserAgentsRow(
|
||||
when {
|
||||
viewModel.isLoading -> {
|
||||
// 显示加载状态
|
||||
println("UserAgentsRow: 显示加载状态")
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@@ -94,7 +90,6 @@ fun UserAgentsRow(
|
||||
}
|
||||
viewModel.error != null -> {
|
||||
// 显示错误状态
|
||||
println("UserAgentsRow: 显示错误状态, error = ${viewModel.error}")
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@@ -110,7 +105,6 @@ fun UserAgentsRow(
|
||||
}
|
||||
viewModel.agents.isEmpty() -> {
|
||||
// 显示空状态
|
||||
println("UserAgentsRow: 显示空状态, agents.size = ${viewModel.agents.size}")
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@@ -126,7 +120,6 @@ fun UserAgentsRow(
|
||||
}
|
||||
else -> {
|
||||
// 显示智能体列表
|
||||
println("UserAgentsRow: 显示智能体列表, agents.size = ${viewModel.agents.size}")
|
||||
LazyRow(
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp),
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
|
||||
@@ -28,16 +28,15 @@ class UserAgentsViewModel : ViewModel() {
|
||||
|
||||
fun loadUserAgents(userId: Int?) {
|
||||
viewModelScope.launch {
|
||||
// 先清理之前的数据
|
||||
clearAgents()
|
||||
isLoading = true
|
||||
error = null
|
||||
|
||||
try {
|
||||
println("UserAgentsViewModel: 开始加载智能体数据, userId = $userId")
|
||||
agentLoader.loadData(AgentLoaderExtraArgs(authorId = userId))
|
||||
println("UserAgentsViewModel: 加载完成, agents.size = ${agents.size}")
|
||||
} catch (e: Exception) {
|
||||
error = e.message ?: "加载失败"
|
||||
println("UserAgentsViewModel: 加载失败, error = $error")
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
isLoading = false
|
||||
|
||||
@@ -27,6 +27,7 @@ fun AccountProfileV2(id: String){
|
||||
}
|
||||
ProfileV3(
|
||||
moments = model.moments,
|
||||
agents = model.agents,
|
||||
profile = model.profile,
|
||||
isSelf = isSelf,
|
||||
postCount = model.momentLoader.total,
|
||||
@@ -53,6 +54,9 @@ fun AccountProfileV2(id: String){
|
||||
model.followUser(id)
|
||||
}
|
||||
}
|
||||
},
|
||||
onAgentClick = { agent ->
|
||||
// TODO: 处理Agent点击事件,导航到聊天页面
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -10,6 +10,9 @@ import com.aiosman.ravenow.data.AccountService
|
||||
import com.aiosman.ravenow.data.AccountServiceImpl
|
||||
import com.aiosman.ravenow.data.UserServiceImpl
|
||||
import com.aiosman.ravenow.entity.AccountProfileEntity
|
||||
import com.aiosman.ravenow.entity.AgentEntity
|
||||
import com.aiosman.ravenow.entity.AgentLoader
|
||||
import com.aiosman.ravenow.entity.AgentLoaderExtraArgs
|
||||
import com.aiosman.ravenow.entity.MomentEntity
|
||||
import com.aiosman.ravenow.entity.MomentLoader
|
||||
import com.aiosman.ravenow.entity.MomentLoaderExtraArgs
|
||||
@@ -32,6 +35,13 @@ class AccountProfileViewModel : ViewModel() {
|
||||
}
|
||||
}
|
||||
var moments by mutableStateOf<List<MomentEntity>>(listOf())
|
||||
|
||||
var agentLoader = AgentLoader().apply {
|
||||
onListChanged = {
|
||||
agents = it
|
||||
}
|
||||
}
|
||||
var agents by mutableStateOf<List<AgentEntity>>(listOf())
|
||||
init {
|
||||
EventBus.getDefault().register(this)
|
||||
}
|
||||
@@ -54,6 +64,12 @@ class AccountProfileViewModel : ViewModel() {
|
||||
profile?.let {
|
||||
try {
|
||||
momentLoader.loadData(MomentLoaderExtraArgs(authorId = it.id))
|
||||
// 根据是否是当前用户来决定传递authorId
|
||||
// 如果是当前用户,传递null以调用getMyAgent()
|
||||
// 如果是其他用户,传递用户ID以调用getAgent(authorId)
|
||||
val isSelf = it.id.toString() == com.aiosman.ravenow.AppState.UserId.toString()
|
||||
val authorId = if (isSelf) null else it.id
|
||||
agentLoader.loadData(AgentLoaderExtraArgs(authorId = authorId))
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user