Skip to content

Commit

Permalink
[BUD-50] feat : buddy 추가 삭제 기능 구현(#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
KDW03 committed Apr 9, 2023
1 parent 79862e3 commit cc2e596
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 == ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ interface BuddyService {
@Body buddyRequest: BuddyRequest
): CommonResponse<BuddyId>

@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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class BuddyDetailDataLocal(
birthday,
adoptDay,
BuddyLocalToDto.transformNeutered(neutered),
gender
BuddyLocalToDto.transformGender(gender)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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()
}
}
}
Expand All @@ -133,9 +135,8 @@ class BuddyViewModel @Inject constructor(
}
}

private fun uploadBuddyImg(token: String, buddyId: Long): Deferred<Unit> {
private fun uploadBuddyImg(token: String, buddyId: Long, imgUri: Uri): Deferred<Unit> {
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())
Expand All @@ -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 {
Expand All @@ -158,6 +181,7 @@ class BuddyViewModel @Inject constructor(
}
}


fun readMasterProfile() {
viewModelScope.launch(Dispatchers.IO) {
val token = dataStoreUseCase.bearerJsonWebToken.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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())!!)
Expand All @@ -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()
}
}
}
18 changes: 6 additions & 12 deletions buddy/src/main/res/drawable/img_right_arrow.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="14dp"
android:height="15dp"
android:viewportWidth="14"
android:viewportHeight="15">
<group>
<clip-path
android:pathData="M0,0.5h14v14h-14z"/>
<path
android:pathData="M3.786,12.243L4.818,13.275L10.593,7.5L4.818,1.725L3.786,2.757L8.528,7.5L3.786,12.243Z"
android:fillColor="#000000"/>
</group>
<vector android:height="12.857142dp" android:viewportHeight="15"
android:viewportWidth="14" android:width="12dp" xmlns:android="http://schemas.android.com/apk/res/android">
<group>
<clip-path android:pathData="M0,0.5h14v14h-14z"/>
<path android:fillColor="#000000" android:pathData="M3.786,12.243L4.818,13.275L10.593,7.5L4.818,1.725L3.786,2.757L8.528,7.5L3.786,12.243Z"/>
</group>
</vector>
1 change: 0 additions & 1 deletion buddy/src/main/res/layout/fragment_my_buddy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 2 additions & 4 deletions buddy/src/main/res/layout/fragment_put_buddy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

</com.google.android.material.textfield.TextInputLayout>

Expand All @@ -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" />
</com.google.android.material.textfield.TextInputLayout>


Expand Down

0 comments on commit cc2e596

Please sign in to comment.