Skip to content

Commit

Permalink
refactor(FeedDetailActivity): 댓글 수정 관련 LiveDate 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
ki960213 committed Dec 15, 2023
1 parent de7ece3 commit bba8337
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ class FeedDetailActivity :
hideKeyboard()
}
binding.onUpdatedCommentSubmitButtonClick = {
val commentId = viewModel.editingCommentId.value
if (commentId != null) viewModel.updateComment(commentId, it)
val comment = viewModel.editingComment.value
if (comment != null) viewModel.updateComment(comment.id, it)
hideKeyboard()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ class FeedDetailViewModel @Inject constructor(
val isFeedDetailWrittenByLoginUser: Boolean
get() = feed.writer.id == uid

private val _editingCommentId = MutableLiveData<Long?>()
val editingCommentId: LiveData<Long?> = _editingCommentId
val editingCommentContent: LiveData<String?> = _editingCommentId.map { commentId ->
if (commentId == null) null else commentUiStates.find { it.comment.id == commentId }?.comment?.content
}
private val _editingComment = MutableLiveData<Comment?>()
val editingComment: LiveData<Comment?> = _editingComment

val isEditingComment: LiveData<Boolean> = _editingComment.map { it != null }

private val _canSubmitComment = NotNullMutableLiveData(true)
val canSubmitComment: NotNullLiveData<Boolean> = _canSubmitComment
Expand Down Expand Up @@ -188,7 +187,7 @@ class FeedDetailViewModel @Inject constructor(

fun updateComment(commentId: Long, content: String): Job = commandAndRefresh(
command = { commentRepository.updateComment(commentId, content) },
onSuccess = { _editingCommentId.value = null },
onSuccess = { _editingComment.value = null },
onFailure = { _, _ -> _uiEvent.value = FeedDetailUiEvent.CommentUpdateFail },
onStart = { _canSubmitComment.value = false },
onFinish = { _canSubmitComment.value = true },
Expand All @@ -200,13 +199,16 @@ class FeedDetailViewModel @Inject constructor(
)

fun startEditComment(commentId: Long) {
_editingCommentId.value = commentId
_editingComment.value = commentUiStates
.find { it.comment.id == commentId }
?.comment
?: return
unhighlightJob?.cancel()
_feedDetailUiState.value = _feedDetailUiState.value.highlightComment(commentId)
}

fun cancelEditComment() {
_editingCommentId.value = null
_editingComment.value = null
_feedDetailUiState.value = _feedDetailUiState.value.unhighlightComment()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:hint="@string/comments_edit_comment_hint"
app:visible="@{vm.editingCommentId == null}"
app:visible="@{!vm.isEditingComment}"
app:onSubmit="@{(content) -> onCommentSubmitButtonClick.invoke(content)}"
app:isSubmitEnabled="@{vm.canSubmitComment}"
app:submitButtonLabel="@string/comments_comment_submit_button_label" />
Expand All @@ -86,8 +86,8 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:text="@{vm.editingCommentContent}"
app:visible="@{vm.editingCommentId != null}"
app:text="@{vm.editingComment.content}"
app:visible="@{vm.isEditingComment}"
app:isSubmitEnabled="@{vm.canSubmitComment}"
app:submitButtonLabel="@string/all_update_button_label"
app:cancelButtonLabel="@string/all_cancel"
Expand Down

0 comments on commit bba8337

Please sign in to comment.