From c15bd13180af8ec1a009995269b1fb71229e0713 Mon Sep 17 00:00:00 2001 From: Diego Lima Date: Tue, 9 Jan 2024 18:13:18 -0300 Subject: [PATCH] Moving and renaming Pagination to Page --- .../gitcollection/core/common/paging/Page.kt} | 6 +++--- .../network/github/GitHubRepositoryManagerNetwork.kt | 4 ++-- .../github/apollo/GitHubRepositoryManagerApollo.kt | 8 ++++---- .../retrofit/GitHubRepositoryManagerRetrofit.kt | 12 ++++++------ .../github/retrofit/model/RetrofitModelExtensions.kt | 4 ++-- data/project/build.gradle.kts | 1 + .../gitcollection/data/project/ProjectDataSource.kt | 4 ++-- .../data/project/paging/ProjectPagingSource.kt | 9 +++++++-- .../project/remote/GitHubProjectDataSourceNetwork.kt | 6 +++--- 9 files changed, 30 insertions(+), 24 deletions(-) rename core/{network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/model/Pagination.kt => common/src/main/kotlin/com/diegocarloslima/gitcollection/core/common/paging/Page.kt} (79%) diff --git a/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/model/Pagination.kt b/core/common/src/main/kotlin/com/diegocarloslima/gitcollection/core/common/paging/Page.kt similarity index 79% rename from core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/model/Pagination.kt rename to core/common/src/main/kotlin/com/diegocarloslima/gitcollection/core/common/paging/Page.kt index c727ed6..c395da8 100644 --- a/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/model/Pagination.kt +++ b/core/common/src/main/kotlin/com/diegocarloslima/gitcollection/core/common/paging/Page.kt @@ -16,9 +16,9 @@ * with this program. If not, see . */ -package com.diegocarloslima.gitcollection.core.network.model +package com.diegocarloslima.gitcollection.core.common.paging /** - * This class holds information about pagination. + * This class holds information about a page in the context of data pagination. */ -data class Pagination(val size: Int, val key: String? = null) +data class Page(val size: Int, val key: String? = null) diff --git a/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/GitHubRepositoryManagerNetwork.kt b/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/GitHubRepositoryManagerNetwork.kt index 10f69a0..184c15f 100644 --- a/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/GitHubRepositoryManagerNetwork.kt +++ b/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/GitHubRepositoryManagerNetwork.kt @@ -18,9 +18,9 @@ package com.diegocarloslima.gitcollection.core.network.github +import com.diegocarloslima.gitcollection.core.common.paging.Page import com.diegocarloslima.gitcollection.core.network.github.model.RepositoryResultsNetwork import com.diegocarloslima.gitcollection.core.network.github.model.SortOrder -import com.diegocarloslima.gitcollection.core.network.model.Pagination /** * Interface that establishes a contract for managing GitHub repositories through the network. @@ -29,6 +29,6 @@ interface GitHubRepositoryManagerNetwork { suspend fun searchRepositories( query: String, sortOrder: SortOrder, - pagination: Pagination, + page: Page, ): RepositoryResultsNetwork } diff --git a/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/apollo/GitHubRepositoryManagerApollo.kt b/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/apollo/GitHubRepositoryManagerApollo.kt index d0fd73b..6b30917 100644 --- a/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/apollo/GitHubRepositoryManagerApollo.kt +++ b/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/apollo/GitHubRepositoryManagerApollo.kt @@ -20,6 +20,7 @@ package com.diegocarloslima.gitcollection.core.network.github.apollo import com.apollographql.apollo3.ApolloClient import com.apollographql.apollo3.api.Optional +import com.diegocarloslima.gitcollection.core.common.paging.Page import com.diegocarloslima.gitcollection.core.network.di.GitHub import com.diegocarloslima.gitcollection.core.network.github.GitHubRepositoryManagerNetwork import com.diegocarloslima.gitcollection.core.network.github.GitHubSearchRepositoriesQuery @@ -27,7 +28,6 @@ import com.diegocarloslima.gitcollection.core.network.github.apollo.model.mapToN import com.diegocarloslima.gitcollection.core.network.github.apollo.model.queryValue import com.diegocarloslima.gitcollection.core.network.github.model.RepositoryResultsNetwork import com.diegocarloslima.gitcollection.core.network.github.model.SortOrder -import com.diegocarloslima.gitcollection.core.network.model.Pagination import javax.inject.Inject /** @@ -39,12 +39,12 @@ internal class GitHubRepositoryManagerApollo @Inject constructor( override suspend fun searchRepositories( query: String, sortOrder: SortOrder, - pagination: Pagination, + page: Page, ): RepositoryResultsNetwork { val fullQuery = "$query ${sortOrder.queryValue}" val searchRepositories = GitHubSearchRepositoriesQuery( - Optional.presentIfNotNull(pagination.key), - pagination.size, + Optional.presentIfNotNull(page.key), + page.size, fullQuery, ) val data = client.query(searchRepositories).execute().dataAssertNoErrors diff --git a/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/retrofit/GitHubRepositoryManagerRetrofit.kt b/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/retrofit/GitHubRepositoryManagerRetrofit.kt index dfbc00e..e32dc66 100644 --- a/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/retrofit/GitHubRepositoryManagerRetrofit.kt +++ b/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/retrofit/GitHubRepositoryManagerRetrofit.kt @@ -18,6 +18,7 @@ package com.diegocarloslima.gitcollection.core.network.github.retrofit +import com.diegocarloslima.gitcollection.core.common.paging.Page import com.diegocarloslima.gitcollection.core.network.github.GitHubRepositoryManagerNetwork import com.diegocarloslima.gitcollection.core.network.github.model.RepositoryResultsNetwork import com.diegocarloslima.gitcollection.core.network.github.model.SortOrder @@ -25,7 +26,6 @@ import com.diegocarloslima.gitcollection.core.network.github.retrofit.model.mapT import com.diegocarloslima.gitcollection.core.network.github.retrofit.model.orderValue import com.diegocarloslima.gitcollection.core.network.github.retrofit.model.pageNumber import com.diegocarloslima.gitcollection.core.network.github.retrofit.model.sortValue -import com.diegocarloslima.gitcollection.core.network.model.Pagination import javax.inject.Inject /** @@ -37,18 +37,18 @@ internal class GitHubRepositoryManagerRetrofit @Inject constructor( override suspend fun searchRepositories( query: String, sortOrder: SortOrder, - pagination: Pagination, + page: Page, ): RepositoryResultsNetwork { val result = service.searchRepositories( query, sortOrder.sortValue, sortOrder.orderValue, - pagination.size, - pagination.pageNumber, + page.size, + page.pageNumber, ) - val nextPageNumber = if (result.totalCount > pagination.pageNumber * pagination.size) { - pagination.pageNumber.inc() + val nextPageNumber = if (result.totalCount > page.pageNumber * page.size) { + page.pageNumber.inc() } else { null } diff --git a/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/retrofit/model/RetrofitModelExtensions.kt b/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/retrofit/model/RetrofitModelExtensions.kt index 5503ec8..2e985fe 100644 --- a/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/retrofit/model/RetrofitModelExtensions.kt +++ b/core/network/src/main/kotlin/com/diegocarloslima/gitcollection/core/network/github/retrofit/model/RetrofitModelExtensions.kt @@ -18,9 +18,9 @@ package com.diegocarloslima.gitcollection.core.network.github.retrofit.model +import com.diegocarloslima.gitcollection.core.common.paging.Page import com.diegocarloslima.gitcollection.core.network.github.model.SortOrder import com.diegocarloslima.gitcollection.core.network.github.model.SortOrder.STARS_DESC -import com.diegocarloslima.gitcollection.core.network.model.Pagination internal val SortOrder.sortValue: String get() = when (this) { @@ -32,5 +32,5 @@ internal val SortOrder.orderValue: String STARS_DESC -> "desc" } -internal val Pagination.pageNumber: Int +internal val Page.pageNumber: Int get() = this.key?.toIntOrNull() ?: 1 diff --git a/data/project/build.gradle.kts b/data/project/build.gradle.kts index 33ad32d..6f2cbec 100644 --- a/data/project/build.gradle.kts +++ b/data/project/build.gradle.kts @@ -26,6 +26,7 @@ android { } dependencies { + implementation(project(":core:common")) implementation(project(":core:network")) implementation(libs.androidx.paging.runtime) diff --git a/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/ProjectDataSource.kt b/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/ProjectDataSource.kt index 622c06e..b59f4f5 100644 --- a/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/ProjectDataSource.kt +++ b/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/ProjectDataSource.kt @@ -18,12 +18,12 @@ package com.diegocarloslima.gitcollection.data.project -import com.diegocarloslima.gitcollection.core.network.model.Pagination +import com.diegocarloslima.gitcollection.core.common.paging.Page import com.diegocarloslima.gitcollection.data.project.model.ProjectPage /** * Interface that establishes a contract for a git repository projects data source. */ internal interface ProjectDataSource { - suspend fun getPopularProjects(pagination: Pagination): ProjectPage + suspend fun getPopularProjects(page: Page): ProjectPage } diff --git a/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/paging/ProjectPagingSource.kt b/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/paging/ProjectPagingSource.kt index ddd929e..ea6b36b 100644 --- a/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/paging/ProjectPagingSource.kt +++ b/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/paging/ProjectPagingSource.kt @@ -21,7 +21,7 @@ package com.diegocarloslima.gitcollection.data.project.paging import android.util.Log import androidx.paging.PagingSource import androidx.paging.PagingState -import com.diegocarloslima.gitcollection.core.network.model.Pagination +import com.diegocarloslima.gitcollection.core.common.paging.Page import com.diegocarloslima.gitcollection.data.project.ProjectDataSourceRemote import com.diegocarloslima.gitcollection.data.project.model.Project @@ -31,7 +31,12 @@ internal class ProjectPagingSource( override suspend fun load(params: LoadParams): LoadResult { return try { - val projectPage = remoteDataSource.getPopularProjects(Pagination(PAGE_SIZE, params.key)) + val projectPage = remoteDataSource.getPopularProjects( + com.diegocarloslima.gitcollection.core.common.paging.Page( + PAGE_SIZE, + params.key, + ), + ) LoadResult.Page( data = projectPage.projects, prevKey = params.key, diff --git a/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/remote/GitHubProjectDataSourceNetwork.kt b/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/remote/GitHubProjectDataSourceNetwork.kt index 6d3a1a8..c4f235d 100644 --- a/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/remote/GitHubProjectDataSourceNetwork.kt +++ b/data/project/src/main/kotlin/com/diegocarloslima/gitcollection/data/project/remote/GitHubProjectDataSourceNetwork.kt @@ -18,9 +18,9 @@ package com.diegocarloslima.gitcollection.data.project.remote +import com.diegocarloslima.gitcollection.core.common.paging.Page import com.diegocarloslima.gitcollection.core.network.github.GitHubRepositoryManagerNetwork import com.diegocarloslima.gitcollection.core.network.github.model.SortOrder.STARS_DESC -import com.diegocarloslima.gitcollection.core.network.model.Pagination import com.diegocarloslima.gitcollection.data.project.ProjectDataSourceRemote import com.diegocarloslima.gitcollection.data.project.model.ProjectPage import com.diegocarloslima.gitcollection.data.project.model.mapToProjectPage @@ -32,11 +32,11 @@ import javax.inject.Inject internal class GitHubProjectDataSourceNetwork @Inject constructor( private val repositoryManager: GitHubRepositoryManagerNetwork, ) : ProjectDataSourceRemote { - override suspend fun getPopularProjects(pagination: Pagination): ProjectPage = + override suspend fun getPopularProjects(page: Page): ProjectPage = repositoryManager.searchRepositories( QUERY_ANDROID, STARS_DESC, - pagination, + page, ).mapToProjectPage() }