-
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
20 changed files
with
403 additions
and
87 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
app/src/main/java/st/slex/csplashscreen/data/model/remote/image/RemoteImageSearchModel.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,10 @@ | ||
package st.slex.csplashscreen.data.model.remote.image | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class RemoteImageSearchModel( | ||
val total: Int?, | ||
val total_pages: Int?, | ||
@SerializedName("results") | ||
val results: List<RemoteImageModel> | ||
) |
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
7 changes: 7 additions & 0 deletions
7
app/src/main/java/st/slex/csplashscreen/data/photos/QueryPhotos.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,7 @@ | ||
package st.slex.csplashscreen.data.photos | ||
|
||
sealed class QueryPhotos { | ||
class CollectionPhotos(val query: String) : QueryPhotos() | ||
object AllPhotos : QueryPhotos() | ||
object EmptyQuery : QueryPhotos() | ||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/st/slex/csplashscreen/data/search/QuerySearch.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,6 @@ | ||
package st.slex.csplashscreen.data.search | ||
|
||
sealed class QuerySearch { | ||
class SearchPhotos(val text: String) : QuerySearch() | ||
object EmptyQuery : QuerySearch() | ||
} |
62 changes: 62 additions & 0 deletions
62
app/src/main/java/st/slex/csplashscreen/data/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,62 @@ | ||
package st.slex.csplashscreen.data.search | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import dagger.assisted.Assisted | ||
import dagger.assisted.AssistedFactory | ||
import dagger.assisted.AssistedInject | ||
import retrofit2.HttpException | ||
import st.slex.csplashscreen.data.core.toImageModel | ||
import st.slex.csplashscreen.data.model.ui.image.ImageModel | ||
import st.slex.csplashscreen.utiles.API_KEY | ||
|
||
class SearchPagingSource @AssistedInject constructor( | ||
private val service: SearchService, | ||
@Assisted val query: QuerySearch | ||
) : PagingSource<Int, ImageModel>() { | ||
|
||
override fun getRefreshKey(state: PagingState<Int, ImageModel>): Int? { | ||
val anchorPosition = state.anchorPosition ?: return null | ||
val anchorPage = state.closestPageToPosition(anchorPosition) ?: return null | ||
return anchorPage.prevKey?.plus(1) ?: anchorPage.nextKey?.minus(1) | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ImageModel> { | ||
try { | ||
val pageNumber = params.key ?: INITIAL_PAGE_NUMBER | ||
val pageSize = params.loadSize | ||
|
||
val response = when (query) { | ||
is QuerySearch.SearchPhotos -> | ||
service.searchPhoto(query.text, pageNumber, pageSize, API_KEY) | ||
is QuerySearch.EmptyQuery -> null | ||
} | ||
|
||
return if (response?.body() != null && response.isSuccessful) { | ||
|
||
val photos = response.body()!!.results.map { | ||
it.toImageModel() | ||
} | ||
|
||
val nextPageNumber = if (photos.isEmpty()) null else pageNumber + 1 | ||
val prevPageNumber = if (pageNumber > 1) pageNumber - 1 else null | ||
LoadResult.Page(photos, prevPageNumber, nextPageNumber) | ||
} else { | ||
LoadResult.Error(HttpException(response)) | ||
} | ||
} catch (e: HttpException) { | ||
return LoadResult.Error(e) | ||
} catch (e: Exception) { | ||
return LoadResult.Error(e) | ||
} | ||
} | ||
|
||
@AssistedFactory | ||
interface Factory { | ||
fun create(@Assisted query: QuerySearch): SearchPagingSource | ||
} | ||
|
||
companion object { | ||
const val INITIAL_PAGE_NUMBER = 1 | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/st/slex/csplashscreen/data/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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package st.slex.csplashscreen.data.search | ||
|
||
import androidx.paging.PagingSource | ||
import st.slex.csplashscreen.data.model.ui.image.ImageModel | ||
import javax.inject.Inject | ||
|
||
interface SearchRepository { | ||
|
||
fun queryAll(query: QuerySearch): PagingSource<Int, ImageModel> | ||
|
||
class Base @Inject constructor( | ||
private val searchPagingSource: SearchPagingSource.Factory | ||
) : SearchRepository { | ||
|
||
override fun queryAll(query: QuerySearch): PagingSource<Int, ImageModel> = | ||
searchPagingSource.create(query) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/st/slex/csplashscreen/data/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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package st.slex.csplashscreen.data.search | ||
|
||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
import retrofit2.http.Query | ||
import st.slex.csplashscreen.data.model.remote.image.RemoteImageSearchModel | ||
import st.slex.csplashscreen.utiles.* | ||
|
||
|
||
interface SearchService { | ||
|
||
@GET("/$GET_SEARCH/$GET_PHOTOS") | ||
suspend fun searchPhoto( | ||
@Query(QUERY) query: String, | ||
@Query(QUERY_PAGE) page: Int, | ||
@Query(QUERY_PAGE_SIZE) page_size: Int, | ||
@Query(QUERY_API_KEY) api_key: String | ||
): Response<RemoteImageSearchModel> | ||
} |
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.