diff --git a/shared/src/commonMain/composeResources/values-en/strings.xml b/shared/src/commonMain/composeResources/values-en/strings.xml index a569f44..d8271bb 100644 --- a/shared/src/commonMain/composeResources/values-en/strings.xml +++ b/shared/src/commonMain/composeResources/values-en/strings.xml @@ -998,6 +998,10 @@ Show %1$d more uploads %1$d live %1$d videos + Default order + Reverse order + Newest + Oldest %1$d steps searched Search watch history diff --git a/shared/src/commonMain/composeResources/values/strings.xml b/shared/src/commonMain/composeResources/values/strings.xml index 6e3b527..7b5e9f2 100644 --- a/shared/src/commonMain/composeResources/values/strings.xml +++ b/shared/src/commonMain/composeResources/values/strings.xml @@ -1060,6 +1060,10 @@ 展开其余 %1$d 条连发 %1$d 直播 共 %1$d 个视频 + 默认顺序 + 倒序排列 + 最新发布 + 最早发布 已检索 %1$d 步 搜索历史记录 diff --git a/shared/src/commonMain/kotlin/dev/bilby/data/QueueContext.kt b/shared/src/commonMain/kotlin/dev/bilby/data/QueueContext.kt index 3db7387..f803f5e 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/data/QueueContext.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/data/QueueContext.kt @@ -30,6 +30,7 @@ sealed interface QueueContext { val isSeason: Boolean, val name: String, val page: Int = 1, + val reversed: Boolean = false, ) : QueueContext /** 空间页的投稿栏,带着当时的排序与空间内搜索词。 */ diff --git a/shared/src/commonMain/kotlin/dev/bilby/data/QueueSourceRepository.kt b/shared/src/commonMain/kotlin/dev/bilby/data/QueueSourceRepository.kt index bc411ca..90d70f1 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/data/QueueSourceRepository.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/data/QueueSourceRepository.kt @@ -50,7 +50,7 @@ class QueueSourceRepository( // 这条就挪到了下一页。 is QueueContext.UpDynamics -> openUpDynamics(context, bvid) is QueueContext.Collection -> openFeed( - feed = collectionFeed(context.mid, context.id, context.isSeason, context.page), + feed = collectionFeed(context.mid, context.id, context.isSeason, context.page, reversed = context.reversed), bvid = bvid, label = collectionLabel(context.isSeason, context.name), source = QueueSource(context.mid, context.id, context.isSeason, context.name), @@ -271,9 +271,16 @@ class QueueSourceRepository( getString(Res.string.queue_source_series, name) } - private fun collectionFeed(mid: Long, id: Long, isSeason: Boolean, anchorPage: Int, fallbackUpName: String = "") = + private fun collectionFeed( + mid: Long, + id: Long, + isSeason: Boolean, + anchorPage: Int, + reversed: Boolean = false, + fallbackUpName: String = "", + ) = PagedFeed(anchorPage, SpaceRepository.COLLECTION_PAGE_SIZE) { page -> - when (val r = spaceRepository.loadCollectionDetail(mid, id, isSeason, page)) { + when (val r = spaceRepository.loadCollectionDetail(mid, id, isSeason, page, reversed = reversed)) { is BiliResult.Ok -> FeedPage( items = r.value.items.map { it.toQueueItem(fallbackUpName = fallbackUpName) }, hasMore = page * SpaceRepository.COLLECTION_PAGE_SIZE < r.value.total && r.value.items.isNotEmpty(), diff --git a/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt b/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt index f9a7198..174d28d 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt @@ -384,12 +384,13 @@ class SpaceRepository(private val client: BiliClient) { id: Long, isSeason: Boolean, page: Int, + reversed: Boolean = false, ): BiliResult { val (url, params) = if (isSeason) { "${BiliConstants.WEB_HOST}/x/polymer/web-space/seasons_archives_list" to mapOf( "mid" to mid.toString(), "season_id" to id.toString(), - "sort_reverse" to "false", + "sort_reverse" to reversed.toString(), "page_size" to COLLECTION_PAGE_SIZE.toString(), "page_num" to page.toString(), "web_location" to "333.1387", @@ -398,7 +399,7 @@ class SpaceRepository(private val client: BiliClient) { "${BiliConstants.WEB_HOST}/x/series/archives" to mapOf( "mid" to mid.toString(), "series_id" to id.toString(), - "sort" to "desc", + "sort" to if (reversed) "asc" else "desc", "ps" to COLLECTION_PAGE_SIZE.toString(), "pn" to page.toString(), "web_location" to "333.1387", diff --git a/shared/src/commonMain/kotlin/dev/bilby/ui/BilbyApp.kt b/shared/src/commonMain/kotlin/dev/bilby/ui/BilbyApp.kt index 1f92a6f..702f8ee 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/ui/BilbyApp.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/ui/BilbyApp.kt @@ -4542,14 +4542,26 @@ private fun CollectionRoute( val state by vm.state.collectAsStateWithLifecycle() CollectionScreen( title = key.name, + isSeason = key.isSeason, state = state, onBack = onBack, onLoadMore = { vm.loadMore() }, onRefresh = vm::refresh, + onReversedChange = vm::setReversed, onVideoClick = { item -> val index = state.items.indexOf(item).coerceAtLeast(0) val page = index / SpaceRepository.COLLECTION_PAGE_SIZE + 1 - onVideoClick(item.bvid, QueueContext.Collection(key.mid, key.id, key.isSeason, key.name, page)) + onVideoClick( + item.bvid, + QueueContext.Collection( + mid = key.mid, + id = key.id, + isSeason = key.isSeason, + name = key.name, + page = page, + reversed = state.reversed, + ), + ) }, ) } diff --git a/shared/src/commonMain/kotlin/dev/bilby/ui/space/CollectionScreen.kt b/shared/src/commonMain/kotlin/dev/bilby/ui/space/CollectionScreen.kt index 5bcaf80..634de46 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/ui/space/CollectionScreen.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/ui/space/CollectionScreen.kt @@ -1,9 +1,12 @@ package dev.bilby.ui.space +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.padding import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material3.ExperimentalMaterial3Api @@ -13,6 +16,7 @@ import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MediumFlexibleTopAppBar import androidx.compose.material3.Scaffold +import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable @@ -33,10 +37,13 @@ import dev.bilby.ui.CaptionDragHeight import dev.bilby.ui.LocalWindowCaption import dev.bilby.ui.components.BilbyTooltip import dev.bilby.ui.components.RefreshBox +import dev.bilby.ui.components.SortMenu import dev.bilby.ui.components.TopBarRefreshButton import dev.bilby.ui.errorTextRes import dev.bilby.ui.padScaffoldExceptBottom import dev.bilby.ui.rememberCaptionSlot +import dev.bilby.ui.theme.Dimens +import dev.bilby.ui.theme.Spacing import dev.bilby.ui.theme.tabular import dev.bilby.ui.windowDragArea import kotlinx.coroutines.flow.MutableStateFlow @@ -51,6 +58,7 @@ data class CollectionUiState( val items: List = emptyList(), val page: Int = 1, val total: Int = 0, + val reversed: Boolean = false, val loading: Boolean = false, val appending: Boolean = false, val refreshing: Boolean = false, @@ -72,13 +80,32 @@ class CollectionViewModel( private val _state = MutableStateFlow(CollectionUiState()) val state: StateFlow = _state.asStateFlow() - /** 刷新会把游标退回第一页。代次让迟到的旧第一页即使参数相同也写不回来。 */ + /** 刷新或切换排序会把游标退回第一页。代次让迟到的旧响应即使参数相同也写不回来。 */ private var generation = 0L init { loadMore() } + fun setReversed(reversed: Boolean) { + if (reversed == _state.value.reversed) return + generation++ + _state.update { + it.copy( + reversed = reversed, + items = emptyList(), + page = 1, + loading = false, + appending = false, + refreshing = false, + hasMore = true, + error = null, + errorRes = null, + ) + } + loadMore(replace = true) + } + fun refresh() { generation++ _state.update { @@ -96,12 +123,24 @@ class CollectionViewModel( if (current.loading || current.appending || !current.hasMore) return val firstPage = replace || current.items.isEmpty() val requestedPage = if (replace) 1 else current.page + val requestedReversed = current.reversed val requestedGeneration = generation _state.update { it.copy(loading = firstPage, appending = !firstPage, error = null, errorRes = null) } viewModelScope.launch { - val result = repository.loadCollectionDetail(mid, id, isSeason, requestedPage) + val result = repository.loadCollectionDetail( + mid = mid, + id = id, + isSeason = isSeason, + page = requestedPage, + reversed = requestedReversed, + ) _state.update { state -> - if (requestedGeneration != generation || state.page != requestedPage) return@update state + if (requestedGeneration != generation || + state.page != requestedPage || + state.reversed != requestedReversed + ) { + return@update state + } when (result) { is BiliResult.Ok -> { val pageItems = result.value.items @@ -134,21 +173,33 @@ class CollectionViewModel( } } +private val SeasonSortOptions = listOf( + false to Res.string.collection_sort_default, + true to Res.string.collection_sort_reverse, +) + +private val SeriesSortOptions = listOf( + false to Res.string.collection_sort_newest, + true to Res.string.collection_sort_oldest, +) + @OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class) @Composable fun CollectionScreen( title: String, + isSeason: Boolean, state: CollectionUiState, onBack: () -> Unit, onLoadMore: () -> Unit, onRefresh: () -> Unit, + onReversedChange: (Boolean) -> Unit, onVideoClick: (SpaceVideoItem) -> Unit, modifier: Modifier = Modifier, ) { val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior() val totalCount = state.total.takeIf { it > 0 } ?: state.items.size - val subtitle = if (totalCount > 0) stringResource(Res.string.collection_total_count, totalCount) else null val errorText = state.errorRes?.let { stringResource(it) } ?: state.error + val sortOptions = if (isSeason) SeasonSortOptions else SeriesSortOptions Scaffold( modifier = modifier.nestedScroll(scrollBehavior.nestedScrollConnection), @@ -173,17 +224,6 @@ fun CollectionScreen( ) } }, - subtitle = subtitle?.let { countLabel -> - { - Text( - text = countLabel, - style = MaterialTheme.typography.bodyMedium.tabular, - color = MaterialTheme.colorScheme.onSurfaceVariant, - maxLines = 1, - overflow = TextOverflow.Ellipsis, - ) - } - }, navigationIcon = { Row(verticalAlignment = Alignment.CenterVertically) { BilbyTooltip(text = backLabel, shortcut = "Alt+←") { @@ -218,8 +258,66 @@ fun CollectionScreen( emptyText = stringResource(Res.string.space_empty_collection_detail), onLoadMore = onLoadMore, onVideoClick = onVideoClick, + header = if (totalCount > 0) { + { + Column( + verticalArrangement = Arrangement.spacedBy(Spacing.Tight), + modifier = Modifier + .fillMaxWidth() + .padding(top = Spacing.Hair, bottom = Spacing.Tight), + ) { + CollectionHeaderBar( + totalCount = totalCount, + reversed = state.reversed, + sortOptions = sortOptions, + onReversedChange = onReversedChange, + ) + } + } + } else { + null + }, modifier = Modifier.fillMaxSize(), ) } } } + +@OptIn(ExperimentalMaterial3ExpressiveApi::class) +@Composable +private fun CollectionHeaderBar( + totalCount: Int, + reversed: Boolean, + sortOptions: List>, + onReversedChange: (Boolean) -> Unit, +) { + val summaryText = if (totalCount > 0) stringResource(Res.string.collection_total_count, totalCount) else "" + Surface( + color = MaterialTheme.colorScheme.surfaceContainer, + shape = MaterialTheme.shapes.large, + modifier = Modifier.fillMaxWidth(), + ) { + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(Spacing.Tight), + modifier = Modifier + .fillMaxWidth() + .heightIn(min = Dimens.MinTouchTarget) + .padding(start = Spacing.Comfortable, end = Spacing.Tight, top = Spacing.Hair, bottom = Spacing.Hair), + ) { + Text( + text = summaryText, + style = MaterialTheme.typography.titleSmallEmphasized.tabular, + color = MaterialTheme.colorScheme.onSurfaceVariant, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + modifier = Modifier.weight(1f), + ) + SortMenu( + options = sortOptions, + selected = reversed, + onSelect = onReversedChange, + ) + } + } +}