1+ package com.segment.analytics.liveplugins.kotlin
2+
3+ import android.content.SharedPreferences
4+ import androidx.core.content.edit
5+ import com.segment.analytics.kotlin.core.utilities.getBoolean
6+ import com.segment.analytics.kotlin.core.utilities.getDouble
7+ import com.segment.analytics.kotlin.core.utilities.getInt
8+ import com.segment.analytics.kotlin.core.utilities.getLong
9+ import com.segment.analytics.kotlin.core.utilities.getString
10+ import com.segment.analytics.substrata.kotlin.JSArray
11+ import com.segment.analytics.substrata.kotlin.JSObject
12+ import com.segment.analytics.substrata.kotlin.JSScope
13+ import com.segment.analytics.substrata.kotlin.JsonElementConverter
14+ import kotlinx.serialization.encodeToString
15+ import kotlinx.serialization.json.Json
16+ import kotlinx.serialization.json.JsonObject
17+ import kotlinx.serialization.json.buildJsonObject
18+ import kotlinx.serialization.json.put
19+
20+ class JSStorage {
21+
22+ internal var sharedPreferences: SharedPreferences ? = null
23+
24+ private var engine: JSScope ? = null
25+
26+ // JSEngine requires an empty constructor to be able to export this class
27+ constructor () {}
28+
29+ constructor (sharedPreferences: SharedPreferences , engine: JSScope ) {
30+ this .sharedPreferences = sharedPreferences
31+ this .engine = engine
32+ }
33+
34+ fun setValue (key : String , value : Boolean ) {
35+ save(key, value, TYPE_BOOLEAN )
36+ }
37+
38+ fun setValue (key : String , value : Double ) {
39+ save(key, value, TYPE_DOUBLE )
40+ }
41+
42+ fun setValue (key : String , value : Int ) {
43+ save(key, value, TYPE_INT )
44+ }
45+
46+ fun setValue (key : String , value : String ) {
47+ save(key, value, TYPE_STRING )
48+ }
49+
50+ fun setValue (key : String , value : Long ) {
51+ save(key, value, TYPE_LONG )
52+ }
53+
54+ fun setValue (key : String , value : JSObject ) {
55+ save(key, value, TYPE_OBJECT )
56+ }
57+
58+ fun setValue (key : String , value : JSArray ) {
59+ save(key, value, TYPE_ARRAY )
60+ }
61+
62+ fun getValue (key : String ): Any? {
63+ return this .sharedPreferences?.getString(key, null )?.let {
64+ Json .decodeFromString<JsonObject >(it).unwrap()
65+ }
66+ }
67+
68+ private fun save (key : String , value : Any , type : String ) {
69+ val jsonObject = buildJsonObject {
70+ put(PROP_TYPE , type)
71+ put(PROP_VALUE , Json .encodeToString(value))
72+ }
73+
74+ this .sharedPreferences?.edit(commit = true ) { putString(key, Json .encodeToString(jsonObject)) }
75+ }
76+
77+ private fun JsonObject.unwrap (): Any? {
78+ return when (this .getString(PROP_TYPE )) {
79+ TYPE_BOOLEAN -> this .getBoolean(PROP_VALUE )
80+ TYPE_INT -> this .getInt(PROP_VALUE )
81+ TYPE_DOUBLE -> this .getDouble(PROP_VALUE )
82+ TYPE_STRING -> this .getString(PROP_VALUE )
83+ TYPE_LONG -> this .getLong(PROP_VALUE )
84+ else -> {
85+ this [PROP_VALUE ]?.let {
86+ engine?.await {
87+ JsonElementConverter .write(it, context)
88+ }
89+ }
90+ }
91+ }
92+ }
93+
94+ companion object {
95+ const val PROP_TYPE = " type"
96+ const val PROP_VALUE = " value"
97+ const val TYPE_BOOLEAN = " boolean"
98+ const val TYPE_INT = " int"
99+ const val TYPE_DOUBLE = " double"
100+ const val TYPE_STRING = " string"
101+ const val TYPE_LONG = " long"
102+ const val TYPE_OBJECT = " object"
103+ const val TYPE_ARRAY = " array"
104+ }
105+ }
0 commit comments