Skip to content

Commit

Permalink
feat: 서버의 데이터베이스 버전이 달라지면 디스크 캐시를 리프레쉬한다
Browse files Browse the repository at this point in the history
  • Loading branch information
murjune committed Oct 22, 2024
1 parent d4b926e commit fce3e8a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ class LocalDexDataSource(
logger.logError(it, "LocalDexDataSource - pokemons() 에서 발생")
}.getOrThrow()

suspend fun savePokemons(pokemons: List<Pokemon>) =
suspend fun savePokemons(pokemons: List<Pokemon>): Unit =
runCatching {
pokemonDao.savePokemons(pokemons.map { it.toEntity() })
}.onFailure {
logger.logError(it, "LocalDexDataSource - savePokemons() 에서 발생")
}.getOrThrow()

suspend fun clear(): Unit =
runCatching {
pokemonDao.clear()
}.onFailure {
logger.logError(it, "LocalDexDataSource - clearPokemons() 에서 발생")
}.getOrThrow()

companion object {
private var instance: LocalDexDataSource? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package poke.rogue.helper.data.datasource

import poke.rogue.helper.local.datastore.VersionDataStore

class LocalVersionDataSource(
private val versionDataStore: VersionDataStore,
) {
fun databaseVersion() = versionDataStore.databaseVersion()

suspend fun saveDatabaseVersion(version: Int) = versionDataStore.saveDatabaseVersion(version)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package poke.rogue.helper.data.datasource

import poke.rogue.helper.analytics.AnalyticsLogger
import poke.rogue.helper.data.exception.getOrThrow
import poke.rogue.helper.data.exception.onFailure
import poke.rogue.helper.remote.service.VersionService

class RemoteVersionDataSource(
private val versionService: VersionService,
private val logger: AnalyticsLogger,
) {
suspend fun databaseVersion(): Int =
versionService.databaseVersion()
.onFailure {
logger.logError(throwable, "RemoteVersionDataSource - databaseVersion() 에서 발생")
}.getOrThrow().version
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ import org.koin.dsl.module
import poke.rogue.helper.data.datasource.LocalBattleDataSource
import poke.rogue.helper.data.datasource.LocalDexDataSource
import poke.rogue.helper.data.datasource.LocalTypeDataSource
import poke.rogue.helper.data.datasource.LocalVersionDataSource
import poke.rogue.helper.data.datasource.RemoteAbilityDataSource
import poke.rogue.helper.data.datasource.RemoteBattleDataSource
import poke.rogue.helper.data.datasource.RemoteBiomeDataSource
import poke.rogue.helper.data.datasource.RemoteDexDataSource
import poke.rogue.helper.data.datasource.RemoteVersionDataSource

internal val dataSourceModule
get() =
module {
singleOf(::LocalBattleDataSource)
singleOf(::LocalDexDataSource)
singleOf(::LocalTypeDataSource)
singleOf(::LocalVersionDataSource)

singleOf(::RemoteBattleDataSource)
singleOf(::RemoteAbilityDataSource)
singleOf(::RemoteDexDataSource)
singleOf(::RemoteBiomeDataSource)
singleOf(::RemoteVersionDataSource)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ package poke.rogue.helper.data.repository

import android.content.Context
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
import poke.rogue.helper.analytics.AnalyticsLogger
import poke.rogue.helper.analytics.analyticsLogger
import poke.rogue.helper.data.cache.GlideImageCacher
import poke.rogue.helper.data.cache.ImageCacher
import poke.rogue.helper.data.datasource.LocalDexDataSource
import poke.rogue.helper.data.datasource.LocalVersionDataSource
import poke.rogue.helper.data.datasource.RemoteDexDataSource
import poke.rogue.helper.data.datasource.RemoteVersionDataSource
import poke.rogue.helper.data.model.Biome
import poke.rogue.helper.data.model.Pokemon
import poke.rogue.helper.data.model.PokemonBiome
import poke.rogue.helper.data.model.PokemonDetail
import poke.rogue.helper.data.model.PokemonFilter
import poke.rogue.helper.data.model.PokemonSort
import poke.rogue.helper.data.utils.logPokemonDetail
import poke.rogue.helper.local.datastore.VersionDataStore
import poke.rogue.helper.remote.injector.ServiceModule
import poke.rogue.helper.stringmatcher.has

class DefaultDexRepository(
Expand All @@ -24,24 +29,36 @@ class DefaultDexRepository(
private val imageCacher: ImageCacher,
private val biomeRepository: BiomeRepository,
private val analyticsLogger: AnalyticsLogger,
private val localVersionDataSource: LocalVersionDataSource,
private val remoteVersionService: RemoteVersionDataSource,
) : DexRepository {
private var cachedPokemons: List<Pokemon> = emptyList()

override suspend fun warmUp() {
if (localPokemonDataSource.pokemons().isEmpty()) {
val shouldUpdateDatabase = shouldUpdateDatabaseVersion()
val emptyDiskCache = localPokemonDataSource.pokemons().isEmpty()

if (shouldUpdateDatabase || emptyDiskCache) {
val pokemons = remotePokemonDataSource.pokemons2()
cachePokemonData(pokemons)
}
cachedPokemons = localPokemonDataSource.pokemons()
}

private suspend fun shouldUpdateDatabaseVersion(): Boolean {
val localVersion = localVersionDataSource.databaseVersion().firstOrNull()
val remoteVersion = remoteVersionService.databaseVersion()
return (localVersion ?: 0) < remoteVersion
}

private suspend fun cachePokemonData(pokemons: List<Pokemon>) =
coroutineScope {
val urls = pokemons.take(PLELOAD_POKEMON_COUNT).map { it.imageUrl }
launch {
imageCacher.cacheImages(urls)
}
launch {
localPokemonDataSource.clear()
localPokemonDataSource.savePokemons(pokemons)
}
}
Expand Down Expand Up @@ -127,6 +144,8 @@ class DefaultDexRepository(
GlideImageCacher.instance(),
DefaultBiomeRepository.instance(),
analyticsLogger(),
LocalVersionDataSource(VersionDataStore(context)),
RemoteVersionDataSource(ServiceModule.versionService(), analyticsLogger()),
)
}

Expand Down

0 comments on commit fce3e8a

Please sign in to comment.