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

fixed some problems of downloaded tab and caches #5020

Merged
merged 6 commits into from
Dec 22, 2024
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
95 changes: 37 additions & 58 deletions composeApp/src/androidMain/kotlin/it/fast4x/rimusic/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteQuery
import it.fast4x.rimusic.enums.AlbumSortBy
import it.fast4x.rimusic.enums.ArtistSortBy
import it.fast4x.rimusic.enums.BuiltInPlaylist
import it.fast4x.rimusic.enums.PlaylistSongSortBy
import it.fast4x.rimusic.enums.PlaylistSortBy
import it.fast4x.rimusic.enums.SongSortBy
Expand Down Expand Up @@ -512,14 +513,11 @@ interface Database {
FROM Song
LEFT JOIN SongAlbumMap ON Song.id = SongAlbumMap.songId
LEFT JOIN Album ON Album.id = SongAlbumMap.albumId
LEFT JOIN Format ON Format.songId = Song.id and Song.id in (:filterList)
WHERE Song.totalPlayTimeMs >= :showHidden
LEFT JOIN Format ON Format.songId = Song.id
WHERE Song.id in (:filterList)
ORDER BY Song.ROWID
""")
fun sortAllSongsByRowId_Filtered(
@MagicConstant(intValues = [1, 0]) showHidden: Int,
filterList: List<String>
): Flow<List<SongEntity>>
fun sortAllSongsByRowId_Filtered(filterList: List<String>): Flow<List<SongEntity>>

@Query("""
SELECT DISTINCT Song.*, Format.contentLength, Album.title
Expand All @@ -545,18 +543,15 @@ interface Database {
LEFT JOIN SongAlbumMap ON Song.id = SongAlbumMap.songId
LEFT JOIN Album ON Album.id = SongAlbumMap.albumId
LEFT JOIN Format ON Format.songId = Song.id
WHERE Song.totalPlayTimeMs >= :showHidden and Song.id in (:filterList)
WHERE Song.id in (:filterList)
ORDER BY
CASE
WHEN Song.title LIKE "$EXPLICIT_PREFIX%" THEN SUBSTR(Song.title, LENGTH('$EXPLICIT_PREFIX') + 1)
ELSE Song.title
END
COLLATE NOCASE
""")
fun sortAllSongsByTitle_Filtered(
@MagicConstant(intValues = [1, 0]) showHidden: Int,
filterList: List<String>
): Flow<List<SongEntity>>
fun sortAllSongsByTitle_Filtered(filterList: List<String>): Flow<List<SongEntity>>

@Query("""
SELECT DISTINCT Song.*, Format.contentLength, Album.title
Expand All @@ -577,13 +572,11 @@ interface Database {
LEFT JOIN SongAlbumMap ON Song.id = SongAlbumMap.songId
LEFT JOIN Album ON Album.id = SongAlbumMap.albumId
LEFT JOIN Format ON Format.songId = Song.id
WHERE Song.totalPlayTimeMs >= :showHidden and Song.id in (:filterList)
WHERE Song.id in (:filterList)
ORDER BY Song.totalPlayTimeMs
""")
fun sortAllSongsByPlayTime_Filtered(
@MagicConstant(intValues = [1, 0]) showHidden: Int,
filterList: List<String>
): Flow<List<SongEntity>>
filterList: List<String>): Flow<List<SongEntity>>

@Query("""
SELECT DISTINCT Song.*, Format.contentLength, Album.title
Expand All @@ -606,13 +599,10 @@ interface Database {
LEFT JOIN SongAlbumMap ON Song.id = SongAlbumMap.songId
LEFT JOIN Album ON Album.id = SongAlbumMap.albumId
LEFT JOIN Format ON Format.songId = Song.id
WHERE Song.totalPlayTimeMs >= :showHidden and Song.id in (:filterList)
WHERE Song.id in (:filterList)
ORDER BY E.timestamp
""")
fun sortAllSongsByDatePlayed_Filtered(
@MagicConstant(intValues = [1, 0]) showHidden: Int,
filterList: List<String>
): Flow<List<SongEntity>>
fun sortAllSongsByDatePlayed_Filtered(filterList: List<String>): Flow<List<SongEntity>>

@Query("""
SELECT DISTINCT Song.*, Format.contentLength, Album.title
Expand All @@ -632,14 +622,11 @@ interface Database {
FROM Song
LEFT JOIN SongAlbumMap ON Song.id = SongAlbumMap.songId
LEFT JOIN Album ON Album.id = SongAlbumMap.albumId
LEFT JOIN Format ON Format.songId = Song.id and Song.id in (:filterList)
WHERE Song.totalPlayTimeMs >= :showHidden
LEFT JOIN Format ON Format.songId = Song.id
WHERE Song.id in (:filterList)
ORDER BY Song.likedAt
""")
fun sortAllSongsByLikedAt_Filtered(
@MagicConstant(intValues = [1, 0]) showHidden: Int,
filterList: List<String>
): Flow<List<SongEntity>>
fun sortAllSongsByLikedAt_Filtered(filterList: List<String>): Flow<List<SongEntity>>

@Query("""
SELECT DISTINCT Song.*, Format.contentLength, Album.title
Expand All @@ -660,13 +647,10 @@ interface Database {
LEFT JOIN SongAlbumMap ON Song.id = SongAlbumMap.songId
LEFT JOIN Album ON Album.id = SongAlbumMap.albumId
LEFT JOIN Format ON Format.songId = Song.id
WHERE Song.totalPlayTimeMs >= :showHidden and Song.id in (:filterList)
WHERE Song.id in (:filterList)
ORDER BY Song.artistsText COLLATE NOCASE
""")
fun sortAllSongsByArtist_Filtered(
@MagicConstant(intValues = [1, 0]) showHidden: Int,
filterList: List<String>
): Flow<List<SongEntity>>
fun sortAllSongsByArtist_Filtered(filterList: List<String>): Flow<List<SongEntity>>

@Query("""
SELECT DISTINCT Song.*, Format.contentLength, Album.title
Expand All @@ -687,13 +671,10 @@ interface Database {
LEFT JOIN SongAlbumMap ON Song.id = SongAlbumMap.songId
LEFT JOIN Album ON Album.id = SongAlbumMap.albumId
LEFT JOIN Format ON Format.songId = Song.id
WHERE Song.totalPlayTimeMs >= :showHidden and Song.id in (:filterList)
WHERE Song.id in (:filterList)
ORDER BY Song.durationText
""")
fun sortAllSongsByDuration_Filtered(
@MagicConstant(intValues = [1, 0]) showHidden: Int,
filterList: List<String>
): Flow<List<SongEntity>>
fun sortAllSongsByDuration_Filtered(filterList: List<String>): Flow<List<SongEntity>>

@Query("""
SELECT DISTINCT Song.*, Format.contentLength, Album.title
Expand All @@ -714,13 +695,10 @@ interface Database {
LEFT JOIN SongAlbumMap ON Song.id = SongAlbumMap.songId
LEFT JOIN Album ON Album.id = SongAlbumMap.albumId
LEFT JOIN Format ON Format.songId = Song.id
WHERE Song.totalPlayTimeMs >= :showHidden and Song.id in (:filterList)
WHERE Song.id in (:filterList)
ORDER BY Album.title COLLATE NOCASE
""")
fun sortAllSongsByAlbum_Filtered(
@MagicConstant(intValues = [1, 0]) showHidden: Int,
filterList: List<String>
): Flow<List<SongEntity>>
fun sortAllSongsByAlbum_Filtered(filterList: List<String>): Flow<List<SongEntity>>

/**
* Fetch all songs from the database and sort them
Expand Down Expand Up @@ -751,26 +729,27 @@ interface Database {
sortBy: SongSortBy,
sortOrder: SortOrder,
@MagicConstant(intValues = [1, 0]) showHidden: Int,
filterList: List<String>
filterList: List<String>,
playList: BuiltInPlaylist
): Flow<List<SongEntity>> = when( sortBy ) {
// Due to the unknown amount of songs, letting SQLite handle
// the sorting is a better idea
SongSortBy.PlayTime -> if (filterList.isEmpty()) sortAllSongsByPlayTime( showHidden )
else sortAllSongsByPlayTime_Filtered( showHidden, filterList )
SongSortBy.Title -> if (filterList.isEmpty()) sortAllSongsByTitle( showHidden )
else sortAllSongsByTitle_Filtered( showHidden, filterList )
SongSortBy.DateAdded -> if (filterList.isEmpty()) sortAllSongsByRowId( showHidden )
else sortAllSongsByRowId_Filtered( showHidden, filterList )
SongSortBy.DatePlayed -> if (filterList.isEmpty()) sortAllSongsByDatePlayed( showHidden )
else sortAllSongsByDatePlayed_Filtered( showHidden, filterList )
SongSortBy.DateLiked -> if (filterList.isEmpty()) sortAllSongsByLikedAt( showHidden )
else sortAllSongsByLikedAt_Filtered( showHidden, filterList )
SongSortBy.Artist -> if (filterList.isEmpty()) sortAllSongsByArtist( showHidden )
else sortAllSongsByArtist_Filtered( showHidden, filterList )
SongSortBy.Duration -> if (filterList.isEmpty()) sortAllSongsByDuration( showHidden )
else sortAllSongsByDuration_Filtered( showHidden, filterList )
SongSortBy.AlbumName -> if (filterList.isEmpty()) sortAllSongsByAlbum( showHidden )
else sortAllSongsByAlbum_Filtered( showHidden, filterList )
SongSortBy.PlayTime -> if (filterList.isEmpty() && playList != BuiltInPlaylist.Downloaded) sortAllSongsByPlayTime( showHidden )
else sortAllSongsByPlayTime_Filtered(filterList )
SongSortBy.Title -> if (filterList.isEmpty() && playList != BuiltInPlaylist.Downloaded) sortAllSongsByTitle( showHidden )
else sortAllSongsByTitle_Filtered(filterList )
SongSortBy.DateAdded -> if (filterList.isEmpty() && playList != BuiltInPlaylist.Downloaded) sortAllSongsByRowId( showHidden )
else sortAllSongsByRowId_Filtered(filterList )
SongSortBy.DatePlayed -> if (filterList.isEmpty() && playList != BuiltInPlaylist.Downloaded) sortAllSongsByDatePlayed( showHidden )
else sortAllSongsByDatePlayed_Filtered(filterList )
SongSortBy.DateLiked -> if (filterList.isEmpty() && playList != BuiltInPlaylist.Downloaded) sortAllSongsByLikedAt( showHidden )
else sortAllSongsByLikedAt_Filtered(filterList )
SongSortBy.Artist -> if (filterList.isEmpty() && playList != BuiltInPlaylist.Downloaded) sortAllSongsByArtist( showHidden )
else sortAllSongsByArtist_Filtered(filterList )
SongSortBy.Duration -> if (filterList.isEmpty() && playList != BuiltInPlaylist.Downloaded) sortAllSongsByDuration( showHidden )
else sortAllSongsByDuration_Filtered(filterList )
SongSortBy.AlbumName -> if (filterList.isEmpty() && playList != BuiltInPlaylist.Downloaded) sortAllSongsByAlbum( showHidden )
else sortAllSongsByAlbum_Filtered(filterList )
}.map( sortOrder::applyTo )

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ object MyDownloadHelper {
set(download.request.id, download)
}
}
getDownloads()
// getDownloads()
}

@Synchronized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,14 @@ fun HomeSongs(

when( builtInPlaylist ) {
BuiltInPlaylist.All -> {
Database.listAllSongs( sortBy = songSort.sortBy, sortOrder = songSort.sortOrder, showHidden = hiddenSongs.isShown(), filterList = emptyList() )
Database.listAllSongs( sortBy = songSort.sortBy, sortOrder = songSort.sortOrder, showHidden = hiddenSongs.isShown(), filterList = emptyList(), BuiltInPlaylist.All)
}
BuiltInPlaylist.Downloaded -> {
val filterList = MyDownloadHelper.downloads.value.values.filter {
it.state == Download.STATE_COMPLETED
}.map { it.request.id }
println("HomeSongs: filterList: ${filterList.size} total downloads ${MyDownloadHelper.downloads.value.size}")
Database.listAllSongs( sortBy = songSort.sortBy, sortOrder = songSort.sortOrder, showHidden = hiddenSongs.isShown(), filterList = filterList )
Database.listAllSongs( sortBy = songSort.sortBy, sortOrder = songSort.sortOrder, showHidden = hiddenSongs.isShown(), filterList = filterList, BuiltInPlaylist.Downloaded)
}
BuiltInPlaylist.Favorites -> Database.listFavoriteSongs( songSort.sortBy, songSort.sortOrder )
BuiltInPlaylist.Offline -> Database.listOfflineSongs( songSort.sortBy, songSort.sortOrder )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import it.fast4x.rimusic.colorPalette
import it.fast4x.rimusic.service.MyDownloadHelper
import it.fast4x.rimusic.typography
import it.fast4x.rimusic.utils.asMediaItem
import java.io.FileInputStream
import java.io.FileOutputStream
import java.text.SimpleDateFormat
import java.util.Date
import java.util.concurrent.CancellationException
import kotlin.system.exitProcess

@SuppressLint("SuspiciousIndentation")
Expand Down Expand Up @@ -300,6 +303,14 @@ fun DataSettings() {
onConfirm = {
binder?.downloadCache?.keys?.forEach { song ->
binder.downloadCache.removeResource(song)

CoroutineScope(Dispatchers.IO).launch {
Database.song(song).collect {
val mediaItem = it?.asMediaItem ?: throw CancellationException()
MyDownloadHelper.removeDownload(context, mediaItem)
throw CancellationException()
}
}
}
}
)
Expand Down Expand Up @@ -352,7 +363,7 @@ fun DataSettings() {
SettingsDescription(text = stringResource(R.string.cache_cleared))

Coil.imageLoader(context).diskCache?.let { diskCache ->
val diskCacheSize = remember(diskCache) {
val diskCacheSize = remember(diskCache.size, cleanCacheImages) {
diskCache.size
}

Expand Down Expand Up @@ -424,7 +435,7 @@ fun DataSettings() {
}

binder?.cache?.let { cache ->
val diskCacheSize = remember(cache) {
val diskCacheSize = remember(cache.cacheSpace, cleanCacheOfflineSongs) {
cache.cacheSpace
}

Expand Down Expand Up @@ -501,7 +512,7 @@ fun DataSettings() {
}

binder?.downloadCache?.let { downloadCache ->
val diskDownloadCacheSize = remember(downloadCache) {
val diskDownloadCacheSize = remember(downloadCache.cacheSpace, cleanDownloadCache) {
downloadCache.cacheSpace
}

Expand Down