-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLDClient.ios.kt
148 lines (131 loc) · 5.18 KB
/
LDClient.ios.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.myunidays.launchdarkly
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
@Suppress("TooManyFunctions")
actual class LDClient actual constructor(
appContext: Any?,
config: LDConfig,
context: LDContext,
onReady: () -> Unit
) {
private var ios: cocoapods.LaunchDarkly.LDClient? = null
internal actual val json: Json = Json {
ignoreUnknownKeys = true
}
actual val allFlags: Map<String, LDValue>
get() = ios
?.allFlags()
?.map { it.key as String to LDValue(it.value as cocoapods.LaunchDarkly.LDValue) }
?.toMap()
?: emptyMap()
init {
cocoapods.LaunchDarkly.LDClient.startWithConfiguration(config.ios, context.ios) {
ios = cocoapods.LaunchDarkly.LDClient.get()
onReady()
}
}
actual fun boolVariation(
key: String,
defaultValue: Boolean
): Boolean = ios?.boolVariationForKey(key, defaultValue) ?: defaultValue
actual fun intVariation(
key: String,
defaultValue: Int
): Int =
(ios?.integerVariationForKey(key, defaultValue.toLong()) ?: defaultValue.toLong()).toInt()
actual fun doubleVariation(
key: String,
defaultValue: Double
): Double = ios?.doubleVariationForKey(key, defaultValue) ?: defaultValue
actual fun stringVariation(
key: String,
defaultValue: String
): String = ios?.stringVariationForKey(key, defaultValue) ?: defaultValue
actual fun boolVariationDetail(
key: String,
defaultValue: Boolean
): EvaluationDetailInterface<Boolean> =
BoolEvaluationDetail(ios!!.boolVariationDetailForKey(key, defaultValue))
actual fun intVariationDetail(
key: String,
defaultValue: Int
): EvaluationDetailInterface<Int> =
IntegerEvaluationDetail(
ios!!.integerVariationDetailForKey(
key,
defaultValue.toLong()
)
)
actual fun doubleVariationDetail(
key: String,
defaultValue: Double
): EvaluationDetailInterface<Double> = DoubleEvaluationDetail(ios!!.doubleVariationDetailForKey(key, defaultValue))
actual fun stringVariationDetail(
key: String,
defaultValue: String
): EvaluationDetailInterface<String> = StringEvaluationDetail(ios!!.stringVariationDetailForKey(key, defaultValue))
actual fun close() {
ios?.close()
}
actual fun jsonValueVariation(
key: String,
defaultValue: LDValue
): LDValue = LDValue(ios!!.jsonVariationForKey(key, defaultValue.ios))
actual fun <T> jsonValueVariation(
key: String,
deserializer: KSerializer<T>
): T? =
ios?.jsonVariationForKey(key, cocoapods.LaunchDarkly.LDValue.ofNull())
.takeUnless { it?.getType() == cocoapods.LaunchDarkly.LDValueTypeNull }
?.let { remoteValue ->
json.decodeFromString(
deserializer,
JsonObject(
remoteValue.dictValue()
.mapNotNull {
runCatching {
it.key as String to it.value as cocoapods.LaunchDarkly.LDValue
}.getOrNull()
}.associate { it.first to JsonPrimitive(it.second.stringValue()) }
).toString()
)
}
actual fun <T> jsonListValueVariation(
key: String,
deserializer: KSerializer<T>
): List<T> =
ios?.jsonVariationForKey(key, cocoapods.LaunchDarkly.LDValue.ofNull())
.takeUnless { it?.getType() == cocoapods.LaunchDarkly.LDValueTypeNull }
?.let { remoteValue ->
json.decodeFromString(
ListSerializer(deserializer),
JsonArray(
remoteValue.arrayValue()
.filterIsInstance<cocoapods.LaunchDarkly.LDValue>()
.map { singleRemoteValue ->
JsonObject(
singleRemoteValue.dictValue()
.mapNotNull {
runCatching {
it.key as String to
it.value as cocoapods.LaunchDarkly.LDValue
}
.getOrNull()
}
.associate {
it.first to JsonPrimitive(it.second.stringValue())
}
)
}
).toString()
)
}
?: emptyList()
actual fun identify(context: LDContext) {
ios?.identifyWithContext(context.ios)
}
}