Skip to content

Commit

Permalink
Add config option to use old file name for local storage preferences …
Browse files Browse the repository at this point in the history
…instead of namespaces for backward compatibility in existing apps
  • Loading branch information
wkarl committed Jun 14, 2023
1 parent f315d3c commit 47b902b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ private constructor() {
class LocalStorageConfig {
var enabled: Boolean = false
var useDefaultLocalStorage: Boolean = true

/**
* Only disable namespaces if a particular instance of JsBridge requires access to local
* storage key/value pairs that were saved with a previous version of the library.
*
* You should try to avoid using multiple unrelated instances of JsBridge without namespaces
* or with an identical namespace. An exception would be if you want to explicitly share data
* between instances and the possibility of key name collisions is not an issue.
*/
var useNamespaces: Boolean = true
}

class JvmConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ interface LocalStorageInteface : JsToNativeInterface {
fun clear()
}

class LocalStorage(context: Context, namespace: String) : LocalStorageInteface {
class LocalStorage(context: Context, namespace: String?) : LocalStorageInteface {

private val localStoragePreferences = context.getSharedPreferences(
"${namespace.takeIf { it.isNotEmpty() } ?: "default"}.LOCAL_STORAGE_PREFERENCES",
namespace?.let { "${it.takeIf { it.isNotEmpty() } ?: "default"}.LOCAL_STORAGE_PREFERENCES" }
?: "${context.applicationInfo.packageName}.LOCAL_STORAGE_PREFERENCE_FILE_KEY",
Context.MODE_PRIVATE
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class LocalStorageExtension(

init {
if (config.useDefaultLocalStorage) {
val localStorage: LocalStorageInteface = LocalStorage(context, namespace)
val localStorage: LocalStorageInteface = LocalStorage(context, namespace.takeIf { config.useNamespaces })
val localStorageJsValue = JsValue.fromNativeObject(jsBridge, localStorage)
localStorageJsValue.assignToGlobal("localStorage")
}
Expand Down

0 comments on commit 47b902b

Please sign in to comment.