Skip to content

Commit

Permalink
feat: dataStore에 서버 버전 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
murjune committed Oct 22, 2024
1 parent 2294d6b commit d4b926e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.jupiter.api.DisplayName
import org.koin.test.KoinTest
import org.koin.test.get
import poke.rogue.helper.local.di.testLocalModule
import poke.rogue.helper.local.entity.PokemonEntity
import poke.rogue.helper.local.entity.pokemonEntity
import poke.rogue.helper.local.utils.KoinAndroidUnitTestRule

Expand All @@ -30,9 +31,29 @@ class PokemonDaoTest : KoinTest {
// when
dao.savePokemons(pokemons)
val actual = dao.pokemons()
println(actual)
// then
val expect = pokemons
actual shouldBe expect
}

@Test
@DisplayName("포켓몬 저장 후 저장 확인, 삭제 후 삭제 확인")
fun `test2`() =
runTest {
// given
val pokemons =
listOf(pokemonEntity(id = "1", name = "피카츄"), pokemonEntity(id = "2", name = "라이츄"))
// when
dao.savePokemons(pokemons)
val actual = dao.pokemons()
// then
val expect = pokemons
actual shouldBe expect
// when
dao.clear()
val actual2 = dao.pokemons()
// then
val expect2 = emptyList<PokemonEntity>()
actual2 shouldBe expect2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ interface PokemonDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun savePokemons(pokemons: List<PokemonEntity>)

@Query("DELETE FROM Pokemon")
suspend fun clear()

companion object {
fun instance(context: Context): PokemonDao {
return PokeRogueDatabase.instance(context).pokemonDao()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package poke.rogue.helper.local.datastore

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class VersionDataStore(private val context: Context) {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = DATA_BASE_NAME)

fun databaseVersion(): Flow<Int?> =
context.dataStore.data.map { preferences ->
preferences[DATABASE_VERSION_KEY]
}

suspend fun saveDatabaseVersion(version: Int) {
context.dataStore.edit { preferences ->
preferences[DATABASE_VERSION_KEY] = version
}
}

companion object {
private const val DATA_BASE_NAME = "datastore_version"
private val DATABASE_VERSION_KEY = intPreferencesKey("database_version")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package poke.rogue.helper.local.di
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module
import poke.rogue.helper.local.datastore.BattleDataStore
import poke.rogue.helper.local.datastore.VersionDataStore

internal val dataStoreModule
get() =
module {
singleOf(::BattleDataStore)
singleOf(::VersionDataStore)
}

0 comments on commit d4b926e

Please sign in to comment.