From d4b418c10f0b6acd976f9257637825ffdc0d5407 Mon Sep 17 00:00:00 2001 From: yunsehwan Date: Fri, 20 Sep 2024 19:46:32 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]=20=EB=A7=81=ED=81=AC=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=B4=20=ED=8F=AC=ED=82=B7?= =?UTF-8?q?=EC=9D=98=20=EB=A7=81=ED=81=AC=20=EA=B0=9C=EC=88=98=EA=B0=80=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=EB=90=98=EC=97=87=EC=9D=84=20=EB=95=8C,=20?= =?UTF-8?q?=EC=9D=B4=EB=A5=BC=20=ED=99=88=20=ED=99=94=EB=A9=B4=20=EB=B0=8F?= =?UTF-8?q?=20=ED=8F=AC=ED=82=B7=20=EC=83=81=EC=84=B8=20=ED=99=94=EB=A9=B4?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EB=B0=98=EC=98=81=ED=95=98=EC=A7=80=20?= =?UTF-8?q?=EB=AA=BB=ED=95=98=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/feature/navigation/args/PokitArg.kt | 5 +++ .../navigation/args/PokitUpdateEvent.kt | 11 ++++++ .../strayalpaca/addlink/AddLinkViewModel.kt | 8 +++++ .../pokit/home/pokit/PokitViewModel.kt | 22 ++++++++++++ .../pokitdetail/PokitDetailViewModel.kt | 36 +++++++++++++++---- 5 files changed, 75 insertions(+), 7 deletions(-) diff --git a/core/feature/src/main/java/pokitmons/pokit/core/feature/navigation/args/PokitArg.kt b/core/feature/src/main/java/pokitmons/pokit/core/feature/navigation/args/PokitArg.kt index 6a67a471..74d3b761 100644 --- a/core/feature/src/main/java/pokitmons/pokit/core/feature/navigation/args/PokitArg.kt +++ b/core/feature/src/main/java/pokitmons/pokit/core/feature/navigation/args/PokitArg.kt @@ -10,3 +10,8 @@ data class PokitArg( val imageUrl: String, val title: String, ) : Parcelable + +data class LinkCountChangedPokitIds( + val increasedPokitId: Int?, + val decreasedPokitId: Int?, +) diff --git a/core/feature/src/main/java/pokitmons/pokit/core/feature/navigation/args/PokitUpdateEvent.kt b/core/feature/src/main/java/pokitmons/pokit/core/feature/navigation/args/PokitUpdateEvent.kt index a506e3ec..c516186d 100644 --- a/core/feature/src/main/java/pokitmons/pokit/core/feature/navigation/args/PokitUpdateEvent.kt +++ b/core/feature/src/main/java/pokitmons/pokit/core/feature/navigation/args/PokitUpdateEvent.kt @@ -16,6 +16,9 @@ object PokitUpdateEvent { private val _addedPokit = MutableSharedFlow() val addedPokit = _addedPokit.asSharedFlow() + private val _countModifiedPokitIds = MutableSharedFlow() + val countModifiedPokitIds = _countModifiedPokitIds.asSharedFlow() + fun updatePokit(pokitArg: PokitArg) { CoroutineScope(Dispatchers.Default).launch { _updatedPokit.emit(pokitArg) @@ -33,4 +36,12 @@ object PokitUpdateEvent { _addedPokit.emit(pokitArg) } } + + fun updatePokitLinkCount(linkRemovedPokitId: Int? = null, linkAddedPokitId: Int? = null) { + if (linkRemovedPokitId == linkAddedPokitId) return + + CoroutineScope(Dispatchers.Default).launch { + _countModifiedPokitIds.emit(LinkCountChangedPokitIds(increasedPokitId = linkAddedPokitId, decreasedPokitId = linkRemovedPokitId)) + } + } } diff --git a/feature/addlink/src/main/java/com/strayalpaca/addlink/AddLinkViewModel.kt b/feature/addlink/src/main/java/com/strayalpaca/addlink/AddLinkViewModel.kt index 4cd939b2..36ab79c1 100644 --- a/feature/addlink/src/main/java/com/strayalpaca/addlink/AddLinkViewModel.kt +++ b/feature/addlink/src/main/java/com/strayalpaca/addlink/AddLinkViewModel.kt @@ -86,6 +86,9 @@ class AddLinkViewModel @Inject constructor( val currentLinkId: Int? = savedStateHandle.get("link_id")?.toIntOrNull() + // 수정 이전 pokit과 수정 이후 pokit이 다른 경우를 체크하기 위해서만 사용 + private var prevPokitId: Int? = null + init { initPokitAddEventDetector() @@ -147,6 +150,7 @@ class AddLinkViewModel @Inject constructor( step = ScreenStep.IDLE ) } + prevPokitId = responseResult.categoryId _title.update { response.result.title } _memo.update { response.result.memo } _linkUrl.update { response.result.data } @@ -266,6 +270,10 @@ class AddLinkViewModel @Inject constructor( if (isCreate) { LinkUpdateEvent.createSuccess(linkArg) } else { + PokitUpdateEvent.updatePokitLinkCount( + linkAddedPokitId = currentSelectedPokit.id.toIntOrNull(), + linkRemovedPokitId = prevPokitId + ) LinkUpdateEvent.modifySuccess(linkArg) } diff --git a/feature/home/src/main/java/pokitmons/pokit/home/pokit/PokitViewModel.kt b/feature/home/src/main/java/pokitmons/pokit/home/pokit/PokitViewModel.kt index ee98a078..da537a88 100644 --- a/feature/home/src/main/java/pokitmons/pokit/home/pokit/PokitViewModel.kt +++ b/feature/home/src/main/java/pokitmons/pokit/home/pokit/PokitViewModel.kt @@ -33,6 +33,7 @@ import pokitmons.pokit.domain.usecase.pokit.GetPokitsUseCase import pokitmons.pokit.home.model.HomeSideEffect import pokitmons.pokit.home.model.HomeToastMessage import javax.inject.Inject +import kotlin.math.max import com.strayalpaca.pokitdetail.model.Link as DetailLink import pokitmons.pokit.domain.model.pokit.Pokit as DomainPokit @@ -102,6 +103,27 @@ class PokitViewModel @Inject constructor( pokitPaging.modifyItem(modifiedPokit) } } + + viewModelScope.launch { + PokitUpdateEvent.countModifiedPokitIds.collectLatest { linkCountChangedPokitIds -> + linkCountChangedPokitIds.increasedPokitId?.let { linkCountIncreasedPokitId -> + val currentPokit = pokitPaging.pagingData.value.find { it.id == linkCountIncreasedPokitId.toString() } + currentPokit?.let { linkCountIncreasedPokit -> + val increasedLinkCount = linkCountIncreasedPokit.count + 1 + pokitPaging.modifyItem(linkCountIncreasedPokit.copy(count = increasedLinkCount)) + } + } + + linkCountChangedPokitIds.decreasedPokitId?.let { linkCountDecreasedPokitId -> + val currentPokit = pokitPaging.pagingData.value.find { it.id == linkCountDecreasedPokitId.toString() } + currentPokit?.let { linkCountDecreasedPokit -> + val decreasedLinkCount = max(0, linkCountDecreasedPokit.count - 1) + pokitPaging.modifyItem(linkCountDecreasedPokit.copy(count = decreasedLinkCount)) + } + } + + } + } } private fun initPokitRemoveEventDetector() { diff --git a/feature/pokitdetail/src/main/java/com/strayalpaca/pokitdetail/PokitDetailViewModel.kt b/feature/pokitdetail/src/main/java/com/strayalpaca/pokitdetail/PokitDetailViewModel.kt index ab79a2ea..b220c328 100644 --- a/feature/pokitdetail/src/main/java/com/strayalpaca/pokitdetail/PokitDetailViewModel.kt +++ b/feature/pokitdetail/src/main/java/com/strayalpaca/pokitdetail/PokitDetailViewModel.kt @@ -33,6 +33,7 @@ import pokitmons.pokit.domain.usecase.pokit.DeletePokitUseCase import pokitmons.pokit.domain.usecase.pokit.GetPokitUseCase import pokitmons.pokit.domain.usecase.pokit.GetPokitsUseCase import javax.inject.Inject +import kotlin.math.max @HiltViewModel class PokitDetailViewModel @Inject constructor( @@ -119,13 +120,18 @@ class PokitDetailViewModel @Inject constructor( viewModelScope.launch { LinkUpdateEvent.updatedLink.collectLatest { updatedLink -> val targetLink = linkPaging.pagingData.value.find { it.id == updatedLink.id.toString() } ?: return@collectLatest - val modifiedLink = targetLink.copy( - title = updatedLink.title, - imageUrl = updatedLink.thumbnail, - domainUrl = updatedLink.domain, - createdAt = updatedLink.createdAt - ) - linkPaging.modifyItem(modifiedLink) + + if (updatedLink.pokitId.toString() != targetLink.pokitId) { + linkPaging.deleteItem(targetLink.id) + } else { + val modifiedLink = targetLink.copy( + title = updatedLink.title, + imageUrl = updatedLink.thumbnail, + domainUrl = updatedLink.domain, + createdAt = updatedLink.createdAt + ) + linkPaging.modifyItem(modifiedLink) + } } } } @@ -135,6 +141,10 @@ class PokitDetailViewModel @Inject constructor( LinkUpdateEvent.removedLink.collectLatest { removedLinkId -> val targetLink = linkPaging.pagingData.value.find { it.id == removedLinkId.toString() } ?: return@collectLatest linkPaging.deleteItem(targetLink.id) + + val currentPokit = state.value.currentPokit ?: return@collectLatest + val changedLinkCount = max(currentPokit.count - 1, 0) + _state.update { it.copy(currentPokit = currentPokit.copy(count = changedLinkCount)) } } } } @@ -150,6 +160,18 @@ class PokitDetailViewModel @Inject constructor( _state.update { it.copy(currentPokit = pokit) } } } + + viewModelScope.launch { + PokitUpdateEvent.countModifiedPokitIds.collectLatest { linkCountChangedPokitIds -> + val currentPokit = state.value.currentPokit ?: return@collectLatest + linkCountChangedPokitIds.decreasedPokitId?.let { targetId -> + if (targetId.toString() == currentPokit.id) { + val changedLinkCount = max(currentPokit.count - 1, 0) + _state.update { it.copy(currentPokit = currentPokit.copy(count = changedLinkCount)) } + } + } + } + } } private fun getPokit(pokitId: Int, linkCount: Int) {