Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT/#109] 설정뷰 / 계좌 조회 API 구현 #110

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@
android:exported="false"
android:screenOrientation="portrait" />

<activity
android:name="co.orange.presentation.bank.add.BankAddActivity"
android:exported="false"
android:screenOrientation="portrait" />

<activity
android:name="co.orange.presentation.main.profile.ReportActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ interface SettingDataSource {
accountId: Long,
request: BankRequestDto,
): BaseResponse<BankDto>

suspend fun getUserBank(): BaseResponse<BankDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ data class SettingDataSourceImpl
accountId: Long,
request: BankRequestDto,
): BaseResponse<BankDto> = settingService.putToModBank(accountId, request)

override suspend fun getUserBank(): BaseResponse<BankDto> = settingService.getUserBank()
}
8 changes: 4 additions & 4 deletions data/src/main/java/co/orange/data/dto/response/BankDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import kotlinx.serialization.Serializable
@Serializable
data class BankDto(
@SerialName("accountId")
val accountId: Long,
val accountId: Long? = null,
@SerialName("name")
val name: String,
val name: String? = null,
@SerialName("bank")
val bank: String,
val bank: String? = null,
@SerialName("accountNumber")
val accountNumber: String,
val accountNumber: String? = null,
) {
fun toModel() = BankModel(accountId, name, bank, accountNumber)
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ class SettingRepositoryImpl
runCatching {
settingDataSource.putToModBank(accountId, request.toDto()).data.toModel()
}

override suspend fun getUserBank(): Result<BankModel> =
runCatching {
settingDataSource.getUserBank().data.toModel()
}
}
3 changes: 3 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 @@ -53,4 +53,7 @@ interface SettingService {
@Path("id") accountId: Long,
@Body request: BankRequestDto,
): BaseResponse<BankDto>

@GET("/api/v1/mypage/setting/account")
suspend fun getUserBank(): BaseResponse<BankDto>
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package co.orange.domain.entity.response

data class BankModel(
val accountId: Long,
val name: String,
val bank: String,
val accountNumber: String,
val accountId: Long? = null,
val name: String? = null,
val bank: String? = null,
val accountNumber: String? = null,
)
7 changes: 7 additions & 0 deletions domain/src/main/kotlin/co/orange/domain/enums/BankType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ enum class BankType(val displayName: String, val code: String) {
SAVING("저축은행", "SAVING"),
HSBC("HSBC 은행", "HSBC"),
DEUTSCHE("도이치 은행", "DEUTSCHE"),
;

companion object {
fun fromCode(code: String): String? {
return values().find { it.code == code }?.displayName
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ interface SettingRepository {
accountId: Long,
request: BankRequestModel,
): Result<BankModel>

suspend fun getUserBank(): Result<BankModel>
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ package co.orange.presentation.bank

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.setOnSingleClickListener
import co.orange.core.extension.stringOf
import co.orange.core.extension.toast
import co.orange.core.state.UiState
import co.orange.domain.entity.response.BankModel
import co.orange.domain.enums.BankType
import co.orange.presentation.bank.add.BankAddActivity
import co.orange.presentation.bank.add.BankAddActivity.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.ActivityBankBinding

Expand All @@ -20,7 +30,13 @@ class BankActivity : BaseActivity<ActivityBankBinding>(R.layout.activity_bank) {

initBackBtnListener()
initBankInfoBtnListener()
setDeliveryUi(viewModel.mockBankModel)
observeUserBankState()
}

override fun onResume() {
super.onResume()

viewModel.getUserBankFromServer()
}

private fun initBackBtnListener() {
Expand All @@ -40,11 +56,26 @@ class BankActivity : BaseActivity<ActivityBankBinding>(R.layout.activity_bank) {
}
}

private fun observeUserBankState() {
viewModel.getUserBankState.flowWithLifecycle(lifecycle).distinctUntilChanged()
.onEach { state ->
when (state) {
is UiState.Success -> setDeliveryUi(state.data)
is UiState.Failure -> toast(stringOf(R.string.error_msg))
else -> return@onEach
}
}.launchIn(lifecycleScope)
}

private fun setDeliveryUi(item: BankModel) {
with(binding) {
tvBankName.text = item.bank
tvBankAccount.text = item.accountNumber
tvBankOwner.text = item.name
btnBankAdd.isVisible = item.accountId == null
btnBankMod.isVisible = item.accountId != null
if (item.accountId != null) {
tvBankName.text = item.bank?.let { BankType.fromCode(it) }
tvBankAccount.text = item.accountNumber
tvBankOwner.text = item.name
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
package co.orange.presentation.bank

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import co.orange.core.state.UiState
import co.orange.domain.entity.response.BankModel
import co.orange.domain.repository.SettingRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class BankViewModel
@Inject
constructor(
// private val feedRepository: FeedRepository,
private val settingRepository: SettingRepository,
) : ViewModel() {
var accountId: Long = -1
val mockBankModel =
BankModel(
0,
"김상호",
"우리은행",
"82-12341234-1234",
)

private val _getUserBankState = MutableStateFlow<UiState<BankModel>>(UiState.Empty)
val getUserBankState: StateFlow<UiState<BankModel>> = _getUserBankState

fun getUserBankFromServer() {
viewModelScope.launch {
settingRepository.getUserBank()
.onSuccess {
it.accountId?.let { accountId = it }
_getUserBankState.value = UiState.Success(it)
}
.onFailure {
_getUserBankState.value = UiState.Failure(it.message.orEmpty())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import co.orange.core.base.BaseActivity
import co.orange.core.extension.setOnSingleClickListener
import co.orange.core.extension.stringOf
import co.orange.core.extension.toast
import co.orange.presentation.bank.type.BankTypeBottomSheet
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn
Expand All @@ -21,11 +22,14 @@ import kr.genti.presentation.databinding.ActivityBankAddBinding
class BankAddActivity : BaseActivity<ActivityBankAddBinding>(R.layout.activity_bank_add) {
val viewModel by viewModels<BankAddViewModel>()

private var bankTypeBottomSheet: BankTypeBottomSheet? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding.vm = viewModel
initBackBtnListener()
initBankTypeListener()
initConfirmBtnListener()
observeAddressResult()
}
Expand All @@ -34,6 +38,13 @@ class BankAddActivity : BaseActivity<ActivityBankAddBinding>(R.layout.activity_b
binding.btnBack.setOnSingleClickListener { finish() }
}

private fun initBankTypeListener() {
binding.btnAddBankName.setOnSingleClickListener {
bankTypeBottomSheet = BankTypeBottomSheet()
bankTypeBottomSheet?.show(supportFragmentManager, BOTTOM_SHEET_BANK_TYPE)
}
}

private fun initConfirmBtnListener() {
val accountId = intent.getLongExtra(EXTRA_ACCOUNT_ID, -1)
binding.btnConfirm.setOnSingleClickListener {
Expand All @@ -57,9 +68,16 @@ class BankAddActivity : BaseActivity<ActivityBankAddBinding>(R.layout.activity_b
}.launchIn(lifecycleScope)
}

override fun onDestroy() {
super.onDestroy()
bankTypeBottomSheet = null
}

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

private const val EXTRA_ACCOUNT_ID = "EXTRA_ACCOUNT_ID"

@JvmStatic
fun createIntent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ class BankAddViewModel
private val settingRepository: SettingRepository,
private val userRepository: UserRepository,
) : ViewModel() {
var ownerName = MutableLiveData<String>()
var ownerName = ""
var maskedName = MutableLiveData<String>()

var bankName = MutableLiveData<String>()
var bankCode = ""
var accountNumber = MutableLiveData<String>()

val isBankSelected = MutableLiveData(false)
Expand All @@ -35,21 +38,21 @@ class BankAddViewModel
}

private fun getUserName() {
ownerName.value =
userRepository.getUserName().takeIf { it.isNotEmpty() }?.maskName() ?: return
ownerName = userRepository.getUserName()
maskedName.value = ownerName.takeIf { it.isNotEmpty() }?.maskName() ?: return
}

fun checkIsCompleted() {
isCompleted.value =
(ownerName.value != null && isBankSelected.value == true && accountNumber.value != null)
(!maskedName.value.isNullOrEmpty() && bankCode.isNotEmpty() && !accountNumber.value.isNullOrEmpty())
}

fun postToAddBankToServer() {
viewModelScope.launch {
settingRepository.postToAddBank(
BankRequestModel(
ownerName.value.orEmpty(),
bankName.value.orEmpty(),
ownerName,
bankCode,
accountNumber.value.orEmpty(),
),
).onSuccess {
Expand All @@ -65,8 +68,8 @@ class BankAddViewModel
settingRepository.putToModBank(
accountId,
BankRequestModel(
ownerName.value.orEmpty(),
bankName.value.orEmpty(),
ownerName,
bankCode,
accountNumber.value.orEmpty(),
),
).onSuccess {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class BankTypeBottomSheet :

private fun initBankTypeClickListener(item: BankType) {
with(viewModel) {
bankName.value = item.code
bankName.value = item.displayName
bankCode = item.code
isBankSelected.value = true
}
dismiss()
Expand Down
6 changes: 3 additions & 3 deletions presentation/src/main/res/layout/activity_bank_add.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="8dp"
android:text="@={vm.ownerName}"
android:text="@={vm.maskedName}"
android:textColor="@color/gray_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down Expand Up @@ -100,7 +100,7 @@
app:layout_constraintTop_toBottomOf="@id/border_add_bank_owner_name" />

<LinearLayout
android:id="@+id/layout_add_bank_name"
android:id="@+id/btn_add_bank_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
Expand Down Expand Up @@ -140,7 +140,7 @@
android:background="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/layout_add_bank_name" />
app:layout_constraintTop_toBottomOf="@id/btn_add_bank_name" />

<TextView
android:id="@+id/tv_add_bank_number_title"
Expand Down
Loading