Skip to content

Commit

Permalink
feat: update api call to releases (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
kikin81 authored May 20, 2024
1 parent bbfb825 commit 6c11e22
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 86 deletions.
3 changes: 3 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android {

defaultConfig {
applicationId = "us.kikin.android.gamingbacklog"
minSdk = 24
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package us.kikin.android.gamingbacklog

import android.app.Application
import coil.ImageLoader
import coil.ImageLoaderFactory
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class BacklogApplication : Application()
class BacklogApplication : Application(), ImageLoaderFactory {
override fun newImageLoader(): ImageLoader {
return ImageLoader.Builder(this)
.crossfade(true)
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ class MainViewModel

init {
appEntryUseCases.readAppEntry().onEach { shouldStartFromHomeScreen ->
startDestination = if (shouldStartFromHomeScreen) {
Route.GamesNavigation.route
} else {
Route.AppStartNavigation.route
}
startDestination =
if (shouldStartFromHomeScreen) {
Route.GamesNavigation.route
} else {
Route.AppStartNavigation.route
}
delay(300)
splashCondition = false
}.launchIn(viewModelScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,43 @@ import androidx.paging.PagingState
import com.api.igdb.apicalypse.APICalypse
import com.api.igdb.apicalypse.Sort
import com.api.igdb.request.IGDBWrapper
import com.api.igdb.request.games
import proto.Game
import com.api.igdb.request.releaseDates
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import proto.ReleaseDate
import java.util.Date

class GamesPagingSource(
private val igdbWrapper: IGDBWrapper,
) : PagingSource<Int, Game>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Game> {
private val afterDate: Date,
) : PagingSource<Int, ReleaseDate>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ReleaseDate> {
val offset = params.key ?: 0
val limit = 10
return try {
val games =
igdbWrapper.games(
APICalypse()
.fields("*")
.limit(limit)
.offset(offset)
.sort("release_dates.date", Sort.ASCENDING),
).distinctBy { it.id }
LoadResult.Page(
data = games,
prevKey = if (offset == 0) null else offset - limit,
nextKey = if (games.isEmpty()) null else offset + limit,
)
withContext(Dispatchers.IO) {
val games =
igdbWrapper.releaseDates(
APICalypse()
.fields("game.name, platform.name, human, game.cover.*")
.where("date > ${afterDate.time / 1000}")
.limit(limit)
.offset(offset)
.sort("date", Sort.ASCENDING),
).distinctBy { it.id }
LoadResult.Page(
data = games.distinctBy { it.id },
prevKey = if (offset == 0) null else offset - limit,
nextKey = if (games.isEmpty()) null else offset + limit,
)
}
} catch (e: Exception) {
e.printStackTrace()
LoadResult.Error(e)
}
}

override fun getRefreshKey(state: PagingState<Int, Game>): Int? {
override fun getRefreshKey(state: PagingState<Int, ReleaseDate>): Int? {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.api.igdb.request.IGDBWrapper
import kotlinx.coroutines.flow.Flow
import proto.Game
import proto.ReleaseDate
import us.kikin.android.gamingbacklog.data.remote.GamesPagingSource
import us.kikin.android.gamingbacklog.domain.repository.GamesRepository
import java.util.Date

class GamesRepositoryImpl(
private val igdbWrapper: IGDBWrapper,
) : GamesRepository {
override fun getGames(): Flow<PagingData<Game>> {
override fun getGames(afterDate: Date): Flow<PagingData<ReleaseDate>> {
return Pager(
config = PagingConfig(pageSize = 10),
pagingSourceFactory = {
GamesPagingSource(
igdbWrapper = igdbWrapper,
afterDate = afterDate,
)
},
).flow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package us.kikin.android.gamingbacklog.domain.repository

import androidx.paging.PagingData
import kotlinx.coroutines.flow.Flow
import proto.Game
import proto.ReleaseDate
import java.util.Date

interface GamesRepository {
fun getGames(): Flow<PagingData<Game>>
fun getGames(afterDate: Date): Flow<PagingData<ReleaseDate>>
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package us.kikin.android.gamingbacklog.domain.usecase.games

import us.kikin.android.gamingbacklog.domain.repository.GamesRepository
import java.util.Date

class GetGames(
private val gamesRepository: GamesRepository,
) {
operator fun invoke() = gamesRepository.getGames()
operator fun invoke(afterDate: Date) = gamesRepository.getGames(afterDate = afterDate)
}
Loading

0 comments on commit 6c11e22

Please sign in to comment.