From 51321e33ca0ec12db4bb40e0fda89dd9b70d4607 Mon Sep 17 00:00:00 2001 From: Naveen Date: Sun, 20 Aug 2023 23:51:21 +0530 Subject: [PATCH] Properly handle show filename config --- .../musicplayer/helpers/AudioHelper.kt | 29 ++++++++++--------- .../musicplayer/helpers/SimpleMediaScanner.kt | 2 -- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/AudioHelper.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/AudioHelper.kt index d60ad857..454240ba 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/AudioHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/AudioHelper.kt @@ -10,7 +10,6 @@ import kotlin.time.Duration.Companion.milliseconds class AudioHelper(private val context: Context) { private val config = context.config - private val showFilename = config.showFilename fun insertTracks(tracks: List) { context.tracksDAO.insertAll(tracks) @@ -22,7 +21,7 @@ class AudioHelper(private val context: Context) { fun getAllTracks(): ArrayList { val tracks = context.tracksDAO.getAll() - .distinctBy { "${it.path}/${it.mediaStoreId}" } as ArrayList + .applyProperFilenames(config.showFilename) tracks.sortSafely(config.trackSorting) return tracks @@ -49,10 +48,7 @@ class AudioHelper(private val context: Context) { fun getFolderTracks(folder: String): ArrayList { val tracks = context.tracksDAO.getTracksFromFolder(folder) - .distinctBy { "${it.path}/${it.mediaStoreId}" } - .onEach { - it.title = it.getProperTitle(showFilename) - } as ArrayList + .applyProperFilenames(config.showFilename) tracks.sortSafely(config.getProperFolderSorting(folder)) return tracks @@ -91,7 +87,8 @@ class AudioHelper(private val context: Context) { } fun getArtistTracks(artistId: Long): ArrayList { - return context.tracksDAO.getTracksFromArtist(artistId) as ArrayList + return context.tracksDAO.getTracksFromArtist(artistId) + .applyProperFilenames(config.showFilename) } fun getArtistTracks(artists: List): ArrayList { @@ -126,14 +123,14 @@ class AudioHelper(private val context: Context) { fun getAlbumTracks(albumId: Long): ArrayList { val tracks = context.tracksDAO.getTracksFromAlbum(albumId) - .distinctBy { "${it.path}/${it.mediaStoreId}" } as ArrayList + .applyProperFilenames(config.showFilename) tracks.sortWith(compareBy({ it.trackId }, { it.title.lowercase() })) return tracks } fun getAlbumTracks(albums: List): ArrayList { return albums.flatMap { getAlbumTracks(it.id) } - .distinctBy { "${it.path}/${it.mediaStoreId}" } as ArrayList + .applyProperFilenames(config.showFilename) } private fun deleteAlbum(id: Long) { @@ -166,7 +163,7 @@ class AudioHelper(private val context: Context) { fun getGenreTracks(genreId: Long): ArrayList { val tracks = context.tracksDAO.getGenreTracks(genreId) - .distinctBy { "${it.path}/${it.mediaStoreId}" } as ArrayList + .applyProperFilenames(config.showFilename) tracks.sortSafely(config.trackSorting) return tracks @@ -174,7 +171,7 @@ class AudioHelper(private val context: Context) { fun getGenreTracks(genres: List): ArrayList { val tracks = genres.flatMap { context.tracksDAO.getGenreTracks(it.id) } - .distinctBy { "${it.path}/${it.mediaStoreId}" } as ArrayList + .applyProperFilenames(config.showFilename) tracks.sortSafely(config.trackSorting) return tracks @@ -197,9 +194,8 @@ class AudioHelper(private val context: Context) { } fun getPlaylistTracks(playlistId: Int): ArrayList { - val tracks = context.tracksDAO.getTracksFromPlaylist(playlistId).onEach { - it.title = it.getProperTitle(showFilename) - } as ArrayList + val tracks = context.tracksDAO.getTracksFromPlaylist(playlistId) + .applyProperFilenames(config.showFilename) tracks.sortSafely(config.getProperPlaylistSorting(playlistId)) return tracks @@ -275,3 +271,8 @@ class AudioHelper(private val context: Context) { } } } + +private fun Collection.applyProperFilenames(showFilename: Int): ArrayList { + return distinctBy { "${it.path}/${it.mediaStoreId}" } + .onEach { it.title = it.getProperTitle(showFilename) } as ArrayList +} diff --git a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/SimpleMediaScanner.kt b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/SimpleMediaScanner.kt index f78c3417..fdaa783a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/SimpleMediaScanner.kt +++ b/app/src/main/kotlin/com/simplemobiletools/musicplayer/helpers/SimpleMediaScanner.kt @@ -227,7 +227,6 @@ class SimpleMediaScanner(private val context: Application) { projection.add(Audio.Media.GENRE_ID) } - val showFilename = config.showFilename context.queryCursor(uri, projection.toTypedArray(), showErrors = true) { cursor -> val id = cursor.getLongValue(Audio.Media._ID) val title = cursor.getStringValue(Audio.Media.TITLE) @@ -265,7 +264,6 @@ class SimpleMediaScanner(private val context: Application) { coverArt = coverArt, playListId = 0, trackId = trackId, folderName = folderName, albumId = albumId, artistId = artistId, genreId = genreId, year = year, dateAdded = dateAdded, orderInPlaylist = 0 ) - track.title = track.getProperTitle(showFilename) tracks.add(track) } }