|
| 1 | +package com.eltex.androidschool.adapter.comments |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.content.Context |
| 5 | +import android.graphics.drawable.InsetDrawable |
| 6 | +import android.os.Build |
| 7 | +import android.util.TypedValue |
| 8 | +import android.view.LayoutInflater |
| 9 | +import android.view.MenuItem |
| 10 | +import android.view.View |
| 11 | +import android.view.ViewGroup |
| 12 | +import androidx.appcompat.view.menu.MenuBuilder |
| 13 | +import androidx.appcompat.widget.PopupMenu |
| 14 | +import androidx.recyclerview.widget.ListAdapter |
| 15 | +import com.eltex.androidschool.BuildConfig |
| 16 | +import com.eltex.androidschool.R |
| 17 | +import com.eltex.androidschool.databinding.CardCommentBinding |
| 18 | +import com.eltex.androidschool.ui.comments.CommentUiModel |
| 19 | +import com.eltex.androidschool.utils.extensions.singleVibrationWithSystemCheck |
| 20 | +import com.eltex.androidschool.utils.helper.LoggerHelper |
| 21 | + |
| 22 | +class CommentsAdapter( |
| 23 | + private val listener: CommentListener, |
| 24 | + private val context: Context, |
| 25 | + private val currentUserId: Long, |
| 26 | +) : ListAdapter<CommentUiModel, CommentViewHolder>(CommentItemCallback()) { |
| 27 | + |
| 28 | + interface CommentListener { |
| 29 | + fun onLikeClicked(comment: CommentUiModel) |
| 30 | + fun onDeleteClicked(comment: CommentUiModel) |
| 31 | + fun onGetUserClicked(comment: CommentUiModel) |
| 32 | + } |
| 33 | + |
| 34 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentViewHolder { |
| 35 | + val binding = CardCommentBinding.inflate(LayoutInflater.from(parent.context), parent, false) |
| 36 | + |
| 37 | + val viewHolder = CommentViewHolder(binding = binding) |
| 38 | + |
| 39 | + binding.like.setOnClickListener { |
| 40 | + listener.onLikeClicked(getItem(viewHolder.bindingAdapterPosition)) |
| 41 | + } |
| 42 | + |
| 43 | + binding.avatar.setOnClickListener { |
| 44 | + listener.onGetUserClicked(getItem(viewHolder.bindingAdapterPosition)) |
| 45 | + } |
| 46 | + |
| 47 | + binding.author.setOnClickListener { |
| 48 | + listener.onGetUserClicked(getItem(viewHolder.bindingAdapterPosition)) |
| 49 | + } |
| 50 | + |
| 51 | + binding.menu.setOnClickListener { view: View -> |
| 52 | + showPopupMenu(view = view, position = viewHolder.bindingAdapterPosition) |
| 53 | + } |
| 54 | + |
| 55 | + return viewHolder |
| 56 | + } |
| 57 | + |
| 58 | + override fun onBindViewHolder(holder: CommentViewHolder, position: Int) { |
| 59 | + holder.bindComment( |
| 60 | + comment = getItem(position), |
| 61 | + currentUserId = currentUserId |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + override fun onBindViewHolder(holder: CommentViewHolder, position: Int, payloads: List<Any?>) { |
| 67 | + if (payloads.isNotEmpty()) { |
| 68 | + payloads.forEach { comment: Any? -> |
| 69 | + if (comment is CommentPayload) { |
| 70 | + holder.bindPayload(payload = comment) |
| 71 | + } |
| 72 | + } |
| 73 | + } else { |
| 74 | + onBindViewHolder(holder = holder, position = position) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @SuppressLint("RestrictedApi", "ObsoleteSdkInt") |
| 79 | + private fun showPopupMenu(view: View, position: Int) { |
| 80 | + context.singleVibrationWithSystemCheck(35) |
| 81 | + |
| 82 | + val resources = view.context.resources |
| 83 | + |
| 84 | + val iconMarginPx = TypedValue.applyDimension( |
| 85 | + TypedValue.COMPLEX_UNIT_DIP, 8f, resources.displayMetrics |
| 86 | + ).toInt() |
| 87 | + |
| 88 | + val popup = PopupMenu(view.context, view).apply { |
| 89 | + inflate(R.menu.menu_comment) |
| 90 | + |
| 91 | + if (menu is MenuBuilder) { |
| 92 | + val menuBuilder = menu as MenuBuilder |
| 93 | + menuBuilder.setOptionalIconsVisible(true) |
| 94 | + |
| 95 | + for (item in menuBuilder.visibleItems) { |
| 96 | + if (item.icon != null) { |
| 97 | + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { |
| 98 | + item.icon = InsetDrawable(item.icon, iconMarginPx, 0, iconMarginPx, 0) |
| 99 | + } else { |
| 100 | + item.icon = object : |
| 101 | + InsetDrawable(item.icon, iconMarginPx, 0, iconMarginPx, 0) { |
| 102 | + override fun getIntrinsicWidth(): Int { |
| 103 | + return intrinsicHeight + iconMarginPx + iconMarginPx |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + setOnMenuItemClickListener { menuItem: MenuItem -> |
| 112 | + when (menuItem.itemId) { |
| 113 | + R.id.delete_comment -> { |
| 114 | + listener.onDeleteClicked(getItem(position)) |
| 115 | + context.singleVibrationWithSystemCheck(35) |
| 116 | + |
| 117 | + true |
| 118 | + } |
| 119 | + |
| 120 | + else -> { |
| 121 | + false |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + popup.show() |
| 128 | + } |
| 129 | +} |
0 commit comments