Skip to content

Commit

Permalink
优化推荐/热门/动态的列表性能
Browse files Browse the repository at this point in the history
  • Loading branch information
aaa1115910 committed Sep 24, 2024
1 parent 76edf81 commit ef2aeb7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -47,10 +48,18 @@ fun DynamicsScreen(
val context = LocalContext.current
val scope = rememberCoroutineScope()
var currentFocusedIndex by remember { mutableIntStateOf(0) }
val shouldLoadMore by remember {
derivedStateOf { currentFocusedIndex + 24 > dynamicViewModel.dynamicList.size }
}

LaunchedEffect(currentFocusedIndex) {
if (currentFocusedIndex + 24 > dynamicViewModel.dynamicList.size) {
scope.launch(Dispatchers.Default) { dynamicViewModel.loadMore() }
//不能直接使用 LaunchedEffect(currentFocusedIndex),会导致整个页面重组
LaunchedEffect(shouldLoadMore) {
if (shouldLoadMore) {
scope.launch(Dispatchers.IO) {
dynamicViewModel.loadMore()
//加载完成后重置shouldLoadMore为false,避免如果加载失败后无法重新加载
currentFocusedIndex = -100
}
}
}

Expand Down
14 changes: 11 additions & 3 deletions app/src/main/kotlin/dev/aaa1115910/bv/screen/home/PopularScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -44,10 +45,17 @@ fun PopularScreen(
val context = LocalContext.current
val scope = rememberCoroutineScope()
var currentFocusedIndex by remember { mutableIntStateOf(0) }
val shouldLoadMore by remember {
derivedStateOf { currentFocusedIndex + 24 > popularViewModel.popularVideoList.size }
}

LaunchedEffect(currentFocusedIndex) {
if (currentFocusedIndex + 24 > popularViewModel.popularVideoList.size) {
scope.launch(Dispatchers.IO) { popularViewModel.loadMore() }
LaunchedEffect(shouldLoadMore) {
if (shouldLoadMore) {
scope.launch(Dispatchers.IO) {
popularViewModel.loadMore()
//加载完成后重置shouldLoadMore为false,避免如果加载失败后无法重新加载
currentFocusedIndex = -100
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -44,10 +45,18 @@ fun RecommendScreen(
val context = LocalContext.current
val scope = rememberCoroutineScope()
var currentFocusedIndex by remember { mutableIntStateOf(0) }
val shouldLoadMore by remember {
derivedStateOf { currentFocusedIndex + 24 > recommendViewModel.recommendVideoList.size }
}

LaunchedEffect(currentFocusedIndex) {
if (currentFocusedIndex + 24 > recommendViewModel.recommendVideoList.size) {
scope.launch(Dispatchers.IO) { recommendViewModel.loadMore() }
//不能直接使用 LaunchedEffect(currentFocusedIndex),会导致整个页面重组
LaunchedEffect(shouldLoadMore) {
if (shouldLoadMore) {
scope.launch(Dispatchers.IO) {
recommendViewModel.loadMore()
//加载完成后重置shouldLoadMore为false,避免如果加载失败后无法重新加载
currentFocusedIndex = -100
}
}
}

Expand Down

0 comments on commit ef2aeb7

Please sign in to comment.