diff --git a/core/data/src/main/java/com/youthtalk/repository/SearchRepositoryImpl.kt b/core/data/src/main/java/com/youthtalk/repository/SearchRepositoryImpl.kt index 5942aff5..0163336f 100644 --- a/core/data/src/main/java/com/youthtalk/repository/SearchRepositoryImpl.kt +++ b/core/data/src/main/java/com/youthtalk/repository/SearchRepositoryImpl.kt @@ -10,15 +10,13 @@ import com.core.exception.NoDataException import com.youthtalk.data.CommunityService import com.youthtalk.datasource.post.PostKeywordRemoteMediator import com.youthtalk.datasource.room.YouthDatabase -import com.youthtalk.dto.PolicyDetailResponse import com.youthtalk.model.post.Post import com.youthtalk.model.post.PostSubject import com.youthtalk.model.post.PostType -import com.youthtalk.utils.ErrorUtils.throwableError +import com.youthtalk.utils.ErrorUtils.createResult import javax.inject.Inject import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow -import timber.log.Timber class SearchRepositoryImpl @Inject constructor( private val dataSource: DataStoreDataSource, @@ -32,47 +30,34 @@ class SearchRepositoryImpl @Inject constructor( } @OptIn(ExperimentalPagingApi::class) - override fun getKeywordPost(keyword: String, communityType: PostSubject, postType: PostType): Flow>> = flow { - emit( - Pager( - config = PagingConfig( - pageSize = 10, - enablePlaceholders = true - ), - remoteMediator = PostKeywordRemoteMediator( - communityService = communityService, - keyword = keyword, - postType = postType, - postSubject = communityType, - youthDatabase = youthDatabase - ) - ) { - youthDatabase.postDao().getPagingSource(postType = postType) - }.flow - ) + override fun getKeywordPost(keyword: String, communityType: PostSubject, postType: PostType): Flow> { + return Pager( + config = PagingConfig( + pageSize = 10, + enablePlaceholders = true + ), + remoteMediator = PostKeywordRemoteMediator( + communityService = communityService, + keyword = keyword, + postType = postType, + postSubject = communityType, + youthDatabase = youthDatabase + ) + ) { + youthDatabase.postDao().getPagingSource(postType = postType) + }.flow } - override fun getKeywordPostCount(keyword: String, communityType: PostSubject): Flow = flow { + override suspend fun getKeywordPostCount(keyword: String, communityType: PostSubject): Result = createResult { val type = when (communityType) { PostSubject.REVIEW -> "review" PostSubject.POST -> "post" } - runCatching { - communityService.getSearchPosts( - keyword = keyword, - page = 0, - type = type, - size = 1 - ) - } - .onSuccess { response -> - response.data?.let { data -> - emit(data.total) - } ?: throw NoDataException("no Data") - } - .onFailure { - Timber.e("SearchRepositoryImpl getKeywordPostCount error $it") - throwableError(it) - } + communityService.getSearchPosts( + keyword = keyword, + page = 0, + type = type, + size = 1 + ).data?.total ?: throw NoDataException() } } diff --git a/core/dataApi/src/main/java/com/core/dataapi/repository/SearchRepository.kt b/core/dataApi/src/main/java/com/core/dataapi/repository/SearchRepository.kt index 8584afb2..debe16ef 100644 --- a/core/dataApi/src/main/java/com/core/dataapi/repository/SearchRepository.kt +++ b/core/dataApi/src/main/java/com/core/dataapi/repository/SearchRepository.kt @@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.Flow interface SearchRepository { fun getRecentList(): Flow> - fun getKeywordPost(keyword: String, communityType: PostSubject, postType: PostType): Flow>> - fun getKeywordPostCount(keyword: String, communityType: PostSubject): Flow + fun getKeywordPost(keyword: String, communityType: PostSubject, postType: PostType): Flow> + suspend fun getKeywordPostCount(keyword: String, communityType: PostSubject): Result suspend fun postRecentList(recentList: List) } diff --git a/core/domain/src/main/java/com/core/domain/usercase/search/GetKeywordPostCountUseCase.kt b/core/domain/src/main/java/com/core/domain/usercase/search/GetKeywordPostCountUseCase.kt index d46910c6..eb5d8af0 100644 --- a/core/domain/src/main/java/com/core/domain/usercase/search/GetKeywordPostCountUseCase.kt +++ b/core/domain/src/main/java/com/core/domain/usercase/search/GetKeywordPostCountUseCase.kt @@ -7,5 +7,5 @@ import javax.inject.Inject class GetKeywordPostCountUseCase @Inject constructor( private val searchRepository: SearchRepository ) { - operator fun invoke(keyword: String, communityType: PostSubject) = searchRepository.getKeywordPostCount(keyword, communityType) + suspend operator fun invoke(keyword: String, communityType: PostSubject) = searchRepository.getKeywordPostCount(keyword, communityType) } diff --git a/feature/search/src/main/java/com/youth/search/viewmodel/CommunitySearchViewModel.kt b/feature/search/src/main/java/com/youth/search/viewmodel/CommunitySearchViewModel.kt index d4487d9e..19fa8b16 100644 --- a/feature/search/src/main/java/com/youth/search/viewmodel/CommunitySearchViewModel.kt +++ b/feature/search/src/main/java/com/youth/search/viewmodel/CommunitySearchViewModel.kt @@ -19,8 +19,6 @@ import dagger.hilt.android.lifecycle.HiltViewModel import javax.inject.Inject import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.collectLatest -import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.launch import timber.log.Timber @@ -72,26 +70,19 @@ class CommunitySearchViewModel @Inject constructor( list.add(0, search) setRecentlyListUseCase(list) - combine( - getKeywordPostUseCase(search, postSubject = communityType, postType = PostType.SEARCH), - getKeywordPostCountUseCase(keyword = search, communityType = communityType) - ) { posts, count -> - Pair(posts, count) - } - .onStart { - setState { copy(searchState = SearchState.SEARCH) } - } - .catch { - Timber.e("CommunitySearchViewModel search error $it") - } - .collectLatest { (posts, count) -> - setState { - copy( - searchPost = posts.cachedIn(viewModelScope), - totalCount = count - ) - } + setState { copy(searchState = SearchState.SEARCH) } + + val keywordPost = getKeywordPostUseCase(search, postSubject = communityType, postType = PostType.SEARCH) + getKeywordPostCountUseCase(keyword = search, communityType = communityType).onSuccess { keywordCount -> + setState { + copy( + searchPost = keywordPost.cachedIn(viewModelScope), + totalCount = keywordCount + ) } + }.onFailure { + Timber.e("error : $it") + } } }