Properly handle deleting playlists

Also, use the newer createDeleteRequest methods
This commit is contained in:
Naveen 2023-09-05 20:01:33 +05:30
parent 1e37faa87b
commit 7eddd47038
No known key found for this signature in database
GPG key ID: 0E155DAD31671DA3

View file

@ -5,12 +5,9 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller import com.qtalk.recyclerviewfastscroller.RecyclerViewFastScroller
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.deleteFiles
import com.simplemobiletools.commons.extensions.getFilenameFromPath
import com.simplemobiletools.commons.extensions.highlightTextPart import com.simplemobiletools.commons.extensions.highlightTextPart
import com.simplemobiletools.commons.extensions.setupViewBackground import com.simplemobiletools.commons.extensions.setupViewBackground
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.musicplayer.R import com.simplemobiletools.musicplayer.R
import com.simplemobiletools.musicplayer.databinding.ItemPlaylistBinding import com.simplemobiletools.musicplayer.databinding.ItemPlaylistBinding
@ -18,6 +15,7 @@ import com.simplemobiletools.musicplayer.dialogs.NewPlaylistDialog
import com.simplemobiletools.musicplayer.dialogs.RemovePlaylistDialog import com.simplemobiletools.musicplayer.dialogs.RemovePlaylistDialog
import com.simplemobiletools.musicplayer.extensions.audioHelper import com.simplemobiletools.musicplayer.extensions.audioHelper
import com.simplemobiletools.musicplayer.extensions.config import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.inlines.indexOfFirstOrNull
import com.simplemobiletools.musicplayer.models.Events import com.simplemobiletools.musicplayer.models.Events
import com.simplemobiletools.musicplayer.models.Playlist import com.simplemobiletools.musicplayer.models.Playlist
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
@ -56,50 +54,31 @@ class PlaylistsAdapter(
} }
private fun askConfirmDelete() { private fun askConfirmDelete() {
RemovePlaylistDialog(context) { delete -> RemovePlaylistDialog(context) { deleteFiles ->
val ids = getSelectedItems().map { it.id } as ArrayList<Int> val playlists = getSelectedItems().toMutableList() as ArrayList<Playlist>
if (delete) { val ids = playlists.map { it.id } as ArrayList<Int>
if (deleteFiles) {
ensureBackgroundThread { ensureBackgroundThread {
deletePlaylistSongs(ids) { val tracksToDelete = ids.flatMap { context.audioHelper.getPlaylistTracks(it) }
removePlaylists() context.deleteTracks(tracksToDelete) {
removePlaylists(playlists)
} }
} }
} else { } else {
removePlaylists() removePlaylists(playlists)
} }
} }
} }
private fun deletePlaylistSongs(ids: ArrayList<Int>, callback: () -> Unit) { private fun removePlaylists(playlistsToDelete: ArrayList<Playlist>) {
var cnt = ids.size val positions = playlistsToDelete.mapNotNull { playlist ->
ids.map { id -> items.indexOfFirstOrNull { it.id == playlist.id }
val paths = context.audioHelper.getPlaylistTracks(id).map { it.path } } as ArrayList<Int>
val fileDirItems = paths.map { FileDirItem(it, it.getFilenameFromPath()) } as ArrayList<FileDirItem>
context.deleteFiles(fileDirItems) {
if (--cnt <= 0) {
callback()
}
}
}
}
private fun removePlaylists() {
val playlistsToDelete = ArrayList<Playlist>(selectedKeys.size)
val positions = ArrayList<Int>()
for (key in selectedKeys) {
val playlist = getItemWithKey(key) ?: continue
val position = items.indexOfFirst { it.id == key }
if (position != -1) {
positions.add(position + positionOffset)
}
playlistsToDelete.add(playlist)
}
items.removeAll(playlistsToDelete.toSet())
ensureBackgroundThread { ensureBackgroundThread {
context.audioHelper.deletePlaylists(playlistsToDelete) context.audioHelper.deletePlaylists(playlistsToDelete)
context.runOnUiThread { context.runOnUiThread {
items.removeAll(playlistsToDelete.toSet())
removeSelectedItems(positions) removeSelectedItems(positions)
} }
@ -107,8 +86,6 @@ class PlaylistsAdapter(
} }
} }
private fun getItemWithKey(key: Int): Playlist? = items.firstOrNull { it.id == key }
private fun showRenameDialog() { private fun showRenameDialog() {
NewPlaylistDialog(context, items[getItemKeyPosition(selectedKeys.first())]) { NewPlaylistDialog(context, items[getItemKeyPosition(selectedKeys.first())]) {
context.runOnUiThread { context.runOnUiThread {