2021-07-12 10:14:26 +02:00
|
|
|
package audio.funkwhale.ffa.fragments
|
2019-11-05 21:23:29 +01:00
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.fragment.app.Fragment
|
2020-06-25 01:26:15 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2019-11-05 21:23:29 +01:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
2021-09-10 13:36:28 +02:00
|
|
|
import audio.funkwhale.ffa.adapters.FavoriteListener
|
2021-07-12 10:14:26 +02:00
|
|
|
import audio.funkwhale.ffa.adapters.TracksAdapter
|
2021-07-16 10:03:52 +02:00
|
|
|
import audio.funkwhale.ffa.databinding.PartialQueueBinding
|
2021-09-10 13:36:28 +02:00
|
|
|
import audio.funkwhale.ffa.repositories.FavoritesRepository
|
2021-07-16 10:03:52 +02:00
|
|
|
import audio.funkwhale.ffa.utils.Command
|
|
|
|
import audio.funkwhale.ffa.utils.CommandBus
|
|
|
|
import audio.funkwhale.ffa.utils.Event
|
|
|
|
import audio.funkwhale.ffa.utils.EventBus
|
|
|
|
import audio.funkwhale.ffa.utils.Request
|
|
|
|
import audio.funkwhale.ffa.utils.RequestBus
|
|
|
|
import audio.funkwhale.ffa.utils.Response
|
|
|
|
import audio.funkwhale.ffa.utils.wait
|
2019-11-05 21:23:29 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
|
|
|
import kotlinx.coroutines.flow.collect
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
class LandscapeQueueFragment : Fragment() {
|
2021-07-16 10:03:52 +02:00
|
|
|
|
|
|
|
private var _binding: PartialQueueBinding? = null
|
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
2021-09-10 13:36:28 +02:00
|
|
|
lateinit var favoritesRepository: FavoritesRepository
|
|
|
|
|
2019-11-05 21:23:29 +01:00
|
|
|
private var adapter: TracksAdapter? = null
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
2021-09-17 19:14:18 +02:00
|
|
|
favoritesRepository = FavoritesRepository(context)
|
|
|
|
|
2019-11-05 21:23:29 +01:00
|
|
|
watchEventBus()
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View {
|
|
|
|
_binding = PartialQueueBinding.inflate(inflater)
|
2021-09-17 19:14:18 +02:00
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
return binding.root.apply {
|
2021-09-10 13:36:28 +02:00
|
|
|
adapter = TracksAdapter(
|
|
|
|
layoutInflater,
|
|
|
|
context,
|
|
|
|
fromQueue = true,
|
|
|
|
favoriteListener = FavoriteListener(favoritesRepository)
|
|
|
|
).also {
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.queue.layoutManager = LinearLayoutManager(context)
|
|
|
|
binding.queue.adapter = it
|
2019-11-05 21:23:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
_binding = null
|
|
|
|
}
|
|
|
|
|
2019-11-05 21:23:29 +01:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.queue.visibility = View.GONE
|
|
|
|
binding.placeholder.visibility = View.VISIBLE
|
2019-11-05 21:23:29 +01:00
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.queueShuffle.setOnClickListener {
|
2020-09-05 16:12:22 +02:00
|
|
|
CommandBus.send(Command.ShuffleQueue)
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.queueSave.setOnClickListener {
|
2020-09-26 17:58:06 +02:00
|
|
|
adapter?.data?.let {
|
|
|
|
CommandBus.send(Command.AddToPlaylist(it))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.queueClear.setOnClickListener {
|
2020-09-05 16:12:22 +02:00
|
|
|
CommandBus.send(Command.ClearQueue)
|
|
|
|
}
|
|
|
|
|
2019-11-05 21:23:29 +01:00
|
|
|
refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun refresh() {
|
2020-06-25 01:26:15 +02:00
|
|
|
activity?.lifecycleScope?.launch(Main) {
|
2019-11-05 21:23:29 +01:00
|
|
|
RequestBus.send(Request.GetQueue).wait<Response.Queue>()?.let { response ->
|
|
|
|
adapter?.let {
|
|
|
|
it.data = response.queue.toMutableList()
|
|
|
|
it.notifyDataSetChanged()
|
|
|
|
|
|
|
|
if (it.data.isEmpty()) {
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.queue.visibility = View.GONE
|
|
|
|
binding.placeholder.visibility = View.VISIBLE
|
2019-11-05 21:23:29 +01:00
|
|
|
} else {
|
2021-07-16 10:03:52 +02:00
|
|
|
binding.queue.visibility = View.VISIBLE
|
|
|
|
binding.placeholder.visibility = View.GONE
|
2019-11-05 21:23:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun watchEventBus() {
|
2020-06-25 01:26:15 +02:00
|
|
|
activity?.lifecycleScope?.launch(Main) {
|
2019-11-05 21:23:29 +01:00
|
|
|
EventBus.get().collect { message ->
|
|
|
|
when (message) {
|
|
|
|
is Event.QueueChanged -> refresh()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 14:54:13 +02:00
|
|
|
|
2020-06-25 01:26:15 +02:00
|
|
|
activity?.lifecycleScope?.launch(Main) {
|
2020-06-24 14:54:13 +02:00
|
|
|
CommandBus.get().collect { command ->
|
|
|
|
when (command) {
|
|
|
|
is Command.RefreshTrack -> refresh()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 21:23:29 +01:00
|
|
|
}
|
2021-07-02 13:55:49 +02:00
|
|
|
}
|