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
|
|
|
|
|
2020-05-23 21:47:23 +00:00
|
|
|
import android.annotation.SuppressLint
|
2019-05-15 20:55:57 +00:00
|
|
|
import android.app.Dialog
|
2018-12-05 04:29:55 +00:00
|
|
|
import android.os.Bundle
|
2020-05-23 21:47:23 +00:00
|
|
|
import android.text.TextUtils
|
|
|
|
import android.view.LayoutInflater
|
2019-05-15 20:55:57 +00:00
|
|
|
import androidx.fragment.app.DialogFragment
|
2018-12-05 04:29:55 +00:00
|
|
|
import code.name.monkey.appthemehelper.util.MaterialUtil
|
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
|
2018-12-05 04:29:55 +00:00
|
|
|
import code.name.monkey.retromusic.model.Song
|
|
|
|
import code.name.monkey.retromusic.util.PlaylistsUtil
|
2020-05-23 21:47:23 +00:00
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
2019-05-15 20:55:57 +00:00
|
|
|
import com.google.android.material.textfield.TextInputEditText
|
|
|
|
import com.google.android.material.textfield.TextInputLayout
|
2020-05-23 22:26:45 +00:00
|
|
|
import kotlinx.android.synthetic.main.dialog_playlist.view.*
|
2019-05-15 20:55:57 +00:00
|
|
|
|
|
|
|
class CreatePlaylistDialog : DialogFragment() {
|
|
|
|
|
2020-05-23 21:47:23 +00:00
|
|
|
@SuppressLint("InflateParams")
|
2019-08-01 08:27:05 +00:00
|
|
|
override fun onCreateDialog(
|
2020-02-19 07:08:12 +00:00
|
|
|
savedInstanceState: Bundle?
|
2019-08-01 08:27:05 +00:00
|
|
|
): Dialog {
|
2020-05-23 21:47:23 +00:00
|
|
|
val view = LayoutInflater.from(requireActivity()).inflate(R.layout.dialog_playlist, null)
|
2020-05-23 22:26:45 +00:00
|
|
|
val playlistView: TextInputEditText = view.actionNewPlaylist
|
|
|
|
val playlistContainer: TextInputLayout = view.actionNewPlaylistContainer
|
2020-05-23 21:47:23 +00:00
|
|
|
MaterialUtil.setTint(playlistContainer, false)
|
2019-05-15 20:55:57 +00:00
|
|
|
|
2020-05-23 22:26:45 +00:00
|
|
|
return MaterialAlertDialogBuilder(
|
|
|
|
requireActivity(),
|
|
|
|
R.style.ThemeOverlay_MaterialComponents_Dialog_Alert
|
|
|
|
)
|
2020-05-23 21:47:23 +00:00
|
|
|
.setTitle(R.string.new_playlist_title)
|
|
|
|
.setView(view)
|
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.setPositiveButton(
|
|
|
|
R.string.create_action
|
|
|
|
) { _, _ ->
|
2020-05-23 22:26:45 +00:00
|
|
|
val extra = extraNotNull<ArrayList<Song>>(EXTRA_SONG)
|
2020-05-23 21:47:23 +00:00
|
|
|
val playlistName = playlistView.text.toString()
|
|
|
|
if (!TextUtils.isEmpty(playlistName)) {
|
|
|
|
val playlistId = PlaylistsUtil.createPlaylist(
|
|
|
|
requireContext(),
|
|
|
|
playlistView.text.toString()
|
|
|
|
)
|
|
|
|
if (playlistId != -1) {
|
|
|
|
PlaylistsUtil.addToPlaylist(requireContext(), extra.value, playlistId, true)
|
2019-05-15 20:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-19 07:08:12 +00:00
|
|
|
}
|
2020-05-23 21:47:23 +00:00
|
|
|
.create()
|
2020-07-19 11:21:15 +00:00
|
|
|
.colorButtons()
|
2020-05-23 21:47:23 +00:00
|
|
|
}
|
2019-03-28 13:32:53 +00:00
|
|
|
|
2018-12-05 04:29:55 +00:00
|
|
|
companion object {
|
|
|
|
@JvmOverloads
|
2020-02-19 07:08:12 +00:00
|
|
|
@JvmStatic
|
2018-12-05 04:29:55 +00:00
|
|
|
fun create(song: Song? = null): CreatePlaylistDialog {
|
|
|
|
val list = ArrayList<Song>()
|
|
|
|
if (song != null) {
|
|
|
|
list.add(song)
|
|
|
|
}
|
|
|
|
return create(list)
|
|
|
|
}
|
|
|
|
|
2020-02-19 07:08:12 +00:00
|
|
|
@JvmStatic
|
2018-12-05 04:29:55 +00:00
|
|
|
fun create(songs: ArrayList<Song>): CreatePlaylistDialog {
|
|
|
|
val dialog = CreatePlaylistDialog()
|
|
|
|
val args = Bundle()
|
2020-05-23 22:26:45 +00:00
|
|
|
args.putParcelableArrayList(EXTRA_SONG, songs)
|
2018-12-05 04:29:55 +00:00
|
|
|
dialog.arguments = args
|
|
|
|
return dialog
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|