From d3f363263bb067b86ed4179c436d759e67a42a56 Mon Sep 17 00:00:00 2001 From: Shantanu Gupta Date: Tue, 18 May 2021 10:55:54 +0530 Subject: [PATCH 01/15] android implementation --- .idea/inspectionProfiles/Project_Default.xml | 6 ++ .idea/libraries/Dart_SDK.xml | 32 ++++-- .idea/libraries/Flutter_Plugins.xml | 9 ++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 101 +++++++++++------- android/build.gradle | 4 + .../flutter_split/FlutterSplitPlugin.java | 94 +++++++++++++++- example/android/gradle.properties | 1 + example/lib/main.dart | 12 ++- flutter_split.iml | 3 +- lib/flutter_split.dart | 30 ++++++ 11 files changed, 247 insertions(+), 51 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/libraries/Flutter_Plugins.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..c83a369 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/libraries/Dart_SDK.xml b/.idea/libraries/Dart_SDK.xml index b209152..4d50be1 100644 --- a/.idea/libraries/Dart_SDK.xml +++ b/.idea/libraries/Dart_SDK.xml @@ -1,17 +1,27 @@ - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/libraries/Flutter_Plugins.xml b/.idea/libraries/Flutter_Plugins.xml new file mode 100644 index 0000000..53449da --- /dev/null +++ b/.idea/libraries/Flutter_Plugins.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4bc641a..bb8e09d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,45 +1,74 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + - - - - - + + - - - - - - - + + + - + + + + - + + + + + + + 1620111596787 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/android/build.gradle b/android/build.gradle index 3d8457c..16826ec 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -31,3 +31,7 @@ android { disable 'InvalidPackage' } } + +dependencies{ + implementation 'io.split.client:android-client:2.7.0' +} diff --git a/android/src/main/java/com/imumz/flutter_split/FlutterSplitPlugin.java b/android/src/main/java/com/imumz/flutter_split/FlutterSplitPlugin.java index a6438a3..c62ec78 100644 --- a/android/src/main/java/com/imumz/flutter_split/FlutterSplitPlugin.java +++ b/android/src/main/java/com/imumz/flutter_split/FlutterSplitPlugin.java @@ -1,13 +1,29 @@ package com.imumz.flutter_split; +import android.content.Context; + import androidx.annotation.NonNull; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeoutException; + import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; +import io.split.android.client.SplitClient; +import io.split.android.client.SplitClientConfig; +import io.split.android.client.SplitFactory; +import io.split.android.client.SplitFactoryBuilder; +import io.split.android.client.api.Key; +import io.split.android.client.events.SplitEvent; +import io.split.android.client.events.SplitEventTask; /** FlutterSplitPlugin */ public class FlutterSplitPlugin implements FlutterPlugin, MethodCallHandler { @@ -16,10 +32,23 @@ public class FlutterSplitPlugin implements FlutterPlugin, MethodCallHandler { /// This local reference serves to register the plugin with the Flutter Engine and unregister it /// when the Flutter Engine is detached from the Activity private MethodChannel channel; + private Context appContext; + + + //"52bo50e6gavj7dpma3b0l5j2vpqep0f146nj" + private String apikey; + + //Split + private SplitClient client; + + // error codes + private static String SDK_NOT_INITIALIZED = "SNI001"; + @Override public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_split"); + this.appContext = flutterPluginBinding.getApplicationContext(); channel.setMethodCallHandler(this); } @@ -27,7 +56,70 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { if (call.method.equals("getPlatformVersion")) { result.success("Android " + android.os.Build.VERSION.RELEASE); - } else { + }else if(call.method.equals("initializeSdk")){ + + this.apikey = call.argument("appKey"); + SplitClientConfig config = SplitClientConfig.builder().build(); + + // Create a new user key to be evaluated + String matchingKey = "key"; + Key k = new Key(matchingKey); + // Create factory + SplitFactory splitFactory = null; + try { + splitFactory = SplitFactoryBuilder.build(this.apikey, k, config, this.appContext); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (TimeoutException e) { + e.printStackTrace(); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + // Get Split Client instance + this.client = splitFactory.client(); + + }else if(call.method.equals("trackEvent")){ + String eventType = call.argument("eventName"); + HashMap map = new HashMap(); + if(this.client!=null){ + boolean res = client.track(eventType,map); + if(res){ + result.success(true); + }else{ + + } + }else{ + result.error(SDK_NOT_INITIALIZED,"Sdk is not initialized",""); + } + }else if(call.method.equals("getTreatment")){ + String key = call.argument("key"); + HashMap attr = call.argument("attributes"); + if(this.client!=null){ + String treatment = client.getTreatment(key,attr); + result.success(treatment); + }else{ + result.error(SDK_NOT_INITIALIZED,"Sdk is not initialized",""); + } + }else if(call.method.equals("getTreatments")){ + List keys = call.argument("keys"); + HashMap attr = call.argument("attributes"); + if(this.client!=null){ + Map treatment = client.getTreatments(keys,attr); + result.success(treatment); + }else{ + result.error(SDK_NOT_INITIALIZED,"Sdk is not initialized",""); + } + }else if(call.method.equals("dispose")){ + if(this.client!=null){ + this.client.destroy(); + result.success(true); + }else{ + result.error(SDK_NOT_INITIALIZED,"Sdk is not initialized",""); + } + } + else { result.notImplemented(); } } diff --git a/example/android/gradle.properties b/example/android/gradle.properties index 94adc3a..a673820 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1,3 +1,4 @@ org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true +android.enableR8=true diff --git a/example/lib/main.dart b/example/lib/main.dart index 3e51bfa..cc43313 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -15,9 +15,11 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { String _platformVersion = 'Unknown'; + FlutterSplit flutterSplit = FlutterSplit(); @override void initState() { + flutterSplit.initializeSdk(); super.initState(); initPlatformState(); } @@ -49,8 +51,14 @@ class _MyAppState extends State { appBar: AppBar( title: const Text('Plugin example app'), ), - body: Center( - child: Text('Running on: $_platformVersion\n'), + body: Column( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + Text('Running on: $_platformVersion\n'), + FlatButton(onPressed: ()async{ + print(await flutterSplit.getTreatment('', {})); + }, child: Text('treamtemt')) + ], ), ), ); diff --git a/flutter_split.iml b/flutter_split.iml index 429df7d..022d450 100644 --- a/flutter_split.iml +++ b/flutter_split.iml @@ -8,11 +8,12 @@ + + - diff --git a/lib/flutter_split.dart b/lib/flutter_split.dart index 33908cd..f330fce 100644 --- a/lib/flutter_split.dart +++ b/lib/flutter_split.dart @@ -11,4 +11,34 @@ class FlutterSplit { final String version = await _channel.invokeMethod('getPlatformVersion'); return version; } + + Future initializeSdk(String appKey)async{ + Map attr = {}; + attr['appKey'] = appKey; + await _channel.invokeMethod('initializeSdk',attr); + } + + Future getTreatment(String key,Map attr)async{ + Map attributes = { + "kes":key, + "attributes":attr + }; + return await _channel.invokeMethod('getTreatment',attributes); + } + + Future getTreatments(List keys, Map attr)async{ + Map attributes = { + "keys":keys, + "attributes":attr + }; + return await _channel.invokeMethod('getTreatments',attributes); + } + + Future dispose()async{ + await _channel.invokeMethod('dispose'); + } + + static Future trackEvent(String eventName,Map props)async{ + + } } From 3916c64ddda0807bea8cdd52cc954526adddaa4c Mon Sep 17 00:00:00 2001 From: guptaShantanu Date: Tue, 18 May 2021 11:12:57 +0530 Subject: [PATCH 02/15] android implementation --- .idea/workspace.xml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bb8e09d..3930f4e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,14 +2,7 @@ - - - - - - -