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

82 lines
3.0 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.
*
*/
package code.name.monkey.retromusic.dialogs
2019-05-15 20:55:57 +00:00
import android.app.Dialog
import android.os.Bundle
2020-05-23 21:47:23 +00:00
import android.text.TextUtils
2020-09-05 18:24:05 +00:00
import androidx.core.os.bundleOf
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
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.databinding.DialogPlaylistBinding
2020-07-19 11:21:15 +00:00
import code.name.monkey.retromusic.extensions.colorButtons
2020-09-05 19:11:48 +00:00
import code.name.monkey.retromusic.extensions.extra
2020-07-19 14:39:04 +00:00
import code.name.monkey.retromusic.extensions.materialDialog
2020-09-05 18:24:05 +00:00
import code.name.monkey.retromusic.fragments.LibraryViewModel
import code.name.monkey.retromusic.model.Song
2019-05-15 20:55:57 +00:00
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
2020-09-05 18:24:05 +00:00
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
2019-05-15 20:55:57 +00:00
class CreatePlaylistDialog : DialogFragment() {
private var _binding: DialogPlaylistBinding? = null
private val binding get() = _binding!!
2020-09-05 18:24:05 +00:00
private val libraryViewModel by sharedViewModel<LibraryViewModel>()
companion object {
fun create(song: Song): CreatePlaylistDialog {
val list = mutableListOf<Song>()
list.add(song)
return create(list)
}
fun create(songs: List<Song>): CreatePlaylistDialog {
return CreatePlaylistDialog().apply {
arguments = bundleOf(EXTRA_SONG to songs)
}
}
}
2019-05-15 20:55:57 +00:00
2020-09-05 18:24:05 +00:00
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
_binding = DialogPlaylistBinding.inflate(layoutInflater)
2020-09-05 19:11:48 +00:00
val songs: List<Song> = extra<List<Song>>(EXTRA_SONG).value ?: emptyList()
val playlistView: TextInputEditText = binding.actionNewPlaylist
val playlistContainer: TextInputLayout = binding.actionNewPlaylistContainer
2020-07-19 14:39:04 +00:00
return materialDialog(R.string.new_playlist_title)
.setView(binding.root)
2020-05-23 21:47:23 +00:00
.setPositiveButton(
R.string.create_action
) { _, _ ->
val playlistName = playlistView.text.toString()
if (!TextUtils.isEmpty(playlistName)) {
2020-10-08 19:44:10 +00:00
libraryViewModel.addToPlaylist(playlistName, songs)
2020-09-05 18:24:05 +00:00
} else {
playlistContainer.error = "Playlist is can't be empty"
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
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
2020-10-06 08:46:04 +00:00
}