Skip to content

Commit

Permalink
πŸ–‹οΈ λΆ„μ„λœ 감정 μ‚¬μš©μž μˆ˜μ • κΈ°λŠ₯ (#109)
Browse files Browse the repository at this point in the history
* [feat] 감정 μˆ˜μ • | API μΆ”κ°€

* [feat] 감정 μˆ˜μ • | UseCase μΆ”κ°€

* [feat] 감정 μˆ˜μ • | 감정 별 Selector μΆ”κ°€

* [feat] 감정 μˆ˜μ • | View, ViewModel
  • Loading branch information
moondev03 authored Aug 15, 2024
1 parent 1a3b434 commit 9b37315
Show file tree
Hide file tree
Showing 18 changed files with 525 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import com.nabi.data.model.BaseResponse
import com.nabi.data.model.PageableResponse
import com.nabi.data.model.emotion.AddDiaryEmotionResponseDTO
import com.nabi.data.model.emotion.DiaryStatisticsResponseDTO
import com.nabi.data.model.emotion.PatchEmotionResDTO
import com.nabi.data.model.emotion.SearchEmotionResponseDTO
import retrofit2.http.Header
import retrofit2.http.Path

interface EmotionRemoteDataSource {

Expand All @@ -28,4 +31,10 @@ interface EmotionRemoteDataSource {
diaryId: Int,
emotionState: String
): Result<BaseResponse<AddDiaryEmotionResponseDTO>>

suspend fun patchDiaryEmotion(
accessToken: String,
diaryId: Int,
emotion: String
): Result<BaseResponse<PatchEmotionResDTO>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.nabi.data.model.BaseResponse
import com.nabi.data.model.PageableResponse
import com.nabi.data.model.emotion.AddDiaryEmotionResponseDTO
import com.nabi.data.model.emotion.DiaryStatisticsResponseDTO
import com.nabi.data.model.emotion.PatchEmotionResDTO
import com.nabi.data.model.emotion.SearchEmotionResponseDTO
import com.nabi.data.service.EmotionService
import javax.inject.Inject
Expand Down Expand Up @@ -101,4 +102,26 @@ class EmotionRemoteDataSourceImpl @Inject constructor(
Result.failure(e)
}
}

override suspend fun patchDiaryEmotion(
accessToken: String,
diaryId: Int,
emotion: String
): Result<BaseResponse<PatchEmotionResDTO>> {
return try {
val response = emotionService.patchDiaryEmotion(accessToken, diaryId, emotion)
if (response.isSuccessful) {
val emotionResponse = response.body()
if (emotionResponse != null) {
Result.success(emotionResponse)
} else {
Result.failure(Exception("Patch Diary Emotion failed: response body is null"))
}
} else {
Result.failure(Exception("Patch Diary Emotion failed: ${response.message()}"))
}
} catch (e: Exception) {
Result.failure(e)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.nabi.data.model.emotion
import com.google.gson.annotations.SerializedName


data class PatchEmotionResDTO(
@SerializedName("diaryId") val diaryId: Int,
@SerializedName("emotion") val emotion: String,
@SerializedName("isEdited") val isEdited: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,28 @@ class EmotionRepositoryImpl @Inject constructor(
Result.failure(result.exceptionOrNull() ?: Exception("Unknown error"))
}
}

override suspend fun patchDiaryEmotion(
accessToken: String,
diaryId: Int,
emotion: String
): Result<String> {
val result = emotionRemoteDataSource.patchDiaryEmotion(accessToken, diaryId, emotion)

return if (result.isSuccess) {
val res = result.getOrNull()
if (res != null) {
val data = res.data
if (data != null) {
Result.success(data.emotion)
} else {
Result.failure(Exception("Patch Diary Emotion Failed: data is null"))
}
} else {
Result.failure(Exception("Patch Diary Emotion Failed: response body is null"))
}
} else {
Result.failure(result.exceptionOrNull() ?: Exception("Unknown error"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import com.nabi.data.model.BaseResponse
import com.nabi.data.model.PageableResponse
import com.nabi.data.model.emotion.AddDiaryEmotionResponseDTO
import com.nabi.data.model.emotion.DiaryStatisticsResponseDTO
import com.nabi.data.model.emotion.PatchEmotionResDTO
import com.nabi.data.model.emotion.SearchEmotionResponseDTO
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query
Expand Down Expand Up @@ -42,4 +44,11 @@ interface EmotionService {
@Path("diaryId") diaryId: Int,
@Path("emotionState") emotionState: String
): Response<BaseResponse<AddDiaryEmotionResponseDTO>>

@PATCH("/emotion/{diaryId}/{emotion}")
suspend fun patchDiaryEmotion(
@Header("Authorization") accessToken: String,
@Path("diaryId") diaryId: Int,
@Path("emotion") emotion: String
): Response<BaseResponse<PatchEmotionResDTO>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ interface EmotionRepository {
diaryId: Int,
emotionState: String
): Result<AddDiaryEmotionMsg>

suspend fun patchDiaryEmotion(
accessToken: String,
diaryId: Int,
emotion: String
): Result<String>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.nabi.domain.usecase.emotion

import com.nabi.domain.model.emotion.AddDiaryEmotionMsg
import com.nabi.domain.repository.EmotionRepository

class PatchDiaryEmotionUseCase(private val repository: EmotionRepository) {
suspend operator fun invoke(
accessToken: String,
diaryId: Int,
emotion: String
): Result<String> {
return repository.patchDiaryEmotion("Bearer $accessToken", diaryId, emotion)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.nabi.domain.repository.EmotionRepository
import com.nabi.domain.usecase.emotion.AddDiaryEmotionUseCase
import com.nabi.domain.usecase.emotion.GetDiaryEmotionUseCase
import com.nabi.domain.usecase.emotion.GetEmotionStatisticsUseCase
import com.nabi.domain.usecase.emotion.PatchDiaryEmotionUseCase
import com.nabi.domain.usecase.emotion.SearchEmotionUseCase
import dagger.Module
import dagger.Provides
Expand Down Expand Up @@ -46,4 +47,12 @@ object EmotionUseCaseModule {
): AddDiaryEmotionUseCase {
return AddDiaryEmotionUseCase(repository = repository)
}

@Provides
@Singleton
fun providePatchDiaryEmotionUseCase(
repository: EmotionRepository
): PatchDiaryEmotionUseCase {
return PatchDiaryEmotionUseCase(repository = repository)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ class DetailDiaryFragment(

deleteDialog.show(parentFragmentManager, "DeleteDiaryDialog")
}

binding.ivEmotion.setOnClickListener{
UpdateEmotionDialog(viewModel.currentEmotion).apply {
setButtonClickListener(object : UpdateEmotionDialog.OnDoneButtonClickListener{
override fun onClick(emotion: String) {
viewModel.patchDiaryEmotion(diaryId, emotion)
}
})
}.show(requireActivity().supportFragmentManager, "")
}
}

override fun setObserver() {
Expand Down Expand Up @@ -148,6 +158,31 @@ class DetailDiaryFragment(
}
}
}

viewModel.patchState.observe(viewLifecycleOwner){
when (it) {
is UiState.Loading -> {}
is UiState.Failure -> {
showToast("감정 μˆ˜μ • μ‹€νŒ¨")
}

is UiState.Success -> {
val resourceId = when (it.data) {
"행볡" -> R.drawable.img_happiness
"우울" -> R.drawable.img_sadness
"화남" -> R.drawable.img_anger
"λΆˆμ•ˆ" -> R.drawable.img_anxiety
"지루함" -> R.drawable.img_boredom
else -> {
binding.btnEmotionReload.visibility = View.VISIBLE
R.drawable.img_no_emotion
}
}

binding.ivEmotion.setImageResource(resourceId)
}
}
}
}

private fun formatDate(inputDate: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.nabi.domain.usecase.bookmark.DeleteBookmarkUseCase
import com.nabi.domain.usecase.datastore.GetAccessTokenUseCase
import com.nabi.domain.usecase.diary.DeleteDiaryUseCase
import com.nabi.domain.usecase.diary.GetDiaryDetailUseCase
import com.nabi.domain.usecase.emotion.PatchDiaryEmotionUseCase
import com.nabi.nabi.utils.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
Expand All @@ -22,6 +23,7 @@ class DetailDiaryViewModel @Inject constructor(
private val addBookmarkUseCase: AddBookmarkUseCase,
private val deleteBookmarkUseCase: DeleteBookmarkUseCase,
private val deleteDiaryUseCase: DeleteDiaryUseCase,
private val patchDiaryEmotionUseCase: PatchDiaryEmotionUseCase,
private val getAccessTokenUseCase: GetAccessTokenUseCase,
) : ViewModel() {

Expand All @@ -40,6 +42,11 @@ class DetailDiaryViewModel @Inject constructor(
private val _deleteDiaryState = MutableLiveData<UiState<DeleteDiaryMsg>>(UiState.Loading)
val deleteDiaryState: LiveData<UiState<DeleteDiaryMsg>> get() = _deleteDiaryState

private val _patchState = MutableLiveData<UiState<String>>(UiState.Loading)
val patchState: LiveData<UiState<String>> get() = _patchState

var currentEmotion: String = ""

fun fetchData(diaryId: Int) {
_diaryState.value = UiState.Loading

Expand All @@ -48,6 +55,7 @@ class DetailDiaryViewModel @Inject constructor(

getDiaryDetailUseCase(accessToken, diaryId)
.onSuccess {
currentEmotion = it.emotion ?: ""
_isBookmarked.value = it.isBookmarked
_diaryState.value = UiState.Success(it)
}.onFailure { e ->
Expand Down Expand Up @@ -102,4 +110,19 @@ class DetailDiaryViewModel @Inject constructor(
}
}
}

fun patchDiaryEmotion(diaryId: Int, emotion: String){
_patchState.value = UiState.Loading

viewModelScope.launch {
val accessToken = getAccessTokenUseCase.invoke().getOrNull().orEmpty()

patchDiaryEmotionUseCase(accessToken, diaryId, emotion)
.onSuccess {
_patchState.value = UiState.Success(it)
}.onFailure { e ->
_patchState.value = UiState.Failure(message = e.message.toString())
}
}
}
}
Loading

0 comments on commit 9b37315

Please sign in to comment.