-
Notifications
You must be signed in to change notification settings - Fork 1
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
[FEAT/#328] 탐색 뷰 / 페이징 추가 #329
Changes from 4 commits
4e56e07
459e0f2
4773996
e5e8b3f
a88bca6
d5cbc53
5ffc166
08689a6
4e3ab37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.terning.data.search.pagingsource | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.terning.data.search.datasource.SearchDataSource | ||
import com.terning.data.search.dto.request.SearchRequestDto | ||
import com.terning.data.search.dto.response.SearchResultResponseDto | ||
|
||
class SearchPagingSource( | ||
private val query: String, | ||
private val sortBy: String, | ||
private val dataSource: SearchDataSource, | ||
) : PagingSource<Int, Pair<Int, SearchResultResponseDto.SearchAnnouncementDto>>() { | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Pair<Int, SearchResultResponseDto.SearchAnnouncementDto>> { | ||
return try { | ||
val nextParamKey = params.key ?: 0 | ||
|
||
val response = dataSource.getSearch( | ||
request = SearchRequestDto( | ||
keyword = query, | ||
sortBy = sortBy, | ||
page = nextParamKey, | ||
size = params.loadSize | ||
) | ||
) | ||
val totalCount = response.result.totalCount | ||
val hasNextPage = response.result.hasNext | ||
|
||
val nextKey = if (hasNextPage) nextParamKey + 1 else null | ||
|
||
LoadResult.Page( | ||
data = response.result.announcements.map { | ||
Pair(totalCount, it) | ||
}, | ||
prevKey = null, | ||
nextKey = nextKey | ||
) | ||
} catch (e: Exception) { | ||
LoadResult.Error(e) | ||
} | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, Pair<Int, SearchResultResponseDto.SearchAnnouncementDto>>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
val anchorPage = state.closestPageToPosition(anchorPosition) | ||
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
package com.terning.data.search.repositoryimpl | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import androidx.paging.map | ||
import com.terning.data.search.datasource.SearchDataSource | ||
import com.terning.data.search.dto.request.SearchRequestDto | ||
import com.terning.data.search.mapper.toSearchBannerList | ||
import com.terning.data.search.mapper.toSearchPopularAnnouncementList | ||
import com.terning.data.search.mapper.toSearchResultList | ||
import com.terning.data.search.pagingsource.SearchPagingSource | ||
import com.terning.domain.search.entity.SearchBanner | ||
import com.terning.domain.search.entity.SearchPopularAnnouncement | ||
import com.terning.domain.search.entity.SearchResult | ||
import com.terning.domain.search.repository.SearchRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
class SearchRepositoryImpl @Inject constructor( | ||
private val searchDataSource: SearchDataSource, | ||
) : SearchRepository { | ||
override suspend fun getSearchList( | ||
|
||
override fun getSearchList( | ||
query: String, | ||
sortBy: String, | ||
page: Int, | ||
size: Int, | ||
): Result<List<SearchResult>> { | ||
return runCatching { | ||
searchDataSource.getSearch( | ||
SearchRequestDto( | ||
keyword = query, | ||
sortBy = sortBy, | ||
page = page, | ||
size = size | ||
) | ||
).result.toSearchResultList() | ||
): Flow<PagingData<SearchResult>> { | ||
return Pager( | ||
PagingConfig(pageSize = 100) | ||
) { | ||
SearchPagingSource( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 |
||
query = query, | ||
sortBy = sortBy, | ||
dataSource = searchDataSource | ||
) | ||
}.flow.map { pagedData -> | ||
pagedData.map { it.second.toSearchResultList(it.first) } | ||
} | ||
} | ||
|
||
|
@@ -43,7 +49,7 @@ class SearchRepositoryImpl @Inject constructor( | |
} | ||
|
||
override suspend fun getSearchBannersList(): Result<List<SearchBanner>> = | ||
kotlin.runCatching { | ||
runCatching { | ||
searchDataSource.getSearchBanners().result.toSearchBannerList() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
plugins { | ||
alias(libs.plugins.terning.kotlin) | ||
} | ||
|
||
dependencies { | ||
implementation(libs.paging.common) | ||
implementation(libs.coroutines.core) | ||
implementation(libs.coroutines.test) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그리구 이 부분은 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
사소한거긴한데,, 모듈에 대한 의존성이랑 구분하기 위해서 한 줄 띄워주면 좋을 것 가타요 ㅎㅎ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그리고
implementation(libs.androidx.paging.common.android)
이건 새로 추가된 라이브러리 같은데 기존에 있었던 라이브러리로는 대체 불가능한가요...?!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 저거 추가했었는데 페이징 플로가 데이터에서 도메인을 거쳐서 UI에 넘어가기 때문에 도메인에 저 라이브러리가 필요합니다!