Skip to content

Commit

Permalink
Drop DetailedSong model
Browse files Browse the repository at this point in the history
  • Loading branch information
vfsfitvnm committed Oct 29, 2022
1 parent f3995b8 commit 3322174
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 163 deletions.
39 changes: 22 additions & 17 deletions app/src/main/kotlin/it/vfsfitvnm/vimusic/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import it.vfsfitvnm.vimusic.enums.SongSortBy
import it.vfsfitvnm.vimusic.enums.SortOrder
import it.vfsfitvnm.vimusic.models.Album
import it.vfsfitvnm.vimusic.models.Artist
import it.vfsfitvnm.vimusic.models.DetailedSong
import it.vfsfitvnm.vimusic.models.DetailedSongWithContentLength
import it.vfsfitvnm.vimusic.models.SongWithContentLength
import it.vfsfitvnm.vimusic.models.Event
import it.vfsfitvnm.vimusic.models.Format
import it.vfsfitvnm.vimusic.models.Info
import it.vfsfitvnm.vimusic.models.Playlist
import it.vfsfitvnm.vimusic.models.PlaylistPreview
import it.vfsfitvnm.vimusic.models.PlaylistWithSongs
Expand All @@ -62,34 +62,34 @@ interface Database {
@Transaction
@Query("SELECT * FROM Song WHERE totalPlayTimeMs > 0 ORDER BY ROWID ASC")
@RewriteQueriesToDropUnusedColumns
fun songsByRowIdAsc(): Flow<List<DetailedSong>>
fun songsByRowIdAsc(): Flow<List<Song>>

@Transaction
@Query("SELECT * FROM Song WHERE totalPlayTimeMs > 0 ORDER BY ROWID DESC")
@RewriteQueriesToDropUnusedColumns
fun songsByRowIdDesc(): Flow<List<DetailedSong>>
fun songsByRowIdDesc(): Flow<List<Song>>

@Transaction
@Query("SELECT * FROM Song WHERE totalPlayTimeMs > 0 ORDER BY title ASC")
@RewriteQueriesToDropUnusedColumns
fun songsByTitleAsc(): Flow<List<DetailedSong>>
fun songsByTitleAsc(): Flow<List<Song>>

@Transaction
@Query("SELECT * FROM Song WHERE totalPlayTimeMs > 0 ORDER BY title DESC")
@RewriteQueriesToDropUnusedColumns
fun songsByTitleDesc(): Flow<List<DetailedSong>>
fun songsByTitleDesc(): Flow<List<Song>>

@Transaction
@Query("SELECT * FROM Song WHERE totalPlayTimeMs > 0 ORDER BY totalPlayTimeMs ASC")
@RewriteQueriesToDropUnusedColumns
fun songsByPlayTimeAsc(): Flow<List<DetailedSong>>
fun songsByPlayTimeAsc(): Flow<List<Song>>

@Transaction
@Query("SELECT * FROM Song WHERE totalPlayTimeMs > 0 ORDER BY totalPlayTimeMs DESC")
@RewriteQueriesToDropUnusedColumns
fun songsByPlayTimeDesc(): Flow<List<DetailedSong>>
fun songsByPlayTimeDesc(): Flow<List<Song>>

fun songs(sortBy: SongSortBy, sortOrder: SortOrder): Flow<List<DetailedSong>> {
fun songs(sortBy: SongSortBy, sortOrder: SortOrder): Flow<List<Song>> {
return when (sortBy) {
SongSortBy.PlayTime -> when (sortOrder) {
SortOrder.Ascending -> songsByPlayTimeAsc()
Expand All @@ -109,7 +109,7 @@ interface Database {
@Transaction
@Query("SELECT * FROM Song WHERE likedAt IS NOT NULL ORDER BY likedAt DESC")
@RewriteQueriesToDropUnusedColumns
fun favorites(): Flow<List<DetailedSong>>
fun favorites(): Flow<List<Song>>

@Query("SELECT * FROM QueuedMediaItem")
fun queue(): List<QueuedMediaItem>
Expand Down Expand Up @@ -187,7 +187,7 @@ interface Database {
@Transaction
@Query("SELECT * FROM Song JOIN SongAlbumMap ON Song.id = SongAlbumMap.songId WHERE SongAlbumMap.albumId = :albumId AND position IS NOT NULL ORDER BY position")
@RewriteQueriesToDropUnusedColumns
fun albumSongs(albumId: String): Flow<List<DetailedSong>>
fun albumSongs(albumId: String): Flow<List<Song>>

@Query("SELECT * FROM Album WHERE bookmarkedAt IS NOT NULL ORDER BY title ASC")
fun albumsByTitleAsc(): Flow<List<Album>>
Expand Down Expand Up @@ -281,15 +281,14 @@ interface Database {
@Transaction
@Query("SELECT * FROM Song JOIN SongArtistMap ON Song.id = SongArtistMap.songId WHERE SongArtistMap.artistId = :artistId AND totalPlayTimeMs > 0 ORDER BY Song.ROWID DESC")
@RewriteQueriesToDropUnusedColumns
fun artistSongs(artistId: String): Flow<List<DetailedSong>>
fun artistSongs(artistId: String): Flow<List<Song>>

@Query("SELECT * FROM Format WHERE songId = :songId")
fun format(songId: String): Flow<Format?>

@Transaction
@Query("SELECT * FROM Song JOIN Format ON id = songId WHERE contentLength IS NOT NULL AND totalPlayTimeMs > 0 ORDER BY Song.ROWID DESC")
@RewriteQueriesToDropUnusedColumns
fun songsWithContentLength(): Flow<List<DetailedSongWithContentLength>>
@Query("SELECT Song.*, contentLength FROM Song JOIN Format ON id = songId WHERE contentLength IS NOT NULL AND totalPlayTimeMs > 0 ORDER BY Song.ROWID DESC")
fun songsWithContentLength(): Flow<List<SongWithContentLength>>

@Query("""
UPDATE SongPlaylistMap SET position =
Expand All @@ -312,12 +311,18 @@ interface Database {
fun loudnessDb(songId: String): Flow<Float?>

@Query("SELECT * FROM Song WHERE title LIKE :query OR artistsText LIKE :query")
fun search(query: String): Flow<List<DetailedSong>>
fun search(query: String): Flow<List<Song>>

@Query("SELECT albumId AS id, NULL AS name FROM SongAlbumMap WHERE songId = :songId")
fun songAlbumInfo(songId: String): Info

@Query("SELECT id, name FROM Artist LEFT JOIN SongArtistMap ON id = artistId WHERE songId = :songId")
fun songArtistInfo(songId: String): List<Info>

@Transaction
@Query("SELECT Song.* FROM Event JOIN Song ON Song.id = songId GROUP BY songId ORDER BY SUM(CAST(playTime AS REAL) / (((:now - timestamp) / 86400000) + 1)) DESC LIMIT 1")
@RewriteQueriesToDropUnusedColumns
fun trending(now: Long = System.currentTimeMillis()): Flow<DetailedSong?>
fun trending(now: Long = System.currentTimeMillis()): Flow<Song?>

@Query("SELECT COUNT (*) FROM Event")
fun eventsCount(): Flow<Int>
Expand Down
47 changes: 0 additions & 47 deletions app/src/main/kotlin/it/vfsfitvnm/vimusic/models/DetailedSong.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ data class PlaylistWithSongs(
entityColumn = "songId"
)
)
val songs: List<DetailedSong>
val songs: List<Song>
)
13 changes: 13 additions & 0 deletions app/src/main/kotlin/it/vfsfitvnm/vimusic/models/Song.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ data class Song(
val likedAt: Long? = null,
val totalPlayTimeMs: Long = 0
) {
val formattedTotalPlayTime: String
get() {
val seconds = totalPlayTimeMs / 1000

val hours = seconds / 3600

return when {
hours == 0L -> "${seconds / 60}m"
hours < 24L -> "${hours}h"
else -> "${hours / 24}d"
}
}

fun toggleLike(): Song {
return copy(
likedAt = if (likedAt == null) System.currentTimeMillis() else null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.vfsfitvnm.vimusic.models

import androidx.compose.runtime.Immutable
import androidx.room.Embedded

@Immutable
data class SongWithContentLength(
@Embedded val song: Song,
val contentLength: Long?
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import androidx.media3.datasource.cache.Cache
import it.vfsfitvnm.vimusic.Database
import it.vfsfitvnm.vimusic.R
import it.vfsfitvnm.vimusic.models.Album
import it.vfsfitvnm.vimusic.models.DetailedSong
import it.vfsfitvnm.vimusic.models.PlaylistPreview
import it.vfsfitvnm.vimusic.models.Song
import it.vfsfitvnm.vimusic.models.SongWithContentLength
import it.vfsfitvnm.vimusic.utils.asMediaItem
import it.vfsfitvnm.vimusic.utils.forcePlayAtIndex
import it.vfsfitvnm.vimusic.utils.forceSeekToNext
Expand All @@ -36,7 +37,7 @@ import kotlinx.coroutines.withContext

class PlayerMediaBrowserService : MediaBrowserService(), ServiceConnection {
private val coroutineScope = CoroutineScope(Dispatchers.IO)
private var lastSongs = emptyList<DetailedSong>()
private var lastSongs = emptyList<Song>()

private var bound = false

Expand Down Expand Up @@ -187,7 +188,7 @@ class PlayerMediaBrowserService : MediaBrowserService(), ServiceConnection {
BrowserMediaItem.FLAG_PLAYABLE
)

private val DetailedSong.asBrowserMediaItem
private val Song.asBrowserMediaItem
inline get() = BrowserMediaItem(
BrowserMediaDescription.Builder()
.setMediaId(MediaId.forSong(id))
Expand Down Expand Up @@ -254,9 +255,10 @@ class PlayerMediaBrowserService : MediaBrowserService(), ServiceConnection {
.first()
.filter { song ->
song.contentLength?.let {
cache.isCached(song.id, 0, song.contentLength)
cache.isCached(song.song.id, 0, it)
} ?: false
}
.map(SongWithContentLength::song)
.shuffled()

MediaId.playlists -> data
Expand All @@ -273,7 +275,7 @@ class PlayerMediaBrowserService : MediaBrowserService(), ServiceConnection {
?.first()

else -> emptyList()
}?.map(DetailedSong::asMediaItem) ?: return@launch
}?.map(Song::asMediaItem) ?: return@launch

withContext(Dispatchers.Main) {
player.forcePlayAtIndex(mediaItems, index.coerceIn(0, mediaItems.size))
Expand Down
Loading

0 comments on commit 3322174

Please sign in to comment.