diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/community/ui/CreatorChannelCommunityGridAdapter.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/community/ui/CreatorChannelCommunityGridAdapter.kt new file mode 100644 index 00000000..ab6e84a4 --- /dev/null +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/community/ui/CreatorChannelCommunityGridAdapter.kt @@ -0,0 +1,81 @@ +package kr.co.vividnext.sodalive.v2.creator.channel.community.ui + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.core.view.isVisible +import androidx.recyclerview.widget.RecyclerView +import kr.co.vividnext.sodalive.databinding.ItemCreatorChannelCommunityGridBinding +import kr.co.vividnext.sodalive.extensions.loadUrl +import kr.co.vividnext.sodalive.extensions.moneyFormat +import kr.co.vividnext.sodalive.v2.creator.channel.community.model.CreatorChannelCommunityImageMode +import kr.co.vividnext.sodalive.v2.creator.channel.community.model.CreatorChannelCommunityPostUiModel + +class CreatorChannelCommunityGridAdapter : RecyclerView.Adapter() { + + private var items: List = emptyList() + + fun submitItems(items: List) { + this.items = items + notifyDataSetChanged() + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(ItemCreatorChannelCommunityGridBinding.inflate(LayoutInflater.from(parent.context), parent, false)) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.bind(items[position]) + } + + override fun getItemCount(): Int = items.size + + class ViewHolder( + private val binding: ItemCreatorChannelCommunityGridBinding + ) : RecyclerView.ViewHolder(binding.root) { + + fun bind(item: CreatorChannelCommunityPostUiModel) = with(binding) { + val squareSize = calculateSquareSize() + if (squareSize > 0) { + root.layoutParams = root.layoutParams.apply { + width = squareSize + height = squareSize + } + } + + val visibleImageUrl = item.imageUrl.takeIf { item.imageMode == CreatorChannelCommunityImageMode.Image } + ivCreatorChannelCommunityGridImage.isVisible = visibleImageUrl != null + if (visibleImageUrl != null) { + ivCreatorChannelCommunityGridImage.loadUrl(visibleImageUrl) + } else { + ivCreatorChannelCommunityGridImage.setImageDrawable(null) + } + tvCreatorChannelCommunityGridTextPreview.isVisible = item.imageMode != CreatorChannelCommunityImageMode.Image + tvCreatorChannelCommunityGridTextPreview.text = item.gridPreviewText + layoutCreatorChannelCommunityGridLockedOverlay.isVisible = item.isLocked + ivCreatorChannelCommunityGridLock.isVisible = item.isLocked + tvCreatorChannelCommunityGridLockPrice.isVisible = item.isLocked + tvCreatorChannelCommunityGridLockPrice.text = item.price.moneyFormat() + tvCreatorChannelCommunityGridNotice.isVisible = item.showNotice + } + + private fun calculateSquareSize(): Int { + val parent = binding.root.parent as? RecyclerView ?: return 0 + val availableWidth = parent.width - parent.paddingStart - parent.paddingEnd + if (availableWidth <= 0) return 0 + val itemHorizontalMargins = (binding.root.layoutParams as? ViewGroup.MarginLayoutParams) + ?.let { + val leftMargin = it.leftMargin + val rightMargin = it.rightMargin + leftMargin + rightMargin + } + ?: 0 + val totalHorizontalMargins = itemHorizontalMargins * GRID_SPAN_COUNT + + return (availableWidth - totalHorizontalMargins).coerceAtLeast(0) / GRID_SPAN_COUNT + } + } + + companion object { + private const val GRID_SPAN_COUNT = 3 + } +} diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/community/ui/CreatorChannelCommunityListAdapter.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/community/ui/CreatorChannelCommunityListAdapter.kt new file mode 100644 index 00000000..2fffd8ca --- /dev/null +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/creator/channel/community/ui/CreatorChannelCommunityListAdapter.kt @@ -0,0 +1,90 @@ +package kr.co.vividnext.sodalive.v2.creator.channel.community.ui + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.core.view.isVisible +import androidx.recyclerview.widget.RecyclerView +import coil.transform.CircleCropTransformation +import kr.co.vividnext.sodalive.R +import kr.co.vividnext.sodalive.databinding.ItemCreatorChannelCommunityListBinding +import kr.co.vividnext.sodalive.extensions.loadUrl +import kr.co.vividnext.sodalive.extensions.moneyFormat +import kr.co.vividnext.sodalive.v2.creator.channel.community.model.CreatorChannelCommunityPostUiModel + +class CreatorChannelCommunityListAdapter( + private val onPlayClick: (CreatorChannelCommunityPostUiModel) -> Unit = {}, + private val onOwnerMoreClick: (CreatorChannelCommunityPostUiModel) -> Unit = {}, + private val isPlayingContent: (Long) -> Boolean = { false } +) : RecyclerView.Adapter() { + + private var items: List = emptyList() + + fun submitItems(items: List) { + this.items = items + notifyDataSetChanged() + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder( + ItemCreatorChannelCommunityListBinding.inflate(LayoutInflater.from(parent.context), parent, false), + onPlayClick, + onOwnerMoreClick, + isPlayingContent + ) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.bind(items[position]) + } + + override fun getItemCount(): Int = items.size + + class ViewHolder( + private val binding: ItemCreatorChannelCommunityListBinding, + private val onPlayClick: (CreatorChannelCommunityPostUiModel) -> Unit, + private val onOwnerMoreClick: (CreatorChannelCommunityPostUiModel) -> Unit, + private val isPlayingContent: (Long) -> Boolean + ) : RecyclerView.ViewHolder(binding.root) { + + fun bind(item: CreatorChannelCommunityPostUiModel) = with(binding) { + ivCreatorChannelCommunityListProfile.loadUrl(item.creatorProfileUrl) { + crossfade(true) + placeholder(R.drawable.bg_placeholder) + transformations(CircleCropTransformation()) + } + tvCreatorChannelCommunityListNickname.text = item.creatorNickname + tvCreatorChannelCommunityListTime.text = item.createdAtText + tvCreatorChannelCommunityListNotice.isVisible = item.showNotice + tvCreatorChannelCommunityListBody.text = item.content + tvCreatorChannelCommunityListCommentCount.text = item.commentCount.moneyFormat() + tvCreatorChannelCommunityListCommentCount.isVisible = item.showComment + ivCreatorChannelCommunityListComment.isVisible = item.showComment + tvCreatorChannelCommunityListLikeCount.text = item.likeCount.moneyFormat() + + val visibleImageUrl = item.imageUrl.takeUnless { item.isLocked } + layoutCreatorChannelCommunityListImageContainer.isVisible = + visibleImageUrl != null || item.isLocked || item.showPlayButton + ivCreatorChannelCommunityListImage.isVisible = visibleImageUrl != null + if (visibleImageUrl != null) { + ivCreatorChannelCommunityListImage.loadUrl(visibleImageUrl) + } else { + ivCreatorChannelCommunityListImage.setImageDrawable(null) + } + layoutCreatorChannelCommunityListLockedOverlay.isVisible = item.isLocked + ivCreatorChannelCommunityListLock.isVisible = item.isLocked + tvCreatorChannelCommunityListLockedPrice.isVisible = item.isLocked + tvCreatorChannelCommunityListLockedPrice.text = item.price.moneyFormat() + ivCreatorChannelCommunityListPlay.isVisible = item.showPlayButton + ivCreatorChannelCommunityListPlay.setImageResource( + if (isPlayingContent(item.postId)) R.drawable.ic_player_pause else R.drawable.ic_new_player_play + ) + ivCreatorChannelCommunityListPlay.setOnClickListener { onPlayClick(item) } + + layoutCreatorChannelCommunityListTopActions.isVisible = item.showOwnerMore || item.showOwnerTopPrice + tvCreatorChannelCommunityListTopPrice.isVisible = item.showOwnerTopPrice + tvCreatorChannelCommunityListTopPrice.text = item.price.moneyFormat() + ivCreatorChannelCommunityListOwnerMore.isVisible = item.showOwnerMore + ivCreatorChannelCommunityListOwnerMore.setOnClickListener { onOwnerMoreClick(item) } + } + } +}