Skip to content

Commit 12c9451

Browse files
committed
Add photos searching
1 parent c5d90c5 commit 12c9451

20 files changed

+403
-87
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package st.slex.csplashscreen.data.model.remote.image
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class RemoteImageSearchModel(
6+
val total: Int?,
7+
val total_pages: Int?,
8+
@SerializedName("results")
9+
val results: List<RemoteImageModel>
10+
)

app/src/main/java/st/slex/csplashscreen/data/photos/PhotosPagingSource.kt

+12-18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import st.slex.csplashscreen.utiles.API_KEY
1212

1313
class PhotosPagingSource @AssistedInject constructor(
1414
private val service: PhotosService,
15-
@Assisted("query") private val query: List<String>
15+
@Assisted val query: QueryPhotos
1616
) : PagingSource<Int, ImageModel>() {
1717

1818
override fun getRefreshKey(state: PagingState<Int, ImageModel>): Int? {
@@ -22,30 +22,24 @@ class PhotosPagingSource @AssistedInject constructor(
2222
}
2323

2424
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ImageModel> {
25-
if (query.isEmpty()) {
26-
return LoadResult.Page(emptyList(), prevKey = null, nextKey = null)
27-
}
2825
try {
2926
val pageNumber = params.key ?: INITIAL_PAGE_NUMBER
3027
val pageSize = params.loadSize
3128

32-
val response = if (query.size > 1) {
33-
service.getPhotos(
34-
query[0],
35-
query[1],
36-
query[2],
37-
pageNumber,
38-
pageSize,
39-
API_KEY
40-
)
41-
} else {
42-
service.getPhotos(query[0], pageNumber, pageSize, API_KEY)
29+
val response = when (query) {
30+
is QueryPhotos.AllPhotos ->
31+
service.getPhotos(pageNumber, pageSize, API_KEY)
32+
is QueryPhotos.CollectionPhotos ->
33+
service.getPhotos(query.query, pageNumber, pageSize, API_KEY)
34+
is QueryPhotos.EmptyQuery -> null
4335
}
4436

45-
return if (response.isSuccessful) {
37+
return if (response?.body() != null && response.isSuccessful) {
38+
4639
val photos = response.body()!!.map {
4740
it.toImageModel()
4841
}
42+
4943
val nextPageNumber = if (photos.isEmpty()) null else pageNumber + 1
5044
val prevPageNumber = if (pageNumber > 1) pageNumber - 1 else null
5145
LoadResult.Page(photos, prevPageNumber, nextPageNumber)
@@ -61,10 +55,10 @@ class PhotosPagingSource @AssistedInject constructor(
6155

6256
@AssistedFactory
6357
interface Factory {
64-
fun create(@Assisted("query") query: List<String>): PhotosPagingSource
58+
fun create(@Assisted query: QueryPhotos): PhotosPagingSource
6559
}
6660

6761
companion object {
68-
const val INITIAL_PAGE_NUMBER = 0
62+
const val INITIAL_PAGE_NUMBER = 1
6963
}
7064
}

app/src/main/java/st/slex/csplashscreen/data/photos/PhotosRepository.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import javax.inject.Inject
77

88
interface PhotosRepository {
99

10-
fun queryAll(query: List<String>): PagingSource<Int, ImageModel>
10+
fun queryAll(query: QueryPhotos): PagingSource<Int, ImageModel>
1111

1212
class Base @Inject constructor(
1313
private val photosPagingSourceFactory: PhotosPagingSource.Factory
1414
) : PhotosRepository {
1515

16-
override fun queryAll(query: List<String>): PagingSource<Int, ImageModel> =
16+
override fun queryAll(query: QueryPhotos): PagingSource<Int, ImageModel> =
1717
photosPagingSourceFactory.create(query)
1818
}
1919
}

app/src/main/java/st/slex/csplashscreen/data/photos/PhotosService.kt

+4-9
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@ import retrofit2.http.GET
55
import retrofit2.http.Path
66
import retrofit2.http.Query
77
import st.slex.csplashscreen.data.model.remote.image.RemoteImageModel
8-
import st.slex.csplashscreen.utiles.QUERY_API_KEY
9-
import st.slex.csplashscreen.utiles.QUERY_PAGE
10-
import st.slex.csplashscreen.utiles.QUERY_PAGE_SIZE
8+
import st.slex.csplashscreen.utiles.*
119

1210
interface PhotosService {
1311

14-
@GET("{query1}/{query2}/{query3}")
12+
@GET("$GET_COLLECTIONS/{query}/$GET_PHOTOS")
1513
suspend fun getPhotos(
16-
@Path("query1") query1: String,
17-
@Path("query2") query2: String,
18-
@Path("query3") query3: String,
14+
@Path("query") query: String,
1915
@Query(QUERY_PAGE) page: Int,
2016
@Query(QUERY_PAGE_SIZE) page_size: Int,
2117
@Query(QUERY_API_KEY) api_key: String
2218
): Response<List<RemoteImageModel>>
2319

24-
@GET("{q}")
20+
@GET(GET_PHOTOS)
2521
suspend fun getPhotos(
26-
@Path("q") query: String,
2722
@Query(QUERY_PAGE) page: Int,
2823
@Query(QUERY_PAGE_SIZE) page_size: Int,
2924
@Query(QUERY_API_KEY) api_key: String
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package st.slex.csplashscreen.data.photos
2+
3+
sealed class QueryPhotos {
4+
class CollectionPhotos(val query: String) : QueryPhotos()
5+
object AllPhotos : QueryPhotos()
6+
object EmptyQuery : QueryPhotos()
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package st.slex.csplashscreen.data.search
2+
3+
sealed class QuerySearch {
4+
class SearchPhotos(val text: String) : QuerySearch()
5+
object EmptyQuery : QuerySearch()
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package st.slex.csplashscreen.data.search
2+
3+
import androidx.paging.PagingSource
4+
import androidx.paging.PagingState
5+
import dagger.assisted.Assisted
6+
import dagger.assisted.AssistedFactory
7+
import dagger.assisted.AssistedInject
8+
import retrofit2.HttpException
9+
import st.slex.csplashscreen.data.core.toImageModel
10+
import st.slex.csplashscreen.data.model.ui.image.ImageModel
11+
import st.slex.csplashscreen.utiles.API_KEY
12+
13+
class SearchPagingSource @AssistedInject constructor(
14+
private val service: SearchService,
15+
@Assisted val query: QuerySearch
16+
) : PagingSource<Int, ImageModel>() {
17+
18+
override fun getRefreshKey(state: PagingState<Int, ImageModel>): Int? {
19+
val anchorPosition = state.anchorPosition ?: return null
20+
val anchorPage = state.closestPageToPosition(anchorPosition) ?: return null
21+
return anchorPage.prevKey?.plus(1) ?: anchorPage.nextKey?.minus(1)
22+
}
23+
24+
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ImageModel> {
25+
try {
26+
val pageNumber = params.key ?: INITIAL_PAGE_NUMBER
27+
val pageSize = params.loadSize
28+
29+
val response = when (query) {
30+
is QuerySearch.SearchPhotos ->
31+
service.searchPhoto(query.text, pageNumber, pageSize, API_KEY)
32+
is QuerySearch.EmptyQuery -> null
33+
}
34+
35+
return if (response?.body() != null && response.isSuccessful) {
36+
37+
val photos = response.body()!!.results.map {
38+
it.toImageModel()
39+
}
40+
41+
val nextPageNumber = if (photos.isEmpty()) null else pageNumber + 1
42+
val prevPageNumber = if (pageNumber > 1) pageNumber - 1 else null
43+
LoadResult.Page(photos, prevPageNumber, nextPageNumber)
44+
} else {
45+
LoadResult.Error(HttpException(response))
46+
}
47+
} catch (e: HttpException) {
48+
return LoadResult.Error(e)
49+
} catch (e: Exception) {
50+
return LoadResult.Error(e)
51+
}
52+
}
53+
54+
@AssistedFactory
55+
interface Factory {
56+
fun create(@Assisted query: QuerySearch): SearchPagingSource
57+
}
58+
59+
companion object {
60+
const val INITIAL_PAGE_NUMBER = 1
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package st.slex.csplashscreen.data.search
2+
3+
import androidx.paging.PagingSource
4+
import st.slex.csplashscreen.data.model.ui.image.ImageModel
5+
import javax.inject.Inject
6+
7+
interface SearchRepository {
8+
9+
fun queryAll(query: QuerySearch): PagingSource<Int, ImageModel>
10+
11+
class Base @Inject constructor(
12+
private val searchPagingSource: SearchPagingSource.Factory
13+
) : SearchRepository {
14+
15+
override fun queryAll(query: QuerySearch): PagingSource<Int, ImageModel> =
16+
searchPagingSource.create(query)
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package st.slex.csplashscreen.data.search
2+
3+
import retrofit2.Response
4+
import retrofit2.http.GET
5+
import retrofit2.http.Query
6+
import st.slex.csplashscreen.data.model.remote.image.RemoteImageSearchModel
7+
import st.slex.csplashscreen.utiles.*
8+
9+
10+
interface SearchService {
11+
12+
@GET("/$GET_SEARCH/$GET_PHOTOS")
13+
suspend fun searchPhoto(
14+
@Query(QUERY) query: String,
15+
@Query(QUERY_PAGE) page: Int,
16+
@Query(QUERY_PAGE_SIZE) page_size: Int,
17+
@Query(QUERY_API_KEY) api_key: String
18+
): Response<RemoteImageSearchModel>
19+
}

app/src/main/java/st/slex/csplashscreen/di/module/NetworkServiceModule.kt

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import retrofit2.Retrofit
66
import st.slex.csplashscreen.data.collections.CollectionService
77
import st.slex.csplashscreen.data.photo.PhotoService
88
import st.slex.csplashscreen.data.photos.PhotosService
9+
import st.slex.csplashscreen.data.search.SearchService
910

1011
@Module(includes = [RetrofitModule::class])
1112
class NetworkServiceModule {
@@ -21,4 +22,8 @@ class NetworkServiceModule {
2122
@Provides
2223
fun providesPhotoService(retrofit: Retrofit): PhotoService =
2324
retrofit.create(PhotoService::class.java)
25+
26+
@Provides
27+
fun providesPhotoSearchService(retrofit: Retrofit): SearchService =
28+
retrofit.create(SearchService::class.java)
2429
}

app/src/main/java/st/slex/csplashscreen/di/module/RepositoryModule.kt

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
66
import st.slex.csplashscreen.data.collections.CollectionsRepository
77
import st.slex.csplashscreen.data.photo.PhotoRepository
88
import st.slex.csplashscreen.data.photos.PhotosRepository
9+
import st.slex.csplashscreen.data.search.SearchRepository
910

1011
@Module
1112
interface RepositoryModule {
@@ -19,4 +20,8 @@ interface RepositoryModule {
1920
@ExperimentalCoroutinesApi
2021
@Binds
2122
fun bindsPhotoRepository(repository: PhotoRepository.Base): PhotoRepository
23+
24+
@ExperimentalCoroutinesApi
25+
@Binds
26+
fun bindsPhotoSearchRepository(repository: SearchRepository.Base): SearchRepository
2227
}

app/src/main/java/st/slex/csplashscreen/ui/MainActivity.kt

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import st.slex.csplashscreen.ui.collection.Collection
2323
import st.slex.csplashscreen.ui.detail.ImageDetailScreen
2424
import st.slex.csplashscreen.ui.main.MainScreen
2525
import st.slex.csplashscreen.ui.raw_image.RawImageScreen
26+
import st.slex.csplashscreen.ui.search_photos.SearchPhotosScreen
2627
import st.slex.csplashscreen.ui.theme.CSplashScreenTheme
2728
import javax.inject.Inject
2829

@@ -101,5 +102,18 @@ fun NavigationComponent(navController: NavHostController, viewModel: MainViewMod
101102
) {
102103
RawImageScreen(url = it.arguments?.getString("url").toString(), navController)
103104
}
105+
106+
composable(
107+
route = "search_photos/{query}",
108+
arguments = listOf(navArgument("query") { type = NavType.StringType })
109+
) {
110+
var query = it.arguments?.getString("query").toString()
111+
if (query == " ") query = ""
112+
SearchPhotosScreen(
113+
viewModel = viewModel,
114+
navController = navController,
115+
querySearch = query
116+
)
117+
}
104118
}
105119
}

0 commit comments

Comments
 (0)