Skip to content

Commit

Permalink
[BUD-50] refactor : buddy DTO,ViewModel 구분 적용(#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
KDW03 committed Apr 9, 2023
1 parent 8abce2d commit 79862e3
Show file tree
Hide file tree
Showing 18 changed files with 474 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.nocapstone.buddyvet.buddy.databinding.ItemBuddyDetailBinding
import com.nocapstone.buddyvet.buddy.domain.entity.BuddyData
import com.nocapstone.buddyvet.buddy.domain.entity.BuddyDataLocal
import com.nocapstone.buddyvet.buddy.ui.MyBuddyFragment
import com.nocapstone.buddyvet.buddy.ui.MyBuddyFragmentDirections
import com.nocapstone.common_ui.R.menu.common_menu

class BuddyListAdapter(
val fragment: Fragment
) : ListAdapter<BuddyData, RecyclerView.ViewHolder>(BuddyListDiffCallback()) {
) : ListAdapter<BuddyDataLocal, RecyclerView.ViewHolder>(BuddyListDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return BuddyListViewHolder(
ItemBuddyDetailBinding.inflate(
Expand All @@ -31,14 +33,14 @@ class BuddyListAdapter(

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is BuddyListViewHolder) {
val buddyData = getItem(position)
holder.bind(buddyData, position)
val buddyDataLocal = getItem(position)
holder.bind(buddyDataLocal, position)
}
}

inner class BuddyListViewHolder(private val binding: ItemBuddyDetailBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(data: BuddyData, position: Int) {
fun bind(data: BuddyDataLocal, position: Int) {
binding.apply {
buddyData = data
more.setOnClickListener {
Expand All @@ -58,6 +60,7 @@ class BuddyListAdapter(
(fragment as MyBuddyFragment).buddyViewModel.deleteBuddy(id)
}
com.nocapstone.common_ui.R.id.put -> {
fragment.findNavController().navigate(MyBuddyFragmentDirections.putFragment(id))
}
}
true
Expand All @@ -68,12 +71,12 @@ class BuddyListAdapter(
}
}

private class BuddyListDiffCallback : DiffUtil.ItemCallback<BuddyData>() {
override fun areItemsTheSame(oldItem: BuddyData, newItem: BuddyData): Boolean {
private class BuddyListDiffCallback : DiffUtil.ItemCallback<BuddyDataLocal>() {
override fun areItemsTheSame(oldItem: BuddyDataLocal, newItem: BuddyDataLocal): Boolean {
return oldItem === newItem
}

override fun areContentsTheSame(oldItem: BuddyData, newItem: BuddyData): Boolean {
override fun areContentsTheSame(oldItem: BuddyDataLocal, newItem: BuddyDataLocal): Boolean {
return oldItem == newItem
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.nocapstone.buddyvet.buddy.databinding.ItemBuddyProfileBinding
import com.nocapstone.buddyvet.buddy.domain.entity.BuddyData
import com.nocapstone.buddyvet.buddy.domain.entity.BuddyDataLocal

class BuddyProfileListAdapter(
private val ColorChange: Boolean,
private val onClickBuddyListener: (Int) -> Unit
) :
ListAdapter<BuddyData, RecyclerView.ViewHolder>(BuddyProfileListDiffCallback()) {
ListAdapter<BuddyDataLocal, RecyclerView.ViewHolder>(BuddyProfileListDiffCallback()) {

var prePosition = -1

Expand All @@ -35,10 +35,10 @@ class BuddyProfileListAdapter(

inner class BuddyListViewHolder(private val binding: ItemBuddyProfileBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(data: BuddyData, position: Int) {
fun bind(data: BuddyDataLocal, position: Int) {

binding.apply {
buddyData = data
buddyDataLocal = data
}

itemView.setOnClickListener {
Expand All @@ -56,12 +56,12 @@ class BuddyProfileListAdapter(
}
}

private class BuddyProfileListDiffCallback : DiffUtil.ItemCallback<BuddyData>() {
override fun areItemsTheSame(oldItem: BuddyData, newItem: BuddyData): Boolean {
private class BuddyProfileListDiffCallback : DiffUtil.ItemCallback<BuddyDataLocal>() {
override fun areItemsTheSame(oldItem: BuddyDataLocal, newItem: BuddyDataLocal): Boolean {
return oldItem === newItem
}

override fun areContentsTheSame(oldItem: BuddyData, newItem: BuddyData): Boolean {
override fun areContentsTheSame(oldItem: BuddyDataLocal, newItem: BuddyDataLocal): Boolean {
return oldItem == newItem
}
}
13 changes: 13 additions & 0 deletions buddy/src/main/java/com/nocapstone/buddyvet/buddy/BuddyUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.nocapstone.buddyvet.buddy

object BuddyDtoToLocal {
fun transformKind(kind: String): String = if (kind == "D") "강아지" else "고양이"
fun transformGender(gender: String): String = if (gender == "M") "남자" else "여자"
fun transformNeutered(neutered : Boolean) : String = if (neutered) "" else "아니요"
}

object BuddyLocalToDto {
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 @@ -8,9 +8,10 @@ import androidx.recyclerview.widget.RecyclerView
import com.nocapstone.buddyvet.buddy.databinding.ItemBuddyProfileBinding
import com.nocapstone.buddyvet.buddy.databinding.ItemBuddyProfileHomeBinding
import com.nocapstone.buddyvet.buddy.domain.entity.BuddyData
import com.nocapstone.buddyvet.buddy.domain.entity.BuddyDataLocal

class HomeBuddyProfileListAdapter() :
ListAdapter<BuddyData, RecyclerView.ViewHolder>(HomeBuddyProfileListDiffCallback()) {
ListAdapter<BuddyDataLocal, RecyclerView.ViewHolder>(HomeBuddyProfileListDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return BuddyListViewHolder(
ItemBuddyProfileHomeBinding.inflate(
Expand All @@ -23,28 +24,27 @@ class HomeBuddyProfileListAdapter() :

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder is BuddyListViewHolder) {
val buddyData = getItem(position)
holder.bind(buddyData, position)
val buddyDataLocal = getItem(position)
holder.bind(buddyDataLocal, position)
}
}

inner class BuddyListViewHolder(private val binding: ItemBuddyProfileHomeBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(data: BuddyData, position: Int) {

fun bind(data: BuddyDataLocal, position: Int) {
binding.apply {
buddyData = data
}
}
}
}

private class HomeBuddyProfileListDiffCallback : DiffUtil.ItemCallback<BuddyData>() {
override fun areItemsTheSame(oldItem: BuddyData, newItem: BuddyData): Boolean {
private class HomeBuddyProfileListDiffCallback : DiffUtil.ItemCallback<BuddyDataLocal>() {
override fun areItemsTheSame(oldItem: BuddyDataLocal, newItem: BuddyDataLocal): Boolean {
return oldItem === newItem
}

override fun areContentsTheSame(oldItem: BuddyData, newItem: BuddyData): Boolean {
override fun areContentsTheSame(oldItem: BuddyDataLocal, newItem: BuddyDataLocal): Boolean {
return oldItem == newItem
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.nocapstone.buddyvet.buddy.domain.entity

import com.nocapstone.buddyvet.buddy.BuddyDtoToLocal

data class BuddyListResponse(
val buddies: List<BuddyData>?
)
Expand All @@ -11,8 +13,16 @@ data class BuddyData(
val gender: String,
val profile: String,
val age: String,
var isSelect : Boolean = false
)
) {
fun replaceForLocal() = BuddyDataLocal(
id,
BuddyDtoToLocal.transformKind(kind),
name,
BuddyDtoToLocal.transformGender(gender),
profile,
age
)
}

data class BuddyId(
val buddyId: Long
Expand All @@ -30,4 +40,14 @@ data class BuddyDetailData(
val adoptDay: String,
val isNeutered: Boolean,
val gender: String
)
) {
fun replaceForLocal() = BuddyDetailDataLocal(
BuddyDtoToLocal.transformKind(kind),
name,
BuddyDtoToLocal.transformGender(gender),
profile,
BuddyDtoToLocal.transformNeutered(isNeutered),
adoptDay,
birthday
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.nocapstone.buddyvet.buddy.domain.entity

import com.nocapstone.buddyvet.buddy.BuddyLocalToDto

// BuddyDetailDataLocal를 BuddyRequest로 변환
// BuddyResponse를 BuddyDataLocal로 변환

data class BuddyDetailDataLocal(
val kind: String,
var name: String? = null,
var gender: String? = null,
var profile: String? = null,
var neutered: String? = null,
var adoptDay: String? = null,
var birthday: String? = null
) {
fun replaceForDto() = BuddyRequest(
BuddyLocalToDto.transformKind(kind),
name,
birthday,
adoptDay,
BuddyLocalToDto.transformNeutered(neutered),
gender
)
}


data class BuddyDataLocal(
val id: Long,
val kind: String,
val name: String,
val gender: String,
val profile: String,
val age: String,
var isSelect: Boolean = false
)



Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.nocapstone.buddyvet.buddy.domain.entity

data class BuddyRequest(
val kind : String,
var name : String = "DOWOO",
var birthday : String = "2023-04-08",
var adoptDay : String = "2023-04-08",
var isNeutered : Boolean = false,
var gender : String = "M"
var name : String?,
var birthday : String?,
var adoptDay : String?,
var isNeutered : Boolean?,
var gender : String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.nocapstone.buddyvet.buddy.domain.usecase

import com.nocapstone.buddyvet.buddy.data.BuddyService
import com.nocapstone.buddyvet.buddy.domain.entity.BuddyData
import com.nocapstone.buddyvet.buddy.domain.entity.BuddyDetailResponse
import com.nocapstone.buddyvet.buddy.domain.entity.BuddyRequest
import com.nocapstone.buddyvet.buddy.domain.entity.MasterInfoResponse
import com.nocapstone.common.data.dto.CommonResponse
Expand All @@ -20,8 +21,8 @@ class BuddyUseCase @Inject constructor(
return buddyService.createBuddy(token, buddyRequest).data.buddyId
}

suspend fun readBuddyDetail(token: String, buddyId: Long) {
buddyService.readBuddyDetail(token, buddyId)
suspend fun readBuddyDetail(token: String, buddyId: Long): BuddyDetailResponse {
return buddyService.readBuddyDetail(token, buddyId).data
}

suspend fun deleteBuddy(token: String, buddyId: Long) {
Expand Down
Loading

0 comments on commit 79862e3

Please sign in to comment.