Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions shared/src/commonMain/composeResources/values-en/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,10 @@
<string name="feed_burst_expand_rest">Show %1$d more uploads</string>
<string name="feed_live_count_badge">%1$d live</string>
<string name="collection_total_count">%1$d videos</string>
<string name="collection_sort_default">Default order</string>
<string name="collection_sort_reverse">Reverse order</string>
<string name="collection_sort_newest">Newest</string>
<string name="collection_sort_oldest">Oldest</string>
<string name="agent_steps_count">%1$d steps searched</string>
<!-- Library in-page search -->
<string name="history_search_hint">Search watch history</string>
Expand Down
4 changes: 4 additions & 0 deletions shared/src/commonMain/composeResources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,10 @@
<string name="feed_burst_expand_rest">展开其余 %1$d 条连发</string>
<string name="feed_live_count_badge">%1$d 直播</string>
<string name="collection_total_count">共 %1$d 个视频</string>
<string name="collection_sort_default">默认顺序</string>
<string name="collection_sort_reverse">倒序排列</string>
<string name="collection_sort_newest">最新发布</string>
<string name="collection_sort_oldest">最早发布</string>
<string name="agent_steps_count">已检索 %1$d 步</string>
<!-- 媒体库页内搜索 -->
<string name="history_search_hint">搜索历史记录</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sealed interface QueueContext {
val isSeason: Boolean,
val name: String,
val page: Int = 1,
val reversed: Boolean = false,
) : QueueContext

/** 空间页的投稿栏,带着当时的排序与空间内搜索词。 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,13 @@ class SpaceRepository(private val client: BiliClient) {
id: Long,
isSeason: Boolean,
page: Int,
reversed: Boolean = false,
): BiliResult<SpaceCollectionDetailPage> {
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",
Expand All @@ -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",
Expand Down
14 changes: 13 additions & 1 deletion shared/src/commonMain/kotlin/dev/bilby/ui/BilbyApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
},
)
}
Expand Down
128 changes: 113 additions & 15 deletions shared/src/commonMain/kotlin/dev/bilby/ui/space/CollectionScreen.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -51,6 +58,7 @@ data class CollectionUiState(
val items: List<SpaceVideoItem> = 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,
Expand All @@ -72,13 +80,32 @@ class CollectionViewModel(
private val _state = MutableStateFlow(CollectionUiState())
val state: StateFlow<CollectionUiState> = _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 {
Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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+←") {
Expand Down Expand Up @@ -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<Pair<Boolean, StringResource>>,
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,
)
}
}
}