funkwhale-app-android/app/src/main/java/audio/funkwhale/ffa/fragments/LandscapeQueueFragment.kt

124 lines
3.3 KiB
Kotlin
Raw Normal View History

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
import androidx.lifecycle.lifecycleScope
2019-11-05 21:23:29 +01:00
import androidx.recyclerview.widget.LinearLayoutManager
import audio.funkwhale.ffa.adapters.FavoriteListener
import audio.funkwhale.ffa.adapters.TracksAdapter
2021-07-16 10:03:52 +02:00
import audio.funkwhale.ffa.databinding.PartialQueueBinding
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!!
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)
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-07-16 10:03:52 +02:00
return binding.root.apply {
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 {
CommandBus.send(Command.ShuffleQueue)
}
2021-07-16 10:03:52 +02:00
binding.queueSave.setOnClickListener {
adapter?.data?.let {
CommandBus.send(Command.AddToPlaylist(it))
}
}
2021-07-16 10:03:52 +02:00
binding.queueClear.setOnClickListener {
CommandBus.send(Command.ClearQueue)
}
2019-11-05 21:23:29 +01:00
refresh()
}
private fun refresh() {
activity?.lifecycleScope?.launch(Main) {
2019-11-05 21:23:29 +01:00
RequestBus.send(Request.GetQueue).wait<Response.Queue>()?.let { response ->
adapter?.let {
2022-12-09 09:49:41 +01:00
it.setUnfilteredData(response.queue.toMutableList())
2019-11-05 21:23:29 +01:00
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() {
activity?.lifecycleScope?.launch(Main) {
2019-11-05 21:23:29 +01:00
EventBus.get().collect { message ->
2022-08-26 14:06:41 +02:00
if (message is Event.QueueChanged) refresh()
2019-11-05 21:23:29 +01:00
}
}
activity?.lifecycleScope?.launch(Main) {
CommandBus.get().collect { command ->
2022-08-26 14:06:41 +02:00
if (command is Command.RefreshTrack) refresh()
}
}
2019-11-05 21:23:29 +01:00
}
2021-07-02 13:55:49 +02:00
}