Skip to content

Commit

Permalink
Moving and renaming Pagination to Page
Browse files Browse the repository at this point in the history
  • Loading branch information
diegocarloslima committed Jan 9, 2024
1 parent dcf05e6 commit c15bd13
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

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)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,6 +29,6 @@ interface GitHubRepositoryManagerNetwork {
suspend fun searchRepositories(
query: String,
sortOrder: SortOrder,
pagination: Pagination,
page: Page,
): RepositoryResultsNetwork
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ 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
import com.diegocarloslima.gitcollection.core.network.github.apollo.model.mapToNetwork
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

/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@

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
import com.diegocarloslima.gitcollection.core.network.github.retrofit.model.mapToNetwork
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

/**
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
1 change: 1 addition & 0 deletions data/project/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ android {
}

dependencies {
implementation(project(":core:common"))
implementation(project(":core:network"))

implementation(libs.androidx.paging.runtime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -31,7 +31,12 @@ internal class ProjectPagingSource(

override suspend fun load(params: LoadParams<String>): LoadResult<String, Project> {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}

Expand Down

0 comments on commit c15bd13

Please sign in to comment.