mirror of
https://github.com/austinried/subtracks.git
synced 2026-11-10 15:11:58 +01:00
limit the home downloaded-songs preview in SQL
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -188,7 +188,7 @@ class LibraryHomeDaoTest {
|
||||
downloads.upsert(SongDownload(sourceId, "s2", DownloadStatus.Queued))
|
||||
downloads.upsert(SongDownload(sourceId, "s3", DownloadStatus.Completed))
|
||||
|
||||
val downloaded = dao.downloadedSongs(sourceId).first()
|
||||
val downloaded = dao.downloadedSongs(sourceId, limit = 5).first()
|
||||
assertEquals(listOf("s1", "s3"), downloaded.map { it.song.id })
|
||||
assertEquals("cover-1", downloaded.first().coverArt)
|
||||
assertEquals(listOf("s1", "s3"), dao.downloadedSongIds(sourceId))
|
||||
@@ -256,7 +256,8 @@ class LibraryHomeDaoTest {
|
||||
downloads.upsert(SongDownload(sourceId, "s3", DownloadStatus.Completed, downloadedAt = 300))
|
||||
downloads.upsert(SongDownload(sourceId, "s2", DownloadStatus.Completed, downloadedAt = 200))
|
||||
|
||||
assertEquals(listOf("s3", "s2", "s1"), dao.downloadedSongs(sourceId).first().map { it.song.id })
|
||||
assertEquals(listOf("s3", "s2", "s1"), dao.downloadedSongs(sourceId, limit = 5).first().map { it.song.id })
|
||||
assertEquals(listOf("s3", "s2"), dao.downloadedSongs(sourceId, limit = 2).first().map { it.song.id })
|
||||
assertEquals(listOf("s3", "s2", "s1"), dao.downloadedSongIds(sourceId))
|
||||
assertEquals(2L, dao.downloadedSongOrdinal(sourceId, "s1"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user