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

Qa/128 보낸 봉투 검색 시 검색결과와 관계 없는 봉투가 보이는 현상 #2

Merged
merged 3 commits into from
Jul 8, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.susu.domain.usecase.friend.SearchFriendUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand Down Expand Up @@ -52,35 +54,38 @@ class SentEnvelopeSearchViewModel @Inject constructor(
}

fun getEnvelopeList(search: String) = viewModelScope.launch {
if (search.isEmpty()) return@launch
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5) 요 부분 제외하구 ... 아래 async 사용한건 로직상 동일해보이는데 맞나요!?
async + await 사용해서 응답속도가 더 빨라졌겠군요~

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵~ 로직 자체는 동일합니다~!
case1) 숫자X 검색어로 친구가 검색됨 -> 친구 id로 검색
case2) 숫자O, 친구 검색 결과 X -> 금액으로만 검색
case3) 숫자O, 친구 검색 결과 O (친구 이름에 숫자가 들어가는 경우) -> 친구 id, 금액으로 모두 검색


val searchedFriends = searchFriendUseCase(search).getOrThrow()

// 친구 검색 결과가 존재하면 봉투 검색
val envelopesByFriend = if (searchedFriends.isNotEmpty()) {
searchSentEnvelopeListUseCase(
param = SearchSentEnvelopeListUseCase.Param(
friendIds = searchedFriends.map { it.friend.id.toInt() },
),
)
} else {
Result.success(emptyList())
val envelopesByFriend = async {
if (searchedFriends.isNotEmpty()) {
searchSentEnvelopeListUseCase(
param = SearchSentEnvelopeListUseCase.Param(
friendIds = searchedFriends.map { it.friend.id.toInt() },
),
).getOrDefault(emptyList())
} else {
emptyList()
}
}

// 숫자 형식일 경우는 금액으로 봉투 검색
val envelopesByAmount = search.toLongOrNull()?.let { amount ->
searchSentEnvelopeListUseCase(
param = SearchSentEnvelopeListUseCase.Param(
fromAmount = amount,
toAmount = amount,
),
)
} ?: Result.success(emptyList())
val envelopesByAmount = async {
search.toLongOrNull()?.let { amount ->
searchSentEnvelopeListUseCase(
param = SearchSentEnvelopeListUseCase.Param(
fromAmount = amount,
toAmount = amount,
),
).getOrDefault(emptyList())
} ?: emptyList()
}

// 두가지 조건을 검색 완료 시 결과를 통합 표시
if (envelopesByFriend.isSuccess && envelopesByAmount.isSuccess) {
val searchedEnvelopes =
envelopesByFriend.getOrDefault(emptyList()) + envelopesByAmount.getOrDefault(emptyList())
intent { copy(envelopeList = searchedEnvelopes.toPersistentList()) }
}
val result = awaitAll(envelopesByFriend, envelopesByAmount).flatten()
intent { copy(envelopeList = result.toPersistentList()) }
}

private fun updateRecentSearchList(searchList: List<String>) {
Expand Down
Loading