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

77 lines
2.7 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.
*/
package code.name.monkey.retromusic.dialogs
import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.DialogFragment
2020-05-23 22:26:45 +00:00
import code.name.monkey.retromusic.EXTRA_SONG
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
import code.name.monkey.retromusic.loaders.PlaylistLoader
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.PlaylistsUtil
2020-07-19 14:39:04 +00:00
2019-03-04 03:55:09 +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))
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 {
PlaylistsUtil.addToPlaylist(
requireContext(),
2020-05-23 22:26:45 +00:00
songs,
2020-05-23 21:47:23 +00:00
playlists[which - 1].id,
true
)
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()
}
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 {
val dialog = AddToPlaylistDialog()
val args = Bundle()
2020-05-23 22:26:45 +00:00
args.putParcelableArrayList(EXTRA_SONG, ArrayList(songs))
dialog.arguments = args
return dialog
}
}
}