From cc2e596e1599234532e53826b7c47b4e574a0de6 Mon Sep 17 00:00:00 2001 From: kimdowoo Date: Sun, 9 Apr 2023 22:41:43 +0900 Subject: [PATCH] =?UTF-8?q?[BUD-50]=20feat=20:=20buddy=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84(#34)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nocapstone/buddyvet/buddy/BuddyUtil.kt | 4 +- .../buddyvet/buddy/data/BuddyService.kt | 7 ++++ .../buddy/domain/entity/BuddyDataLocal.kt | 2 +- .../buddy/domain/usecase/BuddyUseCase.kt | 3 ++ .../buddyvet/buddy/ui/BuddyViewModel.kt | 34 ++++++++++++++--- .../buddyvet/buddy/ui/PutBuddyFragment.kt | 37 ++++++++++++------- .../src/main/res/drawable/img_right_arrow.xml | 18 +++------ .../src/main/res/layout/fragment_my_buddy.xml | 1 - .../main/res/layout/fragment_put_buddy.xml | 6 +-- 9 files changed, 73 insertions(+), 39 deletions(-) diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/BuddyUtil.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/BuddyUtil.kt index 67797af..abfaf01 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/BuddyUtil.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/BuddyUtil.kt @@ -7,7 +7,7 @@ object BuddyDtoToLocal { } object BuddyLocalToDto { - fun transformKind(kind: String): String = if (kind == "강아지") "D" else "C" - fun transformGender(gender: String): String = if (gender == "남자") "M" else "F" + fun transformKind(kind: String?): String = if (kind == "강아지") "D" else "C" + fun transformGender(gender: String?): String = if (gender == "남자") "M" else "F" fun transformNeutered(neutered : String?) : Boolean = neutered == "예" } diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/data/BuddyService.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/data/BuddyService.kt index d42d716..b5555bd 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/data/BuddyService.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/data/BuddyService.kt @@ -18,6 +18,13 @@ interface BuddyService { @Body buddyRequest: BuddyRequest ): CommonResponse + @PATCH("buddies/{buddyId}") + suspend fun putBuddy( + @Header("Authorization") token: String, + @Body buddyRequest: BuddyRequest, + @Path("buddyId") buddyId: Long + ) + @GET("buddies/{buddyId}") suspend fun readBuddyDetail( @Header("Authorization") token: String, diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/entity/BuddyDataLocal.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/entity/BuddyDataLocal.kt index cb2a209..ac154a9 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/entity/BuddyDataLocal.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/entity/BuddyDataLocal.kt @@ -20,7 +20,7 @@ data class BuddyDetailDataLocal( birthday, adoptDay, BuddyLocalToDto.transformNeutered(neutered), - gender + BuddyLocalToDto.transformGender(gender) ) } diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/usecase/BuddyUseCase.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/usecase/BuddyUseCase.kt index 8da8f6c..a27a10e 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/usecase/BuddyUseCase.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/domain/usecase/BuddyUseCase.kt @@ -29,6 +29,9 @@ class BuddyUseCase @Inject constructor( buddyService.deleteBuddy(token, buddyId) } + suspend fun putBuddy(token: String,buddyId: Long,buddyRequest: BuddyRequest){ + buddyService.putBuddy(token, buddyRequest, buddyId) + } suspend fun uploadBuddyImg(token: String, buddyId: Long, image: MultipartBody.Part) { buddyService.uploadBuddyImg(token, buddyId, image) } diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/BuddyViewModel.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/BuddyViewModel.kt index 5b2eae2..c549b69 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/BuddyViewModel.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/BuddyViewModel.kt @@ -3,7 +3,7 @@ package com.nocapstone.buddyvet.buddy.ui import android.content.Context import android.net.Uri import android.provider.OpenableColumns -import android.util.Log +import androidx.core.net.toUri import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.nocapstone.buddyvet.buddy.domain.entity.* @@ -103,10 +103,12 @@ class BuddyViewModel @Inject constructor( val jwt = dataStoreUseCase.bearerJsonWebToken.first()!! val buddyId = buddyUseCase.createBuddy(jwt, _newBuddy.value!!.replaceForDto()) if (_selectImgUri.value != null) { - uploadBuddyImg(jwt, buddyId).await() + val imgUri = _selectImgUri.value!! + uploadBuddyImg(jwt, buddyId, imgUri).await() } - callback.invoke() } catch (e: Exception) { + } finally { + callback.invoke() } } } @@ -133,9 +135,8 @@ class BuddyViewModel @Inject constructor( } } - private fun uploadBuddyImg(token: String, buddyId: Long): Deferred { + private fun uploadBuddyImg(token: String, buddyId: Long, imgUri: Uri): Deferred { return viewModelScope.async(Dispatchers.IO) { - val imgUri = _selectImgUri.value!! val inputStream = context.contentResolver.openInputStream(imgUri) val byteArray = inputStream!!.readBytes() val requestBody = byteArray.toRequestBody("multipart/form-data".toMediaTypeOrNull()) @@ -149,6 +150,28 @@ class BuddyViewModel @Inject constructor( } + fun putBuddy(buddyId: Long, buddyRequest: BuddyRequest, callback: () -> Boolean) { + viewModelScope.launch { + val token = dataStoreUseCase.bearerJsonWebToken.first() + if (token != null) { + try { + buddyUseCase.putBuddy(token, buddyId, buddyRequest) + if (detailBuddy.value?.profile != null) { + uploadBuddyImg( + token, + buddyId, + detailBuddy.value!!.profile!!.toUri() + ).await() + } + } catch (e: Exception) { + + } finally { + callback.invoke() + } + } + } + } + private fun getFileName(context: Context, uri: Uri): String? { val cursor = context.contentResolver.query(uri, null, null, null, null) return cursor?.use { @@ -158,6 +181,7 @@ class BuddyViewModel @Inject constructor( } } + fun readMasterProfile() { viewModelScope.launch(Dispatchers.IO) { val token = dataStoreUseCase.bearerJsonWebToken.first() diff --git a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/PutBuddyFragment.kt b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/PutBuddyFragment.kt index 490945b..1ed376e 100644 --- a/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/PutBuddyFragment.kt +++ b/buddy/src/main/java/com/nocapstone/buddyvet/buddy/ui/PutBuddyFragment.kt @@ -6,12 +6,12 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ArrayAdapter -import android.widget.TextView import androidx.fragment.app.viewModels import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.navArgs import com.nocapstone.buddyvet.buddy.databinding.FragmentPutBuddyBinding -import com.nocapstone.buddyvet.buddy.domain.entity.BuddyDetailData +import com.nocapstone.buddyvet.buddy.domain.entity.BuddyDataLocal +import com.nocapstone.buddyvet.buddy.domain.entity.BuddyDetailDataLocal import com.nocapstone.buddyvet.buddy.domain.entity.BuddyRequest import com.nocapstone.common_ui.CalendarUtil import com.nocapstone.common_ui.DialogForDatePicker @@ -49,6 +49,7 @@ class PutBuddyFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + val neuteredAdapter = ArrayAdapter( requireContext(), R.layout.list_item, @@ -60,11 +61,16 @@ class PutBuddyFragment : Fragment() { genderListItem ) + buddyViewModel.readBuddyDetail(buddyId) + + binding.apply { lifecycleOwner = viewLifecycleOwner viewModel = buddyViewModel + genderTv.setAdapter(genderAdapter) isNeuteredTv.setAdapter(neuteredAdapter) + birthDayTv.setOnClickListener { DialogForDatePicker.Builder(requireContext()) .setInitDate(CalendarUtil.parseStringToDate(binding.birthDayTv.text.toString())!!) @@ -84,27 +90,30 @@ class PutBuddyFragment : Fragment() { imgSelect.setOnClickListener { TedImagePicker.with(requireContext()) .start { - buddyViewModel.setSelectImgUri(it) + buddyViewModel.detailBuddy.value?.profile = it.toString() } } binding.next.setOnClickListener { putData() - findNavController().navigate(com.nocapstone.buddyvet.buddy.R.id.next) } } - buddyViewModel.readBuddyDetail(buddyId) + + } private fun putData() { - with(binding) { - BuddyRequest( - buddyViewModel.getKind(), - nameTv.text.toString(), - birthDayTv.text.toString(), - adoptDayTv.text.toString(), - isNeuteredTv.text.toString().toBoolean(), - genderTv.text.toString() - ) + val request = BuddyDetailDataLocal( + buddyViewModel.getKind(), + binding.nameTv.text.toString(), + binding.genderTv.text.toString(), + null, + binding.isNeuteredTv.text.toString(), + binding.birthDayTv.text.toString(), + binding.adoptDayTv.text.toString(), + ).replaceForDto() + + buddyViewModel.putBuddy(buddyId, request) { + findNavController().popBackStack() } } } \ No newline at end of file diff --git a/buddy/src/main/res/drawable/img_right_arrow.xml b/buddy/src/main/res/drawable/img_right_arrow.xml index 29a2b4d..abe43bc 100644 --- a/buddy/src/main/res/drawable/img_right_arrow.xml +++ b/buddy/src/main/res/drawable/img_right_arrow.xml @@ -1,13 +1,7 @@ - - - - - + + + + + diff --git a/buddy/src/main/res/layout/fragment_my_buddy.xml b/buddy/src/main/res/layout/fragment_my_buddy.xml index db6d954..d4ab19b 100644 --- a/buddy/src/main/res/layout/fragment_my_buddy.xml +++ b/buddy/src/main/res/layout/fragment_my_buddy.xml @@ -52,7 +52,6 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" - android:drawablePadding="2dp" android:fontFamily="@font/cafe24_ssurround_air" android:text="@{viewModel.masterInfo.nickname}" android:textColor="@color/black" diff --git a/buddy/src/main/res/layout/fragment_put_buddy.xml b/buddy/src/main/res/layout/fragment_put_buddy.xml index 49e09cd..58a1a29 100644 --- a/buddy/src/main/res/layout/fragment_put_buddy.xml +++ b/buddy/src/main/res/layout/fragment_put_buddy.xml @@ -82,8 +82,7 @@ android:id="@+id/gender_tv" android:layout_width="match_parent" android:layout_height="wrap_content" - android:inputType="none" - android:text="@{viewModel.detailBuddy.gender}" /> + android:inputType="none" /> @@ -101,8 +100,7 @@ android:id="@+id/isNeutered_tv" android:layout_width="match_parent" android:layout_height="wrap_content" - android:inputType="none" - android:text="@{viewModel.detailBuddy.neutered}" /> + android:inputType="none" />