Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

검색 모아보기 관련 이슈 수정 #200

Merged
merged 5 commits into from
Aug 12, 2024
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
@@ -1,12 +1,11 @@
package team.ppac.data.repository

import androidx.paging.PagingData
import kotlinx.coroutines.flow.Flow
import team.ppac.data.mapper.toMeme
import team.ppac.data.paging.ITEMS_PER_PAGE
import team.ppac.data.paging.createPager
import team.ppac.domain.model.Meme
import team.ppac.domain.model.MemeWatchType
import team.ppac.domain.model.MemeWithPagination
import team.ppac.domain.repository.MemeRepository
import team.ppac.remote.datasource.MemeDataSource
import javax.inject.Inject
Expand All @@ -31,15 +30,25 @@ class MemeRepositoryImpl @Inject constructor(
return memeDataSource.deleteSavedMeme(memeId)
}

override fun getSearchMemes(keyword: String): Flow<PagingData<Meme>> {
return createPager { page ->
memeDataSource.getSearchMemes(
keyword = keyword,
page = page,
size = ITEMS_PER_PAGE,
).map { it.toMeme() }
}.flow
override suspend fun getSearchMemes(keyword: String): MemeWithPagination {
val totalMemeCount = memeDataSource.getSearchMemes(
keyword = keyword,
page = 1,
size = ITEMS_PER_PAGE
).pagination.total

return MemeWithPagination(
totalMemeCount = totalMemeCount,
memes = createPager { page ->
memeDataSource.getSearchMemes(
keyword = keyword,
page = page,
size = ITEMS_PER_PAGE,
).memeList.map { it.toMeme() }
}.flow
)
}

override suspend fun reactMeme(memeId: String): Boolean {
return memeDataSource.reactMeme(memeId)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package team.ppac.domain.model

import androidx.paging.PagingData
import kotlinx.coroutines.flow.Flow

data class MemeWithPagination(
val totalMemeCount: Int,
val memes: Flow<PagingData<Meme>>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import androidx.paging.PagingData
import kotlinx.coroutines.flow.Flow
import team.ppac.domain.model.Meme
import team.ppac.domain.model.MemeWatchType
import team.ppac.domain.model.MemeWithPagination

interface MemeRepository {
suspend fun getMeme(memeId: String): Meme
suspend fun getRecommendMemes(): List<Meme>
suspend fun saveMeme(memeId: String): Boolean
suspend fun deleteSavedMeme(memeId: String): Boolean
fun getSearchMemes(keyword: String): Flow<PagingData<Meme>>
suspend fun getSearchMemes(keyword: String): MemeWithPagination
suspend fun reactMeme(memeId: String): Boolean
suspend fun watchMeme(
memeId: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package team.ppac.domain.usecase

import androidx.paging.PagingData
import kotlinx.coroutines.flow.Flow
import team.ppac.domain.model.Meme
import team.ppac.domain.model.MemeWithPagination
import team.ppac.domain.repository.MemeRepository
import javax.inject.Inject

interface GetSearchMemeUseCase {
operator fun invoke(keyword: String): Flow<PagingData<Meme>>
suspend operator fun invoke(keyword: String): MemeWithPagination
}

internal class GetSearchMemeUseCaseImpl @Inject constructor(
private val memeRepository: MemeRepository,
) : GetSearchMemeUseCase {
override fun invoke(keyword: String): Flow<PagingData<Meme>> {
override suspend fun invoke(keyword: String): MemeWithPagination {
return memeRepository.getSearchMemes(keyword)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package team.ppac.remote.datasource

import team.ppac.remote.model.response.meme.MemeResponse
import team.ppac.remote.model.response.user.SavedMemesResponse

interface MemeDataSource {
suspend fun getMemeById(memeId: String): MemeResponse
Expand All @@ -11,7 +12,7 @@ interface MemeDataSource {
keyword: String,
page: Int,
size: Int,
): List<MemeResponse>
): SavedMemesResponse
suspend fun reactMeme(memeId: String): Boolean
suspend fun watchMeme(
memeId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package team.ppac.remote.datasource.impl
import team.ppac.remote.api.MemeApi
import team.ppac.remote.datasource.MemeDataSource
import team.ppac.remote.model.response.meme.MemeResponse
import team.ppac.remote.model.response.user.SavedMemesResponse
import javax.inject.Inject

internal class MemeDataSourceImpl @Inject constructor(
Expand All @@ -24,8 +25,8 @@ internal class MemeDataSourceImpl @Inject constructor(
return memeApi.deleteSavedMeme(memeId)
}

override suspend fun getSearchMemes(keyword: String, page: Int, size: Int): List<MemeResponse> {
return memeApi.getSearchMemes(keyword, page, size).memeList
override suspend fun getSearchMemes(keyword: String, page: Int, size: Int): SavedMemesResponse {
return memeApi.getSearchMemes(keyword, page, size)
}

override suspend fun reactMeme(memeId: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,16 @@ internal fun SearchDetailScreen(
Column(
modifier = Modifier.padding(innerPadding)
) {
if (searchResults.itemCount == 0) {
if (uiState.totalMemeCount == 0) {
EmptyResultContent()
} else {
SearchDetailResultHeader(totalCount = uiState.totalMemeCount)
SearchDetailResultContent(
searchResults = searchResults,
onMemeClick = onMemeClick,
onCopyClick = onCopyClick
)
}
SearchDetailResultHeader(totalCount = searchResults.itemCount)
SearchDetailResultContent(
searchResults = searchResults,
onMemeClick = onMemeClick,
onCopyClick = onCopyClick
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,26 @@ class SearchDetailViewModel @Inject constructor(
private fun getSearchResults(memeCategory: String) = launch {
updateLoadingState(true)
delay(300L)
val searchResults = getSearchMemeUseCase(memeCategory)

val paginationResults = getSearchMemeUseCase(memeCategory)
val totalMemeCount = paginationResults.totalMemeCount
val searchResults = paginationResults.memes
.map { pagingData ->
pagingData.map { it.toSearchResultUiModel() }
}.cachedIn(viewModelScope)

updateLoadingState(false)
updateSearchResults(memeCategory, searchResults)
updateSearchResults(totalMemeCount, memeCategory, searchResults)
}

private fun updateSearchResults(
totalMemeCount: Int,
memeCategory: String,
searchResults: Flow<PagingData<SearchResultUiModel>>,
) {
reduce {
copy(
totalMemeCount = totalMemeCount,
memeCategory = memeCategory,
searchResults = searchResults
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import team.ppac.search.detail.model.SearchResultUiModel
data class SearchDetailUiState(
val isLoading: Boolean,
val isError: Boolean,
val totalMemeCount: Int,
val memeCategory: String,
val searchResults: Flow<PagingData<SearchResultUiModel>>,
) : UiState {
Expand All @@ -17,6 +18,7 @@ data class SearchDetailUiState(
val INITIAL_STATE = SearchDetailUiState(
isLoading = true,
isError = false,
totalMemeCount = 0,
memeCategory = "",
searchResults = flowOf(PagingData.empty())
)
Expand Down