朋友圈刷新方式调整
This commit is contained in:
@@ -3,6 +3,7 @@ package com.aiosman.ravenow.ui.index.tabs.moment
|
|||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
|
// import android.util.Log
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.aiosman.ravenow.data.MomentService
|
import com.aiosman.ravenow.data.MomentService
|
||||||
@@ -27,7 +28,8 @@ open class BaseMomentModel :ViewModel(){
|
|||||||
|
|
||||||
val momentLoader = MomentLoader().apply {
|
val momentLoader = MomentLoader().apply {
|
||||||
onListChanged = {
|
onListChanged = {
|
||||||
moments = it
|
// 使用新实例触发 Compose 重组
|
||||||
|
moments = it.toList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var refreshing by mutableStateOf(false)
|
var refreshing by mutableStateOf(false)
|
||||||
@@ -50,7 +52,8 @@ open class BaseMomentModel :ViewModel(){
|
|||||||
fun loadMore() {
|
fun loadMore() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
momentLoader.loadMore(extraArgs())
|
momentLoader.loadMore(extraArgs())
|
||||||
moments = momentLoader.list
|
// 使用新实例触发 Compose 重组
|
||||||
|
moments = momentLoader.list.toList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,10 @@ import androidx.compose.runtime.Composable
|
|||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.derivedStateOf
|
import androidx.compose.runtime.derivedStateOf
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import com.aiosman.ravenow.GuestLoginCheckOut
|
import com.aiosman.ravenow.GuestLoginCheckOut
|
||||||
@@ -31,7 +33,7 @@ import kotlinx.coroutines.launch
|
|||||||
@Composable
|
@Composable
|
||||||
fun Dynamic() {
|
fun Dynamic() {
|
||||||
val model = DynamicViewModel
|
val model = DynamicViewModel
|
||||||
var moments = model.moments
|
val moments = model.moments
|
||||||
val navController = LocalNavController.current
|
val navController = LocalNavController.current
|
||||||
|
|
||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
@@ -42,23 +44,46 @@ fun Dynamic() {
|
|||||||
})
|
})
|
||||||
val listState = rememberLazyListState()
|
val listState = rememberLazyListState()
|
||||||
|
|
||||||
// observe list scrolling
|
// 用于跟踪是否已经触发过加载更多
|
||||||
|
var hasTriggeredLoadMore by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
// observe list scrolling with simplified logic
|
||||||
val reachedBottom by remember {
|
val reachedBottom by remember {
|
||||||
derivedStateOf {
|
derivedStateOf {
|
||||||
val lastVisibleItem = listState.layoutInfo.visibleItemsInfo.lastOrNull()
|
val layoutInfo = listState.layoutInfo
|
||||||
lastVisibleItem?.index != 0 && lastVisibleItem?.index == listState.layoutInfo.totalItemsCount - 2
|
val lastVisibleItem = layoutInfo.visibleItemsInfo.lastOrNull()
|
||||||
|
val totalItems = layoutInfo.totalItemsCount
|
||||||
|
|
||||||
|
if (lastVisibleItem == null || totalItems == 0) {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
val isLastItemVisible = lastVisibleItem.index >= totalItems - 2
|
||||||
|
|
||||||
|
// 简化逻辑:只要滑动到底部且还没有触发过,就触发加载
|
||||||
|
isLastItemVisible && !hasTriggeredLoadMore
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// load more if scrolled to bottom
|
// load more if scrolled to bottom
|
||||||
LaunchedEffect(reachedBottom) {
|
LaunchedEffect(reachedBottom) {
|
||||||
if (reachedBottom) {
|
if (reachedBottom) {
|
||||||
|
hasTriggeredLoadMore = true
|
||||||
model.loadMore()
|
model.loadMore()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
model.refreshPager()
|
model.refreshPager()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 监听数据变化,重置加载状态
|
||||||
|
LaunchedEffect(moments.size) {
|
||||||
|
if (moments.size > 0) {
|
||||||
|
// 延迟重置,确保数据已经稳定
|
||||||
|
kotlinx.coroutines.delay(500)
|
||||||
|
hasTriggeredLoadMore = false
|
||||||
|
}
|
||||||
|
}
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxSize()
|
||||||
|
|||||||
Reference in New Issue
Block a user