Fixed crash on song deletion
This commit is contained in:
parent
5731b777f0
commit
63263b270b
3 changed files with 68 additions and 55 deletions
|
@ -18,6 +18,9 @@ import android.app.Activity
|
|||
import android.app.Dialog
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.provider.MediaStore
|
||||
import androidx.activity.result.IntentSenderRequest
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.core.text.HtmlCompat
|
||||
import androidx.fragment.app.DialogFragment
|
||||
|
@ -61,10 +64,26 @@ class DeleteSongsDialog : DialogFragment() {
|
|||
libraryViewModel = activity?.getViewModel() as LibraryViewModel
|
||||
val songs = extraNotNull<List<Song>>(EXTRA_SONG).value
|
||||
if (VersionUtils.hasR()) {
|
||||
dismiss()
|
||||
MusicUtil.deleteTracksR(requireActivity(), songs)
|
||||
val deleteResultLauncher =
|
||||
registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
if ((songs.size == 1) && MusicPlayerRemote.isPlaying(songs[0])) {
|
||||
MusicPlayerRemote.playNextSong()
|
||||
}
|
||||
MusicPlayerRemote.removeFromQueue(songs)
|
||||
reloadTabs()
|
||||
}
|
||||
dismiss()
|
||||
}
|
||||
val pendingIntent =
|
||||
MediaStore.createDeleteRequest(requireActivity().contentResolver, songs.map {
|
||||
MusicUtil.getSongFileUri(it.id)
|
||||
})
|
||||
deleteResultLauncher.launch(
|
||||
IntentSenderRequest.Builder(pendingIntent.intentSender).build()
|
||||
)
|
||||
return super.onCreateDialog(savedInstanceState)
|
||||
} else {
|
||||
val pair = if (songs.size > 1) {
|
||||
Pair(
|
||||
R.string.delete_songs_title,
|
||||
|
@ -87,10 +106,12 @@ class DeleteSongsDialog : DialogFragment() {
|
|||
.title(pair.first)
|
||||
.message(text = pair.second)
|
||||
.noAutoDismiss()
|
||||
.negativeButton(android.R.string.cancel) {
|
||||
.negativeButton(android.R.string.cancel)
|
||||
{
|
||||
dismiss()
|
||||
}
|
||||
.positiveButton(R.string.action_delete) {
|
||||
.positiveButton(R.string.action_delete)
|
||||
{
|
||||
if ((songs.size == 1) && MusicPlayerRemote.isPlaying(songs[0])) {
|
||||
MusicPlayerRemote.playNextSong()
|
||||
}
|
||||
|
@ -112,6 +133,7 @@ class DeleteSongsDialog : DialogFragment() {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
|
|
|
@ -982,7 +982,7 @@ class MusicService : MediaBrowserServiceCompat(),
|
|||
|
||||
val originalDeletePosition = originalPlayingQueue.indexOf(song)
|
||||
if (originalDeletePosition != -1) {
|
||||
playingQueue.removeAt(originalDeletePosition)
|
||||
originalPlayingQueue.removeAt(originalDeletePosition)
|
||||
rePosition(originalDeletePosition)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -524,15 +524,6 @@ object MusicUtil : KoinComponent {
|
|||
}
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.R)
|
||||
fun deleteTracksR(activity: Activity, songs: List<Song>) {
|
||||
removeFromQueue(songs)
|
||||
val pendingIntent = MediaStore.createDeleteRequest(activity.contentResolver, songs.map {
|
||||
getSongFileUri(it.id)
|
||||
})
|
||||
activity.startIntentSenderForResult(pendingIntent.intentSender, 45, null, 0, 0, 0, null)
|
||||
}
|
||||
|
||||
fun songByGenre(genreId: Long): Song {
|
||||
return repository.getSongByGenre(genreId)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue