Skip to content

Commit 56539ae

Browse files
Add variableValue() method, update tests, update example apps (#148)
1 parent d04a678 commit 56539ae

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

android-client-sdk/src/main/java/com/devcycle/sdk/android/api/DVCClient.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,35 @@ class DVCClient private constructor(
287287
}
288288

289289
/**
290-
* Retrieve a Variable from the config. Update the Variable whenever the Config is updated using
290+
* Retrieve a Variable Value. Update the Variable whenever the Config is updated using
291+
* [java.beans.PropertyChangeListener]
292+
*
293+
* [key] is used to identify the Variable in the config
294+
* [defaultValue] is set on the Variable and used to provide a default value if the Variable
295+
* could not be fetched or does not exist
296+
*/
297+
fun variableValue(key: String, defaultValue: String): String {
298+
return this._variable(key, defaultValue).value
299+
}
300+
301+
fun variableValue(key: String, defaultValue: Number): Number {
302+
return this._variable(key,defaultValue).value
303+
}
304+
305+
fun variableValue(key: String, defaultValue: Boolean): Boolean {
306+
return this._variable(key, defaultValue).value
307+
}
308+
309+
fun variableValue(key: String, defaultValue: JSONObject): JSONObject {
310+
return this._variable(key, defaultValue).value
311+
}
312+
313+
fun variableValue(key: String, defaultValue: JSONArray): JSONArray {
314+
return this._variable(key, defaultValue).value
315+
}
316+
317+
/**
318+
* Retrieve a Variable Object. Update the Variable whenever the Config is updated using
291319
* [java.beans.PropertyChangeListener]
292320
*
293321
* [key] is used to identify the Variable in the config

android-client-sdk/src/test/java/com/devcycle/sdk/android/api/DVCClientTests.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,12 +861,16 @@ class DVCClientTests {
861861
val client = createClient("pretend-its-a-real-sdk-key", mockWebServer.url("/").toString())
862862

863863
val variable = client.variable("activate-flag", "Not activated")
864+
val varValue = client.variableValue("activate-flag", "Not activated")
864865
val variable2 = client.variable("activate-flag", "Activated")
866+
val varValue2 = client.variableValue("activate-flag", "Activated")
865867

866868
assert(variable !== variable2)
867869
assert(variable.value === "Not activated")
870+
assert(varValue === "Not activated")
868871
assert(variable.isDefaulted == true)
869872
assert(variable2.value === "Activated")
873+
assert(varValue2 === "Activated")
870874
assert(variable2.isDefaulted == true)
871875
}
872876

@@ -909,9 +913,14 @@ class DVCClientTests {
909913
val client = createClient("pretend-its-a-real-sdk-key", mockWebServer.url("/").toString())
910914

911915
val jsonVar = client.variable("test-feature-json", defaultJSON)
916+
val jsonValue = client.variableValue("test-feature-json", defaultJSON)
912917
val boolVar = client.variable("test-feature", false)
918+
val boolValue = client.variableValue("test-feature", false)
913919
val numVar = client.variable("test-feature-number", 0)
920+
val numValue = client.variableValue("test-feature-number", 0)
914921
val strVar = client.variable("test-feature-string", "Not activated")
922+
val strValue = client.variableValue("test-feature-string", "Not activated")
923+
915924
try {
916925
client.onInitialized(object: DVCCallback<String> {
917926
override fun onSuccess(result: String) {
@@ -932,17 +941,25 @@ class DVCClientTests {
932941

933942
assert(boolVar.value)
934943
assert(boolVar.isDefaulted == false)
944+
// value doesn't get updated
945+
assert(!boolValue)
935946

936947
assert(numVar.value == 42)
937948
assert(numVar.isDefaulted == false)
949+
// value doesn't get updated
950+
assert(numValue == 0)
938951

939952
assert(strVar.value == "it works!")
940953
assert(strVar.isDefaulted == false)
954+
// value doesn't get updated
955+
assert(strValue == "Not activated")
941956

942957
val expectedJSON = JSONObject()
943958
expectedJSON.put("test", "feature")
944959
assert(jsonVar.value.toString() == expectedJSON.toString())
945960
assert(jsonVar.isDefaulted == false)
961+
// value doesn't get updated
962+
assert(jsonValue.toString() !== expectedJSON.toString())
946963
}
947964
}
948965

@@ -983,9 +1000,14 @@ class DVCClientTests {
9831000
val client = createClient("pretend-its-a-real-sdk-key", mockWebServer.url("/").toString())
9841001

9851002
val jsonVar = client.variable("test-feature-json", defaultJSON)
1003+
val jsonValue = client.variableValue("test-feature-json", defaultJSON)
9861004
val boolVar = client.variable("test-feature", false)
1005+
val boolValue = client.variableValue("test-feature", false)
9871006
val numVar = client.variable("test-feature-number", 0)
1007+
val numValue = client.variableValue("test-feature-number", 0)
9881008
val strVar = client.variable("test-feature-string", "Not activated")
1009+
val strValue = client.variableValue("test-feature-string", "Not activated")
1010+
9891011
try {
9901012
client.onInitialized(object: DVCCallback<String> {
9911013
override fun onSuccess(result: String) {
@@ -1007,15 +1029,19 @@ class DVCClientTests {
10071029
// Expect Client to have failed to initialize, all variables default
10081030
assert(!boolVar.value)
10091031
assert(boolVar.isDefaulted == true)
1032+
assert(!boolValue)
10101033

10111034
assert(numVar.value == 0)
10121035
assert(numVar.isDefaulted == true)
1036+
assert(numValue == 0)
10131037

10141038
assert(strVar.value == "Not activated")
10151039
assert(strVar.isDefaulted == true)
1040+
assert(strValue == "Not activated")
10161041

10171042
assert(jsonVar.value.toString() == defaultJSON.toString())
10181043
assert(jsonVar.isDefaulted == true)
1044+
assert(jsonValue.toString() == defaultJSON.toString())
10191045
}
10201046
}
10211047

@@ -1056,9 +1082,14 @@ class DVCClientTests {
10561082
val client = createClient("pretend-its-a-real-sdk-key", mockWebServer.url("/").toString())
10571083

10581084
val jsonVar = client.variable("test-feature-json", defaultJSON)
1085+
val jsonValue = client.variableValue("test-feature-json", defaultJSON)
10591086
val boolVar = client.variable("test-feature", false)
1087+
val boolValue = client.variableValue("test-feature", false)
10601088
val numVar = client.variable("test-feature-number", 0)
1089+
val numValue = client.variableValue("test-feature-number", 0)
10611090
val strVar = client.variable("test-feature-string", "Not activated")
1091+
val strValue = client.variableValue("test-feature-string", "Not activated")
1092+
10621093
try {
10631094
client.onInitialized(object: DVCCallback<String> {
10641095
override fun onSuccess(result: String) {
@@ -1080,15 +1111,19 @@ class DVCClientTests {
10801111
// Expect Client to have failed to initialize, all variables default
10811112
assert(!boolVar.value)
10821113
assert(boolVar.isDefaulted == true)
1114+
assert(!boolValue)
10831115

10841116
assert(numVar.value == 0)
10851117
assert(numVar.isDefaulted == true)
1118+
assert(numValue == 0)
10861119

10871120
assert(strVar.value == "Not activated")
10881121
assert(strVar.isDefaulted == true)
1122+
assert(strValue == "Not activated")
10891123

10901124
assert(jsonVar.value.toString() == defaultJSON.toString())
10911125
assert(jsonVar.isDefaulted == true)
1126+
assert(jsonValue.toString() == defaultJSON.toString())
10921127
}
10931128
}
10941129

@@ -1130,9 +1165,14 @@ class DVCClientTests {
11301165
val client = createClient("pretend-its-a-real-sdk-key", mockWebServer.url("/").toString())
11311166

11321167
val jsonVar = client.variable("test-feature-json", defaultJSON)
1168+
val jsonValue = client.variableValue("test-feature-json", defaultJSON)
11331169
val boolVar = client.variable("test-feature", false)
1170+
val boolValue = client.variableValue("test-feature", false)
11341171
val numVar = client.variable("test-feature-number", 0)
1172+
val numValue = client.variableValue("test-feature-number", 0)
11351173
val strVar = client.variable("test-feature-string", "Not activated")
1174+
val strValue = client.variableValue("test-feature-string", "Not activated")
1175+
11361176
try {
11371177
client.onInitialized(object: DVCCallback<String> {
11381178
override fun onSuccess(result: String) {
@@ -1154,15 +1194,19 @@ class DVCClientTests {
11541194
// Expect Client to have failed to initialize, all variables default
11551195
assert(!boolVar.value)
11561196
assert(boolVar.isDefaulted == true)
1197+
assert(!boolValue)
11571198

11581199
assert(numVar.value == 0)
11591200
assert(numVar.isDefaulted == true)
1201+
assert(numValue == 0)
11601202

11611203
assert(strVar.value == "Not activated")
11621204
assert(strVar.isDefaulted == true)
1205+
assert(strValue == "Not activated")
11631206

11641207
assert(jsonVar.value.toString() == defaultJSON.toString())
11651208
assert(jsonVar.isDefaulted == true)
1209+
assert(jsonValue.toString() == defaultJSON.toString())
11661210
}
11671211
}
11681212

@@ -1204,9 +1248,14 @@ class DVCClientTests {
12041248
val client = createClient("pretend-its-a-real-sdk-key", mockWebServer.url("/").toString())
12051249

12061250
val jsonVar = client.variable("test-feature-json", defaultJSON)
1251+
val jsonValue = client.variableValue("test-feature-json", defaultJSON)
12071252
val boolVar = client.variable("test-feature", false)
1253+
val boolValue = client.variableValue("test-feature", false)
12081254
val numVar = client.variable("test-feature-number", 0)
1255+
val numValue = client.variableValue("test-feature-number", 0)
12091256
val strVar = client.variable("test-feature-string", "Not activated")
1257+
val strValue = client.variableValue("test-feature-string", "Not activated")
1258+
12101259
try {
12111260
client.onInitialized(object: DVCCallback<String> {
12121261
override fun onSuccess(result: String) {
@@ -1228,15 +1277,19 @@ class DVCClientTests {
12281277
// Expect Client to have failed initialization, all variables should default
12291278
assert(!boolVar.value)
12301279
assert(boolVar.isDefaulted == true)
1280+
assert(!boolValue)
12311281

12321282
assert(numVar.value == 0)
12331283
assert(numVar.isDefaulted == true)
1284+
assert(numValue == 0)
12341285

12351286
assert(strVar.value === "Not activated")
12361287
assert(strVar.isDefaulted == true)
1288+
assert(strValue === "Not activated")
12371289

12381290
assert(jsonVar.value === defaultJSON)
12391291
assert(jsonVar.isDefaulted == true)
1292+
assert(jsonValue === defaultJSON)
12401293
}
12411294
}
12421295

java-example/src/main/java/com/devcycle/javaexample/JavaApplication.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
public class JavaApplication extends Application {
2020
Variable<String> variable = null;
21+
String variableValue = null;
2122

2223
@Override
2324
public void onCreate() {
@@ -30,13 +31,14 @@ public void onCreate() {
3031
.withUserId("test_user")
3132
.build()
3233
)
33-
.withSDKKey("<YOUR_MOBILE_SDK_KEY>")
34+
.withSDKKey("<DVC_MOBILE_SDK_KEY>")
3435
.withLogLevel(LogLevel.DEBUG)
3536
.build();
3637

3738
// Use your own demo variable here to see the value change from the defaultValue when the client is initialized
3839
variable = client.variable("<YOUR_VARIABLE_KEY>", "my string variable is not initialized yet");
39-
Toast.makeText(getApplicationContext(), Objects.requireNonNull(variable.getValue()), Toast.LENGTH_SHORT).show();
40+
variableValue = client.variableValue("<YOUR_VARIABLE_KEY>", "my string variable is not initialized yet");
41+
Toast.makeText(getApplicationContext(), Objects.requireNonNull(variableValue), Toast.LENGTH_SHORT).show();
4042

4143
client.onInitialized(new DVCCallback<String>() {
4244
@Override

kotlin-example/src/main/java/com/devcycle/example/KotlinApplication.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import com.devcycle.sdk.android.model.DVCUser
99
import com.devcycle.sdk.android.model.Variable
1010
import com.devcycle.sdk.android.util.DVCLogger
1111
import com.devcycle.sdk.android.util.LogLevel
12-
12+
import java.util.Objects
1313

1414
class KotlinApplication: Application() {
1515
var variable: Variable<String>? = null
16+
var variableValue: String? = null
1617

1718
override fun onCreate() {
1819
super.onCreate()
@@ -24,13 +25,15 @@ class KotlinApplication: Application() {
2425
.withCustomData(mapOf("custom_value" to "test"))
2526
.build()
2627
)
27-
.withSDKKey("<YOUR_MOBILE_SDK_KEY>")
28+
.withSDKKey("<DVC_MOBILE_SDK_KEY>")
2829
.withLogLevel(LogLevel.DEBUG)
2930
.withLogger(DVCLogger.DebugLogger())
3031
.build()
3132

3233
// Use your own demo variable here to see the value change from the defaultValue when the client is initialized
3334
variable = client.variable("<YOUR_VARIABLE_KEY>", "my string variable is not initialized yet");
35+
variableValue = client.variableValue("<YOUR_VARIABLE_KEY>", "default value")
36+
Toast.makeText(applicationContext, Objects.requireNonNull(variableValue), Toast.LENGTH_SHORT).show()
3437

3538
client.onInitialized(object : DVCCallback<String> {
3639
override fun onSuccess(result: String) {

0 commit comments

Comments
 (0)