Skip to content

Commit

Permalink
[feat] #132 댓글 생성 후 리스트와 댓글 총 개수 갱신하기
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeha committed Aug 21, 2023
1 parent 2391a82 commit 092707d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package com.android.go.sopt.winey.data.model.remote.response

import com.android.go.sopt.winey.domain.entity.Comment
import kotlinx.datetime.LocalDateTime
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ResponsePostCommentDto(
@SerialName("commentId")
val commentId: Long,
@SerialName("commentCounter")
val commentCounter: Long,
@SerialName("authorId")
val authorId: Int,
@SerialName("author")
val author: String,
@SerialName("content")
val content: String,
@SerialName("authorId")
val authorId: Int,
@SerialName("authorLevel")
val authorLevel: Int
val authorLevel: Int,
@SerialName("createdAt")
val createdAt: LocalDateTime
) {
fun toComment() = Comment(
commentId = this.commentId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class DetailFeed(
val nickName: String,
val userId: Int,
val writerLevel: Int,
val comments: Long,
var comments: Long,
val timeAgo: String,
val commentList: List<Comment>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class CommentAdapter(
holder.onBind(getItem(position))
}

fun addItem(item: Comment) {
fun addItem(item: Comment): Int {
val newList = currentList.toMutableList()
newList.add(item)
submitList(newList)
return newList.size
}

// todo: 어댑터에서 아이템 삭제한 뒤에, 실제로 서버통신도 해줘야 다음에 GET 할 때 반영된다!
fun deleteItem(position: Int) {
val newList = currentList.toMutableList()
newList.removeAt(position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
private val feedId by lazy { intent.getIntExtra(KEY_FEED_ID, 0) }
private val feedWriterId by lazy { intent.getIntExtra(KEY_FEED_WRITER_ID, 0) }

private var detailFeedAdapter: DetailFeedAdapter? = null
private var commentAdapter: CommentAdapter? = null
private var _detailFeedAdapter: DetailFeedAdapter? = null
private val detailFeedAdapter get() = requireNotNull(_detailFeedAdapter)

private var _commentAdapter: CommentAdapter? = null
private val commentAdapter get() = requireNotNull(_commentAdapter)

private val commentEmptyAdapter by lazy { CommentEmptyAdapter() }

@Inject
Expand All @@ -55,7 +59,7 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
}

private fun initCommentAdapter() {
commentAdapter = CommentAdapter(
_commentAdapter = CommentAdapter(
onPopupMenuClicked = { anchorView, commentAuthorId ->
showPopupMenu(anchorView, commentAuthorId)
}
Expand Down Expand Up @@ -165,7 +169,9 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
when (state) {
is UiState.Success -> {
val comment = state.data ?: return@onEach
commentAdapter?.addItem(comment)
val commentNumber = commentAdapter.addItem(comment)
detailFeedAdapter.updateCommentNumber(commentNumber.toLong())
binding.etComment.text.clear()
}

is UiState.Failure -> {
Expand Down Expand Up @@ -202,7 +208,7 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
Timber.e("DETAIL FEED IS NULL")
return
}
detailFeedAdapter = DetailFeedAdapter(detailFeed)
_detailFeedAdapter = DetailFeedAdapter(detailFeed)
}

private fun switchCommentContainer(commentList: List<Comment>?) {
Expand All @@ -215,7 +221,7 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
binding.rvDetail.adapter = ConcatAdapter(detailFeedAdapter, commentEmptyAdapter)
} else {
binding.rvDetail.adapter = ConcatAdapter(detailFeedAdapter, commentAdapter)
commentAdapter?.submitList(commentList)
commentAdapter.submitList(commentList)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import com.android.go.sopt.winey.domain.entity.DetailFeed

class DetailFeedAdapter(
private val detailFeed: DetailFeed
) :
RecyclerView.Adapter<DetailFeedAdapter.DetailFeedViewHolder>() {
) : RecyclerView.Adapter<DetailFeedAdapter.DetailFeedViewHolder>() {
class DetailFeedViewHolder(
private val binding: ItemDetailFeedBinding
) : RecyclerView.ViewHolder(binding.root) {
fun bind(data: DetailFeed) {
binding.data = data
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetailFeedViewHolder {
return DetailFeedViewHolder(
Expand All @@ -21,18 +27,15 @@ class DetailFeedAdapter(
)
}

override fun getItemCount(): Int = FEED_ITEM_COUNT

override fun onBindViewHolder(holder: DetailFeedViewHolder, position: Int) {
holder.bind(detailFeed)
}

override fun getItemCount(): Int = FEED_ITEM_COUNT

class DetailFeedViewHolder(
private val binding: ItemDetailFeedBinding
) : RecyclerView.ViewHolder(binding.root) {
fun bind(data: DetailFeed) {
binding.data = data
}
fun updateCommentNumber(comments: Long) {
detailFeed.comments = comments
notifyItemChanged(0)
}

companion object {
Expand Down

0 comments on commit 092707d

Please sign in to comment.