Skip to content

Commit

Permalink
[FIX] 링크 수정으로 인해 포킷의 링크 개수가 변경되엇을 때, 이를 홈 화면 및 포킷 상세 화면에서 반영하지 못하는 문제 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
l5x5l committed Sep 20, 2024
1 parent e0a066d commit d4b418c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ data class PokitArg(
val imageUrl: String,
val title: String,
) : Parcelable

data class LinkCountChangedPokitIds(
val increasedPokitId: Int?,
val decreasedPokitId: Int?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ object PokitUpdateEvent {
private val _addedPokit = MutableSharedFlow<PokitArg>()
val addedPokit = _addedPokit.asSharedFlow()

private val _countModifiedPokitIds = MutableSharedFlow<LinkCountChangedPokitIds>()
val countModifiedPokitIds = _countModifiedPokitIds.asSharedFlow()

fun updatePokit(pokitArg: PokitArg) {
CoroutineScope(Dispatchers.Default).launch {
_updatedPokit.emit(pokitArg)
Expand All @@ -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))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class AddLinkViewModel @Inject constructor(

val currentLinkId: Int? = savedStateHandle.get<String>("link_id")?.toIntOrNull()

// 수정 이전 pokit과 수정 이후 pokit이 다른 경우를 체크하기 위해서만 사용
private var prevPokitId: Int? = null

init {
initPokitAddEventDetector()

Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}
}
}
}
Expand All @@ -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)) }
}
}
}
Expand All @@ -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) {
Expand Down

0 comments on commit d4b418c

Please sign in to comment.