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

228 lines
6.6 KiB
Kotlin
Raw Normal View History

package audio.funkwhale.ffa.fragments
2019-08-19 16:50:33 +02:00
import android.os.Bundle
2020-06-13 16:45:58 +02:00
import android.view.Gravity
2021-07-16 10:03:52 +02:00
import android.view.LayoutInflater
2019-08-19 16:50:33 +02:00
import android.view.View
2021-07-16 10:03:52 +02:00
import android.view.ViewGroup
2020-06-13 16:45:58 +02:00
import androidx.appcompat.widget.PopupMenu
2019-08-19 16:50:33 +02:00
import androidx.core.os.bundleOf
import androidx.lifecycle.lifecycleScope
2019-08-19 16:50:33 +02:00
import androidx.recyclerview.widget.RecyclerView
import audio.funkwhale.ffa.R
import audio.funkwhale.ffa.adapters.PlaylistTracksAdapter
2021-07-16 10:03:52 +02:00
import audio.funkwhale.ffa.databinding.FragmentTracksBinding
import audio.funkwhale.ffa.model.Playlist
import audio.funkwhale.ffa.model.PlaylistTrack
import audio.funkwhale.ffa.model.Track
import audio.funkwhale.ffa.repositories.FavoritesRepository
import audio.funkwhale.ffa.repositories.ManagementPlaylistsRepository
import audio.funkwhale.ffa.repositories.PlaylistTracksRepository
import audio.funkwhale.ffa.utils.*
2019-08-19 16:50:33 +02:00
import com.squareup.picasso.Picasso
2020-06-14 00:42:45 +02:00
import jp.wasabeef.picasso.transformations.RoundedCornersTransformation
2019-08-19 16:50:33 +02:00
import kotlinx.coroutines.Dispatchers.Main
2019-10-31 16:17:37 +01:00
import kotlinx.coroutines.flow.collect
2019-08-19 16:50:33 +02:00
import kotlinx.coroutines.launch
2021-07-16 10:03:52 +02:00
class PlaylistTracksFragment : FFAFragment<PlaylistTrack, PlaylistTracksAdapter>() {
override val recycler: RecyclerView get() = binding.tracks
private var _binding: FragmentTracksBinding? = null
private val binding get() = _binding!!
2019-08-19 16:50:33 +02:00
lateinit var favoritesRepository: FavoritesRepository
lateinit var playlistsRepository: ManagementPlaylistsRepository
2019-08-19 16:50:33 +02:00
var albumId = 0
var albumArtist = ""
var albumTitle = ""
var albumCover = ""
companion object {
fun new(playlist: Playlist): PlaylistTracksFragment {
return PlaylistTracksFragment().apply {
arguments = bundleOf(
"albumId" to playlist.id,
"albumArtist" to "N/A",
"albumTitle" to playlist.name,
"albumCover" to ""
)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.apply {
albumId = getInt("albumId")
albumArtist = getString("albumArtist") ?: ""
albumTitle = getString("albumTitle") ?: ""
albumCover = getString("albumCover") ?: ""
}
2021-07-16 10:03:52 +02:00
adapter = PlaylistTracksAdapter(layoutInflater, context, FavoriteListener(), PlaylistListener())
2019-08-19 16:50:33 +02:00
repository = PlaylistTracksRepository(context, albumId)
favoritesRepository = FavoritesRepository(context)
playlistsRepository = ManagementPlaylistsRepository(context)
2019-08-19 16:50:33 +02:00
watchEventBus()
}
2021-07-16 10:03:52 +02:00
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentTracksBinding.inflate(layoutInflater)
swiper = binding.swiper
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
2019-08-19 16:50:33 +02:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
2021-07-16 10:03:52 +02:00
binding.cover.visibility = View.INVISIBLE
binding.covers.visibility = View.VISIBLE
2019-08-19 16:50:33 +02:00
2021-07-16 10:03:52 +02:00
binding.artist.text = "Playlist"
binding.title.text = albumTitle
2019-08-19 16:50:33 +02:00
}
override fun onResume() {
super.onResume()
lifecycleScope.launch(Main) {
2019-08-19 16:50:33 +02:00
RequestBus.send(Request.GetCurrentTrack).wait<Response.CurrentTrack>()?.let { response ->
adapter.currentTrack = response.track
adapter.notifyDataSetChanged()
}
}
var coverHeight: Float? = null
2021-07-16 10:03:52 +02:00
binding.scroller.setOnScrollChangeListener { _: View?, _: Int, scrollY: Int, _: Int, _: Int ->
if (coverHeight == null) {
2021-07-16 10:03:52 +02:00
coverHeight = binding.covers.measuredHeight.toFloat()
}
2021-07-16 10:03:52 +02:00
binding.covers.translationY = (scrollY / 2).toFloat()
coverHeight?.let { height ->
2021-07-16 10:03:52 +02:00
binding.covers.alpha = (height - scrollY.toFloat()) / height
}
}
2021-07-16 10:03:52 +02:00
binding.play.setOnClickListener {
2019-08-19 16:50:33 +02:00
CommandBus.send(Command.ReplaceQueue(adapter.data.map { it.track }.shuffled()))
context.toast("All tracks were added to your queue")
}
2020-06-13 16:45:58 +02:00
context?.let { context ->
2021-07-16 10:03:52 +02:00
binding.actions.setOnClickListener {
PopupMenu(
context,
binding.actions,
Gravity.START,
R.attr.actionOverflowMenuStyle,
0
).apply {
2020-06-13 16:45:58 +02:00
inflate(R.menu.album)
2019-08-19 16:50:33 +02:00
2020-06-13 16:45:58 +02:00
setOnMenuItemClickListener {
when (it.itemId) {
R.id.add_to_queue -> {
CommandBus.send(Command.AddToQueue(adapter.data.map { it.track }))
context.toast("All tracks were added to your queue")
}
R.id.download -> CommandBus.send(Command.PinTracks(adapter.data.map { it.track }))
}
true
}
show()
}
}
2019-08-19 16:50:33 +02:00
}
}
override fun onDataFetched(data: List<PlaylistTrack>) {
2020-08-08 14:51:39 +02:00
data.map { it.track.album }.toSet().map { it?.cover() }.take(4).forEachIndexed { index, url ->
2019-08-19 16:50:33 +02:00
val imageView = when (index) {
2021-07-16 10:03:52 +02:00
0 -> binding.coverTopLeft
1 -> binding.coverTopRight
2 -> binding.coverBottomLeft
3 -> binding.coverBottomRight
else -> binding.coverTopLeft
2019-08-19 16:50:33 +02:00
}
2020-06-14 00:42:45 +02:00
val corner = when (index) {
0 -> RoundedCornersTransformation.CornerType.TOP_LEFT
1 -> RoundedCornersTransformation.CornerType.TOP_RIGHT
2 -> RoundedCornersTransformation.CornerType.BOTTOM_LEFT
3 -> RoundedCornersTransformation.CornerType.BOTTOM_RIGHT
else -> RoundedCornersTransformation.CornerType.TOP_LEFT
}
lifecycleScope.launch(Main) {
Picasso.get()
.maybeLoad(maybeNormalizeUrl(url))
.fit()
.centerCrop()
.transform(RoundedCornersTransformation(16, 0, corner))
.into(imageView)
}
2019-08-19 16:50:33 +02:00
}
}
private fun watchEventBus() {
lifecycleScope.launch(Main) {
CommandBus.get().collect { command ->
when (command) {
is Command.RefreshTrack -> refreshCurrentTrack(command.track)
2019-08-19 16:50:33 +02:00
}
}
}
}
private fun refreshCurrentTrack(track: Track?) {
track?.let {
adapter.currentTrack = track
adapter.notifyDataSetChanged()
}
}
inner class FavoriteListener : PlaylistTracksAdapter.OnFavoriteListener {
override fun onToggleFavorite(id: Int, state: Boolean) {
when (state) {
true -> favoritesRepository.addFavorite(id)
false -> favoritesRepository.deleteFavorite(id)
}
}
}
inner class PlaylistListener : PlaylistTracksAdapter.OnPlaylistListener {
override fun onMoveTrack(from: Int, to: Int) {
playlistsRepository.move(albumId, from, to)
}
override fun onRemoveTrackFromPlaylist(track: Track, index: Int) {
lifecycleScope.launch(Main) {
playlistsRepository.remove(albumId, index)
update()
}
}
}
2021-06-26 13:36:32 +02:00
}