From e71355a2341d7561b8167b4f61e06ed5a4c75db3 Mon Sep 17 00:00:00 2001 From: SsongSik Date: Sat, 24 Jun 2023 01:14:25 +0900 Subject: [PATCH] [feat] : #15 FriendRecordGetAdapter inner class seperate --- .../friend/FriendGetRecordViewHolder.kt | 92 +++++++++++++++++++ .../friend/FriendRecordGetAdapter.kt | 84 +---------------- 2 files changed, 94 insertions(+), 82 deletions(-) create mode 100644 app/src/main/java/com/teampome/pome/presentation/friend/FriendGetRecordViewHolder.kt diff --git a/app/src/main/java/com/teampome/pome/presentation/friend/FriendGetRecordViewHolder.kt b/app/src/main/java/com/teampome/pome/presentation/friend/FriendGetRecordViewHolder.kt new file mode 100644 index 0000000..f08a501 --- /dev/null +++ b/app/src/main/java/com/teampome/pome/presentation/friend/FriendGetRecordViewHolder.kt @@ -0,0 +1,92 @@ +package com.teampome.pome.presentation.friend + +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import com.bumptech.glide.Glide +import com.teampome.pome.R +import com.teampome.pome.databinding.ItemFriendDetailCardBinding +import com.teampome.pome.model.response.GetFriendRecord +import com.teampome.pome.model.response.GetFriends +import java.time.Duration +import java.time.Instant +import java.time.ZonedDateTime +import java.time.format.DateTimeFormatter + +class FriendGetRecordViewHolder( + private val friendDetailRecordClickListener: FriendDetailRecordClickListener, + private val binding : ItemFriendDetailCardBinding, + private val friendsMap: HashMap +) : RecyclerView.ViewHolder(binding.root){ + + fun bind(getFriedRecord: GetFriendRecord){ + with(binding) { + getFriendRecord = getFriedRecord + + friendDetailMoreSettingIv.setOnClickListener { + friendDetailRecordClickListener.onFriendDetailMoreClick(getFriedRecord.id) + } + + val timeString = getTimeAgo(getFriedRecord.createdAt) + friendDetailContentNameTimeTv.text = " · ${getFriedRecord.oneLineMind} · $timeString" + + if(getFriedRecord.emotionResponse.friendEmotions.isEmpty()) { + friendDetailCardLastFriendEmotionCountTv.visibility = View.INVISIBLE + friendDetailCardLastFriendEmotionAiv.visibility = View.INVISIBLE + } else { + friendDetailCardLastFriendEmotionCountTv.visibility = View.VISIBLE + friendDetailCardLastFriendEmotionAiv.visibility = View.VISIBLE + + val count = getFriedRecord.emotionResponse.friendEmotions.size + + if(count >= 10) { + friendDetailCardLastFriendEmotionCountTv.text = "9+" + } else { + friendDetailCardLastFriendEmotionCountTv.text = "+$count" + } + } + + if(getFriedRecord.emotionResponse.myEmotion == null) { + itemView.context?.let{ context -> + Glide.with(context).load(R.drawable.emoji_mint_28).into(friendDetailCardFirstFriendEmotionAiv) + } + } + + friendDetailCardFirstFriendEmotionAiv.setOnClickListener { + val params = friendEmojiRegisterCl.layoutParams + if(friendEmojiRegisterCl.visibility == View.VISIBLE) { + friendEmojiRegisterCl.visibility = View.GONE + params.height = 0 + } else { + friendEmojiRegisterCl.visibility = View.VISIBLE + params.height = ViewGroup.LayoutParams.WRAP_CONTENT + } + friendEmojiRegisterCl.layoutParams = params + } + + val friends = friendsMap[getFriedRecord.nickname] + + friends?.let { friend -> + Glide.with(itemView.context) + .load(friend.imageKey) + .circleCrop() + .into(friendDetailProfileIv) + } + } + } + + private fun getTimeAgo(isoTime: String): String { + val modifiedIsoTime = isoTime.substring(0, isoTime.lastIndexOf(".") + 4) + "Z" + val time = Instant.parse(modifiedIsoTime) + val now = Instant.now() + val duration = Duration.between(time, now) + + return when { + duration.toMinutes() < 1 -> "방금 전" + duration.toMinutes() < 60 -> "${duration.toMinutes()}분 전" + duration.toHours() < 24 -> "${duration.toHours()}시간 전" + duration.toDays() < 2 -> "어제" + else -> ZonedDateTime.parse(modifiedIsoTime).format(DateTimeFormatter.ofPattern("M월 d일")) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/teampome/pome/presentation/friend/FriendRecordGetAdapter.kt b/app/src/main/java/com/teampome/pome/presentation/friend/FriendRecordGetAdapter.kt index 9c312cc..67c5000 100644 --- a/app/src/main/java/com/teampome/pome/presentation/friend/FriendRecordGetAdapter.kt +++ b/app/src/main/java/com/teampome/pome/presentation/friend/FriendRecordGetAdapter.kt @@ -23,7 +23,7 @@ import java.time.format.DateTimeFormatter //친구 기록 조회 class FriendRecordGetAdapter( private val clickListener: FriendDetailRecordClickListener, -) : ListAdapter(BookDiffCallback) { +) : ListAdapter(BookDiffCallback) { private val friendsMap: HashMap = HashMap() @@ -37,95 +37,15 @@ class FriendRecordGetAdapter( return FriendGetRecordViewHolder( clickListener, ItemFriendDetailCardBinding.inflate(LayoutInflater.from(parent.context), parent, false), + friendsMap ) } - @RequiresApi(Build.VERSION_CODES.O) override fun onBindViewHolder(holder: FriendGetRecordViewHolder, position: Int) { val friends = currentList[position] holder.bind(friends) } - inner class FriendGetRecordViewHolder( - private val friendDetailRecordClickListener: FriendDetailRecordClickListener, - private val binding : ItemFriendDetailCardBinding, - ) : RecyclerView.ViewHolder(binding.root){ - - @RequiresApi(Build.VERSION_CODES.O) - fun bind(getFriedRecord: GetFriendRecord){ - with(binding) { - getFriendRecord = getFriedRecord - - friendDetailMoreSettingIv.setOnClickListener { - friendDetailRecordClickListener.onFriendDetailMoreClick(getFriedRecord.id) - } - - val timeString = getTimeAgo(getFriedRecord.createdAt) - friendDetailContentNameTimeTv.text = " · ${getFriedRecord.oneLineMind} · $timeString" - - if(getFriedRecord.emotionResponse.friendEmotions.isEmpty()) { - friendDetailCardLastFriendEmotionCountTv.visibility = View.INVISIBLE - friendDetailCardLastFriendEmotionAiv.visibility = View.INVISIBLE - } else { - friendDetailCardLastFriendEmotionCountTv.visibility = View.VISIBLE - friendDetailCardLastFriendEmotionAiv.visibility = View.VISIBLE - - val count = getFriedRecord.emotionResponse.friendEmotions.size - - if(count >= 10) { - friendDetailCardLastFriendEmotionCountTv.text = "9+" - } else { - friendDetailCardLastFriendEmotionCountTv.text = "+$count" - } - } - - if(getFriedRecord.emotionResponse.myEmotion == null) { - itemView.context?.let{ context -> - Glide.with(context).load(R.drawable.emoji_mint_28).into(friendDetailCardFirstFriendEmotionAiv) - } - } - - friendDetailCardFirstFriendEmotionAiv.setOnClickListener { - val params = friendEmojiRegisterCl.layoutParams - if(friendEmojiRegisterCl.visibility == View.VISIBLE) { - friendEmojiRegisterCl.visibility = View.GONE - params.height = 0 - } else { - friendEmojiRegisterCl.visibility = View.VISIBLE - params.height = ViewGroup.LayoutParams.WRAP_CONTENT - } - friendEmojiRegisterCl.layoutParams = params - } - - val friends = friendsMap[getFriedRecord.nickname] - - friends?.let { friend -> - Glide.with(itemView.context) - .load(friend.imageKey) - .circleCrop() - .into(friendDetailProfileIv) - } - } - } - - @RequiresApi(Build.VERSION_CODES.O) - private fun getTimeAgo(isoTime: String): String { - // 마이크로 초 부분을 잘라냅니다. - val modifiedIsoTime = isoTime.substring(0, isoTime.lastIndexOf(".") + 4) + "Z" - val time = Instant.parse(modifiedIsoTime) - val now = Instant.now() - val duration = Duration.between(time, now) - - return when { - duration.toMinutes() < 1 -> "방금 전" - duration.toMinutes() < 60 -> "${duration.toMinutes()}분 전" - duration.toHours() < 24 -> "${duration.toHours()}시간 전" - duration.toDays() < 2 -> "어제" - else -> ZonedDateTime.parse(modifiedIsoTime).format(DateTimeFormatter.ofPattern("M월 d일")) - } - } - } - companion object{ private val BookDiffCallback = object : DiffUtil.ItemCallback(){ override fun areItemsTheSame(oldItem: GetFriendRecord, newItem: GetFriendRecord): Boolean {