limit the home downloaded-songs preview in SQL

This commit is contained in:
austinried
2026-10-04 00:30:07 +00:00
parent 5dce881704
commit 6eb23445f1
4 changed files with 14 additions and 8 deletions
@@ -709,9 +709,12 @@ interface LibraryDao {
"LEFT JOIN albums ON albums.sourceId = songs.sourceId AND albums.id = songs.albumId " +
"JOIN song_downloads d ON d.sourceId = songs.sourceId AND d.songId = songs.id " +
"WHERE songs.sourceId = :sourceId AND d.status = 'Completed' " +
"ORDER BY d.downloadedAt DESC, songs.title COLLATE NOCASE, songs.id",
"ORDER BY d.downloadedAt DESC, songs.title COLLATE NOCASE, songs.id LIMIT :limit",
)
fun downloadedSongs(sourceId: Long): Flow<List<AlbumSongItem>>
fun downloadedSongs(
sourceId: Long,
limit: Int,
): Flow<List<AlbumSongItem>>
@Query(
"SELECT songs.id FROM songs " +
@@ -88,7 +88,10 @@ class LibraryRepository(
fun starredSongs(sourceId: Long): Flow<PagingData<AlbumSongItem>> = pager(60) { db.libraryDao().starredSongs(sourceId) }
fun downloadedSongs(sourceId: Long): Flow<List<AlbumSongItem>> = db.libraryDao().downloadedSongs(sourceId)
fun downloadedSongs(
sourceId: Long,
limit: Int,
): Flow<List<AlbumSongItem>> = db.libraryDao().downloadedSongs(sourceId, limit)
fun downloadedSongsPage(sourceId: Long): Flow<PagingData<AlbumSongItem>> = pager(60) { db.libraryDao().homeDownloadedSongs(sourceId) }
@@ -31,6 +31,7 @@ import kotlinx.coroutines.launch
const val HOME_ROW_LIMIT = 10
const val HOME_STARRED_LIMIT = 5
const val HOME_DOWNLOADED_LIMIT = 5
data class HomeFeed(
val recentlyPlayedAlbums: List<Album> = emptyList(),
@@ -81,9 +82,7 @@ class HomeViewModel(
}.cachedIn(viewModelScope)
val downloadedSongs: Flow<List<AlbumSongItem>> =
activeSource
.flatMapLatest { libraryRepository.downloadedSongs(it) }
.map { it.take(HOME_STARRED_LIMIT) }
activeSource.flatMapLatest { libraryRepository.downloadedSongs(it, HOME_DOWNLOADED_LIMIT) }
val feed: StateFlow<HomeFeed> =
libraryRepository.activeSourceId