lint
This commit is contained in:
-69
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+69
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+71
-23
@@ -4,7 +4,8 @@ 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 =
|
||||||
|
mapOf<String, Any>(
|
||||||
"playSoundChat" to true,
|
"playSoundChat" to true,
|
||||||
"playSoundFollow" to false,
|
"playSoundFollow" to false,
|
||||||
"playSoundLike" to false,
|
"playSoundLike" to false,
|
||||||
@@ -17,13 +18,18 @@ val DEFAULTS = mapOf<String, Any>(
|
|||||||
|
|
||||||
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,42 +74,63 @@ 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
|
||||||
|
.edit()
|
||||||
|
.apply {
|
||||||
putString(key, value)
|
putString(key, value)
|
||||||
}.apply()
|
}.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
|
||||||
|
.edit()
|
||||||
|
.apply {
|
||||||
putFloat(key, value)
|
putFloat(key, value)
|
||||||
}.apply()
|
}.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
|
||||||
|
.edit()
|
||||||
|
.apply {
|
||||||
putBoolean(key, value)
|
putBoolean(key, value)
|
||||||
}.apply()
|
}.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
|
||||||
|
.edit()
|
||||||
|
.apply {
|
||||||
putStringSet(key, value)
|
putStringSet(key, value)
|
||||||
}.apply()
|
}.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeValue(key: String) {
|
fun removeValue(key: String) {
|
||||||
val safeInstance = getInstance(context)
|
val safeInstance = getInstance(context)
|
||||||
safeInstance.edit().apply {
|
safeInstance
|
||||||
|
.edit()
|
||||||
|
.apply {
|
||||||
remove(key)
|
remove(key)
|
||||||
}.apply()
|
}.apply()
|
||||||
}
|
}
|
||||||
@@ -121,9 +148,15 @@ 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
|
||||||
|
.edit()
|
||||||
|
.apply {
|
||||||
when (value) {
|
when (value) {
|
||||||
is String -> putString(key, value)
|
is String -> putString(key, value)
|
||||||
is Float -> putFloat(key, value)
|
is Float -> putFloat(key, value)
|
||||||
@@ -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 =
|
||||||
|
set.toMutableSet().apply {
|
||||||
add(value)
|
add(value)
|
||||||
}
|
}
|
||||||
safeInstance.edit().apply {
|
safeInstance
|
||||||
|
.edit()
|
||||||
|
.apply {
|
||||||
putStringSet(key, newSet)
|
putStringSet(key, newSet)
|
||||||
}.apply()
|
}.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 =
|
||||||
|
set.toMutableSet().apply {
|
||||||
remove(value)
|
remove(value)
|
||||||
}
|
}
|
||||||
safeInstance.edit().apply {
|
safeInstance
|
||||||
|
.edit()
|
||||||
|
.apply {
|
||||||
putStringSet(key, newSet)
|
putStringSet(key, newSet)
|
||||||
}.apply()
|
}.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)
|
||||||
|
|||||||
Reference in New Issue
Block a user