Skip to content

Commit

Permalink
Add function to save datastore data in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomanaia committed Jul 8, 2024
1 parent a0c7f83 commit 3ba12cf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,18 @@ object GameDataPreferencesCommon {
key = floatPreferencesKey("hue_lightness"),
defaultValue = TileColorsGenerator.DEFAULT_LIGHTNESS
)

internal fun allPreferences() = listOf(
Grid,
CurrentScore,
BestScore,
GridSize,
DarkThemeConfig,
AmoledMode,
SeedColor,
IncrementHue,
HueIncrementValue,
HueSaturation,
HueLightness
)
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
package com.joaomanaia.game2048.core.datastore.manager

import com.joaomanaia.game2048.core.common.preferences.GameDataPreferencesCommon
import com.joaomanaia.game2048.core.datastore.MutablePreferences
import com.joaomanaia.game2048.core.datastore.Preferences
import com.joaomanaia.game2048.core.datastore.PreferencesKey
import com.joaomanaia.game2048.core.datastore.PreferencesPair
import com.joaomanaia.game2048.core.datastore.edit
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.browser.localStorage
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map

private val logger = KotlinLogging.logger("DataStoreManagerImpl")

actual class DataStoreManagerImpl : DataStoreManager {
private val mutablePreferences = MutableStateFlow<Preferences>(MutablePreferences())
private val mutablePreferences = MutableStateFlow<Preferences>(
MutablePreferences(preferencesMap = loadFromLocalStorage())
)
override val preferenceFlow: Flow<Preferences> = mutablePreferences.asSharedFlow()

private fun loadFromLocalStorage(): MutableMap<PreferencesKey<*>, Any> {
val allPrefs = GameDataPreferencesCommon.allPreferences()
val loadedPrefs = mutableMapOf<PreferencesKey<*>, Any>()

for (i in 0 until localStorage.length) {
val keyStr = localStorage.key(i) ?: continue
val req = allPrefs.find { it.key.name == keyStr } ?: continue

val value = getPreferenceValueFromLocalStorage(req)
loadedPrefs[req.key] = value
}

logger.debug { "Loaded ${loadedPrefs.size} preferences from localStorage" }

return loadedPrefs
}

private fun <T> getPreferenceValueFromLocalStorage(req: PreferenceRequest<T>): T {
val valueStr = localStorage.getItem(req.key.name) ?: return req.defaultValue

return when (req.defaultValue) {
is String -> valueStr
is Boolean -> valueStr.toBoolean()
is Int -> valueStr.toInt()
is Float -> valueStr.toFloat()
else -> throw IllegalArgumentException("Unsupported type: ${req.defaultValue!!::class}")
} as T
}

override suspend fun <T> getPreference(preferenceEntry: PreferenceRequest<T>): T {
return preferenceFlow
.firstOrNull()
Expand All @@ -27,17 +63,29 @@ actual class DataStoreManagerImpl : DataStoreManager {
}.distinctUntilChanged()

override suspend fun <T> editPreference(key: PreferencesKey<T>, newValue: T) {
saveToLocalStorage(key, newValue)
mutablePreferences.edit { preferences -> preferences[key] = newValue }
}

override suspend fun editPreferences(vararg prefs: PreferencesPair<*>) {
mutablePreferences.edit { preferences ->
prefs.forEach {
preferences.plusAssign(it)
prefs.forEach { pref ->
saveToLocalStorage(pref.key, pref.value)
preferences.plusAssign(pref)
}
}
}

private fun saveToLocalStorage(key: PreferencesKey<*>, value: Any?) {
when (value) {
is String -> localStorage.setItem(key.name, value)
is Boolean -> localStorage.setItem(key.name, value.toString())
is Int -> localStorage.setItem(key.name, value.toString())
is Float -> localStorage.setItem(key.name, value.toString())
else -> throw IllegalArgumentException("Unsupported type: $value")
}
}

override suspend fun clearPreferences() {
mutablePreferences.edit { preferences -> preferences.clear() }
}
Expand Down

0 comments on commit 3ba12cf

Please sign in to comment.