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

85 lines
3.1 KiB
Kotlin
Raw Normal View History

2020-10-06 08:46:04 +00:00
/*
* Copyright (c) 2020 Hemanth Savarla.
*
* 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
2018-12-06 08:52:57 +00:00
import android.os.Bundle
2020-03-01 11:29:50 +00:00
import androidx.core.text.HtmlCompat
2019-05-15 20:55:57 +00:00
import androidx.fragment.app.DialogFragment
2020-05-23 22:26:45 +00:00
import code.name.monkey.retromusic.EXTRA_SONG
2018-12-06 08:52:57 +00:00
import code.name.monkey.retromusic.R
2020-07-19 11:21:15 +00:00
import code.name.monkey.retromusic.extensions.colorButtons
2020-05-23 21:47:23 +00:00
import code.name.monkey.retromusic.extensions.extraNotNull
2020-07-19 14:39:04 +00:00
import code.name.monkey.retromusic.extensions.materialDialog
2020-09-09 12:37:25 +00:00
import code.name.monkey.retromusic.fragments.LibraryViewModel
2019-07-31 16:42:19 +00:00
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
2020-09-09 12:37:25 +00:00
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
2018-12-06 08:52:57 +00:00
2019-05-15 20:55:57 +00:00
class DeleteSongsDialog : DialogFragment() {
2020-09-09 12:37:25 +00:00
private val libraryViewModel by sharedViewModel<LibraryViewModel>()
2020-05-23 13:53:10 +00:00
2020-09-09 12:37:25 +00:00
companion object {
fun create(song: Song): DeleteSongsDialog {
val list = ArrayList<Song>()
list.add(song)
return create(list)
}
2019-07-31 16:42:19 +00:00
2020-09-09 12:37:25 +00:00
fun create(songs: List<Song>): DeleteSongsDialog {
val dialog = DeleteSongsDialog()
val args = Bundle()
args.putParcelableArrayList(EXTRA_SONG, ArrayList(songs))
dialog.arguments = args
return dialog
}
}
2019-07-31 16:42:19 +00:00
2019-05-15 20:55:57 +00:00
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
2020-05-23 22:26:45 +00:00
val songs = extraNotNull<List<Song>>(EXTRA_SONG).value
2020-08-21 18:01:52 +00:00
val pair = if (songs.size > 1) {
Pair(
2020-09-09 12:37:25 +00:00
R.string.delete_songs_title,
HtmlCompat.fromHtml(
2020-08-21 18:01:52 +00:00
String.format(getString(R.string.delete_x_songs), songs.size),
HtmlCompat.FROM_HTML_MODE_LEGACY
)
2020-05-23 21:47:23 +00:00
)
} else {
2020-08-21 18:01:52 +00:00
Pair(
R.string.delete_song_title,
HtmlCompat.fromHtml(
String.format(getString(R.string.delete_song_x), songs[0].title),
HtmlCompat.FROM_HTML_MODE_LEGACY
)
2020-05-23 21:47:23 +00:00
)
2018-12-06 08:52:57 +00:00
}
2019-07-31 16:42:19 +00:00
2020-08-21 18:01:52 +00:00
return materialDialog(pair.first)
.setMessage(pair.second)
2020-05-23 21:47:23 +00:00
.setCancelable(false)
.setPositiveButton(R.string.action_delete) { _, _ ->
2020-09-09 12:37:25 +00:00
if (songs.isNotEmpty() and (songs.size == 1) and MusicPlayerRemote.isPlaying(songs.first())) {
2020-05-23 21:47:23 +00:00
MusicPlayerRemote.playNextSong()
2019-07-31 16:42:19 +00:00
}
2020-09-09 12:37:25 +00:00
MusicUtil.deleteTracks(requireActivity(), songs)
libraryViewModel.deleteTracks(songs)
2018-12-06 08:52:57 +00:00
}
2020-05-23 21:47:23 +00:00
.create()
2020-07-19 11:21:15 +00:00
.colorButtons()
2018-12-06 08:52:57 +00:00
}
2020-10-06 08:46:04 +00:00
}