PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/dialogs/DeleteSongsDialog.kt

123 lines
4.4 KiB
Kotlin
Raw Normal View History

2019-03-03 09:20:15 +00:00
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
2018-12-06 08:52:57 +00:00
package code.name.monkey.retromusic.dialogs
2019-05-15 20:55:57 +00:00
import android.app.Dialog
2019-07-31 16:42:19 +00:00
import android.content.Intent
import android.net.Uri
2018-12-06 08:52:57 +00:00
import android.os.Bundle
import android.text.Html
2019-05-15 20:55:57 +00:00
import androidx.fragment.app.DialogFragment
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.R
2019-07-31 16:42:19 +00:00
import code.name.monkey.retromusic.activities.saf.SAFGuideActivity
import code.name.monkey.retromusic.helper.MusicPlayerRemote
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.MusicUtil
2019-09-17 19:36:13 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
2019-07-31 16:42:19 +00:00
import code.name.monkey.retromusic.util.SAFUtil
2019-09-17 19:36:13 +00:00
import com.afollestad.materialdialogs.LayoutMode
2019-05-15 20:55:57 +00:00
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.bottomsheets.BottomSheet
2018-12-06 08:52:57 +00:00
2019-05-15 20:55:57 +00:00
class DeleteSongsDialog : DialogFragment() {
2019-07-31 16:42:19 +00:00
@JvmField
var currentSong: Song? = null
@JvmField
var songsToRemove: List<Song>? = null
private var deleteSongsAsyncTask: DeleteSongsAsyncTask? = null
2019-05-15 20:55:57 +00:00
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
2019-07-31 16:42:19 +00:00
val songs: ArrayList<Song>? = arguments?.getParcelableArrayList("songs")
var title = 0
var content: CharSequence = ""
if (songs != null) {
if (songs.size > 1) {
title = R.string.delete_songs_title
content = Html.fromHtml(getString(R.string.delete_x_songs, songs.size))
} else {
title = R.string.delete_song_title
content = Html.fromHtml(getString(R.string.delete_song_x, songs[0].title))
}
2018-12-06 08:52:57 +00:00
}
2019-07-31 16:42:19 +00:00
2019-09-17 19:36:13 +00:00
return MaterialDialog(requireContext(), BottomSheet(LayoutMode.WRAP_CONTENT)).show {
2019-05-15 20:55:57 +00:00
title(title)
message(text = content)
2019-07-31 16:42:19 +00:00
negativeButton(android.R.string.cancel) {
dismiss()
}
2019-09-17 19:36:13 +00:00
cornerRadius(PreferenceUtil.getInstance(requireContext()).dialogCorner)
2019-07-31 16:42:19 +00:00
noAutoDismiss()
2019-05-15 20:55:57 +00:00
positiveButton(R.string.action_delete) {
2019-07-31 16:42:19 +00:00
if (songs != null) {
if ((songs.size == 1) && MusicPlayerRemote.isPlaying(songs[0])) {
MusicPlayerRemote.playNextSong()
}
}
songsToRemove = songs
deleteSongsAsyncTask = DeleteSongsAsyncTask(this@DeleteSongsDialog)
deleteSongsAsyncTask?.execute(DeleteSongsAsyncTask.LoadingInfo(songs, null))
2018-12-06 08:52:57 +00:00
}
}
}
2019-07-31 16:42:19 +00:00
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
SAFGuideActivity.REQUEST_CODE_SAF_GUIDE -> {
SAFUtil.openTreePicker(this)
}
SAFUtil.REQUEST_SAF_PICK_TREE,
SAFUtil.REQUEST_SAF_PICK_FILE -> {
if (deleteSongsAsyncTask != null) {
deleteSongsAsyncTask?.cancel(true)
}
deleteSongsAsyncTask = DeleteSongsAsyncTask(this)
deleteSongsAsyncTask?.execute(
DeleteSongsAsyncTask.LoadingInfo(
requestCode,
resultCode,
data
)
)
2019-07-31 16:42:19 +00:00
}
}
}
fun deleteSongs(songs: List<Song>, safUris: List<Uri>?) {
2020-01-24 17:27:43 +00:00
MusicUtil.deleteTracks(requireActivity(), songs, safUris) { this.dismiss() }
2019-07-31 16:42:19 +00:00
}
2018-12-06 08:52:57 +00:00
companion object {
fun create(song: Song): DeleteSongsDialog {
val list = ArrayList<Song>()
list.add(song)
return create(list)
}
2020-02-17 11:20:08 +00:00
fun create(songs: List<Song>): DeleteSongsDialog {
2018-12-06 08:52:57 +00:00
val dialog = DeleteSongsDialog()
val args = Bundle()
2020-02-17 11:20:08 +00:00
args.putParcelableArrayList("songs", ArrayList(songs))
2018-12-06 08:52:57 +00:00
dialog.arguments = args
return dialog
}
}
}