From f78a9f1c40f078b12700a0049c628831706511fe Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 28 Mar 2024 16:58:08 +0900 Subject: [PATCH 1/9] [flutter] move parseEvent to utils --- flutter/nativebrik_bridge/lib/embedding.dart | 28 ++----------------- .../lib/utils/parse_event.dart | 26 +++++++++++++++++ 2 files changed, 28 insertions(+), 26 deletions(-) create mode 100644 flutter/nativebrik_bridge/lib/utils/parse_event.dart diff --git a/flutter/nativebrik_bridge/lib/embedding.dart b/flutter/nativebrik_bridge/lib/embedding.dart index 63adda2..f388cb3 100644 --- a/flutter/nativebrik_bridge/lib/embedding.dart +++ b/flutter/nativebrik_bridge/lib/embedding.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:nativebrik_bridge/remote_config.dart'; import 'package:nativebrik_bridge/utils/random.dart'; +import 'package:nativebrik_bridge/utils/parse_event.dart'; import './channel/nativebrik_bridge_platform_interface.dart'; enum EventPayloadType { integer, string, timestamp, unknown } @@ -122,7 +123,7 @@ class _EmbeddingState extends State { return Future.value(true); case 'on-event': if (widget.onEvent == null) return Future.value(false); - widget.onEvent?.call(_parseEvent(call.arguments)); + widget.onEvent?.call(parseEvent(call.arguments)); return Future.value(true); default: return Future.value(false); @@ -196,28 +197,3 @@ class _BridgeView extends StatelessWidget { } } } - -EventPayloadType _parseEventPayloadType(dynamic type) { - switch (type) { - case "INTEGER": - return EventPayloadType.integer; - case "STRING": - return EventPayloadType.string; - case "TIMESTAMPZ": - return EventPayloadType.timestamp; - default: - return EventPayloadType.unknown; - } -} - -Event _parseEvent(dynamic arguments) { - final map = arguments; - final name = map["name"] as String?; - final deepLink = map["deepLink"] as String?; - final rawPayload = map["payload"] as List? ?? []; - final payload = rawPayload - .map((e) => EventPayload(e["name"] as String? ?? "", - e["value"] as String? ?? "", _parseEventPayloadType(e["type"]))) - .toList(); - return Event(name, deepLink, payload); -} diff --git a/flutter/nativebrik_bridge/lib/utils/parse_event.dart b/flutter/nativebrik_bridge/lib/utils/parse_event.dart new file mode 100644 index 0000000..3ce8f51 --- /dev/null +++ b/flutter/nativebrik_bridge/lib/utils/parse_event.dart @@ -0,0 +1,26 @@ +import 'package:nativebrik_bridge/embedding.dart'; + +EventPayloadType _parseEventPayloadType(dynamic type) { + switch (type) { + case "INTEGER": + return EventPayloadType.integer; + case "STRING": + return EventPayloadType.string; + case "TIMESTAMPZ": + return EventPayloadType.timestamp; + default: + return EventPayloadType.unknown; + } +} + +Event parseEvent(dynamic arguments) { + final map = arguments; + final name = map["name"] as String?; + final deepLink = map["deepLink"] as String?; + final rawPayload = map["payload"] as List? ?? []; + final payload = rawPayload + .map((e) => EventPayload(e["name"] as String? ?? "", + e["value"] as String? ?? "", _parseEventPayloadType(e["type"]))) + .toList(); + return Event(name, deepLink, payload); +} From b8d80dd29b1e92cf74aaa2e0ce7b448bb2bd2e77 Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 28 Mar 2024 16:58:33 +0900 Subject: [PATCH 2/9] [flutter] support global event handler to bridge client --- .../lib/nativebrik_bridge.dart | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/flutter/nativebrik_bridge/lib/nativebrik_bridge.dart b/flutter/nativebrik_bridge/lib/nativebrik_bridge.dart index e56cfae..6447749 100644 --- a/flutter/nativebrik_bridge/lib/nativebrik_bridge.dart +++ b/flutter/nativebrik_bridge/lib/nativebrik_bridge.dart @@ -1,3 +1,6 @@ +import 'package:flutter/services.dart'; +import 'package:nativebrik_bridge/embedding.dart'; +import 'package:nativebrik_bridge/utils/parse_event.dart'; import 'channel/nativebrik_bridge_platform_interface.dart'; /// A bridge client to the nativebrik SDK. @@ -11,12 +14,36 @@ import 'channel/nativebrik_bridge_platform_interface.dart'; /// ``` class NativebrikBridge { final String projectId; + final List _listeners = []; + final MethodChannel _channel = const MethodChannel("nativebrik_bridge"); NativebrikBridge(this.projectId) { NativebrikBridgePlatform.instance.connectClient(projectId); + _channel.setMethodCallHandler(_handleMethod); } Future getNativebrikSDKVersion() { return NativebrikBridgePlatform.instance.getNativebrikSDKVersion(); } + + addEventListener(EventHandler listener) { + _listeners.add(listener); + } + + removeEventListener(EventHandler listener) { + _listeners.remove(listener); + } + + Future _handleMethod(MethodCall call) async { + switch (call.method) { + case 'on-event': + final event = parseEvent(call.arguments); + for (var listener in _listeners) { + listener(event); + } + return Future.value(true); + default: + return Future.value(true); + } + } } From f57cc03af07c47e2a77da59e6641a95f49c41622 Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 28 Mar 2024 16:58:44 +0900 Subject: [PATCH 3/9] [flutter] Add event listener for Nativebrik and update print statements --- flutter/nativebrik_bridge/example/lib/main.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/flutter/nativebrik_bridge/example/lib/main.dart b/flutter/nativebrik_bridge/example/lib/main.dart index bc85220..7e2081b 100644 --- a/flutter/nativebrik_bridge/example/lib/main.dart +++ b/flutter/nativebrik_bridge/example/lib/main.dart @@ -34,6 +34,10 @@ class _MyAppState extends State { // setState to update our non-existent appearance. if (!mounted) return; + nativebrik.addEventListener((event) { + print("Nativebrik Global Embedding Event: $event"); + }); + var config = NativebrikRemoteConfig("cnoku4223akg00e5m630"); var variant = await config.fetch(); var message = await variant.get("message"); @@ -55,7 +59,7 @@ class _MyAppState extends State { children: [ NativebrikEmbedding("TOP_COMPONENT", height: 270, onEvent: (event) { - print("Nativebrik Event: ${event.payload}"); + print("Nativebrik Embedding Event: ${event.payload}"); }), const Text("Text 2"), Text(_message), From d37bae21fe724fae2e7b2ce2fd7443202278cda0 Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 28 Mar 2024 16:59:12 +0900 Subject: [PATCH 4/9] [flutter] Update Nativebrik dep version to 0.5.5 --- flutter/nativebrik_bridge/example/ios/Podfile.lock | 8 ++++---- flutter/nativebrik_bridge/ios/nativebrik_bridge.podspec | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/flutter/nativebrik_bridge/example/ios/Podfile.lock b/flutter/nativebrik_bridge/example/ios/Podfile.lock index 5195226..19a7108 100644 --- a/flutter/nativebrik_bridge/example/ios/Podfile.lock +++ b/flutter/nativebrik_bridge/example/ios/Podfile.lock @@ -2,11 +2,11 @@ PODS: - Flutter (1.0.0) - integration_test (0.0.1): - Flutter - - Nativebrik (0.5.0): + - Nativebrik (0.5.5): - YogaKit (~> 2.0.0) - nativebrik_bridge (0.0.1): - Flutter - - Nativebrik (~> 0.5.0) + - Nativebrik (~> 0.5.5) - Yoga (2.0.1) - YogaKit (2.0.1): - Yoga (~> 2.0.1) @@ -33,8 +33,8 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7 integration_test: 13825b8a9334a850581300559b8839134b124670 - Nativebrik: cce9625971439ed9b63b649c60b0d11f3e525e75 - nativebrik_bridge: 6256bcf5bdd5ad5d5e4069f45b3d9f444275e244 + Nativebrik: 6d51f3f905e5535b5638e1d77dbf44f60b9dd411 + nativebrik_bridge: 5d36b3d759d3170aa00841f2e0514d169d064a9c Yoga: 7ac7f65e2d7120bf97e75fd66436ed4c7bf8e37e YogaKit: 4751ba673fab49df29e2235e23c4ea7d6d5a339c diff --git a/flutter/nativebrik_bridge/ios/nativebrik_bridge.podspec b/flutter/nativebrik_bridge/ios/nativebrik_bridge.podspec index 589c6ff..4ae0550 100644 --- a/flutter/nativebrik_bridge/ios/nativebrik_bridge.podspec +++ b/flutter/nativebrik_bridge/ios/nativebrik_bridge.podspec @@ -15,7 +15,7 @@ A new Flutter plugin project. s.source = { :path => '.' } s.source_files = 'Classes/**/*' s.dependency 'Flutter' - s.dependency 'Nativebrik', '~> 0.5.0' + s.dependency 'Nativebrik', '~> 0.5.5' s.ios.deployment_target = '14.0' s.platform = :ios, '14.0' From 40e3f7ff1c0d0d2a4e570e5e085194c35a28f08c Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 28 Mar 2024 16:59:27 +0900 Subject: [PATCH 5/9] [flutter][ios] Refactor NativebrikBridgePlugin initialization and add event handling --- .../ios/Classes/NativebrikBridgePlugin.swift | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/flutter/nativebrik_bridge/ios/Classes/NativebrikBridgePlugin.swift b/flutter/nativebrik_bridge/ios/Classes/NativebrikBridgePlugin.swift index 05f421c..0217abd 100644 --- a/flutter/nativebrik_bridge/ios/Classes/NativebrikBridgePlugin.swift +++ b/flutter/nativebrik_bridge/ios/Classes/NativebrikBridgePlugin.swift @@ -9,16 +9,18 @@ let ON_EVENT_METHOD = "on-event" public class NativebrikBridgePlugin: NSObject, FlutterPlugin { private let manager: NativebrikBridgeManager private let messenger: FlutterBinaryMessenger - init(messenger: FlutterBinaryMessenger, manager: NativebrikBridgeManager) { + private let channel: FlutterMethodChannel + init(messenger: FlutterBinaryMessenger, manager: NativebrikBridgeManager, channel: FlutterMethodChannel) { self.messenger = messenger self.manager = manager + self.channel = channel super.init() } public static func register(with registrar: FlutterPluginRegistrar) { let manager = NativebrikBridgeManager() let messenger = registrar.messenger() let channel = FlutterMethodChannel(name: "nativebrik_bridge", binaryMessenger: messenger) - let instance = NativebrikBridgePlugin(messenger: messenger, manager: manager) + let instance = NativebrikBridgePlugin(messenger: messenger, manager: manager, channel: channel) registrar.addMethodCallDelegate(instance, channel: channel) registrar.register( FLNativeViewFactory(messenger: messenger, manager: manager), @@ -32,7 +34,19 @@ public class NativebrikBridgePlugin: NSObject, FlutterPlugin { result(nativebrikSdkVersion) case "connectClient": let projectId = call.arguments as! String - self.manager.setNativebrikClient(nativebrik: NativebrikClient(projectId: projectId)) + self.manager.setNativebrikClient(nativebrik: NativebrikClient(projectId: projectId, onEvent: { [weak self] event in + self?.channel.invokeMethod(ON_EVENT_METHOD, arguments: [ + "name": event.name as Any?, + "deepLink": event.deepLink as Any?, + "payload": event.payload?.map({ prop in + return [ + "name": prop.name, + "value": prop.value, + "type": prop.type + ] + }), + ]) + })) result("ok") // embedding From e8246bf68c5bc4d9c88097a05e02de3f08afcdfb Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 28 Mar 2024 16:59:52 +0900 Subject: [PATCH 6/9] [flutter][ios] Refactor method name and import SDK version --- .../nativebrik_bridge/NativebrikBridgePluginTest.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/flutter/nativebrik_bridge/android/src/test/kotlin/com/nativebrik/flutter/nativebrik_bridge/NativebrikBridgePluginTest.kt b/flutter/nativebrik_bridge/android/src/test/kotlin/com/nativebrik/flutter/nativebrik_bridge/NativebrikBridgePluginTest.kt index 4bd92c0..8ceec64 100644 --- a/flutter/nativebrik_bridge/android/src/test/kotlin/com/nativebrik/flutter/nativebrik_bridge/NativebrikBridgePluginTest.kt +++ b/flutter/nativebrik_bridge/android/src/test/kotlin/com/nativebrik/flutter/nativebrik_bridge/NativebrikBridgePluginTest.kt @@ -4,6 +4,7 @@ import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel import kotlin.test.Test import org.mockito.Mockito +import com.nativebrik.sdk.VERSION /* * This demonstrates a simple unit test of the Kotlin portion of this plugin's implementation. @@ -15,13 +16,13 @@ import org.mockito.Mockito internal class NativebrikBridgePluginTest { @Test - fun onMethodCall_getPlatformVersion_returnsExpectedValue() { + fun onMethodCall_getNativebrikSDKVersion_returnsExpectedValue() { val plugin = NativebrikBridgePlugin() - val call = MethodCall("getPlatformVersion", null) + val call = MethodCall("getNativebrikSDKVersion", null) val mockResult: MethodChannel.Result = Mockito.mock(MethodChannel.Result::class.java) plugin.onMethodCall(call, mockResult) - Mockito.verify(mockResult).success("Android " + android.os.Build.VERSION.RELEASE) + Mockito.verify(mockResult).success(VERSION) } } From 4365cc74f2840ea36b36b31394eabe4e15f49821 Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 28 Mar 2024 17:00:09 +0900 Subject: [PATCH 7/9] [flutter][android] Update nativebrik SDK dep version to 0.1.1 --- flutter/nativebrik_bridge/android/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flutter/nativebrik_bridge/android/build.gradle b/flutter/nativebrik_bridge/android/build.gradle index 6f54f4e..76e715d 100644 --- a/flutter/nativebrik_bridge/android/build.gradle +++ b/flutter/nativebrik_bridge/android/build.gradle @@ -58,7 +58,7 @@ android { } dependencies { - implementation 'com.nativebrik:sdk:0.1.0' + implementation 'com.nativebrik:sdk:0.1.1' implementation 'androidx.activity:activity-compose:1.8.2' testImplementation 'org.jetbrains.kotlin:kotlin-test' testImplementation 'org.mockito:mockito-core:5.0.0' From c9ff765dff344cd51c9248665433a52153b9563f Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 28 Mar 2024 17:00:52 +0900 Subject: [PATCH 8/9] [flutter][android] support global event handler, and if condition to when condition --- .../NativebrikBridgePlugin.kt | 127 +++++++++++------- 1 file changed, 77 insertions(+), 50 deletions(-) diff --git a/flutter/nativebrik_bridge/android/src/main/kotlin/com/nativebrik/flutter/nativebrik_bridge/NativebrikBridgePlugin.kt b/flutter/nativebrik_bridge/android/src/main/kotlin/com/nativebrik/flutter/nativebrik_bridge/NativebrikBridgePlugin.kt index 5f48b98..3967c12 100644 --- a/flutter/nativebrik_bridge/android/src/main/kotlin/com/nativebrik/flutter/nativebrik_bridge/NativebrikBridgePlugin.kt +++ b/flutter/nativebrik_bridge/android/src/main/kotlin/com/nativebrik/flutter/nativebrik_bridge/NativebrikBridgePlugin.kt @@ -49,57 +49,84 @@ class NativebrikBridgePlugin: FlutterPlugin, MethodCallHandler { @OptIn(DelicateCoroutinesApi::class) override fun onMethodCall(call: MethodCall, result: Result) { - if (call.method == "getNativebrikSDKVersion") { - result.success(VERSION) - } else if (call.method == "connectClient") { - val projectId = call.arguments as String - if (projectId.isEmpty()) { - result.success("ok") - return - } - val client = NativebrikClient(Config(projectId), context) - this.manager.setNativebrikClient(client) - result.success("ok") - } else if (call.method == "connectEmbedding") { - val channelId = call.argument("channelId") as String - val id = call.argument("id") as String - this.manager.connectEmbedding(channelId, id) - result.success("ok") - } else if (call.method == "disconnectEmbedding") { - val channelId = call.arguments as String - this.manager.disconnectEmbedding(channelId) - result.success("ok") - } else if (call.method == "connectRemoteConfig") { - val channelId = call.argument("channelId") as String - val id = call.argument("id") as String - GlobalScope.launch(Dispatchers.IO) { - manager.connectRemoteConfig(channelId, id).onSuccess { - result.success(it) - }.onFailure { - result.success("failed") + when (call.method) { + "getNativebrikSDKVersion" -> { + result.success(VERSION) + } + "connectClient" -> { + val projectId = call.arguments as String + if (projectId.isEmpty()) { + result.success("no") + return } - } - } else if (call.method == "disconnectRemoteConfig") { - val channelId = call.arguments as String - this.manager.disconnectRemoteConfig(channelId) - result.success("ok") - } else if (call.method == "getRemoteConfigValue") { - val channelId = call.argument("channelId") as String - val key = call.argument("key") as String - val value = this.manager.getRemoteConfigValue(channelId, key) - result.success(value) - } else if (call.method == "connectEmbeddingInRemoteConfigValue") { - val channelId = call.argument("channelId") as String - val embeddingChannelId = call.argument("embeddingChannelId") as String - val key = call.argument("key") as String - this.manager.connectEmbeddingInRemoteConfigValue(channelId, key, embeddingChannelId) - result.success("ok") - } else if (call.method == "dispatch") { - val event = call.arguments as String - this.manager.dispatch(event) - result.success("ok") - } else { - result.notImplemented() + val client = NativebrikClient( + Config( + projectId, + onEvent = { it -> + this.channel.invokeMethod(ON_EVENT_METHOD, mapOf( + "name" to it.name, + "deepLink" to it.deepLink, + "payload" to it.payload?.map { prop -> + mapOf( + "name" to prop.name, + "value" to prop.value, + "type" to prop.type, + ) + } + )) + } + ), context) + this.manager.setNativebrikClient(client) + result.success("ok") + } + "connectEmbedding" -> { + val channelId = call.argument("channelId") as String + val id = call.argument("id") as String + this.manager.connectEmbedding(channelId, id) + result.success("ok") + } + "disconnectEmbedding" -> { + val channelId = call.arguments as String + this.manager.disconnectEmbedding(channelId) + result.success("ok") + } + "connectRemoteConfig" -> { + val channelId = call.argument("channelId") as String + val id = call.argument("id") as String + GlobalScope.launch(Dispatchers.IO) { + manager.connectRemoteConfig(channelId, id).onSuccess { + result.success(it) + }.onFailure { + result.success("failed") + } + } + } + "disconnectRemoteConfig" -> { + val channelId = call.arguments as String + this.manager.disconnectRemoteConfig(channelId) + result.success("ok") + } + "getRemoteConfigValue" -> { + val channelId = call.argument("channelId") as String + val key = call.argument("key") as String + val value = this.manager.getRemoteConfigValue(channelId, key) + result.success(value) + } + "connectEmbeddingInRemoteConfigValue" -> { + val channelId = call.argument("channelId") as String + val embeddingChannelId = call.argument("embeddingChannelId") as String + val key = call.argument("key") as String + this.manager.connectEmbeddingInRemoteConfigValue(channelId, key, embeddingChannelId) + result.success("ok") + } + "dispatch" -> { + val event = call.arguments as String + this.manager.dispatch(event) + result.success("ok") + } + else -> { + result.notImplemented() + } } } From 56d85b2f477c8574c4d59078ecd82c45c34addc2 Mon Sep 17 00:00:00 2001 From: RyosukeCla Date: Thu, 28 Mar 2024 17:01:36 +0900 Subject: [PATCH 9/9] [flutter] Update version to 0.2.0 in pubspec.yaml --- flutter/nativebrik_bridge/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flutter/nativebrik_bridge/pubspec.yaml b/flutter/nativebrik_bridge/pubspec.yaml index aa83b1e..08482d1 100644 --- a/flutter/nativebrik_bridge/pubspec.yaml +++ b/flutter/nativebrik_bridge/pubspec.yaml @@ -1,6 +1,6 @@ name: nativebrik_bridge description: "Nativebrik bridge sdk for flutter" -version: 0.1.0 +version: 0.2.0 homepage: "https://nativebrik.com" repository: "https://github.com/plaidev/nativebrik-sdk"