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-05 04:29:55 +00:00
|
|
|
package code.name.monkey.retromusic.dialogs
|
|
|
|
|
2019-07-30 03:41:53 +00:00
|
|
|
import android.app.Dialog
|
2018-12-05 04:29:55 +00:00
|
|
|
import android.os.Bundle
|
2019-07-30 03:41:53 +00:00
|
|
|
import androidx.fragment.app.DialogFragment
|
2020-05-23 22:26:45 +00:00
|
|
|
import code.name.monkey.retromusic.EXTRA_SONG
|
2018-12-05 04:29:55 +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
|
2018-12-05 04:29:55 +00:00
|
|
|
import code.name.monkey.retromusic.loaders.PlaylistLoader
|
|
|
|
import code.name.monkey.retromusic.model.Song
|
2020-05-05 20:41:07 +00:00
|
|
|
import code.name.monkey.retromusic.util.PlaylistsUtil
|
2020-07-19 14:39:04 +00:00
|
|
|
|
2019-03-04 03:55:09 +00:00
|
|
|
|
2019-07-30 03:41:53 +00:00
|
|
|
class AddToPlaylistDialog : DialogFragment() {
|
|
|
|
|
2019-07-30 18:30:19 +00:00
|
|
|
override fun onCreateDialog(
|
2020-02-17 11:20:08 +00:00
|
|
|
savedInstanceState: Bundle?
|
2019-07-30 18:30:19 +00:00
|
|
|
): Dialog {
|
2019-10-28 16:49:35 +00:00
|
|
|
val playlists = PlaylistLoader.getAllPlaylists(requireContext())
|
2020-07-19 14:39:04 +00:00
|
|
|
val playlistNames = mutableListOf<String>()
|
2019-10-28 16:49:35 +00:00
|
|
|
playlistNames.add(requireContext().resources.getString(R.string.action_new_playlist))
|
2020-05-05 20:41:07 +00:00
|
|
|
for (p in playlists) {
|
|
|
|
playlistNames.add(p.name)
|
2019-08-01 08:27:05 +00:00
|
|
|
}
|
2020-07-20 19:51:15 +00:00
|
|
|
|
2020-07-19 14:39:04 +00:00
|
|
|
return materialDialog(R.string.add_playlist_title)
|
2020-05-23 22:26:45 +00:00
|
|
|
.setItems(playlistNames.toTypedArray()) { _, which ->
|
|
|
|
val songs = extraNotNull<ArrayList<Song>>(EXTRA_SONG).value
|
2020-05-23 21:47:23 +00:00
|
|
|
if (which == 0) {
|
2020-05-23 22:26:45 +00:00
|
|
|
CreatePlaylistDialog.create(songs)
|
|
|
|
.show(requireActivity().supportFragmentManager, "ADD_TO_PLAYLIST")
|
2019-08-01 08:27:05 +00:00
|
|
|
} else {
|
2020-05-05 20:41:07 +00:00
|
|
|
PlaylistsUtil.addToPlaylist(
|
2020-02-26 16:49:24 +00:00
|
|
|
requireContext(),
|
2020-05-23 22:26:45 +00:00
|
|
|
songs,
|
2020-05-23 21:47:23 +00:00
|
|
|
playlists[which - 1].id,
|
2020-02-26 16:49:24 +00:00
|
|
|
true
|
2020-05-05 20:41:07 +00:00
|
|
|
)
|
2019-07-30 18:30:19 +00:00
|
|
|
}
|
2020-05-23 22:26:45 +00:00
|
|
|
dismiss()
|
2019-08-01 08:27:05 +00:00
|
|
|
}
|
2020-07-19 11:21:15 +00:00
|
|
|
.create().colorButtons()
|
2018-12-05 04:29:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
fun create(song: Song): AddToPlaylistDialog {
|
|
|
|
val list = ArrayList<Song>()
|
|
|
|
list.add(song)
|
|
|
|
return create(list)
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:20:08 +00:00
|
|
|
fun create(songs: List<Song>): AddToPlaylistDialog {
|
2018-12-05 04:29:55 +00:00
|
|
|
val dialog = AddToPlaylistDialog()
|
|
|
|
val args = Bundle()
|
2020-05-23 22:26:45 +00:00
|
|
|
args.putParcelableArrayList(EXTRA_SONG, ArrayList(songs))
|
2018-12-05 04:29:55 +00:00
|
|
|
dialog.arguments = args
|
|
|
|
return dialog
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|