65 lines
2.2 KiB
Kotlin
65 lines
2.2 KiB
Kotlin
package pl.edu.amu.wmi.socialaggregator.viewholders
|
|
|
|
import android.annotation.SuppressLint
|
|
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 io.reactivex.Observable
|
|
import io.reactivex.Single
|
|
import pl.edu.amu.wmi.socialaggregator.R
|
|
import pl.edu.amu.wmi.socialaggregator.socialplatforms.NotApplicableException
|
|
import pl.edu.amu.wmi.socialaggregator.socialplatforms.Post
|
|
import pl.edu.amu.wmi.socialaggregator.socialplatforms.SocialPlatform
|
|
|
|
@SuppressLint("CheckResult")
|
|
class PostSummaryRecycler(
|
|
socialsObservable: Observable<Pair<SocialPlatform, List<Post>?>>
|
|
) : RecyclerView.Adapter<PostSummaryRecycler.ViewHolder>() {
|
|
|
|
private val socials = mutableListOf<Pair<SocialPlatform, List<Post>?>>()
|
|
|
|
init {
|
|
socialsObservable
|
|
.doOnNext {
|
|
socials.add(it)
|
|
notifyDataSetChanged()
|
|
}
|
|
.subscribe()
|
|
}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
|
val layout = LayoutInflater.from(parent.context)
|
|
.inflate(R.layout.previous_posts, parent, false) as ConstraintLayout
|
|
|
|
val socialName = layout.findViewById<TextView>(R.id.socialTextView)
|
|
val postCount = layout.findViewById<TextView>(R.id.post)
|
|
val imageView = layout.findViewById<ImageView>(R.id.postSocialImage)
|
|
|
|
return ViewHolder(layout, socialName, postCount, imageView)
|
|
}
|
|
|
|
override fun getItemCount(): Int {
|
|
return socials.size
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
val (social, posts) = socials[position]
|
|
val count = posts?.size
|
|
holder.socialName.text = social.getName()
|
|
holder.postCount.text = count?.let {
|
|
count.toString() + if (count > 1) " posts" else " post"
|
|
} ?: "Not applicable"
|
|
holder.imageView.setImageResource(social.getLogo())
|
|
}
|
|
|
|
class ViewHolder(
|
|
root: ConstraintLayout,
|
|
val socialName: TextView,
|
|
val postCount: TextView,
|
|
val imageView: ImageView
|
|
) : RecyclerView.ViewHolder(root)
|
|
|
|
} |