-
Notifications
You must be signed in to change notification settings - Fork 38
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
Support on feature flags loaded #111
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,10 @@ 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 kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.launch | ||
|
||
/** PosthogFlutterPlugin */ | ||
class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler { | ||
|
@@ -29,6 +33,8 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler { | |
channel.setMethodCallHandler(this) | ||
} | ||
|
||
private val isLoaded = MutableStateFlow(false); | ||
|
||
private fun initPlugin(applicationContext: Context) { | ||
try { | ||
// TODO: replace deprecated method API 33 | ||
|
@@ -53,6 +59,9 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler { | |
debug = enableDebug | ||
sdkName = "posthog-flutter" | ||
sdkVersion = postHogVersion | ||
onFeatureFlags = PostHogOnFeatureFlags { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the callback is a good idea but it'd not work in some cases, for example |
||
isLoaded.value = true; | ||
} | ||
} | ||
PostHogAndroid.setup(applicationContext, config) | ||
|
||
|
@@ -64,74 +73,25 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler { | |
override fun onMethodCall(call: MethodCall, result: Result) { | ||
|
||
when (call.method) { | ||
|
||
"identify" -> { | ||
identify(call, result) | ||
} | ||
|
||
"capture" -> { | ||
capture(call, result) | ||
} | ||
|
||
"screen" -> { | ||
screen(call, result) | ||
} | ||
|
||
"alias" -> { | ||
alias(call, result) | ||
} | ||
|
||
"distinctId" -> { | ||
distinctId(result) | ||
} | ||
|
||
"reset" -> { | ||
reset(result) | ||
} | ||
|
||
"disable" -> { | ||
disable(result) | ||
} | ||
|
||
"enable" -> { | ||
enable(result) | ||
} | ||
|
||
"isFeatureEnabled" -> { | ||
isFeatureEnabled(call, result) | ||
} | ||
|
||
"reloadFeatureFlags" -> { | ||
reloadFeatureFlags(result) | ||
} | ||
|
||
"group" -> { | ||
group(call, result) | ||
} | ||
|
||
"getFeatureFlag" -> { | ||
getFeatureFlag(call, result) | ||
} | ||
|
||
"getFeatureFlagPayload" -> { | ||
getFeatureFlagPayload(call, result) | ||
} | ||
|
||
"register" -> { | ||
register(call, result) | ||
} | ||
"unregister" -> { | ||
unregister(call, result) | ||
} | ||
"debug" -> { | ||
debug(call, result) | ||
} | ||
"flush" -> { | ||
flush(result) | ||
} | ||
else -> { | ||
result.notImplemented() | ||
} | ||
"identify" -> identify(call, result) | ||
"capture" -> capture(call, result) | ||
"screen" -> screen(call, result) | ||
"alias" -> alias(call, result) | ||
"distinctId" -> distinctId(result) | ||
"reset" -> reset(result) | ||
"disable" -> disable(result) | ||
"enable" -> enable(result) | ||
"isFeatureEnabled" -> isFeatureEnabled(call, result) | ||
"reloadFeatureFlags" -> reloadFeatureFlags(result) | ||
"group" -> group(call, result) | ||
"getFeatureFlag" -> getFeatureFlag(call, result) | ||
"getFeatureFlagPayload" -> getFeatureFlagPayload(call, result) | ||
"register" -> register(call, result) | ||
"unregister" -> unregister(call, result) | ||
"debug" -> debug(call, result) | ||
"flush" -> flush(result) | ||
"awaitFeatureFlagsLoaded" -> awaitFeatureFlagsLoaded(result) | ||
else -> result.notImplemented() | ||
} | ||
|
||
} | ||
|
@@ -269,6 +229,20 @@ class PosthogFlutterPlugin : FlutterPlugin, MethodCallHandler { | |
} | ||
} | ||
|
||
private fun awaitFeatureFlagsLoaded(result: Result){ | ||
try { | ||
GlobalScope.launch { | ||
isLoaded.collect { isLoaded -> | ||
if(isLoaded){ | ||
result.success() | ||
} | ||
} | ||
} | ||
} catch (error: Throwable){ | ||
result.error("PosthogFlutterException", e.localizedMessage, null) | ||
} | ||
} | ||
|
||
private fun group(call: MethodCall, result: Result) { | ||
try { | ||
val groupType: String = call.argument("groupType")!! | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ import AppKit | |
#endif | ||
|
||
public class PosthogFlutterPlugin: NSObject, FlutterPlugin { | ||
|
||
let isLoadedSubject = CurrentValueSubject<Bool, Error>(false) | ||
|
||
public static func register(with registrar: FlutterPluginRegistrar) { | ||
#if os(iOS) | ||
let channel = FlutterMethodChannel(name: "posthog_flutter", binaryMessenger: registrar.messenger()) | ||
|
@@ -44,6 +47,9 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin { | |
postHogSdkName = "posthog-flutter" | ||
postHogVersion = postHogFlutterVersion | ||
|
||
NotificationCenter.default.addObserver(forName: PostHogSDK.didReceiveFeatureFlags, object: nil, queue: nil) { (notification) in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to https://github.com/PostHog/posthog-flutter/pull/111/files#r1624601103 |
||
isLoadedSubject.send(true) | ||
} | ||
PostHogSDK.shared.setup(config) | ||
// | ||
} | ||
|
@@ -84,6 +90,8 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin { | |
unregister(call, result: result) | ||
case "flush": | ||
flush(result) | ||
case "awaitFeatureFlagsLoaded": | ||
awaitFeatureFlagsLoaded(result) | ||
default: | ||
result(FlutterMethodNotImplemented) | ||
} | ||
|
@@ -242,6 +250,19 @@ public class PosthogFlutterPlugin: NSObject, FlutterPlugin { | |
result(nil) | ||
} | ||
|
||
private func awaitFeatureFlagsLoaded(_ result: @escaping FlutterResult | ||
) { | ||
do { | ||
isLoadedSubject.sink { completion in | ||
if completion { | ||
result(nil) | ||
} | ||
} | ||
} catch let error { | ||
result(FlutterError(code: "PosthogFlutterException", message: error.localizedDescription, details: nil)) | ||
} | ||
} | ||
|
||
private func group( | ||
_ call: FlutterMethodCall, | ||
result: @escaping FlutterResult | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd not use coroutines but rather implement a dependency-free mechanism, there are a few days to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense