90 lines
3.0 KiB
Kotlin
90 lines
3.0 KiB
Kotlin
package pl.edu.amu.wmi.socialaggregator.viewholders
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.widget.ImageView
|
|
import android.widget.LinearLayout
|
|
import android.widget.TextView
|
|
import androidx.constraintlayout.widget.ConstraintLayout
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import com.squareup.picasso.Picasso
|
|
import io.reactivex.Observable
|
|
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 PostDetailsRecycler(
|
|
postsObservable: Observable<List<Post>>
|
|
) : RecyclerView.Adapter<PostDetailsRecycler.ViewHolder>() {
|
|
|
|
val posts = emptyList<Post>().toMutableList()
|
|
|
|
init {
|
|
postsObservable
|
|
.subscribe {
|
|
posts.addAll(it)
|
|
notifyDataSetChanged()
|
|
}
|
|
}
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
|
val layout = LayoutInflater.from(parent.context)
|
|
.inflate(R.layout.item_post, parent, false) as ConstraintLayout
|
|
|
|
val dateTime = layout.findViewById<TextView>(R.id.postDateTimeTextView)
|
|
val postContent = layout.findViewById<TextView>(R.id.postContentTextView)
|
|
val imageView = layout.findViewById<ImageView>(R.id.postSocialImage)
|
|
val imagesContainer = layout.findViewById<LinearLayout>(R.id.imagesContainer)
|
|
|
|
return ViewHolder(layout, dateTime, postContent, imageView, imagesContainer)
|
|
}
|
|
|
|
override fun getItemCount(): Int {
|
|
return posts.size
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
val post = posts[position]
|
|
holder.dateTime.text = post.dateTime
|
|
holder.postContent.text = post.content
|
|
holder.postContent.visibility = if (post.content.isBlank()) View.GONE else View.VISIBLE
|
|
|
|
holder.imageView.setImageResource(post.social.getLogo())
|
|
|
|
holder.imagesContainer.apply {
|
|
removeAllViews()
|
|
post.images.forEach {
|
|
val imageView = ImageView(context)
|
|
addView(imageView)
|
|
|
|
imageView.apply {
|
|
(layoutParams as ViewGroup.MarginLayoutParams)
|
|
.apply {
|
|
marginEnd = 10
|
|
}
|
|
}
|
|
|
|
Picasso.with(context)
|
|
.load(it)
|
|
.placeholder(android.R.drawable.ic_menu_gallery)
|
|
.resize(250, 250)
|
|
.centerCrop()
|
|
.into(imageView)
|
|
}
|
|
}
|
|
}
|
|
|
|
class ViewHolder(
|
|
root: ConstraintLayout,
|
|
val dateTime: TextView,
|
|
val postContent: TextView,
|
|
val imageView: ImageView,
|
|
val imagesContainer: LinearLayout
|
|
|
|
) : RecyclerView.ViewHolder(root)
|
|
|
|
} |