Skip to content

Commit

Permalink
Merge pull request #146 from team-winey/feature/fix-comment-empty-switch
Browse files Browse the repository at this point in the history
[fix] ์ƒ์„ธ ํ”ผ๋“œ / ๋Œ“๊ธ€ ์ƒ์„ฑ, ์‚ญ์ œ ํ›„ empty ๋ทฐ ์ „ํ™˜ํ•˜๊ธฐ
  • Loading branch information
leeeha authored Aug 23, 2023
2 parents 80da0c9 + cccf775 commit f96c88b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ package com.android.go.sopt.winey.presentation.main.feed.detail
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.android.go.sopt.winey.databinding.ItemDetailCommentEmptyBinding
import com.android.go.sopt.winey.databinding.LayoutCommentEmptyBinding

class CommentEmptyAdapter : RecyclerView.Adapter<CommentEmptyAdapter.CommentEmptyViewHolder>() {

class CommentEmptyViewHolder(binding: ItemDetailCommentEmptyBinding) :
class CommentEmptyViewHolder(binding: LayoutCommentEmptyBinding) :
RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentEmptyViewHolder {
return CommentEmptyViewHolder(
ItemDetailCommentEmptyBinding.inflate(
LayoutCommentEmptyBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}

override fun getItemCount(): Int = COMMENT_ITEM_COUNT
override fun getItemCount(): Int = EMPTY_ITEM_COUNT

override fun onBindViewHolder(holder: CommentEmptyViewHolder, position: Int) {}

companion object {
private const val COMMENT_ITEM_COUNT = 1
private const val EMPTY_ITEM_COUNT = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
}
}

private fun initDetailFeedAdapter(detailFeed: DetailFeed?) {
if (detailFeed == null) {
Timber.e("DETAIL FEED IS NULL")
return
}
private fun initDetailFeedAdapter(detailFeed: DetailFeed) {
_detailFeedAdapter =
DetailFeedAdapter(
detailFeed,
Expand Down Expand Up @@ -236,25 +232,20 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_

private fun initCommentCreateButtonClickListener() {
binding.tvCommentCreate.setOnClickListener {
checkEmptyCommentList()
val content = binding.etComment.text.toString()
viewModel.postComment(feedId, content)
}
}

private fun checkEmptyCommentList() {
if (commentAdapter.currentList.size == 0) {
binding.rvDetail.adapter = ConcatAdapter(detailFeedAdapter, commentAdapter)
}
}

private fun initGetFeedDetailObserver() {
viewModel.getFeedDetailState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> {
val detailFeed = state.data
val detailFeed = state.data ?: return@onEach
initDetailFeedAdapter(detailFeed)
switchCommentContainer(detailFeed?.commentList)

val commentList = detailFeed.commentList
switchCommentContainer(commentList)
}

is UiState.Failure -> {
Expand All @@ -266,12 +257,7 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
}.launchIn(lifecycleScope)
}

private fun switchCommentContainer(commentList: List<Comment>?) {
if (commentList == null) {
Timber.e("DETAIL COMMENT LIST IS NULL")
return
}

private fun switchCommentContainer(commentList: List<Comment>) {
if (commentList.isEmpty()) {
binding.rvDetail.adapter = ConcatAdapter(detailFeedAdapter, commentEmptyAdapter)
} else {
Expand Down Expand Up @@ -320,6 +306,11 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
when (state) {
is UiState.Success -> {
val comment = state.data ?: return@onEach

if (isCommentListEmpty()) {
updateRecyclerViewAdapter(ACTION_COMMENT_POST)
}

val commentNumber = commentAdapter.addItem(comment)
detailFeedAdapter.updateCommentNumber(commentNumber.toLong())
binding.etComment.text.clear()
Expand All @@ -334,17 +325,41 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
}.launchIn(lifecycleScope)
}

private fun isCommentListEmpty() = commentAdapter.currentList.isEmpty()

private fun updateRecyclerViewAdapter(action: String) {
when (action) {
ACTION_COMMENT_POST ->
binding.rvDetail.adapter =
ConcatAdapter(detailFeedAdapter, commentAdapter)

ACTION_COMMENT_DELETE -> {
binding.rvDetail.adapter =
ConcatAdapter(detailFeedAdapter, commentEmptyAdapter)
}
}
}

private fun initDeleteCommentStateObserver() {
viewModel.deleteCommentState.flowWithLifecycle(lifecycle)
.onEach { state ->
when (state) {
is UiState.Success -> {
if (state.data == null) return@onEach

val commentNumber = commentAdapter.deleteItem(state.data.commentId)
val commentId = state.data.commentId
val commentNumber = commentAdapter.deleteItem(commentId)
detailFeedAdapter.updateCommentNumber(commentNumber.toLong())

wineySnackbar(binding.root, true, stringOf(R.string.snackbar_comment_delete_success))
if (isCommentNumberZero(commentNumber)) {
updateRecyclerViewAdapter(ACTION_COMMENT_DELETE)
}

wineySnackbar(
binding.root,
true,
stringOf(R.string.snackbar_comment_delete_success)
)
}

is UiState.Failure -> {
Expand All @@ -357,6 +372,8 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_
}.launchIn(lifecycleScope)
}

private fun isCommentNumberZero(commentNumber: Int) = commentNumber == 0

private fun navigateToMainWithBundle(extraKey: String) {
Intent(this, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
Expand Down Expand Up @@ -384,5 +401,8 @@ class DetailActivity : BindingActivity<ActivityDetailBinding>(R.layout.activity_

private const val TARGET_DETAIL_FEED = "detailFeed"
private const val TARGET_COMMENT = "comment"

private const val ACTION_COMMENT_POST = "POST"
private const val ACTION_COMMENT_DELETE = "DELETE"
}
}
116 changes: 0 additions & 116 deletions app/src/main/res/layout/fragment_detail.xml

This file was deleted.

0 comments on commit f96c88b

Please sign in to comment.