diff --git a/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/HomeContent.kt b/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/HomeContent.kt index 9ae073c2..9e9728e7 100644 --- a/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/HomeContent.kt +++ b/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/HomeContent.kt @@ -9,7 +9,7 @@ import androidx.compose.animation.slideOutHorizontally import androidx.compose.animation.togetherWith import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.lazy.grid.rememberLazyGridState +import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material3.Scaffold import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -54,9 +54,9 @@ fun HomeContent( val scope = rememberCoroutineScope() val logger = KotlinLogging.logger("HomeContent") - val recommendState = rememberLazyGridState() - val popularState = rememberLazyGridState() - val dynamicState = rememberLazyGridState() + val recommendState = rememberLazyListState() + val popularState = rememberLazyListState() + val dynamicState = rememberLazyListState() var selectedTab by remember { mutableStateOf(HomeTopNavItem.Recommend) } var focusOnContent by remember { mutableStateOf(false) } @@ -188,9 +188,9 @@ fun HomeContent( } ) { screen -> when (screen) { - HomeTopNavItem.Recommend -> RecommendScreen(lazyGridState = recommendState) - HomeTopNavItem.Popular -> PopularScreen(lazyGridState = popularState) - HomeTopNavItem.Dynamics -> DynamicsScreen(lazyGridState = dynamicState) + HomeTopNavItem.Recommend -> RecommendScreen(lazyListState = recommendState) + HomeTopNavItem.Popular -> PopularScreen(lazyListState = popularState) + HomeTopNavItem.Dynamics -> DynamicsScreen(lazyListState = dynamicState) } } } diff --git a/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/DynamicsScreen.kt b/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/DynamicsScreen.kt index 71cc7457..6998c668 100644 --- a/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/DynamicsScreen.kt +++ b/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/DynamicsScreen.kt @@ -2,13 +2,11 @@ package dev.aaa1115910.bv.screen.main.home import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.lazy.grid.GridCells -import androidx.compose.foundation.lazy.grid.GridItemSpan -import androidx.compose.foundation.lazy.grid.LazyGridState -import androidx.compose.foundation.lazy.grid.LazyVerticalGrid -import androidx.compose.foundation.lazy.grid.itemsIndexed +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.LazyListState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf @@ -20,19 +18,16 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color -import androidx.compose.ui.input.key.Key -import androidx.compose.ui.input.key.KeyEventType -import androidx.compose.ui.input.key.key -import androidx.compose.ui.input.key.onPreviewKeyEvent -import androidx.compose.ui.input.key.type import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp import androidx.tv.material3.Text +import dev.aaa1115910.biliapi.entity.user.DynamicVideo import dev.aaa1115910.bv.activities.video.VideoInfoActivity import dev.aaa1115910.bv.component.LoadingTip import dev.aaa1115910.bv.component.videocard.SmallVideoCard import dev.aaa1115910.bv.entity.carddata.VideoCardData import dev.aaa1115910.bv.entity.proxy.ProxyArea +import dev.aaa1115910.bv.screen.main.ugc.gridItems import dev.aaa1115910.bv.viewmodel.home.DynamicViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -41,7 +36,7 @@ import org.koin.androidx.compose.koinViewModel @Composable fun DynamicsScreen( modifier: Modifier = Modifier, - lazyGridState: LazyGridState, + lazyListState: LazyListState, dynamicViewModel: DynamicViewModel = koinViewModel() ) { val context = LocalContext.current @@ -51,6 +46,14 @@ fun DynamicsScreen( derivedStateOf { currentFocusedIndex + 24 > dynamicViewModel.dynamicList.size } } + val onClickVideo: (DynamicVideo) -> Unit = { dynamic -> + VideoInfoActivity.actionStart( + context = context, + aid = dynamic.aid, + proxyArea = ProxyArea.checkProxyArea(dynamic.title) + ) + } + //不能直接使用 LaunchedEffect(currentFocusedIndex),会导致整个页面重组 LaunchedEffect(shouldLoadMore) { if (shouldLoadMore) { @@ -63,39 +66,36 @@ fun DynamicsScreen( } if (dynamicViewModel.isLogin) { - LazyVerticalGrid( + LazyColumn( modifier = modifier, - state = lazyGridState, - columns = GridCells.Fixed(4), - contentPadding = PaddingValues(24.dp), - verticalArrangement = Arrangement.spacedBy(24.dp), - horizontalArrangement = Arrangement.spacedBy(24.dp) + state = lazyListState ) { - itemsIndexed(dynamicViewModel.dynamicList) { index, dynamic -> - SmallVideoCard( - data = VideoCardData( - avid = dynamic.aid, - title = dynamic.title, - cover = dynamic.cover, - play = dynamic.play, - danmaku = dynamic.danmaku, - upName = dynamic.author, - time = dynamic.duration * 1000L - ), - onClick = { - VideoInfoActivity.actionStart( - context = context, - aid = dynamic.aid, - proxyArea = ProxyArea.checkProxyArea(dynamic.title) - ) - }, - onFocus = { currentFocusedIndex = index } - ) - } + gridItems( + data = dynamicViewModel.dynamicList, + columnCount = 4, + modifier = Modifier + .width(880.dp) + .padding(horizontal = 24.dp, vertical = 12.dp), + horizontalArrangement = Arrangement.spacedBy(24.dp), + itemContent = { index, item -> + SmallVideoCard( + data = VideoCardData( + avid = item.aid, + title = item.title, + cover = item.cover, + play = item.play, + danmaku = item.danmaku, + upName = item.author, + time = item.duration * 1000L + ), + onClick = { onClickVideo(item) }, + onFocus = { currentFocusedIndex = index } + ) + } + ) + if (dynamicViewModel.loading) - item( - span = { GridItemSpan(4) } - ) { + item { Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center @@ -105,9 +105,7 @@ fun DynamicsScreen( } if (!dynamicViewModel.hasMore) - item( - span = { GridItemSpan(4) } - ) { + item { Text( text = "没有更多了捏", color = Color.White diff --git a/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/PopularScreen.kt b/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/PopularScreen.kt index 15d294fd..0574a936 100644 --- a/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/PopularScreen.kt +++ b/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/PopularScreen.kt @@ -2,13 +2,11 @@ package dev.aaa1115910.bv.screen.main.home import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.lazy.grid.GridCells -import androidx.compose.foundation.lazy.grid.GridItemSpan -import androidx.compose.foundation.lazy.grid.LazyGridState -import androidx.compose.foundation.lazy.grid.LazyVerticalGrid -import androidx.compose.foundation.lazy.grid.itemsIndexed +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.LazyListState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf @@ -19,17 +17,14 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.input.key.Key -import androidx.compose.ui.input.key.KeyEventType -import androidx.compose.ui.input.key.key -import androidx.compose.ui.input.key.onPreviewKeyEvent -import androidx.compose.ui.input.key.type import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp +import dev.aaa1115910.biliapi.entity.ugc.UgcItem import dev.aaa1115910.bv.activities.video.VideoInfoActivity import dev.aaa1115910.bv.component.LoadingTip import dev.aaa1115910.bv.component.videocard.SmallVideoCard import dev.aaa1115910.bv.entity.carddata.VideoCardData +import dev.aaa1115910.bv.screen.main.ugc.gridItems import dev.aaa1115910.bv.viewmodel.home.PopularViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -38,7 +33,7 @@ import org.koin.androidx.compose.koinViewModel @Composable fun PopularScreen( modifier: Modifier = Modifier, - lazyGridState: LazyGridState, + lazyListState: LazyListState, popularViewModel: PopularViewModel = koinViewModel() ) { val context = LocalContext.current @@ -48,6 +43,10 @@ fun PopularScreen( derivedStateOf { currentFocusedIndex + 24 > popularViewModel.popularVideoList.size } } + val onClickVideo: (UgcItem) -> Unit = { ugcItem -> + VideoInfoActivity.actionStart(context, ugcItem.aid) + } + LaunchedEffect(shouldLoadMore) { if (shouldLoadMore) { scope.launch(Dispatchers.IO) { @@ -58,33 +57,36 @@ fun PopularScreen( } } - LazyVerticalGrid( + LazyColumn( modifier = modifier, - state = lazyGridState, - columns = GridCells.Fixed(4), - contentPadding = PaddingValues(24.dp), - verticalArrangement = Arrangement.spacedBy(24.dp), - horizontalArrangement = Arrangement.spacedBy(24.dp) + state = lazyListState ) { - itemsIndexed(popularViewModel.popularVideoList) { index, video -> - SmallVideoCard( - data = VideoCardData( - avid = video.aid, - title = video.title, - cover = video.cover, - play = with(video.play) { if (this == -1) null else this }, - danmaku = with(video.danmaku) { if (this == -1) null else this }, - upName = video.author, - time = video.duration * 1000L - ), - onClick = { VideoInfoActivity.actionStart(context, video.aid) }, - onFocus = { currentFocusedIndex = index } - ) - } + gridItems( + data = popularViewModel.popularVideoList, + columnCount = 4, + modifier = Modifier + .width(880.dp) + .padding(horizontal = 24.dp, vertical = 12.dp), + horizontalArrangement = Arrangement.spacedBy(24.dp), + itemContent = { index, item -> + SmallVideoCard( + data = VideoCardData( + avid = item.aid, + title = item.title, + cover = item.cover, + play = with(item.play) { if (this == -1) null else this }, + danmaku = with(item.danmaku) { if (this == -1) null else this }, + upName = item.author, + time = item.duration * 1000L + ), + onClick = { onClickVideo(item) }, + onFocus = { currentFocusedIndex = index } + ) + } + ) + if (popularViewModel.loading) - item( - span = { GridItemSpan(4) } - ) { + item { Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center diff --git a/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/RecommendScreen.kt b/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/RecommendScreen.kt index a1b69be6..e040aa99 100644 --- a/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/RecommendScreen.kt +++ b/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/home/RecommendScreen.kt @@ -2,13 +2,11 @@ package dev.aaa1115910.bv.screen.main.home import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box -import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.lazy.grid.GridCells -import androidx.compose.foundation.lazy.grid.GridItemSpan -import androidx.compose.foundation.lazy.grid.LazyGridState -import androidx.compose.foundation.lazy.grid.LazyVerticalGrid -import androidx.compose.foundation.lazy.grid.itemsIndexed +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.LazyListState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf @@ -19,17 +17,14 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.input.key.Key -import androidx.compose.ui.input.key.KeyEventType -import androidx.compose.ui.input.key.key -import androidx.compose.ui.input.key.onPreviewKeyEvent -import androidx.compose.ui.input.key.type import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp +import dev.aaa1115910.biliapi.entity.ugc.UgcItem import dev.aaa1115910.bv.activities.video.VideoInfoActivity import dev.aaa1115910.bv.component.LoadingTip import dev.aaa1115910.bv.component.videocard.SmallVideoCard import dev.aaa1115910.bv.entity.carddata.VideoCardData +import dev.aaa1115910.bv.screen.main.ugc.gridItems import dev.aaa1115910.bv.viewmodel.home.RecommendViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -38,7 +33,7 @@ import org.koin.androidx.compose.koinViewModel @Composable fun RecommendScreen( modifier: Modifier = Modifier, - lazyGridState: LazyGridState, + lazyListState: LazyListState, recommendViewModel: RecommendViewModel = koinViewModel() ) { val context = LocalContext.current @@ -48,6 +43,10 @@ fun RecommendScreen( derivedStateOf { currentFocusedIndex + 24 > recommendViewModel.recommendVideoList.size } } + val onClickVideo: (UgcItem) -> Unit = { ugcItem -> + VideoInfoActivity.actionStart(context, ugcItem.aid) + } + //不能直接使用 LaunchedEffect(currentFocusedIndex),会导致整个页面重组 LaunchedEffect(shouldLoadMore) { if (shouldLoadMore) { @@ -59,33 +58,36 @@ fun RecommendScreen( } } - LazyVerticalGrid( + LazyColumn( modifier = modifier, - state = lazyGridState, - columns = GridCells.Fixed(4), - contentPadding = PaddingValues(24.dp), - verticalArrangement = Arrangement.spacedBy(24.dp), - horizontalArrangement = Arrangement.spacedBy(24.dp) + state = lazyListState ) { - itemsIndexed(recommendViewModel.recommendVideoList) { index, video -> - SmallVideoCard( - data = VideoCardData( - avid = video.aid, - title = video.title, - cover = video.cover, - play = with(video.play) { if (this == -1) null else this }, - danmaku = with(video.danmaku) { if (this == -1) null else this }, - upName = video.author, - time = video.duration * 1000L - ), - onClick = { VideoInfoActivity.actionStart(context, video.aid) }, - onFocus = { currentFocusedIndex = index } - ) - } + gridItems( + data = recommendViewModel.recommendVideoList, + columnCount = 4, + modifier = Modifier + .width(880.dp) + .padding(horizontal = 24.dp, vertical = 12.dp), + horizontalArrangement = Arrangement.spacedBy(24.dp), + itemContent = { index, item -> + SmallVideoCard( + data = VideoCardData( + avid = item.aid, + title = item.title, + cover = item.cover, + play = with(item.play) { if (this == -1) null else this }, + danmaku = with(item.danmaku) { if (this == -1) null else this }, + upName = item.author, + time = item.duration * 1000L + ), + onClick = { onClickVideo(item) }, + onFocus = { currentFocusedIndex = index } + ) + } + ) + if (recommendViewModel.loading) - item( - span = { GridItemSpan(4) } - ) { + item { Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center diff --git a/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/ugc/UgcCommon.kt b/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/ugc/UgcCommon.kt index 5467e67b..73b1e07d 100644 --- a/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/ugc/UgcCommon.kt +++ b/app/src/main/kotlin/dev/aaa1115910/bv/screen/main/ugc/UgcCommon.kt @@ -14,6 +14,7 @@ import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyListScope import androidx.compose.foundation.lazy.LazyListState +import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.rememberScrollState import androidx.compose.runtime.Composable @@ -107,7 +108,9 @@ fun UgcRegionScaffold( gridItems( data = state.ugcItems, columnCount = 4, - modifier = Modifier.padding(horizontal = 24.dp, vertical = 12.dp), + modifier = Modifier + .width(880.dp) + .padding(horizontal = 24.dp, vertical = 12.dp), horizontalArrangement = Arrangement.spacedBy(24.dp), itemContent = { index, item -> SmallVideoCard( @@ -140,22 +143,29 @@ fun LazyListScope.gridItems( val size = data.count() val rows = if (size == 0) 0 else 1 + (size - 1) / columnCount items(rows, key = key) { rowIndex -> - Row( - verticalAlignment = verticalAlignment, - horizontalArrangement = horizontalArrangement, - modifier = modifier + LazyRow( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.Center ) { - for (columnIndex in 0 until columnCount) { - val itemIndex = rowIndex * columnCount + columnIndex - if (itemIndex < size) { - Box( - modifier = Modifier.weight(1F, fill = true), - propagateMinConstraints = true - ) { - itemContent(itemIndex, data[itemIndex]) + item { + Row( + verticalAlignment = verticalAlignment, + horizontalArrangement = horizontalArrangement, + modifier = modifier + ) { + for (columnIndex in 0 until columnCount) { + val itemIndex = rowIndex * columnCount + columnIndex + if (itemIndex < size) { + Box( + modifier = Modifier.weight(1F, fill = true), + propagateMinConstraints = true + ) { + itemContent(itemIndex, data[itemIndex]) + } + } else { + Spacer(Modifier.weight(1F, fill = true)) + } } - } else { - Spacer(Modifier.weight(1F, fill = true)) } } }