From 9f4e422fe9c6a9f4f14806f890c26caac9be13b1 Mon Sep 17 00:00:00 2001 From: yangsooplus Date: Thu, 1 Feb 2024 15:54:04 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=B3=B4=EB=82=B8=20=EB=B4=89=ED=88=AC?= =?UTF-8?q?=20=EA=B2=80=EC=83=89=20-=20=EC=B9=9C=EA=B5=AC,=20=EA=B8=88?= =?UTF-8?q?=EC=95=A1=EC=9C=BC=EB=A1=9C=20=EA=B2=80=EC=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../envelope/SearchSentEnvelopeListUseCase.kt | 31 ++++++++++++++++ .../SentEnvelopeSearchViewModel.kt | 36 +++++++++++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 domain/src/main/java/com/susu/domain/usecase/envelope/SearchSentEnvelopeListUseCase.kt diff --git a/domain/src/main/java/com/susu/domain/usecase/envelope/SearchSentEnvelopeListUseCase.kt b/domain/src/main/java/com/susu/domain/usecase/envelope/SearchSentEnvelopeListUseCase.kt new file mode 100644 index 00000000..97ed4bb5 --- /dev/null +++ b/domain/src/main/java/com/susu/domain/usecase/envelope/SearchSentEnvelopeListUseCase.kt @@ -0,0 +1,31 @@ +package com.susu.domain.usecase.envelope + +import com.susu.core.common.runCatchingIgnoreCancelled +import com.susu.domain.repository.EnvelopesRepository +import javax.inject.Inject + +class SearchSentEnvelopeListUseCase @Inject constructor( + private val envelopesRepository: EnvelopesRepository, +) { + suspend operator fun invoke(param: Param) = runCatchingIgnoreCancelled { + with(param) { + envelopesRepository.searchEnvelope( + friendIds = friendIds, + types = "SENT", + fromAmount = fromAmount, + toAmount = toAmount, + page = page, + sort = sort, + ledgerId = null, + ) + } + } + + data class Param( + val friendIds: List? = null, + val fromAmount: Long? = null, + val toAmount: Long? = null, + val page: Int? = null, + val sort: String? = null, + ) +} diff --git a/feature/sent/src/main/java/com/susu/feature/envelopesearch/SentEnvelopeSearchViewModel.kt b/feature/sent/src/main/java/com/susu/feature/envelopesearch/SentEnvelopeSearchViewModel.kt index 9a429899..38d09176 100644 --- a/feature/sent/src/main/java/com/susu/feature/envelopesearch/SentEnvelopeSearchViewModel.kt +++ b/feature/sent/src/main/java/com/susu/feature/envelopesearch/SentEnvelopeSearchViewModel.kt @@ -2,9 +2,11 @@ package com.susu.feature.envelopesearch import androidx.lifecycle.viewModelScope import com.susu.core.ui.base.BaseViewModel +import com.susu.domain.usecase.envelope.SearchSentEnvelopeListUseCase import com.susu.domain.usecase.enveloperecentsearch.DeleteEnvelopeRecentSearchUseCase import com.susu.domain.usecase.enveloperecentsearch.GetEnvelopeRecentSearchListUseCase import com.susu.domain.usecase.enveloperecentsearch.UpsertEnvelopeRecentSearchUseCase +import com.susu.domain.usecase.friend.SearchFriendUseCase import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toPersistentList @@ -16,6 +18,8 @@ class EnvelopeSearchViewModel @Inject constructor( private val getEnvelopeRecentSearchUserCase: GetEnvelopeRecentSearchListUseCase, private val deleteEnvelopeRecentSearchUseCase: DeleteEnvelopeRecentSearchUseCase, private val upsertEnvelopeRecentSearchUseCase: UpsertEnvelopeRecentSearchUseCase, + private val searchFriendUseCase: SearchFriendUseCase, + private val searchSentEnvelopeListUseCase: SearchSentEnvelopeListUseCase, ) : BaseViewModel(EnvelopeSearchState()) { fun getEnvelopeRecentSearchList() { @@ -44,8 +48,35 @@ class EnvelopeSearchViewModel @Inject constructor( } fun getEnvelopeList(search: String) = viewModelScope.launch { - // TODO: 친구 검색 -> 결과가 있으면 봉투 검색 - // TODO: 카테고리 검색 -> 결과가 있으면 봉투 검색 + 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 envelopesByAmount = search.toLongOrNull()?.let { amount -> + searchSentEnvelopeListUseCase( + param = SearchSentEnvelopeListUseCase.Param( + fromAmount = amount, + toAmount = amount, + ), + ) + } ?: Result.success(emptyList()) + + // 두가지 조건을 검색 완료 시 결과를 통합 표시 + if (envelopesByFriend.isSuccess && envelopesByAmount.isSuccess) { + val searchedEnvelopes = + envelopesByFriend.getOrDefault(emptyList()).map { it.envelope } + envelopesByAmount.getOrDefault(emptyList()).map { it.envelope } + intent { copy(envelopeList = searchedEnvelopes.toPersistentList()) } + } } private fun updateRecentSearchList(searchList: List) { @@ -53,5 +84,4 @@ class EnvelopeSearchViewModel @Inject constructor( copy(recentSearchKeywordList = searchList.toPersistentList()) } } - }