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.text.TextUtils
- import android.view.Gravity
- import android.view.LayoutInflater
- import android.view.View
- import android.view.ViewGroup
- import android.view.textclassifier.TextClassifier.TYPE_EMAIL
- import android.widget.Button
- import android.widget.LinearLayout
- import android.widget.TextView
- import androidx.appcompat.app.AlertDialog
- import androidx.constraintlayout.widget.ConstraintLayout
- 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.*
- import kotlin.collections.HashMap
- import kotlin.time.ExperimentalTime
- import kotlin.time.seconds
- class WallpostsListAdapter(
- private val wallposts: List<Wallpost>,
- private val recyclerView: RecyclerView) :
- RecyclerView.Adapter<ViewHolder>(), KoinComponent {
- companion object {
- const val WALLPOST_VIEW = 0
- const val COMMENTS_VIEW = 1
- }
- private val repository by inject<WallpostsRepository>()
- val viewTypeFlags: BooleanArray
- val wallpostsPosMap = HashMap<Int, Int>()
- val commentsWpMap = HashMap<Int, Int>()
- val viewHolders = LinkedList<MyViewHolder>()
- val commentViewHolders = LinkedList<CommentsViewHolder>()
- init {
- val totalCommentsCount = wallposts.sumBy { wp -> wp.comments.size }
- val viewsCount = wallposts.size + totalCommentsCount
- viewTypeFlags = BooleanArray(viewsCount)
- var adapterIdx = 0
- wallposts.forEachIndexed { wpArrIdx, wp ->
- wallpostsPosMap[adapterIdx] = wpArrIdx
- viewTypeFlags[adapterIdx] = true
- wp.comments.forEachIndexed { commArrIdx, _ ->
- var commAdapterIdx = adapterIdx + commArrIdx + 1
- commentsWpMap[commAdapterIdx] = adapterIdx
- viewTypeFlags[commAdapterIdx] = false
- }
- adapterIdx += wp.comments.size + 1
- }
- }
- class MyViewHolder(layout: ConstraintLayout,
- context: Context) : 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 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 {
- dialog.setTitle("Under construction")
- .setMessage("Functionality is currently under construction. Try again later.")
- .setNeutralButton("Go back") { dialog, _ ->
- dialog.cancel()
- }
- }
- }
- class CommentsViewHolder(layout: LinearLayout) : ViewHolder(layout) {
- val commentHeader: TextView = layout.findViewById(R.id.commentHeader)
- val commentContent: TextView = layout.findViewById(R.id.commentContent)
- val date: TextView = layout.findViewById(R.id.date)
- }
- override fun getItemCount(): Int {
- return viewTypeFlags.size
- }
- override fun getItemViewType(position: Int): Int {
- return if (viewTypeFlags[position]) {
- WALLPOST_VIEW
- } else {
- COMMENTS_VIEW
- }
- }
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
- if (viewType == COMMENTS_VIEW) {
- return commentViewHolders.pop()
- }
- val viewHolder = viewHolders.pop()
- val onLikeClick = { likeBtn: View ->
- val index = recyclerView.getChildLayoutPosition(likeBtn.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
- }
- @ExperimentalTime
- override fun onBindViewHolder(holder: ViewHolder, position: Int) {
- if (holder is MyViewHolder) {
- bindWallpostsViewHolder(holder, position)
- }
- if (holder is CommentsViewHolder) {
- bindCommentsHolder(holder, position)
- }
- }
- private fun bindWallpostsViewHolder(holder: MyViewHolder, position: Int) {
- val wpArrIdx = wallpostsPosMap[position]!!
- val wp = wallposts[wpArrIdx]
- 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
- }
- }
- @ExperimentalTime
- private fun bindCommentsHolder(holder: CommentsViewHolder, position: Int) {
- val relatedWpAdapterIdx = commentsWpMap[position]!!
- val wpArrIdx = wallpostsPosMap[relatedWpAdapterIdx]!!
- val commArrIdx = position - relatedWpAdapterIdx - 1
- val comment = wallposts[wpArrIdx].comments[commArrIdx]
- val headerText = "${comment.username} on ${comment.datePosted}"
- val now = Date()
- val timeDiff = now.time - comment.datePosted.time
- val isRecent = timeDiff.seconds.inSeconds < 100L
- holder.date.text = if (isRecent) "Just now" else "2h"
- holder.commentHeader.text = headerText
- holder.commentContent.text = comment.content
- }
- 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