social-aggregator/app/src/main/java/pl/edu/amu/wmi/socialaggregator/viewholders/SocialWithImageRecycler.kt

52 lines
1.8 KiB
Kotlin

package pl.edu.amu.wmi.socialaggregator.viewholders
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.jakewharton.rxbinding2.view.RxView
import io.reactivex.disposables.Disposable
import pl.edu.amu.wmi.socialaggregator.R
import pl.edu.amu.wmi.socialaggregator.socialplatforms.SocialPlatform
class SocialWithImageRecycler(
val availableSocials: List<SocialPlatform>,
private val imageResource: Int? = null,
private val action: (SocialPlatform) -> Unit
) : RecyclerView.Adapter<SocialWithImageRecycler.ViewHolder>() {
private val disposables = HashMap<SocialPlatform, Disposable>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layout = LayoutInflater.from(parent.context)
.inflate(R.layout.social_platform_image, parent, false) as ConstraintLayout
val textView = layout.findViewById<TextView>(R.id.socialPlatformName)
val imageView = layout.findViewById<ImageView>(R.id.socialPlatformImage)
return ViewHolder(layout, textView, imageView)
}
override fun getItemCount(): Int {
return availableSocials.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val social = availableSocials[position]
holder.text.text = social.getName()
holder.image.setImageResource(imageResource ?: social.getLogo())
disposables[social] = RxView.clicks(holder.image)
.map { social }
.subscribe(action)
}
class ViewHolder(
root: ConstraintLayout,
val text: TextView,
val image: ImageView
) : RecyclerView.ViewHolder(root)
}