From 00824ff7b4a4ebbbe7c42bcb7004d5bd11df045c Mon Sep 17 00:00:00 2001 From: AllenTom Date: Sun, 31 Aug 2025 23:04:52 +0800 Subject: [PATCH] =?UTF-8?q?Refactor:=20=E4=BC=98=E5=8C=96=E4=B8=AA?= =?UTF-8?q?=E4=BA=BA=E4=B8=BB=E9=A1=B5=E5=92=8C=E8=B4=A6=E6=88=B7=E4=B8=BB?= =?UTF-8?q?=E9=A1=B5=E5=B8=96=E5=AD=90=E5=8A=A0=E8=BD=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 统一MyProfileViewModel和AccountProfileViewModel中的帖子加载逻辑,使用一致的pageSize。 - 在ProfileWrap和AccountProfileV2中传递正确的postCount。 - 在ProfileV3中改进了加载更多帖子的触发条件,确保在有更多数据时才触发加载。 - 修复了注册页面勾选协议和促销选项后,错误状态未清除的问题。 - 为DataLoader和ProfileV3中的滚动加载逻辑添加了详细日志,方便调试。 --- .../java/com/aiosman/ravenow/entity/Loader.kt | 9 +++++++ .../index/tabs/profile/MyProfileViewModel.kt | 11 +++++++- .../ui/index/tabs/profile/ProfileV3.kt | 27 ++++++++++++++++--- .../ui/index/tabs/profile/ProfileWrap.kt | 3 +++ .../aiosman/ravenow/ui/login/emailsignup.kt | 8 ++++++ .../ravenow/ui/profile/AccountProfileV2.kt | 13 +++++++++ .../ui/profile/AccountProfileViewModel.kt | 17 ++++++++++++ 7 files changed, 84 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/aiosman/ravenow/entity/Loader.kt b/app/src/main/java/com/aiosman/ravenow/entity/Loader.kt index 952dda0..35784e2 100644 --- a/app/src/main/java/com/aiosman/ravenow/entity/Loader.kt +++ b/app/src/main/java/com/aiosman/ravenow/entity/Loader.kt @@ -1,5 +1,6 @@ package com.aiosman.ravenow.entity +import android.util.Log import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue @@ -22,30 +23,38 @@ abstract class DataLoader { suspend fun loadData( extra: ET ) { + Log.d("DataLoader", "loadData开始 - firstLoad: $firstLoad") if (!firstLoad) { + Log.d("DataLoader", "loadData跳过 - 非首次加载") return } firstLoad = false + Log.d("DataLoader", "调用fetchData - page: $page, pageSize: $pageSize") val result = fetchData(page, pageSize, extra) list = result.list.toMutableList() this.page = page this.total = result.total this.pageSize = pageSize this.hasNext = result.list.size == pageSize + Log.d("DataLoader", "loadData完成 - 数据量: ${list.size}, total: $total, hasNext: $hasNext") onListChanged?.invoke(list) } suspend fun loadMore(extra: ET) { if (firstLoad) { + Log.d("DataLoader", "loadMore跳过 - firstLoad为true") return } if (!hasNext) { + Log.d("DataLoader", "loadMore跳过 - hasNext为false") return } + Log.d("DataLoader", "开始loadMore - 当前页: $page, 当前数据量: ${list.size}") val result = fetchData(page + 1, pageSize, extra) list.addAll(result.list) page += 1 hasNext = result.list.size == pageSize + Log.d("DataLoader", "loadMore完成 - 新页: $page, 新数据量: ${list.size}, 本次获取: ${result.list.size}, hasNext: $hasNext") onListChanged?.invoke(list) } diff --git a/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/MyProfileViewModel.kt b/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/MyProfileViewModel.kt index 3a12eb2..912ee99 100644 --- a/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/MyProfileViewModel.kt +++ b/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/MyProfileViewModel.kt @@ -40,6 +40,7 @@ object MyProfileViewModel : ViewModel() { var moments by mutableStateOf>(emptyList()) var agents by mutableStateOf>(emptyList()) val momentLoader: MomentLoader = MomentLoader().apply { + pageSize = 20 // 设置与后端一致的页面大小 onListChanged = { moments = it } @@ -84,7 +85,15 @@ object MyProfileViewModel : ViewModel() { fun loadMoreMoment() { viewModelScope.launch { - momentLoader.loadMore(extra = MomentLoaderExtraArgs(authorId = profile?.id)) + profile?.let { profileData -> + try { + Log.d("MyProfileViewModel", "loadMoreMoment: 开始加载更多, 当前moments数量: ${moments.size}, hasNext: ${momentLoader.hasNext}") + momentLoader.loadMore(extra = MomentLoaderExtraArgs(authorId = profileData.id)) + Log.d("MyProfileViewModel", "loadMoreMoment: 加载完成, 新的moments数量: ${moments.size}") + } catch (e: Exception) { + Log.e("MyProfileViewModel", "loadMoreMoment: ", e) + } + } ?: Log.w("MyProfileViewModel", "loadMoreMoment: profile为null,无法加载更多") } } diff --git a/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/ProfileV3.kt b/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/ProfileV3.kt index 5fea7e9..fcf40cf 100644 --- a/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/ProfileV3.kt +++ b/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/ProfileV3.kt @@ -3,6 +3,7 @@ package com.aiosman.ravenow.ui.index.tabs.profile import android.content.Context import android.content.Intent import android.net.Uri +import android.util.Log import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.Image import androidx.compose.foundation.background @@ -104,6 +105,7 @@ fun ProfileV3( onLike: (MomentEntity) -> Unit = {}, onComment: (MomentEntity) -> Unit = {}, onAgentClick: (AgentEntity) -> Unit = {}, + postCount: Int? = null, // 新增参数用于传递帖子总数 ) { val model = MyProfileViewModel val state = rememberCollapsingToolbarScaffoldState() @@ -146,8 +148,25 @@ fun ProfileV3( // observe grid scrolling for load more val reachedGridBottom by remember { derivedStateOf { - val lastVisibleItem = gridState.layoutInfo.visibleItemsInfo.lastOrNull() - lastVisibleItem?.index != 0 && lastVisibleItem?.index == gridState.layoutInfo.totalItemsCount - 2 + val layoutInfo = gridState.layoutInfo + val lastVisibleItem = layoutInfo.visibleItemsInfo.lastOrNull() + val totalItems = layoutInfo.totalItemsCount + val visibleItemsCount = layoutInfo.visibleItemsInfo.size + + Log.d("ProfileV3", "滚动状态检查 - totalItems: $totalItems, visibleItems: $visibleItemsCount, lastVisibleIndex: ${lastVisibleItem?.index}, moments.size: ${moments.size}, hasNext: ${model.momentLoader.hasNext}") + + // 如果没有可见item,不触发加载 + if (lastVisibleItem == null || totalItems == 0) { + Log.d("ProfileV3", "跳过加载 - 没有可见item或总数为0") + false + } else { + // 检查是否滚动到最后几个item(考虑到有Spacer item) + // 当接近底部时提前触发加载 + val triggerIndex = maxOf(0, totalItems - 2) + val result = lastVisibleItem.index >= triggerIndex && model.momentLoader.hasNext + Log.d("ProfileV3", "滚动检测结果 - lastVisibleIndex: ${lastVisibleItem.index}, triggerIndex: $triggerIndex, hasNext: ${model.momentLoader.hasNext}, shouldTrigger: $result") + result + } } } @@ -160,7 +179,9 @@ fun ProfileV3( // load more if scrolled to bottom of grid LaunchedEffect(reachedGridBottom) { + Log.d("ProfileV3", "LaunchedEffect触发 - reachedGridBottom: $reachedGridBottom") if (reachedGridBottom) { + Log.d("ProfileV3", "检测到网格滚动到底部,触发加载更多") onLoadMore() } } @@ -328,7 +349,7 @@ fun ProfileV3( profile?.let { UserItem( accountProfileEntity = it, - postCount = moments.size + postCount = postCount ?: if (isSelf) MyProfileViewModel.momentLoader.total else moments.size ) } } diff --git a/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/ProfileWrap.kt b/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/ProfileWrap.kt index bd464aa..44ccb4a 100644 --- a/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/ProfileWrap.kt +++ b/app/src/main/java/com/aiosman/ravenow/ui/index/tabs/profile/ProfileWrap.kt @@ -1,5 +1,6 @@ package com.aiosman.ravenow.ui.index.tabs.profile +import android.util.Log import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.platform.LocalContext @@ -17,6 +18,7 @@ fun ProfileWrap( } ProfileV3( isMain = true, + postCount = MyProfileViewModel.momentLoader.total, onUpdateBanner = { uri, file, context -> MyProfileViewModel.updateUserProfileBanner(uri, file, context) }, @@ -27,6 +29,7 @@ fun ProfileWrap( moments = MyProfileViewModel.moments, agents = MyProfileViewModel.agents, onLoadMore = { + Log.d("ProfileWrap", "onLoadMore被调用") MyProfileViewModel.loadMoreMoment() }, onLike = { moments -> diff --git a/app/src/main/java/com/aiosman/ravenow/ui/login/emailsignup.kt b/app/src/main/java/com/aiosman/ravenow/ui/login/emailsignup.kt index 9009ba3..c7dcb33 100644 --- a/app/src/main/java/com/aiosman/ravenow/ui/login/emailsignup.kt +++ b/app/src/main/java/com/aiosman/ravenow/ui/login/emailsignup.kt @@ -240,6 +240,10 @@ fun EmailSignupScreen() { error = termsError ) { acceptTerms = it + // 当用户勾选时,立即清除错误状态 + if (it) { + termsError = false + } } Spacer(modifier = Modifier.height(16.dp)) CheckboxWithLabel( @@ -250,6 +254,10 @@ fun EmailSignupScreen() { error = promotionsError ) { acceptPromotions = it + // 当用户勾选时,立即清除错误状态 + if (it) { + promotionsError = false + } } } diff --git a/app/src/main/java/com/aiosman/ravenow/ui/profile/AccountProfileV2.kt b/app/src/main/java/com/aiosman/ravenow/ui/profile/AccountProfileV2.kt index 3418d63..0d6d608 100644 --- a/app/src/main/java/com/aiosman/ravenow/ui/profile/AccountProfileV2.kt +++ b/app/src/main/java/com/aiosman/ravenow/ui/profile/AccountProfileV2.kt @@ -1,5 +1,6 @@ package com.aiosman.ravenow.ui.profile +import android.util.Log import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.lifecycle.viewmodel.compose.viewModel @@ -8,6 +9,7 @@ import com.aiosman.ravenow.exp.viewModelFactory import com.aiosman.ravenow.ui.index.tabs.profile.MyProfileViewModel import com.aiosman.ravenow.ui.index.tabs.profile.ProfileV3 import com.aiosman.ravenow.ui.navigateToChat +import com.aiosman.ravenow.ui.navigateToPost @Composable fun AccountProfileV2(id: String){ @@ -27,6 +29,17 @@ fun AccountProfileV2(id: String){ moments = model.moments, profile = model.profile, isSelf = isSelf, + postCount = model.momentLoader.total, + onLoadMore = { + Log.d("AccountProfileV2", "onLoadMore被调用") + model.loadMoreMoment() + }, + onLike = { moment -> + // TODO: 实现点赞逻辑 + }, + onComment = { moment -> + navController.navigateToPost(moment.id) + }, onChatClick = { model.profile?.let { navController.navigateToChat(it.id.toString()) diff --git a/app/src/main/java/com/aiosman/ravenow/ui/profile/AccountProfileViewModel.kt b/app/src/main/java/com/aiosman/ravenow/ui/profile/AccountProfileViewModel.kt index 7ab8e6a..bb63435 100644 --- a/app/src/main/java/com/aiosman/ravenow/ui/profile/AccountProfileViewModel.kt +++ b/app/src/main/java/com/aiosman/ravenow/ui/profile/AccountProfileViewModel.kt @@ -1,5 +1,6 @@ package com.aiosman.ravenow.ui.profile +import android.util.Log import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue @@ -25,6 +26,7 @@ class AccountProfileViewModel : ViewModel() { var profile by mutableStateOf(null) var refreshing by mutableStateOf(false) var momentLoader = MomentLoader().apply { + pageSize = 20 // 设置与后端一致的页面大小 onListChanged = { moments = it } @@ -58,6 +60,21 @@ class AccountProfileViewModel : ViewModel() { } } } + + fun loadMoreMoment() { + viewModelScope.launch { + profile?.let { profileData -> + try { + Log.d("AccountProfileViewModel", "loadMoreMoment: 开始加载更多, 当前moments数量: ${moments.size}, hasNext: ${momentLoader.hasNext}") + momentLoader.loadMore(extra = MomentLoaderExtraArgs(authorId = profileData.id)) + Log.d("AccountProfileViewModel", "loadMoreMoment: 加载完成, 新的moments数量: ${moments.size}") + } catch (e: Exception) { + Log.e("AccountProfileViewModel", "loadMoreMoment: ", e) + } + } ?: Log.w("AccountProfileViewModel", "loadMoreMoment: profile为null,无法加载更多") + } + } + @Subscribe fun onFollowChangeEvent(event: FollowChangeEvent) { if (event.userId == profile?.id) {