2020-05-30 21:16:28 +02:00
|
|
|
package com.github.apognu.otter.adapters
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import com.github.apognu.otter.R
|
|
|
|
import com.github.apognu.otter.fragments.FunkwhaleAdapter
|
2020-06-02 18:50:46 +02:00
|
|
|
import com.github.apognu.otter.utils.Event
|
|
|
|
import com.github.apognu.otter.utils.EventBus
|
2020-05-30 21:16:28 +02:00
|
|
|
import com.github.apognu.otter.utils.Radio
|
2020-06-02 18:50:46 +02:00
|
|
|
import com.github.apognu.otter.views.LoadingImageView
|
2020-05-30 21:16:28 +02:00
|
|
|
import kotlinx.android.synthetic.main.row_radio.view.*
|
2020-06-02 18:50:46 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
|
import kotlinx.coroutines.flow.collect
|
|
|
|
import kotlinx.coroutines.launch
|
2020-05-30 21:16:28 +02:00
|
|
|
|
|
|
|
class RadiosAdapter(val context: Context?, private val listener: OnRadioClickListener) : FunkwhaleAdapter<Radio, RadiosAdapter.ViewHolder>() {
|
|
|
|
interface OnRadioClickListener {
|
2020-06-02 18:50:46 +02:00
|
|
|
fun onClick(holder: ViewHolder, radio: Radio)
|
2020-05-30 21:16:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItemCount() = data.size
|
|
|
|
|
|
|
|
override fun getItemId(position: Int) = data[position].id.toLong()
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RadiosAdapter.ViewHolder {
|
|
|
|
val view = LayoutInflater.from(context).inflate(R.layout.row_radio, parent, false)
|
|
|
|
|
|
|
|
return ViewHolder(view, listener).also {
|
|
|
|
view.setOnClickListener(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: RadiosAdapter.ViewHolder, position: Int) {
|
|
|
|
val radio = data[position]
|
|
|
|
|
2020-06-01 17:05:13 +02:00
|
|
|
holder.art.visibility = View.VISIBLE
|
2020-05-30 21:16:28 +02:00
|
|
|
holder.name.text = radio.name
|
|
|
|
holder.description.text = radio.description
|
2020-06-01 14:38:50 +02:00
|
|
|
|
|
|
|
context?.let { context ->
|
2020-06-01 17:05:13 +02:00
|
|
|
val icon = when (radio.radio_type) {
|
|
|
|
"random" -> R.drawable.shuffle
|
|
|
|
"less-listened" -> R.drawable.sad
|
|
|
|
else -> null
|
|
|
|
}
|
|
|
|
|
|
|
|
icon?.let {
|
2020-06-02 18:50:46 +02:00
|
|
|
holder.native = true
|
2020-06-01 17:05:13 +02:00
|
|
|
|
2020-06-02 18:50:46 +02:00
|
|
|
holder.art.setImageDrawable(context.getDrawable(icon))
|
|
|
|
holder.art.alpha = 0.7f
|
|
|
|
holder.art.setColorFilter(context.getColor(R.color.controlForeground))
|
2020-06-01 14:38:50 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-30 21:16:28 +02:00
|
|
|
}
|
|
|
|
|
2020-06-01 14:38:50 +02:00
|
|
|
inner class ViewHolder(view: View, private val listener: OnRadioClickListener) : RecyclerView.ViewHolder(view), View.OnClickListener {
|
|
|
|
val art = view.art
|
2020-05-30 21:16:28 +02:00
|
|
|
val name = view.name
|
|
|
|
val description = view.description
|
|
|
|
|
2020-06-02 18:50:46 +02:00
|
|
|
var native = false
|
|
|
|
|
2020-05-30 21:16:28 +02:00
|
|
|
override fun onClick(view: View?) {
|
2020-06-02 18:50:46 +02:00
|
|
|
listener.onClick(this, data[layoutPosition])
|
|
|
|
}
|
|
|
|
|
|
|
|
fun spin() {
|
|
|
|
context?.let {
|
|
|
|
val originalDrawable = art.drawable
|
|
|
|
val originalColorFilter = art.colorFilter
|
|
|
|
val imageAnimator = LoadingImageView.start(context, art)
|
|
|
|
|
|
|
|
art.setColorFilter(context.getColor(R.color.controlForeground))
|
|
|
|
|
|
|
|
GlobalScope.launch(Main) {
|
|
|
|
EventBus.get().collect { message ->
|
|
|
|
when (message) {
|
|
|
|
is Event.RadioStarted -> {
|
|
|
|
art.colorFilter = originalColorFilter
|
|
|
|
|
|
|
|
LoadingImageView.stop(context, originalDrawable, art, imageAnimator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-30 21:16:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|