Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add putAllContext method #26

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import com.spotify.confidence.Confidence
import com.spotify.confidence.ConfidenceFactory
import com.spotify.confidence.ConfidenceValue
import com.spotify.confidence.FlagResolution
import com.spotify.confidence.cache.DiskStorage
import com.spotify.confidence.client.ConfidenceValueMap
import com.spotify.confidence.client.ResolveFlags
import com.spotify.confidence.client.SdkMetadata
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
Expand Down Expand Up @@ -111,6 +108,12 @@ class ConfidenceFlutterSdkPlugin: FlutterPlugin, MethodCallHandler, ActivityAwar
confidence.putContext(key, value)
result.success(null)
}
"putAllContext" -> {
val wrappedContext = call.argument<Map<String, Map<String, Any>>>("context")!!
val context: Map<String, ConfidenceValue> = wrappedContext.mapValues { (_, value) -> value.convert() }
confidence.putContext(context)
result.success(null)
}
"track" -> {
val eventName = call.argument<String>("eventName")!!
val wrappedData = call.argument<Map<String, Map<String, Any>>>("data")!!
Expand Down
9 changes: 8 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ class _MyAppState extends State<MyApp> {
try {
await dotenv.load(fileName: ".env");
await _confidenceFlutterSdkPlugin.setup(dotenv.env["API_KEY"]!);
await _confidenceFlutterSdkPlugin.putContext("targeting_key", "random");
await _confidenceFlutterSdkPlugin.putAllContext({
"targeting_key": "random",
"my_bool": false,
"my_int": 1,
"my_double": 1.1,
"my_map": {"key": "value"},
"my_list": ["value1", "value2"]
});
await _confidenceFlutterSdkPlugin.fetchAndActivate();
object =
(_confidenceFlutterSdkPlugin.getObject("hawkflag", <String, dynamic>{})).toString();
Expand Down
13 changes: 13 additions & 0 deletions ios/Classes/ConfidenceFlutterSdkPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ public class ConfidenceFlutterSdkPlugin: NSObject, FlutterPlugin {
confidence?.putContext(key: key, value: value)
result("")
break;
case "putAllContext":
guard let args = call.arguments as? Dictionary<String, Dictionary<String, Any>> else {
result("")
return
}
let context = args["context"] as! Dictionary<String, Dictionary<String, Any>>
let map: ConfidenceStruct = context.mapValues { wrappedValue in
let type = wrappedValue["type"] as! String
return convertValue(type, wrappedValue["value"]!)
}
confidence?.putContext(context: map)
result("")
break;
case "track":
guard let args = call.arguments as? Dictionary<String, Any> else {
return
Expand Down
7 changes: 7 additions & 0 deletions lib/confidence_flutter_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class ConfidenceFlutterSdk {
}
}

Future<void> putAllContext(Map<String, dynamic> context) async {
await ConfidenceFlutterSdkPlatform.instance.putAllContext(context);
if(isInitialized) {
await fetchAndActivate();
}
}

void track(String eventName, Map<String, dynamic> data) {
ConfidenceFlutterSdkPlatform.instance.track(eventName, data);
}
Expand Down
12 changes: 12 additions & 0 deletions lib/confidence_flutter_sdk_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ class MethodChannelConfidenceFlutterSdk extends ConfidenceFlutterSdkPlatform {
);
}

@override
Future<void> putAllContext(Map<String, dynamic> context) async {
final wrappedContext = context.map((key, value) {
return MapEntry(key, toTypedValue(value));
});
await methodChannel
.invokeMethod<void>(
'putAllContext',
{'context': wrappedContext}
);
}

@override
void track(String eventName, Map<String, dynamic> data) {
final wrappedData = data.map((key, value) {
Expand Down
4 changes: 4 additions & 0 deletions lib/confidence_flutter_sdk_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ abstract class ConfidenceFlutterSdkPlatform extends PlatformInterface {
throw UnimplementedError('putContext() has not been implemented.');
}

Future<void> putAllContext(Map<String, dynamic> context) async {
throw UnimplementedError('putAllContext() has not been implemented.');
}

Future<bool> isStorageEmpty() {
throw UnimplementedError('isStorageEmpty() has not been implemented.');
}
Expand Down
Loading