Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement artist items grid continuation #1637

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Checkbox
Expand Down Expand Up @@ -56,6 +57,7 @@ import com.zionhuang.music.ui.component.IconButton
import com.zionhuang.music.ui.component.LocalMenuState
import com.zionhuang.music.ui.component.YouTubeGridItem
import com.zionhuang.music.ui.component.YouTubeListItem
import com.zionhuang.music.ui.component.shimmer.GridItemPlaceHolder
import com.zionhuang.music.ui.component.shimmer.ListItemPlaceHolder
import com.zionhuang.music.ui.component.shimmer.ShimmerHost
import com.zionhuang.music.ui.menu.YouTubeAlbumMenu
Expand All @@ -80,6 +82,7 @@ fun ArtistItemsScreen(
val mediaMetadata by playerConnection.mediaMetadata.collectAsState()

val lazyListState = rememberLazyListState()
val lazyGridState = rememberLazyGridState()
val coroutineScope = rememberCoroutineScope()

val title by viewModel.title.collectAsState()
Expand Down Expand Up @@ -117,6 +120,15 @@ fun ArtistItemsScreen(
}
}

LaunchedEffect(lazyGridState) {
snapshotFlow {
lazyGridState.layoutInfo.visibleItemsInfo.any { it.key == "loading" }
}.collect { shouldLoadMore ->
if (!shouldLoadMore) return@collect
viewModel.loadMore()
}
}

if (itemsPage == null) {
ShimmerHost(
modifier = Modifier.windowInsetsPadding(LocalPlayerAwareWindowInsets.current)
Expand Down Expand Up @@ -208,6 +220,7 @@ fun ArtistItemsScreen(
}
} else {
LazyVerticalGrid(
state = lazyGridState,
columns = GridCells.Adaptive(minSize = GridThumbnailHeight + 24.dp),
contentPadding = LocalPlayerAwareWindowInsets.current.asPaddingValues()
) {
Expand Down Expand Up @@ -268,6 +281,14 @@ fun ArtistItemsScreen(
.animateItem()
)
}

if (itemsPage?.continuation != null) {
item(key = "loading") {
ShimmerHost(Modifier.animateItem()) {
GridItemPlaceHolder(fillMaxWidth = true)
}
}
}
}
}

Expand Down
34 changes: 23 additions & 11 deletions innertube/src/main/java/com/zionhuang/innertube/YouTube.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ object YouTube {
}

suspend fun albumSongs(playlistId: String): Result<List<SongItem>> = runCatching {
var response = innerTube.browse(WEB_REMIX, "VL$playlistId").body<BrowseResponse>()
var response = innerTube.browse(WEB_REMIX, "VL$playlistId", setLogin = true).body<BrowseResponse>()
val songs = response.contents?.twoColumnBrowseResultsRenderer
?.secondaryContents?.sectionListRenderer
?.contents?.firstOrNull()
Expand All @@ -205,7 +205,7 @@ object YouTube {
}

suspend fun artist(browseId: String): Result<ArtistPage> = runCatching {
val response = innerTube.browse(WEB_REMIX, browseId).body<BrowseResponse>()
val response = innerTube.browse(WEB_REMIX, browseId, setLogin = true).body<BrowseResponse>()
ArtistPage(
artist = ArtistItem(
id = browseId,
Expand All @@ -224,7 +224,7 @@ object YouTube {
}

suspend fun artistItems(endpoint: BrowseEndpoint): Result<ArtistItemsPage> = runCatching {
val response = innerTube.browse(WEB_REMIX, endpoint.browseId, endpoint.params).body<BrowseResponse>()
val response = innerTube.browse(WEB_REMIX, endpoint.browseId, endpoint.params, setLogin = true).body<BrowseResponse>()
val gridRenderer = response.contents?.singleColumnBrowseResultsRenderer?.tabs?.firstOrNull()
?.tabRenderer?.content?.sectionListRenderer?.contents?.firstOrNull()
?.gridRenderer
Expand All @@ -236,7 +236,7 @@ object YouTube {
ArtistItemsPage.fromMusicTwoRowItemRenderer(renderer)
}
},
continuation = null
continuation = gridRenderer.continuations?.getContinuation()
)
} else {
ArtistItemsPage(
Expand All @@ -254,13 +254,25 @@ object YouTube {
}

suspend fun artistItemsContinuation(continuation: String): Result<ArtistItemsContinuationPage> = runCatching {
val response = innerTube.browse(WEB_REMIX, continuation = continuation).body<BrowseResponse>()
ArtistItemsContinuationPage(
items = response.continuationContents?.musicPlaylistShelfContinuation?.contents?.mapNotNull {
ArtistItemsContinuationPage.fromMusicResponsiveListItemRenderer(it.musicResponsiveListItemRenderer)
}!!,
continuation = response.continuationContents.musicPlaylistShelfContinuation.continuations?.getContinuation()
)
val response = innerTube.browse(WEB_REMIX, continuation = continuation, setLogin = true).body<BrowseResponse>()
val gridContinuation = response.continuationContents?.gridContinuation
if (gridContinuation != null) {
ArtistItemsContinuationPage(
items = gridContinuation.items.mapNotNull {
it.musicTwoRowItemRenderer?.let { renderer ->
ArtistItemsPage.fromMusicTwoRowItemRenderer(renderer)
}
},
continuation = gridContinuation.continuations?.getContinuation()
)
} else {
ArtistItemsContinuationPage(
items = response.continuationContents?.musicPlaylistShelfContinuation?.contents?.mapNotNull {
ArtistItemsContinuationPage.fromMusicResponsiveListItemRenderer(it.musicResponsiveListItemRenderer)
}!!,
continuation = response.continuationContents.musicPlaylistShelfContinuation.continuations?.getContinuation()
)
}
}

suspend fun playlist(playlistId: String): Result<PlaylistPage> = runCatching {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.serialization.Serializable
data class GridRenderer(
val header: Header?,
val items: List<Item>,
val continuations: List<Continuation>?,
) {
@Serializable
data class Header(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ data class NavigationEndpoint(
?: searchEndpoint
?: queueAddEndpoint
?: shareEntityEndpoint

val anyWatchEndpoint: WatchEndpoint?
get() = watchEndpoint
?: watchPlaylistEndpoint
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.zionhuang.innertube.models.response

import com.zionhuang.innertube.models.Button
import com.zionhuang.innertube.models.Continuation
import com.zionhuang.innertube.models.GridRenderer
import com.zionhuang.innertube.models.Menu
import com.zionhuang.innertube.models.MusicShelfRenderer
import com.zionhuang.innertube.models.ResponseContext
Expand Down Expand Up @@ -49,6 +50,7 @@ data class BrowseResponse(
data class ContinuationContents(
val sectionListContinuation: SectionListContinuation?,
val musicPlaylistShelfContinuation: MusicPlaylistShelfContinuation?,
val gridContinuation: GridContinuation?,
) {
@Serializable
data class SectionListContinuation(
Expand All @@ -61,6 +63,12 @@ data class BrowseResponse(
val contents: List<MusicShelfRenderer.Content>,
val continuations: List<Continuation>?,
)

@Serializable
data class GridContinuation(
val items: List<GridRenderer.Item>,
val continuations: List<Continuation>?,
)
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ data class ArtistItemsPage(
browseId = renderer.navigationEndpoint.browseEndpoint?.browseId ?: return null,
playlistId = renderer.thumbnailOverlay?.musicItemThumbnailOverlayRenderer
?.content?.musicPlayButtonRenderer?.playNavigationEndpoint
?.watchPlaylistEndpoint?.playlistId ?: return null,
?.anyWatchEndpoint?.playlistId ?: return null,
title = renderer.title.runs?.firstOrNull()?.text ?: return null,
artists = null,
year = renderer.subtitle?.runs?.lastOrNull()?.text?.toIntOrNull(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ data class ArtistPage(
browseId = renderer.navigationEndpoint.browseEndpoint?.browseId ?: return null,
playlistId = renderer.thumbnailOverlay?.musicItemThumbnailOverlayRenderer?.content
?.musicPlayButtonRenderer?.playNavigationEndpoint
?.watchPlaylistEndpoint?.playlistId ?: return null,
?.anyWatchEndpoint?.playlistId ?: return null,
title = renderer.title.runs?.firstOrNull()?.text ?: return null,
artists = null,
year = renderer.subtitle?.runs?.lastOrNull()?.text?.toIntOrNull(),
Expand Down