diff --git a/app/src/main/java/com/android/go/sopt/winey/presentation/main/feed/detail/CommentEmptyAdapter.kt b/app/src/main/java/com/android/go/sopt/winey/presentation/main/feed/detail/CommentEmptyAdapter.kt index 990ee0aa..6b2d19b9 100644 --- a/app/src/main/java/com/android/go/sopt/winey/presentation/main/feed/detail/CommentEmptyAdapter.kt +++ b/app/src/main/java/com/android/go/sopt/winey/presentation/main/feed/detail/CommentEmptyAdapter.kt @@ -3,16 +3,16 @@ 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() { - 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 @@ -20,11 +20,11 @@ class CommentEmptyAdapter : RecyclerView.Adapter(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, @@ -236,25 +232,20 @@ class DetailActivity : BindingActivity(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 -> { @@ -266,12 +257,7 @@ class DetailActivity : BindingActivity(R.layout.activity_ }.launchIn(lifecycleScope) } - private fun switchCommentContainer(commentList: List?) { - if (commentList == null) { - Timber.e("DETAIL COMMENT LIST IS NULL") - return - } - + private fun switchCommentContainer(commentList: List) { if (commentList.isEmpty()) { binding.rvDetail.adapter = ConcatAdapter(detailFeedAdapter, commentEmptyAdapter) } else { @@ -320,6 +306,11 @@ class DetailActivity : BindingActivity(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() @@ -334,6 +325,21 @@ class DetailActivity : BindingActivity(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 -> @@ -341,10 +347,19 @@ class DetailActivity : BindingActivity(R.layout.activity_ 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 -> { @@ -357,6 +372,8 @@ class DetailActivity : BindingActivity(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) @@ -384,5 +401,8 @@ class DetailActivity : BindingActivity(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" } } diff --git a/app/src/main/res/layout/fragment_detail.xml b/app/src/main/res/layout/fragment_detail.xml deleted file mode 100644 index 303a09b0..00000000 --- a/app/src/main/res/layout/fragment_detail.xml +++ /dev/null @@ -1,116 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/item_detail_comment_empty.xml b/app/src/main/res/layout/layout_comment_empty.xml similarity index 100% rename from app/src/main/res/layout/item_detail_comment_empty.xml rename to app/src/main/res/layout/layout_comment_empty.xml