Guest User

Untitled

a guest
Jul 18th, 2020
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 7.05 KB | None | 0 0
  1. package com.example.secondtry
  2.  
  3. import android.content.Context
  4. import android.graphics.Color
  5. import android.text.TextUtils
  6. import android.view.Gravity
  7. import android.view.LayoutInflater
  8. import android.view.View
  9. import android.view.ViewGroup
  10. import android.view.textclassifier.TextClassifier.TYPE_EMAIL
  11. import android.widget.Button
  12. import android.widget.LinearLayout
  13. import android.widget.TextView
  14. import androidx.appcompat.app.AlertDialog
  15. import androidx.constraintlayout.widget.ConstraintLayout
  16. import androidx.recyclerview.widget.LinearLayoutManager
  17. import androidx.recyclerview.widget.RecyclerView
  18. import androidx.recyclerview.widget.RecyclerView.ViewHolder
  19. import kotlinx.coroutines.GlobalScope
  20. import kotlinx.coroutines.async
  21. import org.koin.core.KoinComponent
  22. import org.koin.core.inject
  23. import java.util.*
  24. import kotlin.collections.HashMap
  25. import kotlin.time.ExperimentalTime
  26. import kotlin.time.seconds
  27.  
  28. class WallpostsListAdapter(
  29.         private val wallposts: List<Wallpost>,
  30.         private val recyclerView: RecyclerView) :
  31.  
  32.         RecyclerView.Adapter<ViewHolder>(), KoinComponent {
  33.  
  34.     companion object {
  35.         const val WALLPOST_VIEW = 0
  36.         const val COMMENTS_VIEW = 1
  37.     }
  38.  
  39.     private val repository by inject<WallpostsRepository>()
  40.     val viewTypeFlags: BooleanArray
  41.     val wallpostsPosMap = HashMap<Int, Int>()
  42.     val commentsWpMap = HashMap<Int, Int>()
  43.     val viewHolders = LinkedList<MyViewHolder>()
  44.     val commentViewHolders = LinkedList<CommentsViewHolder>()
  45.  
  46.     init {
  47.         val totalCommentsCount = wallposts.sumBy { wp -> wp.comments.size }
  48.         val viewsCount = wallposts.size + totalCommentsCount
  49.         viewTypeFlags = BooleanArray(viewsCount)
  50.  
  51.         var adapterIdx = 0
  52.  
  53.         wallposts.forEachIndexed { wpArrIdx, wp ->
  54.             wallpostsPosMap[adapterIdx] = wpArrIdx
  55.             viewTypeFlags[adapterIdx] = true
  56.  
  57.             wp.comments.forEachIndexed { commArrIdx, _  ->
  58.                 var commAdapterIdx = adapterIdx + commArrIdx + 1
  59.                 commentsWpMap[commAdapterIdx] = adapterIdx
  60.                 viewTypeFlags[commAdapterIdx] = false
  61.             }
  62.  
  63.             adapterIdx += wp.comments.size + 1
  64.         }
  65.     }
  66.  
  67.     class MyViewHolder(layout: ConstraintLayout,
  68.                        context: Context) : ViewHolder(layout) {
  69.         val wpContent: TextView = layout.findViewById(R.id.wpContent)
  70.         val wpHeader: TextView = layout.findViewById(R.id.wpHeader)
  71.         val wpLinks: TextView = layout.findViewById(R.id.wpLinks)
  72.         val likeBtn: Button = layout.findViewById(R.id.btnLike)
  73.         val commentBtn: Button = layout.findViewById(R.id.btnComment)
  74.         val shareBtn: Button = layout.findViewById(R.id.btnShare)
  75.         val dialog = AlertDialog.Builder(context)
  76.  
  77.         init {
  78.             dialog.setTitle("Under construction")
  79.                     .setMessage("Functionality is currently under construction. Try again later.")
  80.                     .setNeutralButton("Go back") { dialog, _ ->
  81.                         dialog.cancel()
  82.                     }
  83.         }
  84.     }
  85.  
  86.     class CommentsViewHolder(layout: LinearLayout) : ViewHolder(layout) {
  87.         val commentHeader: TextView = layout.findViewById(R.id.commentHeader)
  88.         val commentContent: TextView = layout.findViewById(R.id.commentContent)
  89.         val date: TextView = layout.findViewById(R.id.date)
  90.     }
  91.  
  92.     override fun getItemCount(): Int {
  93.         return viewTypeFlags.size
  94.     }
  95.  
  96.     override fun getItemViewType(position: Int): Int {
  97.         return if (viewTypeFlags[position]) {
  98.             WALLPOST_VIEW
  99.         } else {
  100.             COMMENTS_VIEW
  101.         }
  102.     }
  103.  
  104.     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  105.         if (viewType == COMMENTS_VIEW) {
  106.             return commentViewHolders.pop()
  107.         }
  108.  
  109.         val viewHolder = viewHolders.pop()
  110.  
  111.         val onLikeClick = { likeBtn: View ->
  112.             val index = recyclerView.getChildLayoutPosition(likeBtn.parent as View)
  113.             val wp = wallposts[index]
  114.  
  115.             // TODO: check if global scope should be used
  116.             // TODO: check how to avoid writing an unused variable to avoid returning job
  117.             val job = GlobalScope.async {
  118.                 val newLiked = if (wp.liked == null) true else !wp.liked
  119.  
  120.                 repository.likeWallpost(wallposts[index].id, newLiked)
  121.  
  122.                 wallposts[index].liked = newLiked
  123.                 notifyItemChanged(index)
  124.             }
  125.         }
  126.  
  127.         val onCommentClick = { _: View ->
  128.             val dialog = viewHolder.dialog.show()
  129.             val neutralBtn = dialog.getButton(AlertDialog.BUTTON_NEUTRAL)
  130.             val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
  131.             layoutParams.gravity = Gravity.CENTER
  132.             neutralBtn.layoutParams = layoutParams
  133.         }
  134.  
  135.         viewHolder.apply {
  136.             likeBtn.setOnClickListener(onLikeClick)
  137.             commentBtn.setOnClickListener(onCommentClick)
  138.             shareBtn.setOnClickListener(onCommentClick)
  139.         }
  140.  
  141.         return viewHolder
  142.     }
  143.  
  144.     @ExperimentalTime
  145.     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  146.         if (holder is MyViewHolder) {
  147.             bindWallpostsViewHolder(holder, position)
  148.         }
  149.  
  150.         if (holder is CommentsViewHolder) {
  151.             bindCommentsHolder(holder, position)
  152.         }
  153.     }
  154.  
  155.     private fun bindWallpostsViewHolder(holder: MyViewHolder, position: Int) {
  156.         val wpArrIdx = wallpostsPosMap[position]!!
  157.         val wp = wallposts[wpArrIdx]
  158.         val headerText = "${wp.username} on ${wp.datePosted}"
  159.         val linksText = "Show previous comments (${wp.comments.size} of 5) ---- Reply ----"
  160.  
  161.         holder.apply {
  162.             toggleLikeBtn(likeBtn, wp.liked)
  163.  
  164.             wpContent.text = wp.content
  165.             wpHeader.text = headerText
  166.             wpLinks.text = linksText
  167.         }
  168.     }
  169.  
  170.     @ExperimentalTime
  171.     private fun bindCommentsHolder(holder: CommentsViewHolder, position: Int) {
  172.  
  173.         val relatedWpAdapterIdx = commentsWpMap[position]!!
  174.         val wpArrIdx = wallpostsPosMap[relatedWpAdapterIdx]!!
  175.         val commArrIdx = position - relatedWpAdapterIdx - 1
  176.  
  177.         val comment = wallposts[wpArrIdx].comments[commArrIdx]
  178.         val headerText = "${comment.username} on ${comment.datePosted}"
  179.  
  180.         val now = Date()
  181.         val timeDiff = now.time - comment.datePosted.time
  182.         val isRecent = timeDiff.seconds.inSeconds < 100L
  183.  
  184.         holder.date.text = if (isRecent) "Just now" else "2h"
  185.         holder.commentHeader.text = headerText
  186.         holder.commentContent.text = comment.content
  187.     }
  188.  
  189.     private fun toggleLikeBtn(likeBtn: Button, liked: Boolean) {
  190.         val likeImg = if (liked) R.drawable.favourite else R.drawable.like
  191.         val textColor = if (liked) "#065fd4" else "#808080"
  192.  
  193.         likeBtn.setCompoundDrawablesWithIntrinsicBounds(likeImg, 0, 0, 0)
  194.         likeBtn.setTextColor(Color.parseColor(textColor))
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment