Skip to content

Commit

Permalink
Paginate OnboardingScreen games results
Browse files Browse the repository at this point in the history
  • Loading branch information
mr3y-the-programmer committed Oct 20, 2023
1 parent d24691c commit 5db9b92
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mr3y.ludi.shared.core.paging

import app.cash.paging.PagingSource
import app.cash.paging.PagingState
import com.mr3y.ludi.shared.core.CrashReporting
import com.mr3y.ludi.shared.core.model.Game
import com.mr3y.ludi.shared.core.network.datasources.internal.RAWGDataSource
import com.mr3y.ludi.shared.core.network.model.ApiResult
Expand All @@ -11,7 +12,8 @@ import com.mr3y.ludi.shared.core.repository.query.buildGamesFullUrl

class RAWGGamesPagingSource(
private val networkDataSource: RAWGDataSource,
private val query: GamesQueryParameters
private val query: GamesQueryParameters,
private val crashReporting: CrashReporting
) : PagingSource<Int, Game>() {

override fun getRefreshKey(state: PagingState<Int, Game>): Int? {
Expand All @@ -33,7 +35,11 @@ class RAWGGamesPagingSource(
)
}
is ApiResult.Error -> {
response.throwable?.let { LoadResult.Error(it) } ?: LoadResult.Invalid()
if (response.throwable != null) {
crashReporting.recordException(response.throwable, logMessage = "Error occurred while querying RAWG Games with query $query")
return LoadResult.Error(response.throwable)
}
return LoadResult.Invalid()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.mr3y.ludi.shared.core.repository

import app.cash.paging.PagingData
import com.mr3y.ludi.shared.core.model.Game
import com.mr3y.ludi.shared.core.model.GamesGenresPage
import com.mr3y.ludi.shared.core.model.GamesPage
import com.mr3y.ludi.shared.core.model.Result
import com.mr3y.ludi.shared.core.repository.query.GamesQueryParameters
import kotlinx.coroutines.flow.Flow

interface GamesRepository {
suspend fun queryGames(queryParameters: GamesQueryParameters): Result<GamesPage, Throwable>
fun queryGames(queryParameters: GamesQueryParameters): Flow<PagingData<Game>>

suspend fun queryGamesGenres(): Result<GamesGenresPage, Throwable>
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.mr3y.ludi.shared.core.repository.internal

import app.cash.paging.Pager
import app.cash.paging.PagingConfig
import app.cash.paging.PagingData
import com.mr3y.ludi.shared.core.CrashReporting
import com.mr3y.ludi.shared.core.model.Game
import com.mr3y.ludi.shared.core.model.GamesGenresPage
import com.mr3y.ludi.shared.core.model.GamesPage
import com.mr3y.ludi.shared.core.model.Result
import com.mr3y.ludi.shared.core.model.toCoreErrorResult
import com.mr3y.ludi.shared.core.network.datasources.internal.RAWGDataSource
import com.mr3y.ludi.shared.core.network.model.ApiResult
import com.mr3y.ludi.shared.core.network.model.toGamesGenresPage
import com.mr3y.ludi.shared.core.network.model.toGamesPage
import com.mr3y.ludi.shared.core.paging.RAWGGamesPagingSource
import com.mr3y.ludi.shared.core.repository.GamesRepository
import com.mr3y.ludi.shared.core.repository.query.GamesQueryParameters
import com.mr3y.ludi.shared.core.repository.query.buildGamesFullUrl
import kotlinx.coroutines.flow.Flow
import me.tatarka.inject.annotations.Inject

@Inject
Expand All @@ -20,20 +23,14 @@ class DefaultGamesRepository(
private val crashReporting: CrashReporting
) : GamesRepository {

override suspend fun queryGames(queryParameters: GamesQueryParameters): Result<GamesPage, Throwable> {
val fullUrl = buildGamesFullUrl(endpointUrl = "$RAWGApiBaseUrl/games", queryParameters)
return when (val result = rawgDataSource.queryGames(fullUrl)) {
is ApiResult.Success -> {
Result.Success(result.data.toGamesPage())
}
is ApiResult.Error -> {
val errorResult = result.toCoreErrorResult()
if (errorResult.exception != null) {
crashReporting.recordException(errorResult.exception, logMessage = "Error occurred while querying RAWG Games with query $queryParameters")
}
errorResult
}
}
override fun queryGames(queryParameters: GamesQueryParameters): Flow<PagingData<Game>> {
return Pager(DefaultPagingConfig) {
RAWGGamesPagingSource(
rawgDataSource,
queryParameters,
crashReporting
)
}.flow
}

override suspend fun queryGamesGenres(): Result<GamesGenresPage, Throwable> {
Expand All @@ -52,6 +49,7 @@ class DefaultGamesRepository(
}

companion object {
private val DefaultPagingConfig = PagingConfig(pageSize = 20, initialLoadSize = 20)
private const val RAWGApiBaseUrl = "https://api.rawg.io/api"
}
}
Loading

0 comments on commit 5db9b92

Please sign in to comment.