-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
174 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
data/src/main/java/com/everymeal/data/datasource/search/SearchDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.everymeal.data.datasource.search | ||
|
||
import androidx.paging.PagingData | ||
import com.everymeal.data.model.restaruant.RestaurantResponse | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface SearchDataSource { | ||
|
||
suspend fun searchRestraurant( | ||
campusIdx: Int, | ||
keyword: String, | ||
): Flow<PagingData<RestaurantResponse>> | ||
|
||
|
||
} |
35 changes: 35 additions & 0 deletions
35
data/src/main/java/com/everymeal/data/datasource/search/SearchDataSourceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.everymeal.data.datasource.search | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import com.everymeal.data.model.restaruant.RestaurantResponse | ||
import com.everymeal.data.service.search.SearchService | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
|
||
class SearchDataSourceImpl @Inject constructor( | ||
private val searchService: SearchService, | ||
) : SearchDataSource { | ||
override suspend fun searchRestraurant( | ||
campusIdx: Int, | ||
keyword: String, | ||
): Flow<PagingData<RestaurantResponse>> { | ||
val pagingSourceFactory = { | ||
SearchPagingSource( | ||
searchService = searchService, | ||
campusIdx = campusIdx, | ||
keyword = keyword | ||
) | ||
} | ||
return Pager( | ||
config = PagingConfig( | ||
initialLoadSize = 20, | ||
pageSize = 20, | ||
enablePlaceholders = false, | ||
), | ||
pagingSourceFactory = pagingSourceFactory | ||
).flow | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
data/src/main/java/com/everymeal/data/datasource/search/SearchPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.everymeal.data.datasource.search | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.everymeal.data.datasource.restaurant.PAGING_SIZE | ||
import com.everymeal.data.datasource.restaurant.STARTING_PAGE_INDEX | ||
import com.everymeal.data.model.restaruant.RestaurantResponse | ||
import com.everymeal.data.service.search.SearchService | ||
import retrofit2.HttpException | ||
import java.io.IOException | ||
|
||
class SearchPagingSource( | ||
private val searchService: SearchService, | ||
private val campusIdx: Int, | ||
private val keyword: String, | ||
) : PagingSource<Int, RestaurantResponse>() { | ||
override fun getRefreshKey(state: PagingState<Int, RestaurantResponse>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1) | ||
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1) | ||
} | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, RestaurantResponse> { | ||
val position = params.key ?: STARTING_PAGE_INDEX | ||
|
||
return try { | ||
val response = searchService.search( | ||
campusIdx = campusIdx, | ||
keyword = keyword, | ||
offset = position, | ||
limit = PAGING_SIZE | ||
) | ||
val restaurants = response.data?.content ?: emptyList() | ||
LoadResult.Page( | ||
data = restaurants, | ||
prevKey = if (position == STARTING_PAGE_INDEX) null else position - 1, | ||
nextKey = if (restaurants.isEmpty()) null else position + 1 | ||
) | ||
} catch (exception: IOException) { | ||
LoadResult.Error(exception) | ||
} catch (exception: HttpException) { | ||
LoadResult.Error(exception) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 18 additions & 6 deletions
24
data/src/main/java/com/everymeal/data/repository/search/DefaultSearchRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,29 @@ | ||
package com.everymeal.data.repository.search | ||
|
||
import com.everymeal.data.model.restaruant.toRestaurants | ||
import com.everymeal.data.service.search.SearchService | ||
import androidx.paging.PagingData | ||
import androidx.paging.map | ||
import com.everymeal.data.datasource.search.SearchDataSourceImpl | ||
import com.everymeal.data.model.restaruant.toRestaurant | ||
import com.everymeal.domain.model.restaurant.RestaurantDataEntity | ||
import com.everymeal.domain.repository.search.SearchRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
class DefaultSearchRepository @Inject constructor( | ||
private val searchService: SearchService, | ||
private val searchDataSourceImpl: SearchDataSourceImpl, | ||
) : SearchRepository { | ||
override suspend fun search(keyword: String): Result<List<RestaurantDataEntity>> { | ||
return runCatching { | ||
searchService.search(keyword).toRestaurants() | ||
override suspend fun search( | ||
campusIdx: Int, | ||
keyword: String | ||
): Flow<PagingData<RestaurantDataEntity>> { | ||
return searchDataSourceImpl.searchRestraurant( | ||
campusIdx = campusIdx, | ||
keyword = keyword, | ||
).map { pagingData -> | ||
pagingData.map { | ||
it.toRestaurant() | ||
} | ||
} | ||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
data/src/main/java/com/everymeal/data/service/search/SearchService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
package com.everymeal.data.service.search | ||
|
||
import com.everymeal.data.model.BaseResponse | ||
import com.everymeal.data.model.restaruant.SearchRestaurantResponse | ||
import retrofit2.http.GET | ||
import retrofit2.http.Path | ||
import retrofit2.http.Query | ||
|
||
interface SearchService { | ||
// TODO 임시 캠퍼스 ID | ||
@GET("/api/v1/stores/{0}/{keyword}") | ||
@GET("/api/v1/stores/{campusIdx}/{keyword}") | ||
suspend fun search( | ||
@Path("campusIdx") campusIdx: Int, | ||
@Path("keyword") keyword: String, | ||
@Query("offset") offset: Int?, | ||
@Query("limit") limit: Int?, | ||
): SearchRestaurantResponse | ||
} |
4 changes: 3 additions & 1 deletion
4
domain/src/main/java/com/everymeal/domain/repository/search/SearchRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package com.everymeal.domain.repository.search | ||
|
||
import androidx.paging.PagingData | ||
import com.everymeal.domain.model.restaurant.RestaurantDataEntity | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface SearchRepository { | ||
suspend fun search(keyword: String): Result<List<RestaurantDataEntity>> | ||
suspend fun search(campusIdx: Int, keyword: String): Flow<PagingData<RestaurantDataEntity>> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.