Refactor: 优化个人主页和账户主页帖子加载逻辑
- 统一MyProfileViewModel和AccountProfileViewModel中的帖子加载逻辑,使用一致的pageSize。 - 在ProfileWrap和AccountProfileV2中传递正确的postCount。 - 在ProfileV3中改进了加载更多帖子的触发条件,确保在有更多数据时才触发加载。 - 修复了注册页面勾选协议和促销选项后,错误状态未清除的问题。 - 为DataLoader和ProfileV3中的滚动加载逻辑添加了详细日志,方便调试。
This commit is contained in:
@@ -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<T,ET> {
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ object MyProfileViewModel : ViewModel() {
|
||||
var moments by mutableStateOf<List<MomentEntity>>(emptyList())
|
||||
var agents by mutableStateOf<List<AgentEntity>>(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,无法加载更多")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 ->
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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())
|
||||
|
||||
@@ -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<AccountProfileEntity?>(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) {
|
||||
|
||||
Reference in New Issue
Block a user