2021-07-12 10:14:26 +02:00
|
|
|
package audio.funkwhale.ffa.adapters
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.content.Context
|
2020-06-20 15:42:10 +02:00
|
|
|
import android.graphics.PorterDuff
|
|
|
|
import android.graphics.PorterDuffColorFilter
|
2019-08-19 16:50:33 +02:00
|
|
|
import android.view.Gravity
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.appcompat.widget.PopupMenu
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-07-12 10:14:26 +02:00
|
|
|
import audio.funkwhale.ffa.R
|
2021-07-16 10:03:52 +02:00
|
|
|
import audio.funkwhale.ffa.databinding.RowTrackBinding
|
|
|
|
import audio.funkwhale.ffa.fragments.FFAAdapter
|
2021-09-09 09:56:15 +02:00
|
|
|
import audio.funkwhale.ffa.model.Track
|
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.maybeLoad
|
|
|
|
import audio.funkwhale.ffa.utils.maybeNormalizeUrl
|
|
|
|
import audio.funkwhale.ffa.utils.toast
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.squareup.picasso.Picasso
|
|
|
|
import jp.wasabeef.picasso.transformations.RoundedCornersTransformation
|
2021-07-16 10:03:52 +02:00
|
|
|
import java.util.Collections
|
|
|
|
|
|
|
|
class FavoritesAdapter(
|
|
|
|
private val layoutInflater: LayoutInflater,
|
|
|
|
private val context: Context?,
|
2021-09-10 13:36:28 +02:00
|
|
|
private val favoriteListener: FavoriteListener,
|
|
|
|
val fromQueue: Boolean = false,
|
2021-07-16 10:03:52 +02:00
|
|
|
) : FFAAdapter<Track, FavoritesAdapter.ViewHolder>() {
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2021-09-10 09:44:36 +02:00
|
|
|
init {
|
|
|
|
this.stateRestorationPolicy = StateRestorationPolicy.PREVENT_WHEN_EMPTY
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
private lateinit var binding: RowTrackBinding
|
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
var currentTrack: Track? = null
|
|
|
|
|
|
|
|
override fun getItemCount() = data.size
|
|
|
|
|
|
|
|
override fun getItemId(position: Int): Long {
|
2019-10-23 20:21:18 +02:00
|
|
|
return data[position].id.toLong()
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
binding = RowTrackBinding.inflate(layoutInflater, parent, false)
|
|
|
|
|
|
|
|
return ViewHolder(binding, context).also {
|
|
|
|
binding.root.setOnClickListener(it)
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("NewApi")
|
|
|
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
|
|
val favorite = data[position]
|
|
|
|
|
|
|
|
Picasso.get()
|
2020-08-08 14:51:39 +02:00
|
|
|
.maybeLoad(maybeNormalizeUrl(favorite.album?.cover()))
|
2019-08-19 16:50:33 +02:00
|
|
|
.fit()
|
|
|
|
.placeholder(R.drawable.cover)
|
|
|
|
.transform(RoundedCornersTransformation(16, 0))
|
|
|
|
.into(holder.cover)
|
|
|
|
|
2019-10-23 20:21:18 +02:00
|
|
|
holder.title.text = favorite.title
|
|
|
|
holder.artist.text = favorite.artist.name
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2020-06-26 19:42:10 +02:00
|
|
|
context?.let {
|
|
|
|
holder.itemView.background = context.getDrawable(R.drawable.ripple)
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2019-11-16 16:43:01 +01:00
|
|
|
if (favorite.id == currentTrack?.id) {
|
2020-06-26 19:42:10 +02:00
|
|
|
context?.let {
|
|
|
|
holder.itemView.background = context.getDrawable(R.drawable.current)
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
context?.let {
|
2019-10-23 20:21:18 +02:00
|
|
|
when (favorite.favorite) {
|
2019-10-21 19:49:48 +02:00
|
|
|
true -> holder.favorite.setColorFilter(context.getColor(R.color.colorFavorite))
|
|
|
|
false -> holder.favorite.setColorFilter(context.getColor(R.color.colorSelected))
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 15:42:10 +02:00
|
|
|
when (favorite.cached || favorite.downloaded) {
|
2020-06-13 19:50:02 +02:00
|
|
|
true -> holder.title.setCompoundDrawablesWithIntrinsicBounds(R.drawable.downloaded, 0, 0, 0)
|
|
|
|
false -> holder.title.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
|
|
|
|
}
|
|
|
|
|
2020-06-20 15:42:10 +02:00
|
|
|
if (favorite.cached && !favorite.downloaded) {
|
|
|
|
holder.title.compoundDrawables.forEach {
|
2021-07-16 10:03:52 +02:00
|
|
|
it?.colorFilter =
|
|
|
|
PorterDuffColorFilter(context.getColor(R.color.cached), PorterDuff.Mode.SRC_IN)
|
2020-06-20 15:42:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (favorite.downloaded) {
|
|
|
|
holder.title.compoundDrawables.forEach {
|
2021-07-16 10:03:52 +02:00
|
|
|
it?.colorFilter =
|
|
|
|
PorterDuffColorFilter(context.getColor(R.color.downloaded), PorterDuff.Mode.SRC_IN)
|
2020-06-20 15:42:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
holder.favorite.setOnClickListener {
|
2019-10-23 20:21:18 +02:00
|
|
|
favoriteListener.onToggleFavorite(favorite.id, !favorite.favorite)
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
data.remove(favorite)
|
2021-08-27 13:28:23 +02:00
|
|
|
notifyItemRemoved(holder.bindingAdapterPosition)
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
holder.actions.setOnClickListener {
|
|
|
|
context?.let { context ->
|
|
|
|
PopupMenu(context, holder.actions, Gravity.START, R.attr.actionOverflowMenuStyle, 0).apply {
|
|
|
|
inflate(if (fromQueue) R.menu.row_queue else R.menu.row_track)
|
|
|
|
|
|
|
|
setOnMenuItemClickListener {
|
|
|
|
when (it.itemId) {
|
2019-10-23 20:21:18 +02:00
|
|
|
R.id.track_add_to_queue -> CommandBus.send(Command.AddToQueue(listOf(favorite)))
|
|
|
|
R.id.track_play_next -> CommandBus.send(Command.PlayNext(favorite))
|
2020-06-13 19:50:02 +02:00
|
|
|
R.id.track_pin -> CommandBus.send(Command.PinTrack(favorite))
|
2019-10-23 20:21:18 +02:00
|
|
|
R.id.queue_remove -> CommandBus.send(Command.RemoveFromQueue(favorite))
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onItemMove(oldPosition: Int, newPosition: Int) {
|
|
|
|
if (oldPosition < newPosition) {
|
2019-10-21 11:51:32 +02:00
|
|
|
for (i in oldPosition.until(newPosition)) {
|
2019-08-19 16:50:33 +02:00
|
|
|
Collections.swap(data, i, i + 1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i in newPosition.downTo(oldPosition)) {
|
|
|
|
Collections.swap(data, i, i - 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyItemMoved(oldPosition, newPosition)
|
|
|
|
CommandBus.send(Command.MoveFromQueue(oldPosition, newPosition))
|
|
|
|
}
|
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
inner class ViewHolder(binding: RowTrackBinding, val context: Context?) :
|
|
|
|
RecyclerView.ViewHolder(binding.root),
|
|
|
|
View.OnClickListener {
|
|
|
|
val cover = binding.cover
|
|
|
|
val title = binding.title
|
|
|
|
val artist = binding.artist
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2021-07-16 10:03:52 +02:00
|
|
|
val favorite = binding.favorite
|
|
|
|
val actions = binding.actions
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
override fun onClick(view: View?) {
|
|
|
|
when (fromQueue) {
|
|
|
|
true -> CommandBus.send(Command.PlayTrack(layoutPosition))
|
|
|
|
false -> {
|
|
|
|
data.subList(layoutPosition, data.size).plus(data.subList(0, layoutPosition)).apply {
|
2019-10-23 20:21:18 +02:00
|
|
|
CommandBus.send(Command.ReplaceQueue(this))
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
context.toast("All tracks were added to your queue")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|