Skip to content

Commit

Permalink
[refactor] 필터 검색 / AddressSelection 관련 메서드 분리 (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
otter66 committed Oct 26, 2023
1 parent 7b47f51 commit 28b0510
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.teamtripdraw.android.R
import com.teamtripdraw.android.databinding.ActivityAddressSelectionBinding
import com.teamtripdraw.android.support.framework.presentation.event.EventObserver
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -16,55 +17,82 @@ class AddressSelectionActivity : AppCompatActivity() {
private lateinit var binding: ActivityAddressSelectionBinding
private val viewModel: AddressSelectionViewModel by viewModels()

private val siDosAdapter: AddressSelectionAdapter by lazy {
AddressSelectionAdapter(selectEvent = { viewModel.selectSiDo(it) })
}
private val siGunGusAdapter: AddressSelectionAdapter by lazy {
AddressSelectionAdapter(selectEvent = { viewModel.selectSiGunGu(it) })
}
private val eupMyeonDongsAdapter: AddressSelectionAdapter by lazy {
AddressSelectionAdapter(selectEvent = { viewModel.selectEupMyeonDong(it) })
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_address_selection)
binding.lifecycleOwner = this
binding.addressSelectionViewModel = viewModel

initSiDoListView()
initSiGunGuListView()
initEupMyeonDongListView()
initSelectedCompleteView()
initView()
initObserve()
}

private fun initView() {
initSiDosView()
initSiGunGusView()
initEupMyeonDongsView()
}

private fun initObserve() {
initSiDosObserve()
initSiGunGusObserve()
initEupMyeonDongsObserve()
initSelectedCompleteObserve()
}

private fun initSiDoListView() {
val siDoAdapter = AddressSelectionAdapter { viewModel.selectSiDo(it) }
binding.rvAddressSelectionSiDo.adapter = siDoAdapter
private fun initSiDosView() {
binding.rvAddressSelectionSiDo.adapter = siDosAdapter
binding.rvAddressSelectionSiDo.itemAnimator = null
}

private fun initSiGunGusView() {
binding.rvAddressSelectionSiGunGu.adapter = siGunGusAdapter
binding.rvAddressSelectionSiGunGu.itemAnimator = null
}

private fun initEupMyeonDongsView() {
binding.rvAddressSelectionEupMyeonDong.adapter = eupMyeonDongsAdapter
binding.rvAddressSelectionEupMyeonDong.itemAnimator = null
}

private fun initSiDosObserve() {
viewModel.siDos.observe(this) { siDos ->
siDoAdapter.submitList(siDos.toList())
siDosAdapter.submitList(siDos.toList())
}
}

private fun initSiGunGuListView() {
val siGunGuAdapter = AddressSelectionAdapter { viewModel.selectSiGunGu(it) }
binding.rvAddressSelectionSiGunGu.adapter = siGunGuAdapter
binding.rvAddressSelectionSiGunGu.itemAnimator = null

private fun initSiGunGusObserve() {
viewModel.siGunGus.observe(this) { siGunGus ->
siGunGuAdapter.submitList(siGunGus)
siGunGusAdapter.submitList(siGunGus)
}
}

private fun initEupMyeonDongListView() {
val eupMyeonDongAdapter = AddressSelectionAdapter { viewModel.selectEupMyeonDong(it) }
binding.rvAddressSelectionEupMyeonDong.adapter = eupMyeonDongAdapter
binding.rvAddressSelectionEupMyeonDong.itemAnimator = null

private fun initEupMyeonDongsObserve() {
viewModel.eupMyeonDongs.observe(this) { eupMyeonDongs ->
eupMyeonDongAdapter.submitList(eupMyeonDongs)
eupMyeonDongsAdapter.submitList(eupMyeonDongs)
}
}

private fun initSelectedCompleteView() {
viewModel.selectingCompletedEvent.observe(this) {
if (it.content) returnBeforeActivity()
}
private fun initSelectedCompleteObserve() {
viewModel.selectingCompletedEvent.observe(
this,
EventObserver {
if (it) returnSelectedAddressToBeforeActivity()
},
)
}

private fun returnBeforeActivity() {
private fun returnSelectedAddressToBeforeActivity() {
val resultIntent = Intent()
resultIntent.putExtra(INTENT_KEY_ADDRESS, viewModel.address)
setResult(Activity.RESULT_OK, resultIntent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,48 +41,55 @@ class AddressSelectionViewModel @Inject constructor(
}

fun selectSiDo(selectedSiDoItem: UiAddressSelectionItem) {
selectedSiDo.value = selectedSiDoItem.addressName
_siDos.value?.let { siDoItems ->
_siDos.value = siDoItems.toMutableList().map { item ->
val isSelected = item.addressName == selectedSiDoItem.addressName
UiAddressSelectionItem(item.addressName, isSelected)
}
_siDos.value = getSelectedChangedList(siDoItems, selectedSiDoItem)
}

this.selectedSiDo.value = selectedSiDoItem.addressName
selectedSiGunGu.value = ""
selectedEupMyeonDong.value = ""

_siGunGus.value = listOf()
_eupMyeonDongs.value = listOf()

resetSiGunGu()
resetEupMyeonDong()
fetchSiGunGu()
}

fun selectSiGunGu(selectedSiGunGuItem: UiAddressSelectionItem) {
selectedSiGunGu.value = selectedSiGunGuItem.addressName
_siGunGus.value?.let { siGunGuItems ->
_siGunGus.value = siGunGuItems.toMutableList().map { item ->
val isSelected = item.addressName == selectedSiGunGuItem.addressName
UiAddressSelectionItem(item.addressName, isSelected)
}
_siGunGus.value = getSelectedChangedList(siGunGuItems, selectedSiGunGuItem)
}

selectedSiGunGu.value = selectedSiGunGuItem.addressName
selectedEupMyeonDong.value = ""

_eupMyeonDongs.value = listOf()

resetEupMyeonDong()
fetchEupMyeonDong()
}

fun selectEupMyeonDong(selectedEupMyeonDongItem: UiAddressSelectionItem) {
selectedEupMyeonDong.value = selectedEupMyeonDongItem.addressName
_eupMyeonDongs.value?.let { eupMyeonDongs ->
_eupMyeonDongs.value = eupMyeonDongs.toMutableList().map { item ->
val isSelected = item.addressName == selectedEupMyeonDongItem.addressName
_eupMyeonDongs.value = getSelectedChangedList(eupMyeonDongs, selectedEupMyeonDongItem)
}
}

private fun getSelectedChangedList(
list: List<UiAddressSelectionItem>,
selectedItem: UiAddressSelectionItem,
): List<UiAddressSelectionItem> {
return list.toMutableList().map { item ->
val isSelected = item.addressName == selectedItem.addressName
if (isSelected || item.isSelected) { // item의 selected가 true로 변경되거나 false로 변경되는 경우에만 새로운 인스턴스를 생성한다.
UiAddressSelectionItem(item.addressName, isSelected)
} else {
item
}
}
}

selectedEupMyeonDong.value = selectedEupMyeonDongItem.addressName
private fun resetSiGunGu() {
selectedSiGunGu.value = ""
_siGunGus.value = listOf()
}

private fun resetEupMyeonDong() {
selectedEupMyeonDong.value = ""
_eupMyeonDongs.value = listOf()
}

private fun fetchSiDos() {
Expand Down

0 comments on commit 28b0510

Please sign in to comment.