diff --git a/core/domain/src/main/java/com/android/mediproject/core/domain/EditCommentUseCase.kt b/core/domain/src/main/java/com/android/mediproject/core/domain/EditCommentUseCase.kt index 1e1e5d137..e52dee938 100644 --- a/core/domain/src/main/java/com/android/mediproject/core/domain/EditCommentUseCase.kt +++ b/core/domain/src/main/java/com/android/mediproject/core/domain/EditCommentUseCase.kt @@ -5,8 +5,6 @@ import com.android.mediproject.core.model.requestparameters.DeleteCommentParamet import com.android.mediproject.core.model.requestparameters.EditCommentParameter import com.android.mediproject.core.model.requestparameters.LikeCommentParameter import com.android.mediproject.core.model.requestparameters.NewCommentParameter -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.mapLatest import javax.inject.Inject import javax.inject.Singleton @@ -15,39 +13,25 @@ class EditCommentUseCase @Inject constructor( private val commentsRepository: CommentsRepository, ) { - /** - * 댓글을 수정합니다. - */ - fun applyEditedComment(parameter: EditCommentParameter): Flow> = - commentsRepository.editComment(parameter).mapLatest { result -> - result.fold(onSuccess = { Result.success(Unit) }, onFailure = { Result.failure(it) }) - } - - /** - * 댓글을 등록합니다. - */ - fun applyNewComment(parameter: NewCommentParameter): Flow> = - commentsRepository.applyNewComment(parameter).mapLatest { result -> - result.fold( - onSuccess = { - Result.success(Unit) - }, - onFailure = { Result.failure(it) }, - ) - } - - /** - * 댓글 삭제 - */ - fun deleteComment(parameter: DeleteCommentParameter): Flow> = - commentsRepository.deleteComment(parameter).mapLatest { result -> - result.fold(onSuccess = { Result.success(Unit) }, onFailure = { Result.failure(it) }) - } - - /** - * 댓글 좋아요 - */ - fun likeComment(parameter: LikeCommentParameter): Flow> = commentsRepository.likeComment(parameter).mapLatest { result -> + + fun applyEditedComment(parameter: EditCommentParameter): Result = commentsRepository.editComment(parameter).let { result -> result.fold(onSuccess = { Result.success(Unit) }, onFailure = { Result.failure(it) }) } + + + fun applyNewComment(parameter: NewCommentParameter): Result = commentsRepository.applyNewComment(parameter).fold( + onSuccess = { + Result.success(Unit) + }, + onFailure = { Result.failure(it) }, + ) + + + fun deleteComment(parameter: DeleteCommentParameter): Result = + commentsRepository.deleteComment(parameter).fold(onSuccess = { Result.success(Unit) }, onFailure = { Result.failure(it) }) + + + fun likeComment(parameter: LikeCommentParameter): Result = + commentsRepository.likeComment(parameter).fold(onSuccess = { Result.success(Unit) }, onFailure = { Result.failure(it) }) + } diff --git a/core/test/src/main/java/com/android/mediproject/core/test/repositories/FakeSignRepository.kt b/core/test/src/main/java/com/android/mediproject/core/test/repositories/FakeSignRepository.kt index 37e95a6ae..a89201a72 100644 --- a/core/test/src/main/java/com/android/mediproject/core/test/repositories/FakeSignRepository.kt +++ b/core/test/src/main/java/com/android/mediproject/core/test/repositories/FakeSignRepository.kt @@ -1,10 +1,6 @@ package com.android.mediproject.core.test.repositories -import com.android.mediproject.core.data.sign.SignRepository -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.channelFlow - -class FakeSignRepository : SignRepository { +/*class FakeSignRepository : SignRepository { override fun login(loginParameter: com.android.mediproject.core.model.requestparameters.LoginParameter): Flow> = channelFlow { trySend(Result.success(Unit)) } @@ -15,4 +11,4 @@ class FakeSignRepository : SignRepository { override fun signOut() { } -} +}*/ diff --git a/feature/comments/src/main/java/com/android/mediproject/feature/comments/commentsofamedicine/MedicineCommentsViewModel.kt b/feature/comments/src/main/java/com/android/mediproject/feature/comments/commentsofamedicine/MedicineCommentsViewModel.kt index cb038e6c8..05f834f89 100644 --- a/feature/comments/src/main/java/com/android/mediproject/feature/comments/commentsofamedicine/MedicineCommentsViewModel.kt +++ b/feature/comments/src/main/java/com/android/mediproject/feature/comments/commentsofamedicine/MedicineCommentsViewModel.kt @@ -13,9 +13,7 @@ import com.android.mediproject.core.domain.GetCommentsUseCase import com.android.mediproject.core.model.comments.BaseComment import com.android.mediproject.core.model.comments.Comment import com.android.mediproject.core.model.navargs.MedicineBasicInfoArgs -import com.android.mediproject.core.model.requestparameters.DeleteCommentParameter import com.android.mediproject.core.model.requestparameters.EditCommentParameter -import com.android.mediproject.core.model.requestparameters.LikeCommentParameter import com.android.mediproject.core.model.requestparameters.NewCommentParameter import com.android.mediproject.core.model.user.AccountState import com.android.mediproject.core.model.user.onSignedIn @@ -47,6 +45,7 @@ import kotlinx.coroutines.flow.last import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import javax.inject.Inject @HiltViewModel @@ -54,22 +53,20 @@ class MedicineCommentsViewModel @Inject constructor( private val getCommentsUseCase: GetCommentsUseCase, private val editCommentUseCase: EditCommentUseCase, private val getAccountStateUseCase: GetAccountStateUseCase, - @Dispatcher(MediDispatchers.Default) private val defaultDispatcher: CoroutineDispatcher, @Dispatcher(MediDispatchers.IO) private val ioDispatcher: CoroutineDispatcher, ) : BaseViewModel(), ISendText { private val _action = MutableSharedFlow(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST, extraBufferCapacity = 1) - val action get() = _action.asSharedFlow() + val action = _action.asSharedFlow() - private val _medicineBasicInfo = - MutableStateFlow(null) - private val medicineBasicInfo get() = _medicineBasicInfo.asStateFlow() + private val _medicineBasicInfo = MutableStateFlow(null) + private val medicineBasicInfo = _medicineBasicInfo.asStateFlow() - private val _myId = MutableStateFlow(NONE_USER_ID) - private val myId get() = _myId.asStateFlow() + private val _myId = MutableStateFlow(NONE_USER_ID) + private val myId = _myId.asStateFlow() private val _accountState = MutableStateFlow(AccountState.SignedOut) - private val accountState get() = _accountState.asStateFlow() + private val accountState = _accountState.asStateFlow() private val _replyId = MutableStateFlow(NONE_REPLY_ID) private val replyId = _replyId.asStateFlow() @@ -80,7 +77,7 @@ class MedicineCommentsViewModel @Inject constructor( } init { - viewModelScope.launch(defaultDispatcher) { + viewModelScope.launch { getAccountStateUseCase().collectLatest { _accountState.value = it it.onSignedIn { myId, _, _ -> @@ -90,7 +87,6 @@ class MedicineCommentsViewModel @Inject constructor( } } - val comments: StateFlow> = medicineBasicInfo.filterNotNull().flatMapLatest { info -> getCommentsUseCase.getCommentsByMedicineId(info.medicineIdInAws, myId.value).cachedIn(viewModelScope).mapLatest { pagingData -> val signedIn = accountState.value is AccountState.SignedIn @@ -110,76 +106,60 @@ class MedicineCommentsViewModel @Inject constructor( } } } - }.flowOn(defaultDispatcher).stateIn(viewModelScope, SharingStarted.Eagerly, PagingData.empty()) + }.flowOn(ioDispatcher).stateIn(viewModelScope, SharingStarted.Eagerly, PagingData.empty()) - /** - * 답글 등록 - * - * @param comment 답글 내용 - */ private fun applyReply(comment: String) { - viewModelScope.launch(defaultDispatcher) { - editCommentUseCase.applyNewComment( - NewCommentParameter( - medicineId = medicineBasicInfo.filterNotNull().last().medicineIdInAws.toString(), - userId = myId.value.toString(), - content = comment, - parentId = replyId.value.toString(), - ), - ).collectLatest { result -> - result.onSuccess { - // 댓글 등록 성공 - _action.emit(OnCompleteApplyCommentOrReply(true)) - }.onFailure { - _action.emit(OnCompleteApplyCommentOrReply(false)) - } + viewModelScope.launch { + withContext(ioDispatcher) { + editCommentUseCase.applyNewComment( + NewCommentParameter( + medicineId = medicineBasicInfo.filterNotNull().last().medicineIdInAws.toString(), + userId = myId.value.toString(), + content = comment, + parentId = replyId.value.toString(), + ), + ) + }.onSuccess { + _action.emit(OnCompleteApplyCommentOrReply(true)) + }.onFailure { + _action.emit(OnCompleteApplyCommentOrReply(false)) } } } - /** - * 새 댓글 등록 - */ private fun applyNewComment(content: String) { - viewModelScope.launch(defaultDispatcher) { - editCommentUseCase.applyNewComment( - NewCommentParameter( - medicineId = medicineBasicInfo.value!!.medicineIdInAws.toString(), - userId = myId.value.toString(), - content = content, - ), - ).collectLatest { result -> - result.onSuccess { - // 댓글 등록 성공 - _action.emit(OnCompleteApplyCommentOrReply(true)) - }.onFailure { - _action.emit(OnCompleteApplyCommentOrReply(false)) - } + viewModelScope.launch { + withContext(ioDispatcher) { + editCommentUseCase.applyNewComment( + NewCommentParameter( + medicineId = medicineBasicInfo.value!!.medicineIdInAws.toString(), + userId = myId.value.toString(), + content = content, + ), + ) + }.onSuccess { + _action.emit(OnCompleteApplyCommentOrReply(true)) + }.onFailure { + _action.emit(OnCompleteApplyCommentOrReply(false)) } } } - /** - * 수정한 댓글(답글) 등록 - * - * @param comment 수정한 댓글(답글) 정보 - */ private fun editComment(comment: Comment) { - viewModelScope.launch(defaultDispatcher) { - editCommentUseCase.applyEditedComment( - EditCommentParameter( - commentId = comment.commentId, - content = comment.content, - medicineId = medicineBasicInfo.value!!.medicineIdInAws, - ), - ).collectLatest { result -> - result.onSuccess { - // 댓글 수정 성공 - _action.emit(OnCompleteApplyEditComment(true)) - }.onFailure { - _action.emit(OnCompleteApplyEditComment(false)) - } + viewModelScope.launch { + withContext(ioDispatcher) { + editCommentUseCase.applyEditedComment( + EditCommentParameter( + commentId = comment.commentId, + medicineId = medicineBasicInfo.value!!.medicineIdInAws, + content = comment.content, + ), + ) + }.onSuccess { + _action.emit(OnCompleteApplyEditComment(true)) + }.onFailure { + _action.emit(OnCompleteApplyEditComment(false)) } } } @@ -199,52 +179,16 @@ class MedicineCommentsViewModel @Inject constructor( } - /** - * 댓글 삭제 클릭 - * - * @param commentId 삭제할 댓글의 id - */ private fun onClickedDelete(commentId: Long) { - viewModelScope.launch { - _action.emit(OnClickToDeleteComment(commentId)) - } + } - /** - * 댓글 삭제 처리 - */ fun deleteComment(commentId: Long) { - viewModelScope.launch { - editCommentUseCase.deleteComment(DeleteCommentParameter(commentId, medicineBasicInfo.value!!.medicineIdInAws)) - .collectLatest { result -> - result.onSuccess { - // 댓글 삭제 성공 - _action.emit(OnCompleteDeleteComment(true)) - }.onFailure { - _action.emit(OnCompleteDeleteComment(false)) - } - } - } + } - /** - * 댓글 좋아요 클릭 - * - 좋아요 등록 또는 해제를 처리함 - * - * @param commentId LIKE를 등록또는 해제 할 댓글의 id - */ private fun onClickedLike(commentId: Long, isLiked: Boolean) { - viewModelScope.launch { - editCommentUseCase.likeComment(LikeCommentParameter(commentId, medicineBasicInfo.value!!.medicineIdInAws, isLiked)) - .collectLatest { result -> - result.onSuccess { - // like 처리 완료 - _action.emit(OnCompleteLike(true)) - }.onFailure { - _action.emit(OnCompleteLike(false)) - } - } - } + } /** @@ -262,13 +206,6 @@ class MedicineCommentsViewModel @Inject constructor( } } - - /** - * 댓글 등록하기 버튼 클릭 - * - * @param text 댓글 내용 - * - */ override fun onClickedSendButton(text: CharSequence) { viewModelScope.launch { if (text.isEmpty()) {