Skip to content

Commit

Permalink
[MERGE] #62 -> develop
Browse files Browse the repository at this point in the history
[FEAT/#62] 설정뷰 / 배송지 등록 추가, 변경 API 구현
  • Loading branch information
Marchbreeze authored Aug 9, 2024
2 parents 4365f7b + b32e8d7 commit 222c08f
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package co.orange.data.dataSource

import co.orange.data.dto.BaseResponse
import co.orange.data.dto.request.AddressRequestDto
import co.orange.data.dto.response.AddressDto
import co.orange.data.dto.response.SettingInfoDto

interface SettingDataSource {
suspend fun getSettingInfo(): BaseResponse<SettingInfoDto>

suspend fun postToAddAddress(request: AddressRequestDto): BaseResponse<AddressDto>

suspend fun putToModAddress(
addressId: Long,
request: AddressRequestDto,
): BaseResponse<AddressDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package co.orange.data.dataSourceImpl

import co.orange.data.dataSource.SettingDataSource
import co.orange.data.dto.BaseResponse
import co.orange.data.dto.request.AddressRequestDto
import co.orange.data.dto.response.AddressDto
import co.orange.data.dto.response.SettingInfoDto
import co.orange.data.service.SettingService
import javax.inject.Inject
Expand All @@ -12,4 +14,12 @@ data class SettingDataSourceImpl
private val settingService: SettingService,
) : SettingDataSource {
override suspend fun getSettingInfo(): BaseResponse<SettingInfoDto> = settingService.getSettingInfo()

override suspend fun postToAddAddress(request: AddressRequestDto): BaseResponse<AddressDto> =
settingService.postToAddAddress(request)

override suspend fun putToModAddress(
addressId: Long,
request: AddressRequestDto,
): BaseResponse<AddressDto> = settingService.putToModAddress(addressId, request)
}
25 changes: 25 additions & 0 deletions data/src/main/java/co/orange/data/dto/request/AddressRequestDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package co.orange.data.dto.request

import co.orange.domain.entity.request.AddressRequestModel
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class AddressRequestDto(
@SerialName("name")
val name: String,
@SerialName("zipCode")
val zipCode: String,
@SerialName("type")
val type: String,
@SerialName("address")
val address: String,
@SerialName("detailAddress")
val detailAddress: String,
@SerialName("phone")
val phone: String,
) {
companion object {
fun AddressRequestModel.toDto() = AddressRequestDto(name, zipCode, type, address, detailAddress, phone)
}
}
25 changes: 25 additions & 0 deletions data/src/main/java/co/orange/data/dto/response/AddressDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package co.orange.data.dto.response

import co.orange.domain.entity.response.AddressModel
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class AddressDto(
@SerialName("addressId")
val addressId: Long,
@SerialName("name")
val name: String?,
@SerialName("zipCode")
val zipCode: String,
@SerialName("type")
val type: String,
@SerialName("address")
val address: String,
@SerialName("detailAddress")
val detailAddress: String,
@SerialName("phone")
val phone: String?,
) {
fun toModel() = AddressModel(addressId, name, zipCode, type, address, detailAddress, phone)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package co.orange.data.repositoryImpl

import co.orange.data.dataSource.SettingDataSource
import co.orange.data.dto.request.AddressRequestDto.Companion.toDto
import co.orange.domain.entity.request.AddressRequestModel
import co.orange.domain.entity.response.AddressModel
import co.orange.domain.entity.response.SettingInfoModel
import co.orange.domain.repository.SettingRepository
import javax.inject.Inject
Expand All @@ -14,4 +17,17 @@ class SettingRepositoryImpl
runCatching {
settingDataSource.getSettingInfo().data.toModel()
}

override suspend fun postToAddAddress(request: AddressRequestModel): Result<AddressModel> =
runCatching {
settingDataSource.postToAddAddress(request.toDto()).data.toModel()
}

override suspend fun putToModAddress(
addressId: Long,
request: AddressRequestModel,
): Result<AddressModel> =
runCatching {
settingDataSource.putToModAddress(addressId, request.toDto()).data.toModel()
}
}
17 changes: 17 additions & 0 deletions data/src/main/java/co/orange/data/service/SettingService.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package co.orange.data.service

import co.orange.data.dto.BaseResponse
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.GET
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Path

interface SettingService {
@GET("/api/v1/mypage/setting")
suspend fun getSettingInfo(): BaseResponse<SettingInfoDto>

@POST("/api/v1/mypage/setting/address")
suspend fun postToAddAddress(
@Body request: AddressRequestDto,
): BaseResponse<AddressDto>

@PUT("/api/v1/mypage/setting/address/{id}")
suspend fun putToModAddress(
@Path("id") addressId: Long,
@Body request: AddressRequestDto,
): BaseResponse<AddressDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package co.orange.domain.entity.request

data class AddressRequestModel(
val name: 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
@@ -0,0 +1,11 @@
package co.orange.domain.entity.response

data class AddressModel(
val addressId: Long,
val name: 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
@@ -1,7 +1,16 @@
package co.orange.domain.repository

import co.orange.domain.entity.request.AddressRequestModel
import co.orange.domain.entity.response.AddressModel
import co.orange.domain.entity.response.SettingInfoModel

interface SettingRepository {
suspend fun getSettingInfo(): Result<SettingInfoModel>

suspend fun postToAddAddress(request: AddressRequestModel): Result<AddressModel>

suspend fun putToModAddress(
addressId: Long,
request: AddressRequestModel,
): Result<AddressModel>
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package co.orange.presentation.address

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import co.orange.core.base.BaseActivity
import co.orange.core.extension.colorOf
import co.orange.core.extension.setOnSingleClickListener
import co.orange.core.extension.stringOf
import co.orange.core.extension.toast
import co.orange.presentation.address.AddressWebBridge.Companion.EXTRA_ADDRESS
import co.orange.presentation.address.AddressWebBridge.Companion.EXTRA_ZIPCODE
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.ActivityAddressBinding

Expand All @@ -27,16 +36,21 @@ class AddressActivity : BaseActivity<ActivityAddressBinding>(R.layout.activity_a
initBackBtnListener()
initConfirmBtnListener()
initAddressFindBtnListener()
observeAddressResult()
}

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

private fun initConfirmBtnListener() {
// TODO 서버통신
val addressId = intent.getLongExtra(EXTRA_ADDRESS_ID, -1)
binding.btnConfirm.setOnSingleClickListener {
finish()
if (addressId == DEFAULT_ID) {
viewModel.postToAddAddressToServer()
} else {
viewModel.putToModAddressToServer(addressId)
}
}
}

Expand Down Expand Up @@ -69,8 +83,34 @@ class AddressActivity : BaseActivity<ActivityAddressBinding>(R.layout.activity_a
}
}

private fun observeAddressResult() {
viewModel.setAddressResult.flowWithLifecycle(lifecycle).distinctUntilChanged()
.onEach { isSuccess ->
if (isSuccess) {
toast(stringOf(R.string.address_toast))
finish()
} else {
toast(stringOf(R.string.error_msg))
}
}.launchIn(lifecycleScope)
}

override fun onDestroy() {
super.onDestroy()
AddressWebActivity.unregister()
}

companion object {
private const val EXTRA_ADDRESS_ID = "EXTRA_ADDRESS_ID"
const val DEFAULT_ID: Long = -1

@JvmStatic
fun createIntent(
context: Context,
addressId: Long,
): Intent =
Intent(context, AddressActivity::class.java).apply {
putExtra(EXTRA_ADDRESS_ID, addressId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ package co.orange.presentation.address

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import co.orange.domain.entity.request.AddressRequestModel
import co.orange.domain.repository.SettingRepository
import co.orange.domain.repository.UserRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AddressViewModel
@Inject
constructor(
private val settingRepository: SettingRepository,
private val userRepository: UserRepository,
) : ViewModel() {
var zipCode = ""
Expand All @@ -20,6 +27,9 @@ class AddressViewModel

val isCompleted = MutableLiveData(false)

private val _setAddressResult = MutableSharedFlow<Boolean>()
val setAddressResult: SharedFlow<Boolean> = _setAddressResult

init {
getUserName()
getUserPhone()
Expand All @@ -43,4 +53,47 @@ class AddressViewModel
phone.value?.length == 11
)
}

fun postToAddAddressToServer() {
viewModelScope.launch {
settingRepository.postToAddAddress(
AddressRequestModel(
name.value.orEmpty(),
zipCode,
TYPE_ROAD,
address,
detailAddress.value.orEmpty(),
phone.value.orEmpty(),
),
).onSuccess {
_setAddressResult.emit(true)
}.onFailure {
_setAddressResult.emit(false)
}
}
}

fun putToModAddressToServer(addressId: Long) {
viewModelScope.launch {
settingRepository.putToModAddress(
addressId,
AddressRequestModel(
name.value.orEmpty(),
zipCode,
TYPE_ROAD,
address,
detailAddress.value.orEmpty(),
phone.value.orEmpty(),
),
).onSuccess {
_setAddressResult.emit(true)
}.onFailure {
_setAddressResult.emit(false)
}
}
}

companion object {
private const val TYPE_ROAD = "ROAD"
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package co.orange.presentation.setting.delivery

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
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.presentation.address.AddressActivity
import co.orange.presentation.address.AddressActivity.Companion.DEFAULT_ID
import kr.genti.presentation.R
import kr.genti.presentation.databinding.ActivityDeliveryBinding

Expand All @@ -28,14 +28,15 @@ class DeliveryActivity : BaseActivity<ActivityDeliveryBinding>(R.layout.activity
}

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

private fun navigateToAddressView() {
Intent(this, AddressActivity::class.java).apply {
private fun navigateToAddressView(addressId: Long) {
AddressActivity.createIntent(this, addressId).apply {
startActivity(this)
}
}
Expand Down
1 change: 1 addition & 0 deletions presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<string name="address_tv_name_hint">이름를 입력해주세요</string>
<string name="address_tv_phone_hint">휴대폰 번호를 입력해주세요</string>
<string name="address_btn_confirm">입력 완료</string>
<string name="address_toast">배송지 입력이 완료되었습니다</string>

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

0 comments on commit 222c08f

Please sign in to comment.