Skip to content

Commit

Permalink
feat: 보낸 봉투 검색 - 친구, 금액으로 검색
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsooplus committed Feb 1, 2024
1 parent 9294c06 commit 9f4e422
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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<Int>? = null,
val fromAmount: Long? = null,
val toAmount: Long? = null,
val page: Int? = null,
val sort: String? = null,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, EnvelopeSearchEffect>(EnvelopeSearchState()) {

fun getEnvelopeRecentSearchList() {
Expand Down Expand Up @@ -44,14 +48,40 @@ 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<String>) {
intent {
copy(recentSearchKeywordList = searchList.toPersistentList())
}
}

}

0 comments on commit 9f4e422

Please sign in to comment.