@@ -2,9 +2,11 @@ package com.homexlabs.optimizely_dart
22
33import android.app.Activity
44import androidx.annotation.NonNull
5+ import com.optimizely.ab.OptimizelyUserContext
56import com.optimizely.ab.android.sdk.OptimizelyClient
67import com.optimizely.ab.android.sdk.OptimizelyManager
78import com.optimizely.ab.config.Variation
9+ import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption
810import io.flutter.embedding.engine.plugins.FlutterPlugin
911import io.flutter.embedding.engine.plugins.activity.ActivityAware
1012import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
@@ -13,6 +15,7 @@ import io.flutter.plugin.common.MethodChannel
1315import io.flutter.plugin.common.MethodChannel.MethodCallHandler
1416import io.flutter.plugin.common.MethodChannel.Result
1517import io.flutter.plugin.common.PluginRegistry.Registrar
18+ import java.util.concurrent.TimeUnit
1619
1720
1821/* * OptimizelyPlugin */
@@ -25,6 +28,7 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
2528 private lateinit var activity: Activity
2629
2730 private lateinit var optimizelyClient: OptimizelyClient
31+ private lateinit var user: OptimizelyUserContext
2832
2933 override fun onAttachedToEngine (@NonNull flutterPluginBinding : FlutterPlugin .FlutterPluginBinding ) {
3034 channel = MethodChannel (flutterPluginBinding.getFlutterEngine().getDartExecutor(), " optimizely_plugin" )
@@ -61,19 +65,30 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
6165 initOptimizelyManagerAsync(sdkKey!! )
6266 result.success(" " )
6367 }
68+ " setUser" -> {
69+ val userId = call.argument<String >(" user_id" )
70+ val attributes = call.argument<MutableMap <String , Any >>(" attributes" )
71+ setUser(userId!! , attributes!! )
72+ }
6473 " isFeatureEnabled" -> {
6574 val featureKey = call.argument<String >(" feature_key" )
66- val userId = call.argument<String >(" user_id" )
67- val flag = isFeatureEnabled(featureKey!! , userId!! )
75+ val flag = isFeatureEnabled(featureKey!! )
6876 result.success(flag)
6977 }
7078 " getAllFeatureVariables" -> {
7179 val featureKey = call.argument<String >(" feature_key" )
72- val userId = call.argument<String >(" user_id" )
73- val attributes = call.argument<MutableMap <String , Any >>(" attributes" )
74- val variables = getAllFeatureVariables(featureKey!! , userId!! , attributes!! )
80+ val variables = getAllFeatureVariables(featureKey!! )
7581 result.success(variables)
7682 }
83+ " getAllEnabledFeatures" -> {
84+ val features = getAllEnabledFeatures()
85+ result.success(features)
86+ }
87+ " activateGetVariation" -> {
88+ val featureKey = call.argument<String >(" feature_key" )
89+ val variation = activateGetVariation(featureKey!! )
90+ result.success(variation)
91+ }
7792 " getvariation" -> {
7893 val featureKey = call.argument<String >(" feature_key" )
7994 val userId = call.argument<String >(" user_id" )
@@ -83,10 +98,8 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
8398 }
8499 " trackEvent" -> {
85100 val featureKey = call.argument<String >(" feature_key" )
86- val userId = call.argument<String >(" user_id" )
87- val attributes = call.argument<MutableMap <String , Any >>(" attributes" )
88101 val eventTags = call.argument<MutableMap <String , Any >>(" event_tags" )
89- trackEvent(featureKey!! , userId !! , attributes !! , eventTags!! )
102+ trackEvent(featureKey!! , eventTags!! )
90103 result.success(" " )
91104 }
92105 else -> result.notImplemented()
@@ -114,47 +127,60 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
114127 }
115128
116129 private fun initOptimizelyManager (sdkKey : String , dataFile : String ) {
117- val builder = OptimizelyManager .builder()
118- // In Android, the minimum polling interval is 60 seconds. In iOS, the minimum polling
119- // interval is 2 minutes while the app is open. If you specify shorter polling intervals
120- // than these minimums, the SDK will automatically reset the intervals to 60 seconds (Android)
121- // or 2 minutes (iOS).
122- val optimizelyManager = builder.withDatafileDownloadInterval(60 )
130+ val optimizelyManager = OptimizelyManager .builder()
123131 .withSDKKey(sdkKey)
132+ .withDatafileDownloadInterval(15 , TimeUnit .MINUTES )
133+ .withEventDispatchInterval(30 , TimeUnit .SECONDS )
124134 .build(activity.applicationContext)
125135
126136 optimizelyClient = optimizelyManager.initialize(activity.applicationContext, dataFile, true , true )
127137 }
128138
129139 private fun initOptimizelyManagerAsync (sdkKey : String ) {
130- val builder = OptimizelyManager .builder()
131- // In Android, the minimum polling interval is 60 seconds. In iOS, the minimum polling
132- // interval is 2 minutes while the app is open. If you specify shorter polling intervals
133- // than these minimums, the SDK will automatically reset the intervals to 60 seconds (Android)
134- // or 2 minutes (iOS).
135- val optimizelyManager = builder.withDatafileDownloadInterval(60 )
140+ val optimizelyManager = OptimizelyManager .builder()
136141 .withSDKKey(sdkKey)
142+ .withDatafileDownloadInterval(15 , TimeUnit .MINUTES )
143+ .withEventDispatchInterval(30 , TimeUnit .SECONDS )
137144 .build(activity.applicationContext)
138145
139146 optimizelyManager.initialize(activity.applicationContext, null ) {
140147 client -> optimizelyClient = client
141148 }
142149 }
143150
144- private fun isFeatureEnabled (featureKey : String , userId : String ): Boolean {
145- return optimizelyClient.isFeatureEnabled(featureKey, userId)
151+ private fun setUser (userId : String , attributes : MutableMap <String , Any >){
152+ val userTemp = optimizelyClient.createUserContext(userId, attributes)
153+ if (userTemp != null ){
154+ user = userTemp
155+ }
156+ }
157+
158+ private fun isFeatureEnabled (featureKey : String ): Boolean {
159+ val decision = user.decide(featureKey)
160+ return decision.enabled
146161 }
147162
148- private fun getAllFeatureVariables (featureKey : String , userId : String , attributes : MutableMap <String , Any >): Map <String , Any >? {
149- val json = optimizelyClient.getAllFeatureVariables(featureKey, userId, attributes)
150- return json?.toMap()
163+ private fun getAllFeatureVariables (featureKey : String ): Map <String , Any >? {
164+ val decision = user.decide(featureKey)
165+ return decision.variables.toMap()
166+ }
167+
168+ private fun getAllEnabledFeatures (): MutableSet <String > {
169+ val options: List <OptimizelyDecideOption > = listOf (OptimizelyDecideOption .ENABLED_FLAGS_ONLY )
170+ val decisions = user.decideAll(options)
171+ return decisions.keys
151172 }
152173
153174 private fun getVariation (featureKey : String , userId : String , attributes : MutableMap <String , Any >): Variation ? {
154175 return optimizelyClient.getVariation(featureKey, userId, attributes)
155176 }
156177
157- private fun trackEvent (eventKey : String , userId : String , attributes : MutableMap <String , Any >, eventTags : MutableMap <String , Any >) {
158- optimizelyClient.track(eventKey, userId, attributes, eventTags)
178+ private fun activateGetVariation (featureKey : String ): String? {
179+ val decision = user.decide(featureKey)
180+ return decision.variationKey
181+ }
182+
183+ private fun trackEvent (eventKey : String , eventTags : MutableMap <String , Any >) {
184+ user.trackEvent(eventKey, eventTags)
159185 }
160186}
0 commit comments