Guest User

Untitled

a guest
Jul 9th, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.74 KB | None | 0 0
  1. package com.example.secondtry
  2.  
  3. import android.content.Context
  4. import android.graphics.Color
  5. import android.view.Gravity
  6. import android.view.LayoutInflater
  7. import android.view.View
  8. import android.view.ViewGroup
  9. import android.widget.Button
  10. import android.widget.LinearLayout
  11. import android.widget.TextView
  12. import androidx.appcompat.app.AlertDialog
  13. import androidx.recyclerview.widget.LinearLayoutManager
  14. import androidx.recyclerview.widget.RecyclerView
  15. import androidx.recyclerview.widget.RecyclerView.ViewHolder
  16. import kotlinx.coroutines.GlobalScope
  17. import kotlinx.coroutines.async
  18. import org.koin.core.KoinComponent
  19. import org.koin.core.inject
  20. import java.util.*
  21.  
  22. class WallpostsListAdapter(
  23.         private val wallposts: List<Wallpost>,
  24.         private val context: Context,
  25.         private val recyclerView: RecyclerView,
  26.         private val nestedViewsPool: RecyclerView.RecycledViewPool) :
  27.  
  28.         RecyclerView.Adapter<WallpostsListAdapter.MyViewHolder>(), KoinComponent {
  29.  
  30.     private val repository by inject<WallpostsRepository>()
  31.     private val viewHolders = LinkedList<MyViewHolder>()
  32.  
  33.     class MyViewHolder(layout: LinearLayout,
  34.                        context: Context,
  35.                        nestedViewsPool: RecyclerView.RecycledViewPool) : ViewHolder(layout) {
  36.         val wpContent: TextView = layout.findViewById(R.id.wpContent)
  37.         val wpHeader: TextView = layout.findViewById(R.id.wpHeader)
  38.         val wpLinks: TextView = layout.findViewById(R.id.wpLinks)
  39.         val commentsList: RecyclerView = layout.findViewById(R.id.commentsList)
  40.         val innerAdapter = CommentListAdapter()
  41.         val likeBtn: Button = layout.findViewById(R.id.btnLike)
  42.         val commentBtn: Button = layout.findViewById(R.id.btnComment)
  43.         val shareBtn: Button = layout.findViewById(R.id.btnShare)
  44.         val dialog = AlertDialog.Builder(context)
  45.  
  46.         init {
  47.             val manager = LinearLayoutManager(context)
  48.  
  49.             commentsList.apply {
  50.                 isNestedScrollingEnabled = false
  51.                 layoutManager = manager
  52.                 adapter = innerAdapter
  53.             }
  54.  
  55.             dialog.setTitle("Under construction")
  56.                     .setMessage("Functionality is currently under construction. Try again later.")
  57.                     .setNeutralButton("Go back") { dialog, _ ->
  58.                         dialog.cancel()
  59.                     }
  60.  
  61.             commentsList.setRecycledViewPool(nestedViewsPool)
  62.             commentsList.isNestedScrollingEnabled = false
  63.         }
  64.     }
  65.  
  66.     override fun getItemCount(): Int {
  67.         return wallposts.size
  68.     }
  69.  
  70.     override fun getItemId(position: Int): Long {
  71.         return wallposts[position].datePosted.time + position
  72.     }
  73.  
  74.     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
  75.         //Log.w("msg", "View holders size: ${viewHolders.size}")
  76.         if (viewHolders.size == 0) {
  77.             //Log.w("msg", "creating all view holders")
  78.             for (i in 1..10) {
  79.                 val v = LayoutInflater.from(parent.context)
  80.                         .inflate(R.layout.wallpost_view, parent, false) as LinearLayout
  81.                 val viewHolder = MyViewHolder(v, context, nestedViewsPool)
  82.                 viewHolders.push(viewHolder)
  83.             }
  84.         }
  85.  
  86.         val viewHolder = viewHolders.pop()
  87.         /*val v = LayoutInflater.from(parent.context)
  88.                 .inflate(R.layout.wallpost_view, parent, false) as LinearLayout
  89.         val viewHolder = MyViewHolder(v, context, nestedViewsPool)*/
  90.  
  91.         val onLikeClick = { likeBtn: View ->
  92.             val index = recyclerView.getChildLayoutPosition(likeBtn.parent.parent.parent as View)
  93.             val wp = wallposts[index]
  94.  
  95.             // TODO: check if global scope should be used
  96.             // TODO: check how to avoid writing an unused variable to avoid returning job
  97.             val job = GlobalScope.async {
  98.                 val newLiked = if (wp.liked == null) true else !wp.liked
  99.  
  100.                 repository.likeWallpost(wallposts[index].id, newLiked)
  101.  
  102.                 wallposts[index].liked = newLiked
  103.                 notifyItemChanged(index)
  104.             }
  105.         }
  106.  
  107.         val onCommentClick = { _: View ->
  108.             val dialog = viewHolder.dialog.show()
  109.             val neutralBtn = dialog.getButton(AlertDialog.BUTTON_NEUTRAL)
  110.             val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
  111.             layoutParams.gravity = Gravity.CENTER
  112.             neutralBtn.layoutParams = layoutParams
  113.         }
  114.  
  115.         viewHolder.apply {
  116.             likeBtn.setOnClickListener(onLikeClick)
  117.             commentBtn.setOnClickListener(onCommentClick)
  118.             shareBtn.setOnClickListener(onCommentClick)
  119.         }
  120.  
  121.         return viewHolder
  122.     }
  123.  
  124.     override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
  125.         val wp = wallposts[position]
  126.         val headerText = "${wp.username} on ${wp.datePosted}"
  127.         val linksText = "Show previous comments (${wp.comments.size} of 5) ---- Reply ----"
  128.  
  129.         holder.apply {
  130.             toggleLikeBtn(likeBtn, wp.liked)
  131.  
  132.             wpContent.text = wp.content
  133.             wpHeader.text = headerText
  134.             wpLinks.text = linksText
  135.             innerAdapter.values = wp.comments
  136.             commentsList.adapter?.notifyDataSetChanged()
  137.         }
  138.     }
  139.  
  140.     private fun toggleLikeBtn(likeBtn: Button, liked: Boolean) {
  141.         val likeImg = if (liked) R.drawable.favourite else R.drawable.like
  142.         val textColor = if (liked) "#065fd4" else "#808080"
  143.  
  144.         likeBtn.setCompoundDrawablesWithIntrinsicBounds(likeImg, 0, 0, 0)
  145.         likeBtn.setTextColor(Color.parseColor(textColor))
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment