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

Async init #7

Merged
merged 8 commits into from
Jan 21, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ android {
disable 'InvalidPackage'
}
dependencies {
implementation 'com.optimizely.ab:android-sdk:3.7.0'
implementation 'com.optimizely.ab:android-sdk:3.8.0'
implementation 'com.noveogroup.android:android-logger:1.3.6'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package com.policygenius.optimizely_plugin

import android.app.Activity
import androidx.annotation.NonNull
import com.noveogroup.android.log.Log
import com.optimizely.ab.android.sdk.OptimizelyClient
import com.optimizely.ab.android.sdk.OptimizelyManager
import com.optimizely.ab.optimizelyjson.OptimizelyJSON
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
Expand All @@ -14,7 +12,7 @@ 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 java.io.Console


/** OptimizelyPlugin */
class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
Expand Down Expand Up @@ -57,6 +55,11 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
initOptimizelyManager(sdkKey!!, dataFile!!)
result.success("")
}
"initOptimizelyManagerAsync" -> {
val sdkKey = call.argument<String>("sdk_key")
initOptimizelyManagerAsync(sdkKey!!)
result.success("")
}
"isFeatureEnabled" -> {
val featureKey = call.argument<String>("feature_key")
val userId = call.argument<String>("user_id")
Expand All @@ -66,7 +69,7 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
"getAllFeatureVariables" -> {
val featureKey = call.argument<String>("feature_key")
val userId = call.argument<String>("user_id")
val attributes = call.argument<MutableMap<String,Any>>("attributes")
val attributes = call.argument<MutableMap<String, Any>>("attributes")
val variables = getAllFeatureVariables(featureKey!!, userId!!, attributes!!)
result.success(variables)
}
Expand Down Expand Up @@ -107,6 +110,21 @@ class OptimizelyPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
optimizelyClient = optimizelyManager.initialize(activity.applicationContext, dataFile, true, true)
}

private fun initOptimizelyManagerAsync(sdkKey: String) {
val builder = OptimizelyManager.builder()
// In Android, the minimum polling interval is 60 seconds. In iOS, the minimum polling
// interval is 2 minutes while the app is open. If you specify shorter polling intervals
// than these minimums, the SDK will automatically reset the intervals to 60 seconds (Android)
// or 2 minutes (iOS).
val optimizelyManager = builder.withDatafileDownloadInterval(60)
.withSDKKey(sdkKey)
.build(activity.applicationContext)

optimizelyManager.initialize(activity.applicationContext, null) {
client -> optimizelyClient = client
}
}

private fun isFeatureEnabled(featureKey: String, userId: String): Boolean{
val flag = optimizelyClient.isFeatureEnabled(featureKey, userId)

Expand Down
8 changes: 7 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
setupOptimizely();
setupOptimizelyAsync();
}

void setupOptimizely() async {
Expand All @@ -35,6 +35,12 @@ class _MyAppState extends State<MyApp> {
);
}

void setupOptimizelyAsync() async {
await optimizelyPlugin.initOptimizelyManagerAsync(
'your_optimizely_sdk_key',
);
}

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> getPriceFilterFlag() async {
String priceFilterFlag;
Expand Down
47 changes: 43 additions & 4 deletions ios/Classes/SwiftOptimizelyPlugin.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Flutter
import Optimizely

enum InitResult {
case success
case failure(Error)
}

public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {

typealias GetFeatureItems = (featureKey: String, userId: String, attributes: OptimizelyAttributes?)
Expand Down Expand Up @@ -34,12 +39,35 @@ public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {
sdkKey: sdkKey,
periodicDownloadInterval: 60
)

try startClient(client, dataFile: dataFile)
self.client = client

result(nil)
} catch {
result(error)
result(error.localizedDescription)
}
case "initOptimizelyManagerAsync":
do {
let sdkKey: String = try arguments.argument(for: "sdk_key")
let client = OptimizelyClient(
sdkKey: sdkKey,
periodicDownloadInterval: 60
)

try startClient(client) { initResult in
switch initResult {
case .failure(let error):
result(error.localizedDescription)
case .success:
result(nil)
}
}

self.client = client
} catch {
result(error.localizedDescription)
}
case "isFeatureEnabled":
do {
let client = try ensureClient()
Expand All @@ -51,7 +79,7 @@ public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {
)
result(enabled)
} catch {
result(error)
result(error.localizedDescription)
}
case "getAllFeatureVariables":
do {
Expand All @@ -64,7 +92,7 @@ public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {
)
result(json.toMap())
} catch {
result(error)
result(error.localizedDescription)
}
default:
result(FlutterMethodNotImplemented)
Expand All @@ -89,7 +117,18 @@ public class SwiftOptimizelyPlugin: NSObject, FlutterPlugin {
client.start()
}
}


func startClient(_ client: OptimizelyClient, completion: @escaping (InitResult) -> Void) throws {
client.start { clientResult in
switch clientResult {
case .failure(let error):
completion(.failure(error))
case .success(_):
completion(.success)
}
}
}

func getFeatureItems(from arguments: [String: Any]) throws -> GetFeatureItems {
let featureKey: String = try arguments.argument(for: "feature_key")
let userId: String = try arguments.argument(for: "user_id")
Expand Down
2 changes: 1 addition & 1 deletion ios/optimizely_plugin.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Flutter plugin for optimizely SDK
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.dependency 'OptimizelySwiftSDK'
s.dependency 'OptimizelySwiftSDK', '~> 3.6.1'
s.platform = :ios, '10.0'

# Flutter.framework does not contain a i386 slice. Only x86_64 simulators are supported.
Expand Down
9 changes: 9 additions & 0 deletions lib/optimizely_plugin.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';

import 'package:flutter/services.dart';

class OptimizelyPlugin {
Expand All @@ -17,6 +18,14 @@ class OptimizelyPlugin {
});
}

Future<void> initOptimizelyManagerAsync(
String sdkKey,
) async {
await _channel.invokeMethod('initOptimizelyManagerAsync', <String, dynamic>{
'sdk_key': sdkKey,
});
}

Future<bool> isFeatureEnabled(
String featureKey,
userID,
Expand Down