Skip to content

Commit

Permalink
Revert "merge: 댓글 하이라이팅 (#883)" (#889)
Browse files Browse the repository at this point in the history
This reverts commit ce92124.
  • Loading branch information
ki960213 authored Dec 29, 2023
1 parent ce92124 commit aafde96
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 263 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class SubTextInputWindow @JvmOverloads constructor(
init {
applyStyledAttributes(attrs)
addView(binding.root)
isClickable = true
background = context.getColor(R.color.white).toDrawable()
elevation = 5f.dp
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.emmsale.R
import com.emmsale.data.model.Comment
import com.emmsale.databinding.ActivityChildCommentsBinding
import com.emmsale.presentation.base.NetworkActivity
import com.emmsale.presentation.common.extension.hideKeyboard
import com.emmsale.presentation.common.extension.showKeyboard
import com.emmsale.presentation.common.extension.showSnackBar
import com.emmsale.presentation.common.layoutManager.CenterSmoothScroller
import com.emmsale.presentation.common.layoutManager.EndSmoothScroller
import com.emmsale.presentation.common.recyclerView.DividerItemDecoration
import com.emmsale.presentation.common.views.InfoDialog
import com.emmsale.presentation.common.views.WarningDialog
Expand All @@ -27,26 +24,18 @@ import com.emmsale.presentation.ui.childCommentList.ChildCommentsViewModel.Compa
import com.emmsale.presentation.ui.childCommentList.recyclerView.CommentsAdapter
import com.emmsale.presentation.ui.childCommentList.uiState.ChildCommentsUiEvent
import com.emmsale.presentation.ui.feedDetail.FeedDetailActivity
import com.emmsale.presentation.ui.feedDetail.uiState.CommentsUiState
import com.emmsale.presentation.ui.profile.ProfileActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class ChildCommentsActivity :
NetworkActivity<ActivityChildCommentsBinding>(R.layout.activity_child_comments) {

override val viewModel: ChildCommentsViewModel by viewModels()

@Inject
lateinit var centerSmoothScroller: CenterSmoothScroller

@Inject
lateinit var endSmoothScroller: EndSmoothScroller

private val commentsAdapter: CommentsAdapter = CommentsAdapter(
onCommentClick = { },
onCommentClick = { comment -> viewModel.unhighlight(comment.id) },
onAuthorImageClick = { authorId -> ProfileActivity.startActivity(this, authorId) },
onCommentMenuClick = ::showCommentMenuDialog,
)
Expand Down Expand Up @@ -74,21 +63,9 @@ class ChildCommentsActivity :

private fun BottomMenuDialog.addCommentUpdateButton(commentId: Long) {
addMenuItemBelow(context.getString(R.string.all_update_button_label)) {
viewModel.startEditComment(commentId)
viewModel.setEditMode(true, commentId)
binding.stiwCommentUpdate.requestFocusOnEditText()
showKeyboard()
startToEditComment(commentId)
}
}

private fun startToEditComment(commentId: Long) {
val position = viewModel.comments.value.getPosition(commentId)

lifecycleScope.launch {
delay(KEYBOARD_SHOW_WAITING_TIME)
binding.rvChildcommentsChildcomments
.layoutManager
?.startSmoothScroll(endSmoothScroller.apply { targetPosition = position })
}
}

Expand Down Expand Up @@ -153,12 +130,12 @@ class ChildCommentsActivity :
hideKeyboard()
}
binding.onCommentUpdateCancelButtonClick = {
viewModel.cancelEditComment()
viewModel.setEditMode(false)
hideKeyboard()
}
binding.onUpdatedCommentSubmitButtonClick = {
val comment = viewModel.editingComment.value
if (comment != null) viewModel.updateComment(comment.id, it)
val commentId = viewModel.editingCommentId.value
if (commentId != null) viewModel.updateComment(commentId, it)
hideKeyboard()
}
}
Expand Down Expand Up @@ -195,31 +172,23 @@ class ChildCommentsActivity :

private fun observeComments() {
viewModel.comments.observe(this) {
commentsAdapter.submitList(it.commentUiStates) { handleHighlightComment() }
commentsAdapter.submitList(it.commentUiStates) { scrollToIfFirstFetch(it) }
}
}

private fun handleHighlightComment() {
if (highlightCommentId == INVALID_COMMENT_ID || isNotRealFirstEnter()) return

viewModel.isAlreadyFirstFetched = true
viewModel.highlightCommentOnFirstEnter(highlightCommentId)
highlightCommentOnFirstEnter()
}

private fun isNotRealFirstEnter(): Boolean =
viewModel.isAlreadyFirstFetched || viewModel.comments.value.commentUiStates.isEmpty()

private fun highlightCommentOnFirstEnter() {
val position = viewModel.comments.value.getPosition(highlightCommentId)
private fun scrollToIfFirstFetch(commentUiState: CommentsUiState) {
fun cantScroll(): Boolean =
viewModel.isAlreadyFirstFetched || commentUiState.commentUiStates.isEmpty()

if (highlightCommentId == INVALID_COMMENT_ID || cantScroll()) return
val position = viewModel.comments.value.commentUiStates
.indexOfFirst {
it.comment.id == highlightCommentId
}
binding.rvChildcommentsChildcomments.scrollToPosition(position)
lifecycleScope.launch {
delay(100L) // 버그 때문에
binding.rvChildcommentsChildcomments
.layoutManager
?.startSmoothScroll(centerSmoothScroller.apply { targetPosition = position })
}

viewModel.highlight(highlightCommentId)
viewModel.isAlreadyFirstFetched = true
}

private fun observeUiEvent() {
Expand Down Expand Up @@ -273,7 +242,6 @@ class ChildCommentsActivity :
private const val KEY_HIGHLIGHT_COMMENT_ID = "KEY_HIGHLIGHT_COMMENT_ID"
private const val KEY_FROM_POST_DETAIL = "KEY_FROM_POST_DETAIL"
private const val INVALID_COMMENT_ID: Long = -1
private const val KEYBOARD_SHOW_WAITING_TIME = 300L

fun startActivity(
context: Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.map
import androidx.lifecycle.viewModelScope
import com.emmsale.data.model.Comment
import com.emmsale.data.repository.interfaces.CommentRepository
import com.emmsale.data.repository.interfaces.TokenRepository
import com.emmsale.presentation.base.RefreshableViewModel
Expand All @@ -16,8 +14,6 @@ import com.emmsale.presentation.ui.childCommentList.uiState.ChildCommentsUiEvent
import com.emmsale.presentation.ui.feedDetail.uiState.CommentsUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.properties.Delegates.vetoable

Expand All @@ -40,16 +36,16 @@ class ChildCommentsViewModel @Inject constructor(
private val _comments = NotNullMutableLiveData(CommentsUiState())
val comments: NotNullLiveData<CommentsUiState> = _comments

private val _editingComment = MutableLiveData<Comment?>()
val editingComment: LiveData<Comment?> = _editingComment
private val _editingCommentId = MutableLiveData<Long?>()
val editingCommentId: LiveData<Long?> = _editingCommentId

val isEditingComment: LiveData<Boolean> = _editingComment.map { it != null }
val editingCommentContent: LiveData<String?> = _editingCommentId.map { commentId ->
if (commentId == null) null else _comments.value[commentId]?.comment?.content
}

private val _canSubmitComment = NotNullMutableLiveData(true)
val canSubmitComment: NotNullLiveData<Boolean> = _canSubmitComment

private var unhighlightJob: Job? = null

private val _uiEvent = SingleLiveEvent<ChildCommentsUiEvent>()
val uiEvent: LiveData<ChildCommentsUiEvent> = _uiEvent

Expand All @@ -73,7 +69,7 @@ class ChildCommentsViewModel @Inject constructor(

fun updateComment(commentId: Long, content: String): Job = commandAndRefresh(
command = { commentRepository.updateComment(commentId, content) },
onSuccess = { _editingComment.value = null },
onSuccess = { _editingCommentId.value = null },
onFailure = { _, _ -> _uiEvent.value = ChildCommentsUiEvent.CommentUpdateFail },
onStart = { _canSubmitComment.value = false },
onFinish = { _canSubmitComment.value = true },
Expand All @@ -84,26 +80,8 @@ class ChildCommentsViewModel @Inject constructor(
onFailure = { _, _ -> _uiEvent.value = ChildCommentsUiEvent.CommentDeleteFail },
)

fun startEditComment(commentId: Long) {
_editingComment.value = comments.value.commentUiStates
.find { it.comment.id == commentId }
?.comment
?: return
unhighlightJob?.cancel()
_comments.value = _comments.value.highlight(commentId)
}

fun cancelEditComment() {
_editingComment.value = null
_comments.value = _comments.value.unhighlight()
}

fun highlightCommentOnFirstEnter(commentId: Long) {
_comments.value = _comments.value.highlight(commentId)
unhighlightJob = viewModelScope.launch {
delay(COMMENT_HIGHLIGHTING_DURATION_ON_FIRST_ENTER)
_comments.value = _comments.value.unhighlight()
}
fun setEditMode(isEditMode: Boolean, commentId: Long = INVALID_COMMENT_ID) {
_editingCommentId.value = if (isEditMode) commentId else null
}

fun reportComment(commentId: Long): Job = command(
Expand All @@ -128,12 +106,24 @@ class ChildCommentsViewModel @Inject constructor(
onSuccess = { _comments.value = CommentsUiState(uid, it) },
)

fun highlight(commentId: Long) {
val comment = _comments.value.commentUiStates.find { it.comment.id == commentId } ?: return
if (comment.isHighlight) return
_comments.value = _comments.value.highlight(commentId)
}

fun unhighlight(commentId: Long) {
val comment = _comments.value.commentUiStates.find { it.comment.id == commentId } ?: return
if (!comment.isHighlight) return
_comments.value = _comments.value.unhighlight()
}

companion object {
const val KEY_FEED_ID = "KEY_FEED_ID"
const val KEY_PARENT_COMMENT_ID = "KEY_PARENT_COMMENT_ID"

private const val REPORT_DUPLICATE_ERROR_CODE = 400
private const val INVALID_COMMENT_ID: Long = -1

private const val COMMENT_HIGHLIGHTING_DURATION_ON_FIRST_ENTER = 2000L
private const val REPORT_DUPLICATE_ERROR_CODE = 400
}
}
Loading

0 comments on commit aafde96

Please sign in to comment.