Always update 'All tracks' playlist on rescan

Tracks that are explicitly removed by the user won't be added back, a string set preference is used to store the track ids deleted from 'All tracks' playlist. Of course, there are other ways to do this (with or without db modification) but using a preference turned out to be the simplest solution.
This commit is contained in:
Naveen 2023-07-06 07:56:38 +05:30
parent a020847348
commit 39780e43ae
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3
4 changed files with 31 additions and 10 deletions

View file

@ -24,6 +24,7 @@ import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.activities.SimpleActivity
import com.simplemobiletools.musicplayer.dialogs.EditDialog
import com.simplemobiletools.musicplayer.extensions.*
import com.simplemobiletools.musicplayer.helpers.ALL_TRACKS_PLAYLIST_ID
import com.simplemobiletools.musicplayer.helpers.PLAYER_SORT_BY_CUSTOM
import com.simplemobiletools.musicplayer.helpers.TagHelper
import com.simplemobiletools.musicplayer.inlines.indexOfFirstOrNull
@ -151,6 +152,15 @@ class TracksAdapter(
}
activity.tracksDAO.removeTracks(selectedTracks)
// this is to make sure these tracks aren't automatically re-added to the 'All tracks' playlist on rescan
val removedTrackIds = selectedTracks.filter { it.playListId == ALL_TRACKS_PLAYLIST_ID }.map { it.mediaStoreId.toString() }
if (removedTrackIds.isNotEmpty()) {
val config = activity.config
config.tracksRemovedFromAllTracksPlaylist = config.tracksRemovedFromAllTracksPlaylist.apply {
addAll(removedTrackIds)
}
}
EventBus.getDefault().post(Events.PlaylistsUpdated())
activity.runOnUiThread {
positions.sortDescending()
@ -234,12 +244,12 @@ class TracksAdapter(
.transform(CenterCrop(), RoundedCorners(cornerRadius))
context.getTrackCoverArt(track) { coverArt ->
activity.runOnUiThread {
Glide.with(activity)
.load(coverArt)
.apply(options)
.into(findViewById(R.id.track_image))
}
activity.runOnUiThread {
Glide.with(activity)
.load(coverArt)
.apply(options)
.into(findViewById(R.id.track_image))
}
}
track_image.beVisible()

View file

@ -109,6 +109,12 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getBoolean(WAS_ALL_TRACKS_PLAYLIST_CREATED, false)
set(wasAllTracksPlaylistCreated) = prefs.edit().putBoolean(WAS_ALL_TRACKS_PLAYLIST_CREATED, wasAllTracksPlaylistCreated).apply()
var tracksRemovedFromAllTracksPlaylist: MutableSet<String>
get() = prefs.getStringSet(TRACKS_REMOVED_FROM_ALL_TRACKS_PLAYLIST, HashSet())!!
set(tracksRemovedFromAllTracksPlaylist) = prefs.edit().remove(TRACKS_REMOVED_FROM_ALL_TRACKS_PLAYLIST)
.putStringSet(TRACKS_REMOVED_FROM_ALL_TRACKS_PLAYLIST, tracksRemovedFromAllTracksPlaylist)
.apply()
var showTabs: Int
get() = prefs.getInt(SHOW_TABS, allTabsMask)
set(showTabs) = prefs.edit().putInt(SHOW_TABS, showTabs).apply()

View file

@ -64,6 +64,7 @@ const val PLAYBACK_SPEED_PROGRESS = "PLAYBACK_SPEED_PROGRESS"
const val WERE_TRACK_FOLDERS_ADDED = "were_track_folders_added"
const val SHOW_TABS = "show_tabs"
const val WAS_ALL_TRACKS_PLAYLIST_CREATED = "was_all_tracks_playlist_created"
const val TRACKS_REMOVED_FROM_ALL_TRACKS_PLAYLIST = "tracks_removed_from_all_tracks_playlist"
const val LAST_EXPORT_PATH = "last_export_path"
const val EXCLUDED_FOLDERS = "excluded_folders"
const val SORT_PLAYLIST_PREFIX = "sort_playlist_"

View file

@ -264,12 +264,16 @@ class SimpleMediaScanner(private val context: Application) {
val allTracksLabel = context.resources.getString(R.string.all_tracks)
val playlist = Playlist(ALL_TRACKS_PLAYLIST_ID, allTracksLabel)
context.playlistDAO.insert(playlist)
tracks.forEach {
it.playListId = ALL_TRACKS_PLAYLIST_ID
}
RoomHelper(context).insertTracksWithPlaylist(tracks)
config.wasAllTracksPlaylistCreated = true
}
// avoid re-adding tracks that have been explicitly removed from 'All tracks' playlist
val excludedFolders = config.excludedFolders
val tracksRemovedFromAllTracks = config.tracksRemovedFromAllTracksPlaylist.map { it.toLong() }
val tracksWithPlaylist = tracks
.filter { it.mediaStoreId !in tracksRemovedFromAllTracks && it.playListId == 0 && it.path.getParentPath() !in excludedFolders }
.onEach { it.playListId = ALL_TRACKS_PLAYLIST_ID }
RoomHelper(context).insertTracksWithPlaylist(tracksWithPlaylist as ArrayList<Track>)
}
private fun getAllAudioFiles(): ArrayList<Track> {