Skip to content
Merged
Show file tree
Hide file tree
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,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,
Expand All @@ -32,47 +30,34 @@ class SearchRepositoryImpl @Inject constructor(
}

@OptIn(ExperimentalPagingApi::class)
override fun getKeywordPost(keyword: String, communityType: PostSubject, postType: PostType): Flow<Flow<PagingData<Post>>> = 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<PagingData<Post>> {
Comment thread
hegunhee marked this conversation as resolved.
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<Int> = flow {
override suspend fun getKeywordPostCount(keyword: String, communityType: PostSubject): Result<Int> = 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<PolicyDetailResponse>(it)
}
communityService.getSearchPosts(
keyword = keyword,
page = 0,
type = type,
size = 1
).data?.total ?: throw NoDataException()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.Flow

interface SearchRepository {
fun getRecentList(): Flow<List<String>>
fun getKeywordPost(keyword: String, communityType: PostSubject, postType: PostType): Flow<Flow<PagingData<Post>>>
fun getKeywordPostCount(keyword: String, communityType: PostSubject): Flow<Int>
fun getKeywordPost(keyword: String, communityType: PostSubject, postType: PostType): Flow<PagingData<Post>>
suspend fun getKeywordPostCount(keyword: String, communityType: PostSubject): Result<Int>
suspend fun postRecentList(recentList: List<String>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
}
}
}

Expand Down