Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.secondtry
- import android.content.Context
- import android.graphics.Color
- import android.view.Gravity
- import android.view.LayoutInflater
- import android.view.View
- import android.view.ViewGroup
- import android.widget.Button
- import android.widget.LinearLayout
- import android.widget.TextView
- import androidx.appcompat.app.AlertDialog
- import androidx.recyclerview.widget.LinearLayoutManager
- import androidx.recyclerview.widget.RecyclerView
- import androidx.recyclerview.widget.RecyclerView.ViewHolder
- import kotlinx.coroutines.GlobalScope
- import kotlinx.coroutines.async
- import org.koin.core.KoinComponent
- import org.koin.core.inject
- import java.util.*
- class WallpostsListAdapter(
- private val wallposts: List<Wallpost>,
- private val context: Context,
- private val recyclerView: RecyclerView,
- private val nestedViewsPool: RecyclerView.RecycledViewPool) :
- RecyclerView.Adapter<WallpostsListAdapter.MyViewHolder>(), KoinComponent {
- private val repository by inject<WallpostsRepository>()
- private val viewHolders = LinkedList<MyViewHolder>()
- class MyViewHolder(layout: LinearLayout,
- context: Context,
- nestedViewsPool: RecyclerView.RecycledViewPool) : ViewHolder(layout) {
- val wpContent: TextView = layout.findViewById(R.id.wpContent)
- val wpHeader: TextView = layout.findViewById(R.id.wpHeader)
- val wpLinks: TextView = layout.findViewById(R.id.wpLinks)
- val commentsList: RecyclerView = layout.findViewById(R.id.commentsList)
- val innerAdapter = CommentListAdapter()
- val likeBtn: Button = layout.findViewById(R.id.btnLike)
- val commentBtn: Button = layout.findViewById(R.id.btnComment)
- val shareBtn: Button = layout.findViewById(R.id.btnShare)
- val dialog = AlertDialog.Builder(context)
- init {
- val manager = LinearLayoutManager(context)
- commentsList.apply {
- isNestedScrollingEnabled = false
- layoutManager = manager
- adapter = innerAdapter
- }
- dialog.setTitle("Under construction")
- .setMessage("Functionality is currently under construction. Try again later.")
- .setNeutralButton("Go back") { dialog, _ ->
- dialog.cancel()
- }
- commentsList.setRecycledViewPool(nestedViewsPool)
- commentsList.isNestedScrollingEnabled = false
- }
- }
- override fun getItemCount(): Int {
- return wallposts.size
- }
- override fun getItemId(position: Int): Long {
- return wallposts[position].datePosted.time + position
- }
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
- //Log.w("msg", "View holders size: ${viewHolders.size}")
- if (viewHolders.size == 0) {
- //Log.w("msg", "creating all view holders")
- for (i in 1..10) {
- val v = LayoutInflater.from(parent.context)
- .inflate(R.layout.wallpost_view, parent, false) as LinearLayout
- val viewHolder = MyViewHolder(v, context, nestedViewsPool)
- viewHolders.push(viewHolder)
- }
- }
- val viewHolder = viewHolders.pop()
- /*val v = LayoutInflater.from(parent.context)
- .inflate(R.layout.wallpost_view, parent, false) as LinearLayout
- val viewHolder = MyViewHolder(v, context, nestedViewsPool)*/
- val onLikeClick = { likeBtn: View ->
- val index = recyclerView.getChildLayoutPosition(likeBtn.parent.parent.parent as View)
- val wp = wallposts[index]
- // TODO: check if global scope should be used
- // TODO: check how to avoid writing an unused variable to avoid returning job
- val job = GlobalScope.async {
- val newLiked = if (wp.liked == null) true else !wp.liked
- repository.likeWallpost(wallposts[index].id, newLiked)
- wallposts[index].liked = newLiked
- notifyItemChanged(index)
- }
- }
- val onCommentClick = { _: View ->
- val dialog = viewHolder.dialog.show()
- val neutralBtn = dialog.getButton(AlertDialog.BUTTON_NEUTRAL)
- val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
- layoutParams.gravity = Gravity.CENTER
- neutralBtn.layoutParams = layoutParams
- }
- viewHolder.apply {
- likeBtn.setOnClickListener(onLikeClick)
- commentBtn.setOnClickListener(onCommentClick)
- shareBtn.setOnClickListener(onCommentClick)
- }
- return viewHolder
- }
- override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
- val wp = wallposts[position]
- val headerText = "${wp.username} on ${wp.datePosted}"
- val linksText = "Show previous comments (${wp.comments.size} of 5) ---- Reply ----"
- holder.apply {
- toggleLikeBtn(likeBtn, wp.liked)
- wpContent.text = wp.content
- wpHeader.text = headerText
- wpLinks.text = linksText
- innerAdapter.values = wp.comments
- commentsList.adapter?.notifyDataSetChanged()
- }
- }
- private fun toggleLikeBtn(likeBtn: Button, liked: Boolean) {
- val likeImg = if (liked) R.drawable.favourite else R.drawable.like
- val textColor = if (liked) "#065fd4" else "#808080"
- likeBtn.setCompoundDrawablesWithIntrinsicBounds(likeImg, 0, 0, 0)
- likeBtn.setTextColor(Color.parseColor(textColor))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment