This commit is contained in:
Hailey
2024-07-11 18:03:05 -07:00
parent df82678ebf
commit f62f9b5ca4
4 changed files with 175 additions and 127 deletions
@@ -1,69 +0,0 @@
package expo.modules.blueskyswissarmy.sharedprefs
import android.content.Context
import android.util.Log
import expo.modules.kotlin.Promise
import expo.modules.kotlin.jni.JavaScriptValue
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
class ExpoBlueskySharedPrefsModule : Module() {
private fun getContext(): Context {
val context = appContext.reactContext ?: throw Error("Context is null")
return context
}
override fun definition() = ModuleDefinition {
Name("ExpoBlueskySharedPrefs")
Function("setString") { key: String, value: String ->
return@Function SharedPrefs(getContext()).setValue(key, value)
}
Function("setValue") { key: String, value: JavaScriptValue ->
val context = getContext()
Log.d("ExpoBlueskySharedPrefs", "Setting value for key: $key")
try {
if (value.isNumber()) {
SharedPrefs(context).setValue(key, value.getFloat())
} else if (value.isBool()) {
SharedPrefs(context).setValue(key, value.getBool())
} else if (value.isNull() || value.isUndefined()) {
SharedPrefs(context).removeValue(key)
} else {
Log.d(NAME, "Unsupported type: ${value.kind()}")
}
} catch (e: Error) {
Log.d(NAME, "Error setting value: $e")
}
}
Function("removeValue") { key: String ->
return@Function SharedPrefs(getContext()).removeValue(key)
}
Function("getString") { key: String ->
return@Function SharedPrefs(getContext()).getString(key)
}
Function("getNumber") { key: String ->
return@Function SharedPrefs(getContext()).getFloat(key)
}
Function("getBool") { key: String ->
return@Function SharedPrefs(getContext()).getBoolean(key)
}
Function("addToSet") { key: String, value: String ->
return@Function SharedPrefs(getContext()).addToSet(key, value)
}
Function("removeFromSet") { key: String, value: String ->
return@Function SharedPrefs(getContext()).removeFromSet(key, value)
}
Function("setContains") { key: String, value: String ->
return@Function SharedPrefs(getContext()).setContains(key, value)
}
}
}
@@ -0,0 +1,69 @@
package expo.modules.blueskyswissarmy.sharedprefs
import android.content.Context
import android.util.Log
import expo.modules.kotlin.jni.JavaScriptValue
import expo.modules.kotlin.modules.Module
import expo.modules.kotlin.modules.ModuleDefinition
class ExpoBlueskySharedPrefsModule : Module() {
private fun getContext(): Context {
val context = appContext.reactContext ?: throw Error("Context is null")
return context
}
override fun definition() =
ModuleDefinition {
Name("ExpoBlueskySharedPrefs")
Function("setString") { key: String, value: String ->
return@Function SharedPrefs(getContext()).setValue(key, value)
}
Function("setValue") { key: String, value: JavaScriptValue ->
val context = getContext()
Log.d("ExpoBlueskySharedPrefs", "Setting value for key: $key")
try {
if (value.isNumber()) {
SharedPrefs(context).setValue(key, value.getFloat())
} else if (value.isBool()) {
SharedPrefs(context).setValue(key, value.getBool())
} else if (value.isNull() || value.isUndefined()) {
SharedPrefs(context).removeValue(key)
} else {
Log.d(NAME, "Unsupported type: ${value.kind()}")
}
} catch (e: Error) {
Log.d(NAME, "Error setting value: $e")
}
}
Function("removeValue") { key: String ->
return@Function SharedPrefs(getContext()).removeValue(key)
}
Function("getString") { key: String ->
return@Function SharedPrefs(getContext()).getString(key)
}
Function("getNumber") { key: String ->
return@Function SharedPrefs(getContext()).getFloat(key)
}
Function("getBool") { key: String ->
return@Function SharedPrefs(getContext()).getBoolean(key)
}
Function("addToSet") { key: String, value: String ->
return@Function SharedPrefs(getContext()).addToSet(key, value)
}
Function("removeFromSet") { key: String, value: String ->
return@Function SharedPrefs(getContext()).removeFromSet(key, value)
}
Function("setContains") { key: String, value: String ->
return@Function SharedPrefs(getContext()).setContains(key, value)
}
}
}
@@ -4,26 +4,32 @@ import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.util.Log import android.util.Log
val DEFAULTS = mapOf<String, Any>( val DEFAULTS =
"playSoundChat" to true, mapOf<String, Any>(
"playSoundFollow" to false, "playSoundChat" to true,
"playSoundLike" to false, "playSoundFollow" to false,
"playSoundMention" to false, "playSoundLike" to false,
"playSoundQuote" to false, "playSoundMention" to false,
"playSoundReply" to false, "playSoundQuote" to false,
"playSoundRepost" to false, "playSoundReply" to false,
"badgeCount" to 0, "playSoundRepost" to false,
) "badgeCount" to 0,
)
const val NAME = "SharedPrefs" const val NAME = "SharedPrefs"
class SharedPrefs(private val context: Context) { class SharedPrefs(
private val context: Context,
) {
companion object { companion object {
private var hasInitialized = false private var hasInitialized = false
private var instance: SharedPreferences? = null private var instance: SharedPreferences? = null
fun getInstance(context: Context, info: String? = "(no info)"): SharedPreferences { fun getInstance(
context: Context,
info: String? = "(no info)",
): SharedPreferences {
if (instance == null) { if (instance == null) {
Log.d(NAME, "No preferences instance found, creating one.") Log.d(NAME, "No preferences instance found, creating one.")
instance = context.getSharedPreferences("xyz.blueskyweb.app", Context.MODE_PRIVATE) instance = context.getSharedPreferences("xyz.blueskyweb.app", Context.MODE_PRIVATE)
@@ -68,44 +74,65 @@ class SharedPrefs(private val context: Context) {
} }
} }
} }
} }.apply()
.apply()
} }
} }
fun setValue(key: String, value: String) { fun setValue(
key: String,
value: String,
) {
val safeInstance = getInstance(context) val safeInstance = getInstance(context)
safeInstance.edit().apply { safeInstance
putString(key, value) .edit()
}.apply() .apply {
putString(key, value)
}.apply()
} }
fun setValue(key: String, value: Float) { fun setValue(
key: String,
value: Float,
) {
val safeInstance = getInstance(context) val safeInstance = getInstance(context)
safeInstance.edit().apply { safeInstance
putFloat(key, value) .edit()
}.apply() .apply {
putFloat(key, value)
}.apply()
} }
fun setValue(key: String, value: Boolean) { fun setValue(
key: String,
value: Boolean,
) {
val safeInstance = getInstance(context) val safeInstance = getInstance(context)
safeInstance.edit().apply { safeInstance
putBoolean(key, value) .edit()
}.apply() .apply {
putBoolean(key, value)
}.apply()
} }
fun setValue(key: String, value: Set<String>) { fun setValue(
key: String,
value: Set<String>,
) {
val safeInstance = getInstance(context) val safeInstance = getInstance(context)
safeInstance.edit().apply { safeInstance
putStringSet(key, value) .edit()
}.apply() .apply {
putStringSet(key, value)
}.apply()
} }
fun removeValue(key: String) { fun removeValue(key: String) {
val safeInstance = getInstance(context) val safeInstance = getInstance(context)
safeInstance.edit().apply { safeInstance
remove(key) .edit()
}.apply() .apply {
remove(key)
}.apply()
} }
fun getString(key: String): String? { fun getString(key: String): String? {
@@ -121,17 +148,23 @@ class SharedPrefs(private val context: Context) {
return safeInstance.getFloat(key, 0.0f) return safeInstance.getFloat(key, 0.0f)
} }
fun _setAnyValue(key: String, value: Any) { @Suppress("ktlint:standard:function-naming")
fun _setAnyValue(
key: String,
value: Any,
) {
val safeInstance = getInstance(context) val safeInstance = getInstance(context)
safeInstance.edit().apply { safeInstance
when (value) { .edit()
is String -> putString(key, value) .apply {
is Float -> putFloat(key, value) when (value) {
is Boolean -> putBoolean(key, value) is String -> putString(key, value)
is Set<*> -> putStringSet(key, value.map { it.toString() }.toSet()) is Float -> putFloat(key, value)
else -> throw Error("Unsupported type: ${value::class.java}") is Boolean -> putBoolean(key, value)
} is Set<*> -> putStringSet(key, value.map { it.toString() }.toSet())
}.apply() else -> throw Error("Unsupported type: ${value::class.java}")
}
}.apply()
} }
fun getBoolean(key: String): Boolean? { fun getBoolean(key: String): Boolean? {
@@ -145,29 +178,44 @@ class SharedPrefs(private val context: Context) {
return res return res
} }
fun addToSet(key: String, value: String) { fun addToSet(
key: String,
value: String,
) {
val safeInstance = getInstance(context) val safeInstance = getInstance(context)
val set = safeInstance.getStringSet(key, setOf()) ?: setOf() val set = safeInstance.getStringSet(key, setOf()) ?: setOf()
val newSet = set.toMutableSet().apply { val newSet =
add(value) set.toMutableSet().apply {
} add(value)
safeInstance.edit().apply { }
putStringSet(key, newSet) safeInstance
}.apply() .edit()
.apply {
putStringSet(key, newSet)
}.apply()
} }
fun removeFromSet(key: String, value: String) { fun removeFromSet(
key: String,
value: String,
) {
val safeInstance = getInstance(context) val safeInstance = getInstance(context)
val set = safeInstance.getStringSet(key, setOf()) ?: setOf() val set = safeInstance.getStringSet(key, setOf()) ?: setOf()
val newSet = set.toMutableSet().apply { val newSet =
remove(value) set.toMutableSet().apply {
} remove(value)
safeInstance.edit().apply { }
putStringSet(key, newSet) safeInstance
}.apply() .edit()
.apply {
putStringSet(key, newSet)
}.apply()
} }
fun setContains(key: String, value: String): Boolean { fun setContains(
key: String,
value: String,
): Boolean {
val safeInstance = getInstance(context) val safeInstance = getInstance(context)
val set = safeInstance.getStringSet(key, setOf()) ?: setOf() val set = safeInstance.getStringSet(key, setOf()) ?: setOf()
return set.contains(value) return set.contains(value)