diff --git a/data/src/main/java/co/orange/data/dataSource/SettingDataSource.kt b/data/src/main/java/co/orange/data/dataSource/SettingDataSource.kt index 19c8edf1..7538b63f 100644 --- a/data/src/main/java/co/orange/data/dataSource/SettingDataSource.kt +++ b/data/src/main/java/co/orange/data/dataSource/SettingDataSource.kt @@ -14,4 +14,8 @@ interface SettingDataSource { addressId: Long, request: AddressRequestDto, ): BaseResponse + + suspend fun getUserAddress(): BaseResponse + + suspend fun deleteUserAddress(addressId: Long): BaseResponse } diff --git a/data/src/main/java/co/orange/data/dataSourceImpl/SettingDataSourceImpl.kt b/data/src/main/java/co/orange/data/dataSourceImpl/SettingDataSourceImpl.kt index a1dd9f04..0361ba05 100644 --- a/data/src/main/java/co/orange/data/dataSourceImpl/SettingDataSourceImpl.kt +++ b/data/src/main/java/co/orange/data/dataSourceImpl/SettingDataSourceImpl.kt @@ -22,4 +22,8 @@ data class SettingDataSourceImpl addressId: Long, request: AddressRequestDto, ): BaseResponse = settingService.putToModAddress(addressId, request) + + override suspend fun getUserAddress(): BaseResponse = settingService.getUserAddress() + + override suspend fun deleteUserAddress(addressId: Long): BaseResponse = settingService.deleteUserAddress(addressId) } diff --git a/data/src/main/java/co/orange/data/dto/response/AddressDto.kt b/data/src/main/java/co/orange/data/dto/response/AddressDto.kt index 72a3d30b..43b6f923 100644 --- a/data/src/main/java/co/orange/data/dto/response/AddressDto.kt +++ b/data/src/main/java/co/orange/data/dto/response/AddressDto.kt @@ -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") diff --git a/data/src/main/java/co/orange/data/repositoryImpl/SettingRepositoryImpl.kt b/data/src/main/java/co/orange/data/repositoryImpl/SettingRepositoryImpl.kt index 79428700..0bd92ad4 100644 --- a/data/src/main/java/co/orange/data/repositoryImpl/SettingRepositoryImpl.kt +++ b/data/src/main/java/co/orange/data/repositoryImpl/SettingRepositoryImpl.kt @@ -30,4 +30,14 @@ class SettingRepositoryImpl runCatching { settingDataSource.putToModAddress(addressId, request.toDto()).data.toModel() } + + override suspend fun getUserAddress(): Result = + runCatching { + settingDataSource.getUserAddress().data.toModel() + } + + override suspend fun deleteUserAddress(addressId: Long): Result = + runCatching { + settingDataSource.deleteUserAddress(addressId).data + } } diff --git a/data/src/main/java/co/orange/data/service/SettingService.kt b/data/src/main/java/co/orange/data/service/SettingService.kt index e2f525f7..937394c3 100644 --- a/data/src/main/java/co/orange/data/service/SettingService.kt +++ b/data/src/main/java/co/orange/data/service/SettingService.kt @@ -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 @@ -24,4 +25,12 @@ interface SettingService { @Path("id") addressId: Long, @Body request: AddressRequestDto, ): BaseResponse + + @GET("/api/v1/mypage/setting/address") + suspend fun getUserAddress(): BaseResponse + + @DELETE("/api/v1/mypage/setting/address/{id}") + suspend fun deleteUserAddress( + @Path("id") addressId: Long, + ): BaseResponse } diff --git a/domain/src/main/kotlin/co/orange/domain/entity/response/AddressModel.kt b/domain/src/main/kotlin/co/orange/domain/entity/response/AddressModel.kt index a097bb84..3acc6586 100644 --- a/domain/src/main/kotlin/co/orange/domain/entity/response/AddressModel.kt +++ b/domain/src/main/kotlin/co/orange/domain/entity/response/AddressModel.kt @@ -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?, ) diff --git a/domain/src/main/kotlin/co/orange/domain/repository/SettingRepository.kt b/domain/src/main/kotlin/co/orange/domain/repository/SettingRepository.kt index 8f070747..42fc90c3 100644 --- a/domain/src/main/kotlin/co/orange/domain/repository/SettingRepository.kt +++ b/domain/src/main/kotlin/co/orange/domain/repository/SettingRepository.kt @@ -13,4 +13,8 @@ interface SettingRepository { addressId: Long, request: AddressRequestModel, ): Result + + suspend fun getUserAddress(): Result + + suspend fun deleteUserAddress(addressId: Long): Result } diff --git a/presentation/src/main/java/co/orange/presentation/setting/delivery/DeliveryActivity.kt b/presentation/src/main/java/co/orange/presentation/setting/delivery/DeliveryActivity.kt index ea82e4d2..3571c81b 100644 --- a/presentation/src/main/java/co/orange/presentation/setting/delivery/DeliveryActivity.kt +++ b/presentation/src/main/java/co/orange/presentation/setting/delivery/DeliveryActivity.kt @@ -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(R.layout.activity_delivery) { val viewModel by viewModels() @@ -20,7 +30,14 @@ class DeliveryActivity : BaseActivity(R.layout.activity initBackBtnListener() initWebBtnListener() initDeleteBtnListener() - setDeliveryUi(viewModel.mockAddressModel) + observeUserAddressState() + observeDeleteAddressResult() + } + + override fun onResume() { + super.onResume() + + viewModel.getUserAddressFromServer() } private fun initBackBtnListener() { @@ -28,10 +45,9 @@ class DeliveryActivity : BaseActivity(R.layout.activity } private fun initWebBtnListener() { - // TODO: addressId 조회 후 변경 with(binding) { btnDeliveryAdd.setOnSingleClickListener { navigateToAddressView(DEFAULT_ID) } - btnDeliveryMod.setOnSingleClickListener { navigateToAddressView(5) } + btnDeliveryMod.setOnSingleClickListener { navigateToAddressView(viewModel.addressId) } } } @@ -42,20 +58,51 @@ class DeliveryActivity : BaseActivity(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) } } diff --git a/presentation/src/main/java/co/orange/presentation/setting/delivery/DeliveryViewModel.kt b/presentation/src/main/java/co/orange/presentation/setting/delivery/DeliveryViewModel.kt index d55dab0f..d4965bdd 100644 --- a/presentation/src/main/java/co/orange/presentation/setting/delivery/DeliveryViewModel.kt +++ b/presentation/src/main/java/co/orange/presentation/setting/delivery/DeliveryViewModel.kt @@ -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.Empty) + val getUserAddressState: StateFlow> = _getUserAddressState + + private val _deleteAddressResult = MutableSharedFlow() + val deleteAddressResult: SharedFlow = _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) + } + } + } } diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index 7cc422e6..adc3c957 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -61,7 +61,7 @@ 상세 내역 보러가기 거래 번호 %s - (%s) %s + (%s) %s %s 구매 상세 주문 완료 @@ -112,6 +112,7 @@ 휴대폰 번호를 입력해주세요 입력 완료 배송지 입력이 완료되었습니다 + 배송지가 삭제되었습니다 수정 삭제