-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from wkarl/feature/PLAY-3895-local-storage
Default implementation for local storage (@wkarl)
- Loading branch information
Showing
8 changed files
with
151 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
jsbridge/src/main/kotlin/de/prosiebensat1digital/oasisjsbridge/extensions/LocalStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package de.prosiebensat1digital.oasisjsbridge.extensions | ||
|
||
import android.content.Context | ||
|
||
import de.prosiebensat1digital.oasisjsbridge.JsToNativeInterface | ||
import de.prosiebensat1digital.oasisjsbridge.JsonObjectWrapper | ||
import timber.log.Timber | ||
|
||
interface LocalStorageInteface : JsToNativeInterface { | ||
fun setItem(keyName: JsonObjectWrapper, keyValue: JsonObjectWrapper) | ||
fun getItem(keyName: JsonObjectWrapper): JsonObjectWrapper | ||
fun removeItem(keyName: JsonObjectWrapper) | ||
fun clear() | ||
} | ||
|
||
class LocalStorage(context: Context) : LocalStorageInteface { | ||
|
||
private val localStoragePreferences = context.getSharedPreferences( | ||
context.applicationInfo.packageName + ".LOCAL_STORAGE_PREFERENCE_FILE_KEY", | ||
Context.MODE_PRIVATE | ||
) | ||
|
||
override fun setItem(keyName: JsonObjectWrapper, keyValue: JsonObjectWrapper) { | ||
with(localStoragePreferences.edit()) { | ||
putString(keyName.jsonString, keyValue.jsonString) | ||
Timber.d("Saving to local storage -> key: ${keyName.jsonString} value: ${keyValue.jsonString}") | ||
commit() | ||
} | ||
} | ||
|
||
override fun getItem(keyName: JsonObjectWrapper): JsonObjectWrapper { | ||
val value = localStoragePreferences.getString(keyName.jsonString, "null") | ||
checkNotNull(value) | ||
|
||
Timber.d("Loaded from local storage -> key: ${keyName.jsonString} value: $value") | ||
|
||
// Note: default value is an unquoted null-string which is parsed as a null value (not "null"!) | ||
return JsonObjectWrapper(value) | ||
} | ||
|
||
override fun removeItem(keyName: JsonObjectWrapper) { | ||
with(localStoragePreferences.edit()) { | ||
remove(keyName.jsonString) | ||
Timber.d("Removing from local storage -> key: ${keyName.jsonString}") | ||
commit() | ||
} | ||
} | ||
|
||
override fun clear() { | ||
with(localStoragePreferences.edit()) { | ||
clear() | ||
Timber.d("Clearing local storage") | ||
commit() | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...src/main/kotlin/de/prosiebensat1digital/oasisjsbridge/extensions/LocalStorageExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2019 ProSiebenSat1.Digital GmbH. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package de.prosiebensat1digital.oasisjsbridge.extensions | ||
|
||
import android.content.Context | ||
import de.prosiebensat1digital.oasisjsbridge.JsBridge | ||
import de.prosiebensat1digital.oasisjsbridge.JsBridgeConfig | ||
import de.prosiebensat1digital.oasisjsbridge.JsValue | ||
|
||
internal class LocalStorageExtension( | ||
private val jsBridge: JsBridge, | ||
val config: JsBridgeConfig.LocalStorageConfig, | ||
context: Context, | ||
) { | ||
|
||
init { | ||
if (config.useDefaultLocalStorage) { | ||
val localStorageJsValue: JsValue | ||
val localStorage: LocalStorageInteface = LocalStorage(context) | ||
localStorageJsValue = JsValue.fromNativeObject(jsBridge, localStorage) | ||
localStorageJsValue.assignToGlobal("localStorage") | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.16.1 | ||
0.17.0 |