2021-07-02 13:55:49 +02:00
|
|
|
package org.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
|
2019-08-19 16:50:33 +02:00
|
|
|
import android.view.View
|
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
|
2020-06-25 01:26:15 +02:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2019-08-19 16:50:33 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-07-02 13:55:49 +02:00
|
|
|
import org.funkwhale.ffa.R
|
|
|
|
import org.funkwhale.ffa.adapters.PlaylistTracksAdapter
|
|
|
|
import org.funkwhale.ffa.repositories.FavoritesRepository
|
|
|
|
import org.funkwhale.ffa.repositories.ManagementPlaylistsRepository
|
|
|
|
import org.funkwhale.ffa.repositories.PlaylistTracksRepository
|
|
|
|
import org.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.android.synthetic.main.fragment_tracks.*
|
|
|
|
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
|
|
|
|
|
2020-07-10 20:40:18 +02:00
|
|
|
class PlaylistTracksFragment : OtterFragment<PlaylistTrack, PlaylistTracksAdapter>() {
|
2019-08-19 16:50:33 +02:00
|
|
|
override val viewRes = R.layout.fragment_tracks
|
|
|
|
override val recycler: RecyclerView get() = tracks
|
|
|
|
|
2019-10-21 20:26:26 +02:00
|
|
|
lateinit var favoritesRepository: FavoritesRepository
|
2020-09-04 19:56:09 +02:00
|
|
|
lateinit var playlistsRepository: ManagementPlaylistsRepository
|
2019-10-21 20:26:26 +02:00
|
|
|
|
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") ?: ""
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:56:09 +02:00
|
|
|
adapter = PlaylistTracksAdapter(context, FavoriteListener(), PlaylistListener())
|
2019-08-19 16:50:33 +02:00
|
|
|
repository = PlaylistTracksRepository(context, albumId)
|
2019-10-21 20:26:26 +02:00
|
|
|
favoritesRepository = FavoritesRepository(context)
|
2020-09-04 19:56:09 +02:00
|
|
|
playlistsRepository = ManagementPlaylistsRepository(context)
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
watchEventBus()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
|
|
cover.visibility = View.INVISIBLE
|
|
|
|
covers.visibility = View.VISIBLE
|
|
|
|
|
|
|
|
artist.text = "Playlist"
|
|
|
|
title.text = albumTitle
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
|
2020-06-25 01:26:15 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 00:26:22 +02:00
|
|
|
var coverHeight: Float? = null
|
|
|
|
|
|
|
|
scroller.setOnScrollChangeListener { _: View?, _: Int, scrollY: Int, _: Int, _: Int ->
|
|
|
|
if (coverHeight == null) {
|
|
|
|
coverHeight = covers.measuredHeight.toFloat()
|
|
|
|
}
|
|
|
|
|
|
|
|
covers.translationY = (scrollY / 2).toFloat()
|
|
|
|
|
|
|
|
coverHeight?.let { height ->
|
|
|
|
covers.alpha = (height - scrollY.toFloat()) / height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
play.setOnClickListener {
|
|
|
|
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 ->
|
|
|
|
actions.setOnClickListener {
|
|
|
|
PopupMenu(context, actions, Gravity.START, R.attr.actionOverflowMenuStyle, 0).apply {
|
|
|
|
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) {
|
|
|
|
0 -> cover_top_left
|
|
|
|
1 -> cover_top_right
|
|
|
|
2 -> cover_bottom_left
|
|
|
|
3 -> cover_bottom_right
|
|
|
|
else -> cover_top_left
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-01 13:42:15 +01:00
|
|
|
imageView?.let { view ->
|
2020-06-25 01:26:15 +02:00
|
|
|
lifecycleScope.launch(Main) {
|
2019-11-01 13:42:15 +01:00
|
|
|
Picasso.get()
|
|
|
|
.maybeLoad(maybeNormalizeUrl(url))
|
2020-06-14 00:42:45 +02:00
|
|
|
.fit()
|
|
|
|
.centerCrop()
|
|
|
|
.transform(RoundedCornersTransformation(16, 0, corner))
|
2019-11-01 13:42:15 +01:00
|
|
|
.into(view)
|
|
|
|
}
|
2019-10-29 23:41:44 +01:00
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun watchEventBus() {
|
2020-06-25 01:26:15 +02:00
|
|
|
lifecycleScope.launch(Main) {
|
2020-06-24 14:54:13 +02:00
|
|
|
CommandBus.get().collect { command ->
|
|
|
|
when (command) {
|
|
|
|
is Command.RefreshTrack -> refreshCurrentTrack(command.track)
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 20:26:26 +02:00
|
|
|
|
2020-06-24 14:54:13 +02:00
|
|
|
private fun refreshCurrentTrack(track: Track?) {
|
|
|
|
track?.let {
|
|
|
|
adapter.currentTrack = track
|
|
|
|
adapter.notifyDataSetChanged()
|
2020-06-01 21:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 20:26:26 +02:00
|
|
|
inner class FavoriteListener : PlaylistTracksAdapter.OnFavoriteListener {
|
|
|
|
override fun onToggleFavorite(id: Int, state: Boolean) {
|
|
|
|
when (state) {
|
|
|
|
true -> favoritesRepository.addFavorite(id)
|
|
|
|
false -> favoritesRepository.deleteFavorite(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 19:56:09 +02:00
|
|
|
|
|
|
|
inner class PlaylistListener : PlaylistTracksAdapter.OnPlaylistListener {
|
2020-09-05 00:56:39 +02:00
|
|
|
override fun onMoveTrack(from: Int, to: Int) {
|
|
|
|
playlistsRepository.move(albumId, from, to)
|
|
|
|
}
|
|
|
|
|
2020-09-04 19:56:09 +02:00
|
|
|
override fun onRemoveTrackFromPlaylist(track: Track, index: Int) {
|
|
|
|
lifecycleScope.launch(Main) {
|
|
|
|
playlistsRepository.remove(albumId, track, index)
|
|
|
|
update()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 13:36:32 +02:00
|
|
|
}
|