From 95b0f32708c83c982dcd6aba506ccf304a464819 Mon Sep 17 00:00:00 2001 From: Hyesung82 Date: Wed, 22 Feb 2023 15:57:25 +0900 Subject: [PATCH] =?UTF-8?q?#25=20feat:=20=EC=9D=BC=EA=B8=B0=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1=20=EC=9A=94=EC=B2=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data/repository/WriteRepositoryImpl.kt | 3 ++- .../data/source/WriteRemoteDataSource.kt | 3 ++- .../domain/model/WriteDiaryRequest.kt | 2 +- .../domain/model/WriteDiaryResponse.kt | 3 +++ .../domain/model/WriteImageResponse.kt | 4 +-- .../domain/repository/WriteRepository.kt | 3 ++- .../usecase/write/RequestWriteDiaryUseCase.kt | 3 ++- .../cheocharm/presentation/model/Result.kt | 2 +- .../ui/write/LocationViewModel.kt | 6 ++--- .../presentation/ui/write/WriteFragment.kt | 25 +++++++++++++++++-- .../java/com/cheocharm/remote/api/WriteApi.kt | 1 + .../remote/model/request/WriteDiaryDto.kt | 6 ++--- .../source/WriteRemoteDataSourceImpl.kt | 3 ++- 13 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 domain/src/main/java/com/cheocharm/domain/model/WriteDiaryResponse.kt diff --git a/data/src/main/java/com/cheocharm/data/repository/WriteRepositoryImpl.kt b/data/src/main/java/com/cheocharm/data/repository/WriteRepositoryImpl.kt index 767e929f..1711a222 100644 --- a/data/src/main/java/com/cheocharm/data/repository/WriteRepositoryImpl.kt +++ b/data/src/main/java/com/cheocharm/data/repository/WriteRepositoryImpl.kt @@ -4,6 +4,7 @@ import com.cheocharm.data.error.ErrorData import com.cheocharm.data.error.toDomain import com.cheocharm.data.source.WriteRemoteDataSource import com.cheocharm.domain.model.WriteDiaryRequest +import com.cheocharm.domain.model.WriteDiaryResponse import com.cheocharm.domain.model.WriteImageRequest import com.cheocharm.domain.model.WriteImageResponse import com.cheocharm.domain.repository.WriteRepository @@ -22,7 +23,7 @@ class WriteRepositoryImpl @Inject constructor( } } - override suspend fun requestWriteDiary(request: WriteDiaryRequest): Result { + override suspend fun requestWriteDiary(request: WriteDiaryRequest): Result { val result = writeRemoteDataSource.requestWriteDiary(request) return when (val exception = result.exceptionOrNull()) { diff --git a/data/src/main/java/com/cheocharm/data/source/WriteRemoteDataSource.kt b/data/src/main/java/com/cheocharm/data/source/WriteRemoteDataSource.kt index e0fd3309..3e0c8c4c 100644 --- a/data/src/main/java/com/cheocharm/data/source/WriteRemoteDataSource.kt +++ b/data/src/main/java/com/cheocharm/data/source/WriteRemoteDataSource.kt @@ -1,11 +1,12 @@ package com.cheocharm.data.source import com.cheocharm.domain.model.WriteDiaryRequest +import com.cheocharm.domain.model.WriteDiaryResponse import com.cheocharm.domain.model.WriteImageRequest import com.cheocharm.domain.model.WriteImageResponse interface WriteRemoteDataSource { suspend fun requestWriteImages(request: WriteImageRequest): Result - suspend fun requestWriteDiary(request: WriteDiaryRequest): Result + suspend fun requestWriteDiary(request: WriteDiaryRequest): Result } diff --git a/domain/src/main/java/com/cheocharm/domain/model/WriteDiaryRequest.kt b/domain/src/main/java/com/cheocharm/domain/model/WriteDiaryRequest.kt index dc153a3b..35b3d34a 100644 --- a/domain/src/main/java/com/cheocharm/domain/model/WriteDiaryRequest.kt +++ b/domain/src/main/java/com/cheocharm/domain/model/WriteDiaryRequest.kt @@ -1,7 +1,7 @@ package com.cheocharm.domain.model data class WriteDiaryRequest( - val id: Int, + val id: Long, val title: String, val content: String ) diff --git a/domain/src/main/java/com/cheocharm/domain/model/WriteDiaryResponse.kt b/domain/src/main/java/com/cheocharm/domain/model/WriteDiaryResponse.kt new file mode 100644 index 00000000..282e1d7c --- /dev/null +++ b/domain/src/main/java/com/cheocharm/domain/model/WriteDiaryResponse.kt @@ -0,0 +1,3 @@ +package com.cheocharm.domain.model + +data class WriteDiaryResponse(val diaryId: Long) diff --git a/domain/src/main/java/com/cheocharm/domain/model/WriteImageResponse.kt b/domain/src/main/java/com/cheocharm/domain/model/WriteImageResponse.kt index 1a64bf9c..80185ece 100644 --- a/domain/src/main/java/com/cheocharm/domain/model/WriteImageResponse.kt +++ b/domain/src/main/java/com/cheocharm/domain/model/WriteImageResponse.kt @@ -1,5 +1,3 @@ package com.cheocharm.domain.model -data class WriteImageResponse(val statusCode: Int, val customCode: String, val data: Data) { - inner class Data(val diary: Long, val imageURLs: List, message: String) -} +data class WriteImageResponse(val diaryId: Long, val imageURLs: List) diff --git a/domain/src/main/java/com/cheocharm/domain/repository/WriteRepository.kt b/domain/src/main/java/com/cheocharm/domain/repository/WriteRepository.kt index ed8268d8..6bfd8320 100644 --- a/domain/src/main/java/com/cheocharm/domain/repository/WriteRepository.kt +++ b/domain/src/main/java/com/cheocharm/domain/repository/WriteRepository.kt @@ -1,11 +1,12 @@ package com.cheocharm.domain.repository import com.cheocharm.domain.model.WriteDiaryRequest +import com.cheocharm.domain.model.WriteDiaryResponse import com.cheocharm.domain.model.WriteImageRequest import com.cheocharm.domain.model.WriteImageResponse interface WriteRepository { suspend fun requestWriteImages(request: WriteImageRequest): Result - suspend fun requestWriteDiary(request: WriteDiaryRequest): Result + suspend fun requestWriteDiary(request: WriteDiaryRequest): Result } diff --git a/domain/src/main/java/com/cheocharm/domain/usecase/write/RequestWriteDiaryUseCase.kt b/domain/src/main/java/com/cheocharm/domain/usecase/write/RequestWriteDiaryUseCase.kt index cb4cd3fd..75b9e1a8 100644 --- a/domain/src/main/java/com/cheocharm/domain/usecase/write/RequestWriteDiaryUseCase.kt +++ b/domain/src/main/java/com/cheocharm/domain/usecase/write/RequestWriteDiaryUseCase.kt @@ -1,12 +1,13 @@ package com.cheocharm.domain.usecase.write import com.cheocharm.domain.model.WriteDiaryRequest +import com.cheocharm.domain.model.WriteDiaryResponse import com.cheocharm.domain.repository.WriteRepository import javax.inject.Inject class RequestWriteDiaryUseCase @Inject constructor( private val repository: WriteRepository ) { - suspend operator fun invoke(request: WriteDiaryRequest): Result = + suspend operator fun invoke(request: WriteDiaryRequest): Result = repository.requestWriteDiary(request) } diff --git a/presentation/src/main/java/com/cheocharm/presentation/model/Result.kt b/presentation/src/main/java/com/cheocharm/presentation/model/Result.kt index e83eb160..cf7fb9c4 100644 --- a/presentation/src/main/java/com/cheocharm/presentation/model/Result.kt +++ b/presentation/src/main/java/com/cheocharm/presentation/model/Result.kt @@ -1,3 +1,3 @@ package com.cheocharm.presentation.model -data class Result(val isSuccessful: Boolean, val message: String? = null) +data class Result(val isSuccessful: Boolean, val message: String? = null, val data: T? = null) diff --git a/presentation/src/main/java/com/cheocharm/presentation/ui/write/LocationViewModel.kt b/presentation/src/main/java/com/cheocharm/presentation/ui/write/LocationViewModel.kt index bec408d8..c4df7df1 100644 --- a/presentation/src/main/java/com/cheocharm/presentation/ui/write/LocationViewModel.kt +++ b/presentation/src/main/java/com/cheocharm/presentation/ui/write/LocationViewModel.kt @@ -16,8 +16,8 @@ import javax.inject.Inject class LocationViewModel @Inject constructor( private val requestWriteImagesUseCase: RequestWriteImagesUseCase ) : ViewModel() { - private val _result = MutableLiveData() - val result: LiveData = _result + private val _result = MutableLiveData>() + val result: LiveData> = _result fun uploadImages( groupId: Long, @@ -36,7 +36,7 @@ class LocationViewModel @Inject constructor( images ) ).onSuccess { - _result.value = Result(true) + _result.value = Result(true, data = it.diaryId) }.onFailure { _result.value = Result(false, it.message) } diff --git a/presentation/src/main/java/com/cheocharm/presentation/ui/write/WriteFragment.kt b/presentation/src/main/java/com/cheocharm/presentation/ui/write/WriteFragment.kt index 7307daf2..5663d04c 100644 --- a/presentation/src/main/java/com/cheocharm/presentation/ui/write/WriteFragment.kt +++ b/presentation/src/main/java/com/cheocharm/presentation/ui/write/WriteFragment.kt @@ -19,12 +19,16 @@ import com.cheocharm.presentation.R import com.cheocharm.presentation.base.BaseFragment import com.cheocharm.presentation.databinding.FragmentWriteBinding import com.cheocharm.presentation.ui.MainActivity +import dagger.hilt.android.AndroidEntryPoint import jp.wasabeef.richeditor.RichEditor +@AndroidEntryPoint class WriteFragment : BaseFragment(R.layout.fragment_write), MenuProvider { private val locationViewModel: LocationViewModel by navGraphViewModels(R.id.write) + private val writeViewModel: WriteViewModel by navGraphViewModels(R.id.write) { defaultViewModelProviderFactory } private lateinit var editor: RichEditor + private var diaryId: Long? = null override fun onCreateView( inflater: LayoutInflater, @@ -43,6 +47,7 @@ class WriteFragment : BaseFragment(R.layout.fragment_write locationViewModel.result.observe(viewLifecycleOwner) { if (it.isSuccessful) { Log.d(javaClass.name, "이미지 업로드 성공") + diaryId = it.data } else { Log.e(javaClass.name, "이미지 업로드 실패: ${it.message}") } @@ -102,6 +107,14 @@ class WriteFragment : BaseFragment(R.layout.fragment_write binding.btnWritePicture.setOnClickListener { // TODO: 사진 목록 } + + writeViewModel.result.observe(viewLifecycleOwner) { + if (it.isSuccessful) { + Toast.makeText(context, "일기 작성 성공", Toast.LENGTH_SHORT).show() + } else { + Toast.makeText(context, "일기 작성 실패", Toast.LENGTH_SHORT).show() + } + } } override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { @@ -111,8 +124,16 @@ class WriteFragment : BaseFragment(R.layout.fragment_write override fun onMenuItemSelected(menuItem: MenuItem): Boolean { return when (menuItem.itemId) { R.id.menu_base_confirm -> { - // TODO: POST 요청 및 일기 상세 화면으로 이동 - println(editor.html) + // TODO: 일기 작성 완료 화면으로 이동 + + diaryId?.let { + writeViewModel.writeDiary( + it, + binding.etWriteTitle.text.toString(), + editor.html ?: "" + ) + } + true } else -> false diff --git a/remote/src/main/java/com/cheocharm/remote/api/WriteApi.kt b/remote/src/main/java/com/cheocharm/remote/api/WriteApi.kt index 26bae20d..33b15138 100644 --- a/remote/src/main/java/com/cheocharm/remote/api/WriteApi.kt +++ b/remote/src/main/java/com/cheocharm/remote/api/WriteApi.kt @@ -1,5 +1,6 @@ package com.cheocharm.remote.api +import com.cheocharm.domain.model.WriteDiaryResponse import com.cheocharm.remote.model.BaseResponse import com.cheocharm.remote.model.response.write.MyGroup import retrofit2.http.GET diff --git a/remote/src/main/java/com/cheocharm/remote/model/request/WriteDiaryDto.kt b/remote/src/main/java/com/cheocharm/remote/model/request/WriteDiaryDto.kt index 225af658..d9e1a0f9 100644 --- a/remote/src/main/java/com/cheocharm/remote/model/request/WriteDiaryDto.kt +++ b/remote/src/main/java/com/cheocharm/remote/model/request/WriteDiaryDto.kt @@ -1,9 +1,7 @@ package com.cheocharm.remote.model.request -import com.google.gson.annotations.SerializedName - data class WriteDiaryDto( - @SerializedName("diary_id") val id: Int, + val diaryId: Long, val title: String, - @SerializedName("html_content") val html: String + val content: String ) diff --git a/remote/src/main/java/com/cheocharm/remote/source/WriteRemoteDataSourceImpl.kt b/remote/src/main/java/com/cheocharm/remote/source/WriteRemoteDataSourceImpl.kt index d1e0d85f..9312f826 100644 --- a/remote/src/main/java/com/cheocharm/remote/source/WriteRemoteDataSourceImpl.kt +++ b/remote/src/main/java/com/cheocharm/remote/source/WriteRemoteDataSourceImpl.kt @@ -3,6 +3,7 @@ package com.cheocharm.remote.source import com.cheocharm.data.error.ErrorData import com.cheocharm.data.source.WriteRemoteDataSource import com.cheocharm.domain.model.WriteDiaryRequest +import com.cheocharm.domain.model.WriteDiaryResponse import com.cheocharm.domain.model.WriteImageRequest import com.cheocharm.remote.api.WriteApi import com.cheocharm.remote.mapper.toDto @@ -40,7 +41,7 @@ class WriteRemoteDataSourceImpl @Inject constructor( } } - override suspend fun requestWriteDiary(request: WriteDiaryRequest): Result { + override suspend fun requestWriteDiary(request: WriteDiaryRequest): Result { val result = runCatching { api.writeDiary(request.toDto()) } return when (val exception = result.exceptionOrNull()) {