Skip to content

Commit

Permalink
[MERGE] #64 -> develop
Browse files Browse the repository at this point in the history
[FEAT/#64] 설정뷰 / 배송지 조회, 삭제 API 구현
  • Loading branch information
Marchbreeze authored Aug 9, 2024
2 parents 222c08f + d0fc0c0 commit 331bce4
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ interface SettingDataSource {
addressId: Long,
request: AddressRequestDto,
): BaseResponse<AddressDto>

suspend fun getUserAddress(): BaseResponse<AddressDto>

suspend fun deleteUserAddress(addressId: Long): BaseResponse<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ data class SettingDataSourceImpl
addressId: Long,
request: AddressRequestDto,
): BaseResponse<AddressDto> = settingService.putToModAddress(addressId, request)

override suspend fun getUserAddress(): BaseResponse<AddressDto> = settingService.getUserAddress()

override suspend fun deleteUserAddress(addressId: Long): BaseResponse<Boolean> = settingService.deleteUserAddress(addressId)
}
6 changes: 3 additions & 3 deletions data/src/main/java/co/orange/data/dto/response/AddressDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import kotlinx.serialization.Serializable
@Serializable
data class AddressDto(
@SerialName("addressId")
val addressId: Long,
val addressId: Long?,
@SerialName("name")
val name: String?,
@SerialName("zipCode")
val zipCode: String,
val zipCode: String?,
@SerialName("type")
val type: String,
val type: String?,
@SerialName("address")
val address: String,
@SerialName("detailAddress")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ class SettingRepositoryImpl
runCatching {
settingDataSource.putToModAddress(addressId, request.toDto()).data.toModel()
}

override suspend fun getUserAddress(): Result<AddressModel> =
runCatching {
settingDataSource.getUserAddress().data.toModel()
}

override suspend fun deleteUserAddress(addressId: Long): Result<Boolean> =
runCatching {
settingDataSource.deleteUserAddress(addressId).data
}
}
9 changes: 9 additions & 0 deletions data/src/main/java/co/orange/data/service/SettingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import co.orange.data.dto.request.AddressRequestDto
import co.orange.data.dto.response.AddressDto
import co.orange.data.dto.response.SettingInfoDto
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.PUT
Expand All @@ -24,4 +25,12 @@ interface SettingService {
@Path("id") addressId: Long,
@Body request: AddressRequestDto,
): BaseResponse<AddressDto>

@GET("/api/v1/mypage/setting/address")
suspend fun getUserAddress(): BaseResponse<AddressDto>

@DELETE("/api/v1/mypage/setting/address/{id}")
suspend fun deleteUserAddress(
@Path("id") addressId: Long,
): BaseResponse<Boolean>
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package co.orange.domain.entity.response

data class AddressModel(
val addressId: Long,
val addressId: Long?,
val name: String?,
val zipCode: String,
val type: String,
val address: String,
val detailAddress: String,
val zipCode: String?,
val type: String?,
val address: String?,
val detailAddress: String?,
val phone: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ interface SettingRepository {
addressId: Long,
request: AddressRequestModel,
): Result<AddressModel>

suspend fun getUserAddress(): Result<AddressModel>

suspend fun deleteUserAddress(addressId: Long): Result<Boolean>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ package co.orange.presentation.setting.delivery

import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import co.orange.core.base.BaseActivity
import co.orange.core.extension.breakLines
import co.orange.core.extension.setOnSingleClickListener
import co.orange.domain.entity.response.AddressInfoModel
import co.orange.core.extension.stringOf
import co.orange.core.extension.toast
import co.orange.core.state.UiState
import co.orange.presentation.address.AddressActivity
import co.orange.presentation.address.AddressActivity.Companion.DEFAULT_ID
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kr.genti.presentation.R
import kr.genti.presentation.databinding.ActivityDeliveryBinding

@AndroidEntryPoint
class DeliveryActivity : BaseActivity<ActivityDeliveryBinding>(R.layout.activity_delivery) {
val viewModel by viewModels<DeliveryViewModel>()

Expand All @@ -20,18 +30,24 @@ class DeliveryActivity : BaseActivity<ActivityDeliveryBinding>(R.layout.activity
initBackBtnListener()
initWebBtnListener()
initDeleteBtnListener()
setDeliveryUi(viewModel.mockAddressModel)
observeUserAddressState()
observeDeleteAddressResult()
}

override fun onResume() {
super.onResume()

viewModel.getUserAddressFromServer()
}

private fun initBackBtnListener() {
binding.btnBack.setOnSingleClickListener { finish() }
}

private fun initWebBtnListener() {
// TODO: addressId 조회 후 변경
with(binding) {
btnDeliveryAdd.setOnSingleClickListener { navigateToAddressView(DEFAULT_ID) }
btnDeliveryMod.setOnSingleClickListener { navigateToAddressView(5) }
btnDeliveryMod.setOnSingleClickListener { navigateToAddressView(viewModel.addressId) }
}
}

Expand All @@ -42,20 +58,51 @@ class DeliveryActivity : BaseActivity<ActivityDeliveryBinding>(R.layout.activity
}

private fun initDeleteBtnListener() {
// TODO
binding.btnDeliveryDelete.setOnSingleClickListener { }
binding.btnDeliveryDelete.setOnSingleClickListener {
viewModel.deleteUserAddressFromServer()
}
}

private fun setDeliveryUi(item: AddressInfoModel) {
with(binding) {
tvDeliveryName.text = item.recipient
tvDeliveryAddress.text =
getString(
R.string.address_format,
item.zipCode,
item.address,
).breakLines()
tvDeliveryPhone.text = item.phone
}
private fun observeUserAddressState() {
viewModel.getUserAddressState.flowWithLifecycle(lifecycle).distinctUntilChanged()
.onEach { state ->
when (state) {
is UiState.Success -> {
with(binding) {
if (state.data.addressId != null) {
btnDeliveryAdd.isVisible = false
layoutDeliveryItem.isVisible = true
tvDeliveryName.text = state.data.name
tvDeliveryAddress.text =
getString(
R.string.address_format,
state.data.zipCode,
state.data.address,
state.data.detailAddress,
).breakLines()
tvDeliveryPhone.text = state.data.phone
}
}
}

is UiState.Failure -> toast(stringOf(R.string.error_msg))
else -> return@onEach
}
}.launchIn(lifecycleScope)
}

private fun observeDeleteAddressResult() {
viewModel.deleteAddressResult.flowWithLifecycle(lifecycle).distinctUntilChanged()
.onEach { isSuccess ->
if (isSuccess) {
toast(stringOf(R.string.address_delete_toast))
with(binding) {
btnDeliveryAdd.isVisible = true
layoutDeliveryItem.isVisible = false
}
} else {
toast(stringOf(R.string.error_msg))
}
}.launchIn(lifecycleScope)
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,55 @@
package co.orange.presentation.setting.delivery

import androidx.lifecycle.ViewModel
import co.orange.domain.entity.response.AddressInfoModel
import androidx.lifecycle.viewModelScope
import co.orange.core.state.UiState
import co.orange.domain.entity.response.AddressModel
import co.orange.domain.repository.SettingRepository
import co.orange.presentation.address.AddressActivity.Companion.DEFAULT_ID
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DeliveryViewModel
@Inject
constructor(
// private val feedRepository: FeedRepository,
private val settingRepository: SettingRepository,
) : ViewModel() {
val mockAddressModel =
AddressInfoModel(
"김상호",
"04567",
"서울특벌시 성동구 성수이로 137 107동 903호",
"010-3259-0327",
)
var addressId: Long = DEFAULT_ID

private val _getUserAddressState = MutableStateFlow<UiState<AddressModel>>(UiState.Empty)
val getUserAddressState: StateFlow<UiState<AddressModel>> = _getUserAddressState

private val _deleteAddressResult = MutableSharedFlow<Boolean>()
val deleteAddressResult: SharedFlow<Boolean> = _deleteAddressResult

fun getUserAddressFromServer() {
viewModelScope.launch {
settingRepository.getUserAddress()
.onSuccess {
it.addressId?.let { addressId = it }
_getUserAddressState.value = UiState.Success(it)
}
.onFailure {
_getUserAddressState.value = UiState.Failure(it.message.orEmpty())
}
}
}

fun deleteUserAddressFromServer() {
viewModelScope.launch {
settingRepository.deleteUserAddress(addressId)
.onSuccess {
_deleteAddressResult.emit(true)
}
.onFailure {
_deleteAddressResult.emit(false)
}
}
}
}
3 changes: 2 additions & 1 deletion presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<string name="buy_finished_btn_show_detail">상세 내역 보러가기</string>

<string name="transaction_id">거래 번호 %s</string>
<string name="address_format">(%s) %s</string>
<string name="address_format">(%s) %s %s</string>

<string name="buy_info_tv_title">구매 상세</string>
<string name="buy_info_tv_message">주문 완료</string>
Expand Down Expand Up @@ -112,6 +112,7 @@
<string name="address_tv_phone_hint">휴대폰 번호를 입력해주세요</string>
<string name="address_btn_confirm">입력 완료</string>
<string name="address_toast">배송지 입력이 완료되었습니다</string>
<string name="address_delete_toast">배송지가 삭제되었습니다</string>

<string name="btn_mod">수정</string>
<string name="btn_delete">삭제</string>
Expand Down

0 comments on commit 331bce4

Please sign in to comment.