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()) } } - }