From f5d870208100e08825732d6225efa08accbdd7aa Mon Sep 17 00:00:00 2001 From: The-R0 <2861295907@qq.com> Date: Wed, 25 Nov 2026 13:14:15 +0800 Subject: [PATCH 1/2] feat(space,queue): support sorting UP collections by publish time --- .../composeResources/values-en/strings.xml | 3 + .../composeResources/values/strings.xml | 3 + .../kotlin/dev/bilby/data/QueueContext.kt | 1 + .../dev/bilby/data/QueueSourceRepository.kt | 75 +++++- .../kotlin/dev/bilby/data/SpaceRepository.kt | 42 +++- .../kotlin/dev/bilby/ui/BilbyApp.kt | 6 +- .../dev/bilby/ui/space/CollectionScreen.kt | 228 +++++++++++++++++- .../dev/bilby/data/CollectionSortTest.kt | 43 ++++ 8 files changed, 386 insertions(+), 15 deletions(-) create mode 100644 shared/src/commonTest/kotlin/dev/bilby/data/CollectionSortTest.kt diff --git a/shared/src/commonMain/composeResources/values-en/strings.xml b/shared/src/commonMain/composeResources/values-en/strings.xml index a569f44..5b742b5 100644 --- a/shared/src/commonMain/composeResources/values-en/strings.xml +++ b/shared/src/commonMain/composeResources/values-en/strings.xml @@ -998,6 +998,9 @@ Show %1$d more uploads %1$d live %1$d videos + Default + 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..7759fe8 100644 --- a/shared/src/commonMain/composeResources/values/strings.xml +++ b/shared/src/commonMain/composeResources/values/strings.xml @@ -1060,6 +1060,9 @@ 展开其余 %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..067ba89 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 sort: CollectionSort = CollectionSort.Default, ) : 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..66c8cd4 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/data/QueueSourceRepository.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/data/QueueSourceRepository.kt @@ -49,12 +49,7 @@ class QueueSourceRepository( // 从点开时那一页找起。那一页就在眼前,多给一页余量:点开之后 UP 又发了动态, // 这条就挪到了下一页。 is QueueContext.UpDynamics -> openUpDynamics(context, bvid) - is QueueContext.Collection -> openFeed( - feed = collectionFeed(context.mid, context.id, context.isSeason, context.page), - bvid = bvid, - label = collectionLabel(context.isSeason, context.name), - source = QueueSource(context.mid, context.id, context.isSeason, context.name), - ) + is QueueContext.Collection -> openCollection(context, bvid) is QueueContext.UpArchive -> openUpArchive(context, bvid) is QueueContext.NightMode -> openNightMode(context, bvid) is QueueContext.FavFolder -> openFavFolder(context, bvid) @@ -271,9 +266,70 @@ class QueueSourceRepository( getString(Res.string.queue_source_series, name) } - private fun collectionFeed(mid: Long, id: Long, isSeason: Boolean, anchorPage: Int, fallbackUpName: String = "") = + private suspend fun openCollection(context: QueueContext.Collection, bvid: String): OpenedQueue? { + val label = collectionLabel(context.isSeason, context.name) + val source = QueueSource(context.mid, context.id, context.isSeason, context.name) + if (context.sort == CollectionSort.Default) { + return openFeed( + feed = collectionFeed(context.mid, context.id, context.isSeason, context.page), + bvid = bvid, + label = label, + source = source, + ) + } + if (!context.isSeason) { + val reverse = context.sort == CollectionSort.Oldest + return openFeed( + feed = collectionFeed(context.mid, context.id, isSeason = false, anchorPage = context.page, reverse = reverse), + bvid = bvid, + label = label, + source = source, + ) + } + // 合集按时间排序:中小合集(<=300 条)全量拉齐后按发布时间精确排序;超大合集按首屏走向选 sort_reverse。 + val first = when (val r = spaceRepository.loadCollectionDetail(context.mid, context.id, true, 1, reverse = false)) { + is BiliResult.Ok -> r.value + else -> { + BiliLog.w("队列:拉取合集 ${context.id} 第 1 页失败") + return null + } + } + if (first.total <= COLLECTION_FULL_SORT_MAX_ITEMS) { + val all = first.items.toMutableList() + var page = 2 + while (all.size < first.total && page <= COLLECTION_FULL_SORT_MAX_PAGES) { + val next = when (val r = spaceRepository.loadCollectionDetail(context.mid, context.id, true, page, reverse = false)) { + is BiliResult.Ok -> r.value + else -> break + } + if (next.items.isEmpty()) break + all += next.items + page++ + } + val sorted = all.distinctBy { it.bvid } + .sortedByCollectionSort(context.sort) + .map { it.toQueueItem() } + return openFeed(StaticFeed(sorted), bvid, label, source) + } + val reverse = inferSeasonReverse(first.items, context.sort) + return openFeed( + feed = collectionFeed(context.mid, context.id, isSeason = true, anchorPage = context.page, reverse = reverse), + bvid = bvid, + label = label, + source = source, + ) + } + + private fun collectionFeed( + mid: Long, + id: Long, + isSeason: Boolean, + anchorPage: Int, + fallbackUpName: String = "", + reverse: Boolean = false, + ) = 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, reverse = reverse)) { 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(), @@ -442,6 +498,9 @@ class QueueSourceRepository( } private companion object { + const val COLLECTION_FULL_SORT_MAX_PAGES = 10 + const val COLLECTION_FULL_SORT_MAX_ITEMS = COLLECTION_FULL_SORT_MAX_PAGES * SpaceRepository.COLLECTION_PAGE_SIZE + /** 最多看这位 UP 的前几个系列。列表按更新时间排,在播的那个系列排在前面。 */ const val SERIES_SCAN_LIMIT = 4 diff --git a/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt b/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt index f9a7198..5e70e3a 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt @@ -76,6 +76,43 @@ enum class SpaceArchiveOrder(val apiValue: String) { Click("click"), // 最多播放 } +/** + * 合集/系列目录排序方式。 + * + * - [Default]: 保持 UP 主默认顺序(合集 `sort_reverse=false`,系列 `sort=desc`)。 + * - [Newest]: 按投稿发布时间从新到旧。 + * - [Oldest]: 按投稿发布时间从旧到新。 + */ +enum class CollectionSort { + Default, + Newest, + Oldest, +} + +/** 按 [sort] 对合集/系列视频列表做稳定排序。 */ +fun List.sortedByCollectionSort(sort: CollectionSort): List = when (sort) { + CollectionSort.Default -> this + CollectionSort.Newest -> sortedByDescending { it.publishedAtEpochSeconds } + CollectionSort.Oldest -> sortedBy { it.publishedAtEpochSeconds } +} + +/** + * 大型合集无法全量拉取时,根据默认首屏时间戳走向推断 `sort_reverse` 方向: + * 若首条发布时间早于或等于末条(合集原本正序),最新需反转(`true`)、最早不反转(`false`); + * 若原本就是倒序(首条更新),则相反。 + */ +fun inferSeasonReverse(defaultFirstPage: List, sort: CollectionSort): Boolean { + if (sort == CollectionSort.Default) return false + if (defaultFirstPage.size < 2) return sort == CollectionSort.Newest + val ascendingByDefault = defaultFirstPage.first().publishedAtEpochSeconds <= + defaultFirstPage.last().publishedAtEpochSeconds + return when (sort) { + CollectionSort.Newest -> ascendingByDefault + CollectionSort.Oldest -> !ascendingByDefault + CollectionSort.Default -> false + } +} + data class SpaceArchivePage(val total: Int, val items: List) /** @@ -384,12 +421,13 @@ class SpaceRepository(private val client: BiliClient) { id: Long, isSeason: Boolean, page: Int, + reverse: 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 reverse.toString(), "page_size" to COLLECTION_PAGE_SIZE.toString(), "page_num" to page.toString(), "web_location" to "333.1387", @@ -398,7 +436,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 (reverse) "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..115258e 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/ui/BilbyApp.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/ui/BilbyApp.kt @@ -4546,10 +4546,14 @@ private fun CollectionRoute( onBack = onBack, onLoadMore = { vm.loadMore() }, onRefresh = vm::refresh, + onSortChanged = vm::onSortChanged, 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(key.mid, key.id, key.isSeason, key.name, page, sort = state.sort), + ) }, ) } 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..ef531af 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/ui/space/CollectionScreen.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/ui/space/CollectionScreen.kt @@ -25,14 +25,18 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dev.bilby.api.BiliResult import dev.bilby.appendDistinctBy +import dev.bilby.data.CollectionSort import dev.bilby.data.SpaceRepository import dev.bilby.data.SpaceVideoItem +import dev.bilby.data.inferSeasonReverse +import dev.bilby.data.sortedByCollectionSort import dev.bilby.resources.* import dev.bilby.stringResource 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 @@ -49,6 +53,7 @@ import org.jetbrains.compose.resources.StringResource @Immutable data class CollectionUiState( val items: List = emptyList(), + val sort: CollectionSort = CollectionSort.Default, val page: Int = 1, val total: Int = 0, val loading: Boolean = false, @@ -61,6 +66,11 @@ data class CollectionUiState( /** * 一个合集/系列的目录。页面身份由 (mid, id, isSeason) 三样构成,全部来自导航参数。 + * + * 支持按默认顺序、最新发布、最早发布查看: + * - 系列(`isSeason = false`): 直接由接口参数 `sort=desc|asc` 切换正倒序。 + * - 合集(`isSeason = true`): 300 条以内全量拉齐后按发布时间精确排序并本地缓存(后续切换零请求); + * 超大合集按首屏时间戳走向推断 `sort_reverse` 方向分页拉取。 */ class CollectionViewModel( private val mid: Long, @@ -72,15 +82,109 @@ class CollectionViewModel( private val _state = MutableStateFlow(CollectionUiState()) val state: StateFlow = _state.asStateFlow() - /** 刷新会把游标退回第一页。代次让迟到的旧第一页即使参数相同也写不回来。 */ + /** 刷新或切换排序会使旧请求作废。 */ private var generation = 0L + /** 合集在默认顺序下完整缓存的条目;非空且达到 total 时切换排序直接本地重排。 */ + private var fullDefaultCache: List? = null + + /** 默认顺序首屏,用于超大合集推断 sort_reverse 方向。 */ + private var defaultFirstPage: List = emptyList() + + /** 超大合集在时间排序分页时选定的服务端反转标记。 */ + private var activeReverse: Boolean = false + init { loadMore() } + fun onSortChanged(sort: CollectionSort) { + val current = _state.value + if (current.sort == sort) return + generation++ + + // 合集全量缓存已就绪:直接本地排序,无需再发请求。 + val cached = fullDefaultCache + if (isSeason && cached != null) { + _state.update { + it.copy( + sort = sort, + items = cached.sortedByCollectionSort(sort), + loading = false, + appending = false, + refreshing = false, + hasMore = false, + error = null, + errorRes = null, + ) + } + return + } + + // 系列直接走服务端 desc/asc 分页;合集 Default 也走常规分页。 + if (!isSeason || sort == CollectionSort.Default) { + activeReverse = !isSeason && sort == CollectionSort.Oldest + _state.update { + it.copy( + sort = sort, + items = emptyList(), + page = 1, + loading = false, + appending = false, + refreshing = false, + hasMore = true, + error = null, + errorRes = null, + ) + } + loadMore(replace = true) + return + } + + // 合集切换到按时间排序:若总数 <= 300 全量拉齐后精确按发布时间排序;超大合集按首屏推断反转方向分页。 + val knownTotal = current.total + if (knownTotal in 1..FULL_SORT_MAX_ITEMS) { + loadAllSeasonForSort(sort) + } else if (knownTotal > FULL_SORT_MAX_ITEMS) { + activeReverse = inferSeasonReverse(defaultFirstPage, sort) + _state.update { + it.copy( + sort = sort, + items = emptyList(), + page = 1, + loading = false, + appending = false, + refreshing = false, + hasMore = true, + error = null, + errorRes = null, + ) + } + loadMore(replace = true) + } else { + // 首屏还未回来时就点了排序:先走全量拉取流程,首屏返回后再决定是否需要翻页。 + loadAllSeasonForSort(sort) + } + } + fun refresh() { generation++ + fullDefaultCache = null + val sort = _state.value.sort + if (isSeason && sort != CollectionSort.Default) { + _state.update { + it.copy( + refreshing = true, + loading = false, + appending = false, + error = null, + errorRes = null, + ) + } + loadAllSeasonForSort(sort, refreshing = true) + return + } + activeReverse = !isSeason && sort == CollectionSort.Oldest _state.update { it.copy( page = 1, @@ -91,33 +195,132 @@ class CollectionViewModel( loadMore(replace = true) } + private fun loadAllSeasonForSort(sort: CollectionSort, refreshing: Boolean = false) { + val requestedGeneration = generation + _state.update { + it.copy( + sort = sort, + loading = !refreshing, + appending = false, + refreshing = refreshing, + hasMore = false, + error = null, + errorRes = null, + ) + } + viewModelScope.launch { + val firstResult = repository.loadCollectionDetail(mid, id, isSeason = true, page = 1, reverse = false) + if (requestedGeneration != generation) return@launch + when (firstResult) { + is BiliResult.Ok -> { + val total = firstResult.value.total + defaultFirstPage = firstResult.value.items + if (total > FULL_SORT_MAX_ITEMS) { + activeReverse = inferSeasonReverse(defaultFirstPage, sort) + _state.update { + it.copy( + items = emptyList(), + page = 1, + total = total, + loading = false, + appending = false, + refreshing = false, + hasMore = true, + ) + } + loadMore(replace = true) + return@launch + } + val collected = firstResult.value.items.toMutableList() + var page = 2 + while (collected.size < total && page <= FULL_SORT_MAX_PAGES) { + val next = repository.loadCollectionDetail(mid, id, isSeason = true, page = page, reverse = false) + if (requestedGeneration != generation) return@launch + when (next) { + is BiliResult.Ok -> { + if (next.value.items.isEmpty()) break + collected += next.value.items + page++ + } + else -> break + } + } + val deduped = collected.distinctBy { it.bvid } + if (deduped.size >= total || collected.size < page * SpaceRepository.COLLECTION_PAGE_SIZE) { + fullDefaultCache = deduped + } + _state.update { state -> + if (requestedGeneration != generation) return@update state + state.copy( + sort = sort, + items = deduped.sortedByCollectionSort(sort), + page = page, + total = total, + loading = false, + appending = false, + refreshing = false, + hasMore = false, + error = null, + errorRes = null, + ) + } + } + + else -> _state.update { state -> + if (requestedGeneration != generation) return@update state + state.copy( + loading = false, + appending = false, + refreshing = false, + error = null, + errorRes = firstResult.errorTextRes("合集详情"), + ) + } + } + } + } + fun loadMore(replace: Boolean = false) { val current = _state.value if (current.loading || current.appending || !current.hasMore) return val firstPage = replace || current.items.isEmpty() val requestedPage = if (replace) 1 else current.page val requestedGeneration = generation + val sort = current.sort + val reverse = if (isSeason) activeReverse else sort == CollectionSort.Oldest _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, id, isSeason, requestedPage, reverse = reverse) _state.update { state -> if (requestedGeneration != generation || state.page != requestedPage) return@update state when (result) { is BiliResult.Ok -> { val pageItems = result.value.items + if (isSeason && sort == CollectionSort.Default && requestedPage == 1) { + defaultFirstPage = pageItems + } val merged = if (replace) { pageItems.distinctBy { it.bvid } } else { state.items.appendDistinctBy(pageItems) { v -> v.bvid } } + val hasMore = pageItems.isNotEmpty() && merged.size < result.value.total + if (isSeason && sort == CollectionSort.Default && !hasMore) { + fullDefaultCache = merged + } + val displayed = if (isSeason && sort != CollectionSort.Default) { + merged.sortedByCollectionSort(sort) + } else { + merged + } state.copy( - items = merged, + items = displayed, page = if (pageItems.isEmpty()) state.page else requestedPage + 1, total = result.value.total, loading = false, appending = false, refreshing = false, - hasMore = pageItems.isNotEmpty() && merged.size < result.value.total, + hasMore = hasMore, ) } @@ -132,8 +335,19 @@ class CollectionViewModel( } } } + + private companion object { + const val FULL_SORT_MAX_PAGES = 10 + const val FULL_SORT_MAX_ITEMS = FULL_SORT_MAX_PAGES * SpaceRepository.COLLECTION_PAGE_SIZE + } } +private val CollectionSortOptions = listOf( + CollectionSort.Default to Res.string.collection_sort_default, + CollectionSort.Newest to Res.string.collection_sort_newest, + CollectionSort.Oldest to Res.string.collection_sort_oldest, +) + @OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class) @Composable fun CollectionScreen( @@ -143,6 +357,7 @@ fun CollectionScreen( onLoadMore: () -> Unit, onRefresh: () -> Unit, onVideoClick: (SpaceVideoItem) -> Unit, + onSortChanged: (CollectionSort) -> Unit = {}, modifier: Modifier = Modifier, ) { val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior() @@ -198,6 +413,11 @@ fun CollectionScreen( } }, actions = { + SortMenu( + options = CollectionSortOptions, + selected = state.sort, + onSelect = onSortChanged, + ) captionSlot.buttons?.invoke() }, scrollBehavior = scrollBehavior, diff --git a/shared/src/commonTest/kotlin/dev/bilby/data/CollectionSortTest.kt b/shared/src/commonTest/kotlin/dev/bilby/data/CollectionSortTest.kt new file mode 100644 index 0000000..9053f34 --- /dev/null +++ b/shared/src/commonTest/kotlin/dev/bilby/data/CollectionSortTest.kt @@ -0,0 +1,43 @@ +package dev.bilby.data + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class CollectionSortTest { + + private fun video(bvid: String, publishedAt: Long) = SpaceVideoItem( + bvid = bvid, + title = bvid, + coverUrl = "", + durationText = "1:00", + publishedAtEpochSeconds = publishedAt, + playCountText = "1", + danmakuCountText = "0", + ) + + @Test + fun `按默认、最新与最早发布时间排序`() { + val raw = listOf( + video("BV1", 200L), + video("BV2", 500L), + video("BV3", 100L), + ) + assertEquals(listOf("BV1", "BV2", "BV3"), raw.sortedByCollectionSort(CollectionSort.Default).map { it.bvid }) + assertEquals(listOf("BV2", "BV1", "BV3"), raw.sortedByCollectionSort(CollectionSort.Newest).map { it.bvid }) + assertEquals(listOf("BV3", "BV1", "BV2"), raw.sortedByCollectionSort(CollectionSort.Oldest).map { it.bvid }) + } + + @Test + fun `超大合集依据首屏时间戳走向推断反转方向`() { + val ascSeason = listOf(video("BV1", 100L), video("BV2", 300L)) + assertTrue(inferSeasonReverse(ascSeason, CollectionSort.Newest)) + assertFalse(inferSeasonReverse(ascSeason, CollectionSort.Oldest)) + assertFalse(inferSeasonReverse(ascSeason, CollectionSort.Default)) + + val descSeason = listOf(video("BV2", 300L), video("BV1", 100L)) + assertFalse(inferSeasonReverse(descSeason, CollectionSort.Newest)) + assertTrue(inferSeasonReverse(descSeason, CollectionSort.Oldest)) + } +} From ff32c274d652a443b8ee9a072358cedb01c88fa4 Mon Sep 17 00:00:00 2001 From: NihilDigit Date: Mon, 30 Nov 2026 00:54:13 +0800 Subject: [PATCH 2/2] refactor(collection): use native server-side sort params instead of multi-page client scraping --- .../composeResources/values-en/strings.xml | 3 +- .../composeResources/values/strings.xml | 3 +- .../kotlin/dev/bilby/data/QueueContext.kt | 2 +- .../dev/bilby/data/QueueSourceRepository.kt | 68 +--- .../kotlin/dev/bilby/data/SpaceRepository.kt | 43 +-- .../kotlin/dev/bilby/ui/BilbyApp.kt | 12 +- .../dev/bilby/ui/space/CollectionScreen.kt | 338 ++++++------------ .../dev/bilby/data/CollectionSortTest.kt | 43 --- 8 files changed, 134 insertions(+), 378 deletions(-) delete mode 100644 shared/src/commonTest/kotlin/dev/bilby/data/CollectionSortTest.kt diff --git a/shared/src/commonMain/composeResources/values-en/strings.xml b/shared/src/commonMain/composeResources/values-en/strings.xml index 5b742b5..d8271bb 100644 --- a/shared/src/commonMain/composeResources/values-en/strings.xml +++ b/shared/src/commonMain/composeResources/values-en/strings.xml @@ -998,7 +998,8 @@ Show %1$d more uploads %1$d live %1$d videos - Default + Default order + Reverse order Newest Oldest %1$d steps searched diff --git a/shared/src/commonMain/composeResources/values/strings.xml b/shared/src/commonMain/composeResources/values/strings.xml index 7759fe8..7b5e9f2 100644 --- a/shared/src/commonMain/composeResources/values/strings.xml +++ b/shared/src/commonMain/composeResources/values/strings.xml @@ -1060,7 +1060,8 @@ 展开其余 %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 067ba89..f803f5e 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/data/QueueContext.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/data/QueueContext.kt @@ -30,7 +30,7 @@ sealed interface QueueContext { val isSeason: Boolean, val name: String, val page: Int = 1, - val sort: CollectionSort = CollectionSort.Default, + 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 66c8cd4..90d70f1 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/data/QueueSourceRepository.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/data/QueueSourceRepository.kt @@ -49,7 +49,12 @@ class QueueSourceRepository( // 从点开时那一页找起。那一页就在眼前,多给一页余量:点开之后 UP 又发了动态, // 这条就挪到了下一页。 is QueueContext.UpDynamics -> openUpDynamics(context, bvid) - is QueueContext.Collection -> openCollection(context, bvid) + is QueueContext.Collection -> openFeed( + 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), + ) is QueueContext.UpArchive -> openUpArchive(context, bvid) is QueueContext.NightMode -> openNightMode(context, bvid) is QueueContext.FavFolder -> openFavFolder(context, bvid) @@ -266,70 +271,16 @@ class QueueSourceRepository( getString(Res.string.queue_source_series, name) } - private suspend fun openCollection(context: QueueContext.Collection, bvid: String): OpenedQueue? { - val label = collectionLabel(context.isSeason, context.name) - val source = QueueSource(context.mid, context.id, context.isSeason, context.name) - if (context.sort == CollectionSort.Default) { - return openFeed( - feed = collectionFeed(context.mid, context.id, context.isSeason, context.page), - bvid = bvid, - label = label, - source = source, - ) - } - if (!context.isSeason) { - val reverse = context.sort == CollectionSort.Oldest - return openFeed( - feed = collectionFeed(context.mid, context.id, isSeason = false, anchorPage = context.page, reverse = reverse), - bvid = bvid, - label = label, - source = source, - ) - } - // 合集按时间排序:中小合集(<=300 条)全量拉齐后按发布时间精确排序;超大合集按首屏走向选 sort_reverse。 - val first = when (val r = spaceRepository.loadCollectionDetail(context.mid, context.id, true, 1, reverse = false)) { - is BiliResult.Ok -> r.value - else -> { - BiliLog.w("队列:拉取合集 ${context.id} 第 1 页失败") - return null - } - } - if (first.total <= COLLECTION_FULL_SORT_MAX_ITEMS) { - val all = first.items.toMutableList() - var page = 2 - while (all.size < first.total && page <= COLLECTION_FULL_SORT_MAX_PAGES) { - val next = when (val r = spaceRepository.loadCollectionDetail(context.mid, context.id, true, page, reverse = false)) { - is BiliResult.Ok -> r.value - else -> break - } - if (next.items.isEmpty()) break - all += next.items - page++ - } - val sorted = all.distinctBy { it.bvid } - .sortedByCollectionSort(context.sort) - .map { it.toQueueItem() } - return openFeed(StaticFeed(sorted), bvid, label, source) - } - val reverse = inferSeasonReverse(first.items, context.sort) - return openFeed( - feed = collectionFeed(context.mid, context.id, isSeason = true, anchorPage = context.page, reverse = reverse), - bvid = bvid, - label = label, - source = source, - ) - } - private fun collectionFeed( mid: Long, id: Long, isSeason: Boolean, anchorPage: Int, + reversed: Boolean = false, fallbackUpName: String = "", - reverse: Boolean = false, ) = PagedFeed(anchorPage, SpaceRepository.COLLECTION_PAGE_SIZE) { page -> - when (val r = spaceRepository.loadCollectionDetail(mid, id, isSeason, page, reverse = reverse)) { + 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(), @@ -498,9 +449,6 @@ class QueueSourceRepository( } private companion object { - const val COLLECTION_FULL_SORT_MAX_PAGES = 10 - const val COLLECTION_FULL_SORT_MAX_ITEMS = COLLECTION_FULL_SORT_MAX_PAGES * SpaceRepository.COLLECTION_PAGE_SIZE - /** 最多看这位 UP 的前几个系列。列表按更新时间排,在播的那个系列排在前面。 */ const val SERIES_SCAN_LIMIT = 4 diff --git a/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt b/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt index 5e70e3a..174d28d 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/data/SpaceRepository.kt @@ -76,43 +76,6 @@ enum class SpaceArchiveOrder(val apiValue: String) { Click("click"), // 最多播放 } -/** - * 合集/系列目录排序方式。 - * - * - [Default]: 保持 UP 主默认顺序(合集 `sort_reverse=false`,系列 `sort=desc`)。 - * - [Newest]: 按投稿发布时间从新到旧。 - * - [Oldest]: 按投稿发布时间从旧到新。 - */ -enum class CollectionSort { - Default, - Newest, - Oldest, -} - -/** 按 [sort] 对合集/系列视频列表做稳定排序。 */ -fun List.sortedByCollectionSort(sort: CollectionSort): List = when (sort) { - CollectionSort.Default -> this - CollectionSort.Newest -> sortedByDescending { it.publishedAtEpochSeconds } - CollectionSort.Oldest -> sortedBy { it.publishedAtEpochSeconds } -} - -/** - * 大型合集无法全量拉取时,根据默认首屏时间戳走向推断 `sort_reverse` 方向: - * 若首条发布时间早于或等于末条(合集原本正序),最新需反转(`true`)、最早不反转(`false`); - * 若原本就是倒序(首条更新),则相反。 - */ -fun inferSeasonReverse(defaultFirstPage: List, sort: CollectionSort): Boolean { - if (sort == CollectionSort.Default) return false - if (defaultFirstPage.size < 2) return sort == CollectionSort.Newest - val ascendingByDefault = defaultFirstPage.first().publishedAtEpochSeconds <= - defaultFirstPage.last().publishedAtEpochSeconds - return when (sort) { - CollectionSort.Newest -> ascendingByDefault - CollectionSort.Oldest -> !ascendingByDefault - CollectionSort.Default -> false - } -} - data class SpaceArchivePage(val total: Int, val items: List) /** @@ -421,13 +384,13 @@ class SpaceRepository(private val client: BiliClient) { id: Long, isSeason: Boolean, page: Int, - reverse: Boolean = false, + 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 reverse.toString(), + "sort_reverse" to reversed.toString(), "page_size" to COLLECTION_PAGE_SIZE.toString(), "page_num" to page.toString(), "web_location" to "333.1387", @@ -436,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 if (reverse) "asc" else "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 115258e..702f8ee 100644 --- a/shared/src/commonMain/kotlin/dev/bilby/ui/BilbyApp.kt +++ b/shared/src/commonMain/kotlin/dev/bilby/ui/BilbyApp.kt @@ -4542,17 +4542,25 @@ 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, - onSortChanged = vm::onSortChanged, + 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, sort = state.sort), + 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 ef531af..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 @@ -25,11 +29,8 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import dev.bilby.api.BiliResult import dev.bilby.appendDistinctBy -import dev.bilby.data.CollectionSort import dev.bilby.data.SpaceRepository import dev.bilby.data.SpaceVideoItem -import dev.bilby.data.inferSeasonReverse -import dev.bilby.data.sortedByCollectionSort import dev.bilby.resources.* import dev.bilby.stringResource import dev.bilby.ui.CaptionDragHeight @@ -41,6 +42,8 @@ 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 @@ -53,9 +56,9 @@ import org.jetbrains.compose.resources.StringResource @Immutable data class CollectionUiState( val items: List = emptyList(), - val sort: CollectionSort = CollectionSort.Default, val page: Int = 1, val total: Int = 0, + val reversed: Boolean = false, val loading: Boolean = false, val appending: Boolean = false, val refreshing: Boolean = false, @@ -66,11 +69,6 @@ data class CollectionUiState( /** * 一个合集/系列的目录。页面身份由 (mid, id, isSeason) 三样构成,全部来自导航参数。 - * - * 支持按默认顺序、最新发布、最早发布查看: - * - 系列(`isSeason = false`): 直接由接口参数 `sort=desc|asc` 切换正倒序。 - * - 合集(`isSeason = true`): 300 条以内全量拉齐后按发布时间精确排序并本地缓存(后续切换零请求); - * 超大合集按首屏时间戳走向推断 `sort_reverse` 方向分页拉取。 */ class CollectionViewModel( private val mid: Long, @@ -82,109 +80,34 @@ class CollectionViewModel( private val _state = MutableStateFlow(CollectionUiState()) val state: StateFlow = _state.asStateFlow() - /** 刷新或切换排序会使旧请求作废。 */ + /** 刷新或切换排序会把游标退回第一页。代次让迟到的旧响应即使参数相同也写不回来。 */ private var generation = 0L - /** 合集在默认顺序下完整缓存的条目;非空且达到 total 时切换排序直接本地重排。 */ - private var fullDefaultCache: List? = null - - /** 默认顺序首屏,用于超大合集推断 sort_reverse 方向。 */ - private var defaultFirstPage: List = emptyList() - - /** 超大合集在时间排序分页时选定的服务端反转标记。 */ - private var activeReverse: Boolean = false - init { loadMore() } - fun onSortChanged(sort: CollectionSort) { - val current = _state.value - if (current.sort == sort) return + fun setReversed(reversed: Boolean) { + if (reversed == _state.value.reversed) return generation++ - - // 合集全量缓存已就绪:直接本地排序,无需再发请求。 - val cached = fullDefaultCache - if (isSeason && cached != null) { - _state.update { - it.copy( - sort = sort, - items = cached.sortedByCollectionSort(sort), - loading = false, - appending = false, - refreshing = false, - hasMore = false, - error = null, - errorRes = null, - ) - } - return - } - - // 系列直接走服务端 desc/asc 分页;合集 Default 也走常规分页。 - if (!isSeason || sort == CollectionSort.Default) { - activeReverse = !isSeason && sort == CollectionSort.Oldest - _state.update { - it.copy( - sort = sort, - items = emptyList(), - page = 1, - loading = false, - appending = false, - refreshing = false, - hasMore = true, - error = null, - errorRes = null, - ) - } - loadMore(replace = true) - return - } - - // 合集切换到按时间排序:若总数 <= 300 全量拉齐后精确按发布时间排序;超大合集按首屏推断反转方向分页。 - val knownTotal = current.total - if (knownTotal in 1..FULL_SORT_MAX_ITEMS) { - loadAllSeasonForSort(sort) - } else if (knownTotal > FULL_SORT_MAX_ITEMS) { - activeReverse = inferSeasonReverse(defaultFirstPage, sort) - _state.update { - it.copy( - sort = sort, - items = emptyList(), - page = 1, - loading = false, - appending = false, - refreshing = false, - hasMore = true, - error = null, - errorRes = null, - ) - } - loadMore(replace = true) - } else { - // 首屏还未回来时就点了排序:先走全量拉取流程,首屏返回后再决定是否需要翻页。 - loadAllSeasonForSort(sort) + _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++ - fullDefaultCache = null - val sort = _state.value.sort - if (isSeason && sort != CollectionSort.Default) { - _state.update { - it.copy( - refreshing = true, - loading = false, - appending = false, - error = null, - errorRes = null, - ) - } - loadAllSeasonForSort(sort, refreshing = true) - return - } - activeReverse = !isSeason && sort == CollectionSort.Oldest _state.update { it.copy( page = 1, @@ -195,132 +118,45 @@ class CollectionViewModel( loadMore(replace = true) } - private fun loadAllSeasonForSort(sort: CollectionSort, refreshing: Boolean = false) { - val requestedGeneration = generation - _state.update { - it.copy( - sort = sort, - loading = !refreshing, - appending = false, - refreshing = refreshing, - hasMore = false, - error = null, - errorRes = null, - ) - } - viewModelScope.launch { - val firstResult = repository.loadCollectionDetail(mid, id, isSeason = true, page = 1, reverse = false) - if (requestedGeneration != generation) return@launch - when (firstResult) { - is BiliResult.Ok -> { - val total = firstResult.value.total - defaultFirstPage = firstResult.value.items - if (total > FULL_SORT_MAX_ITEMS) { - activeReverse = inferSeasonReverse(defaultFirstPage, sort) - _state.update { - it.copy( - items = emptyList(), - page = 1, - total = total, - loading = false, - appending = false, - refreshing = false, - hasMore = true, - ) - } - loadMore(replace = true) - return@launch - } - val collected = firstResult.value.items.toMutableList() - var page = 2 - while (collected.size < total && page <= FULL_SORT_MAX_PAGES) { - val next = repository.loadCollectionDetail(mid, id, isSeason = true, page = page, reverse = false) - if (requestedGeneration != generation) return@launch - when (next) { - is BiliResult.Ok -> { - if (next.value.items.isEmpty()) break - collected += next.value.items - page++ - } - else -> break - } - } - val deduped = collected.distinctBy { it.bvid } - if (deduped.size >= total || collected.size < page * SpaceRepository.COLLECTION_PAGE_SIZE) { - fullDefaultCache = deduped - } - _state.update { state -> - if (requestedGeneration != generation) return@update state - state.copy( - sort = sort, - items = deduped.sortedByCollectionSort(sort), - page = page, - total = total, - loading = false, - appending = false, - refreshing = false, - hasMore = false, - error = null, - errorRes = null, - ) - } - } - - else -> _state.update { state -> - if (requestedGeneration != generation) return@update state - state.copy( - loading = false, - appending = false, - refreshing = false, - error = null, - errorRes = firstResult.errorTextRes("合集详情"), - ) - } - } - } - } - fun loadMore(replace: Boolean = false) { val current = _state.value 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 - val sort = current.sort - val reverse = if (isSeason) activeReverse else sort == CollectionSort.Oldest _state.update { it.copy(loading = firstPage, appending = !firstPage, error = null, errorRes = null) } viewModelScope.launch { - val result = repository.loadCollectionDetail(mid, id, isSeason, requestedPage, reverse = reverse) + 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 - if (isSeason && sort == CollectionSort.Default && requestedPage == 1) { - defaultFirstPage = pageItems - } val merged = if (replace) { pageItems.distinctBy { it.bvid } } else { state.items.appendDistinctBy(pageItems) { v -> v.bvid } } - val hasMore = pageItems.isNotEmpty() && merged.size < result.value.total - if (isSeason && sort == CollectionSort.Default && !hasMore) { - fullDefaultCache = merged - } - val displayed = if (isSeason && sort != CollectionSort.Default) { - merged.sortedByCollectionSort(sort) - } else { - merged - } state.copy( - items = displayed, + items = merged, page = if (pageItems.isEmpty()) state.page else requestedPage + 1, total = result.value.total, loading = false, appending = false, refreshing = false, - hasMore = hasMore, + hasMore = pageItems.isNotEmpty() && merged.size < result.value.total, ) } @@ -335,35 +171,35 @@ class CollectionViewModel( } } } - - private companion object { - const val FULL_SORT_MAX_PAGES = 10 - const val FULL_SORT_MAX_ITEMS = FULL_SORT_MAX_PAGES * SpaceRepository.COLLECTION_PAGE_SIZE - } } -private val CollectionSortOptions = listOf( - CollectionSort.Default to Res.string.collection_sort_default, - CollectionSort.Newest to Res.string.collection_sort_newest, - CollectionSort.Oldest to Res.string.collection_sort_oldest, +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, - onSortChanged: (CollectionSort) -> 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), @@ -388,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+←") { @@ -413,11 +238,6 @@ fun CollectionScreen( } }, actions = { - SortMenu( - options = CollectionSortOptions, - selected = state.sort, - onSelect = onSortChanged, - ) captionSlot.buttons?.invoke() }, scrollBehavior = scrollBehavior, @@ -438,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, + ) + } + } +} diff --git a/shared/src/commonTest/kotlin/dev/bilby/data/CollectionSortTest.kt b/shared/src/commonTest/kotlin/dev/bilby/data/CollectionSortTest.kt deleted file mode 100644 index 9053f34..0000000 --- a/shared/src/commonTest/kotlin/dev/bilby/data/CollectionSortTest.kt +++ /dev/null @@ -1,43 +0,0 @@ -package dev.bilby.data - -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertTrue -import org.junit.Test - -class CollectionSortTest { - - private fun video(bvid: String, publishedAt: Long) = SpaceVideoItem( - bvid = bvid, - title = bvid, - coverUrl = "", - durationText = "1:00", - publishedAtEpochSeconds = publishedAt, - playCountText = "1", - danmakuCountText = "0", - ) - - @Test - fun `按默认、最新与最早发布时间排序`() { - val raw = listOf( - video("BV1", 200L), - video("BV2", 500L), - video("BV3", 100L), - ) - assertEquals(listOf("BV1", "BV2", "BV3"), raw.sortedByCollectionSort(CollectionSort.Default).map { it.bvid }) - assertEquals(listOf("BV2", "BV1", "BV3"), raw.sortedByCollectionSort(CollectionSort.Newest).map { it.bvid }) - assertEquals(listOf("BV3", "BV1", "BV2"), raw.sortedByCollectionSort(CollectionSort.Oldest).map { it.bvid }) - } - - @Test - fun `超大合集依据首屏时间戳走向推断反转方向`() { - val ascSeason = listOf(video("BV1", 100L), video("BV2", 300L)) - assertTrue(inferSeasonReverse(ascSeason, CollectionSort.Newest)) - assertFalse(inferSeasonReverse(ascSeason, CollectionSort.Oldest)) - assertFalse(inferSeasonReverse(ascSeason, CollectionSort.Default)) - - val descSeason = listOf(video("BV2", 300L), video("BV1", 100L)) - assertFalse(inferSeasonReverse(descSeason, CollectionSort.Newest)) - assertTrue(inferSeasonReverse(descSeason, CollectionSort.Oldest)) - } -}