Skip to content

Commit

Permalink
Merge pull request #21 from plaidev/android-1
Browse files Browse the repository at this point in the history
[flutter] support arguments feature for flutter
  • Loading branch information
RyosukeCla authored Mar 21, 2024
2 parents 4cb3085 + 5954475 commit b39795f
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 50 deletions.
2 changes: 1 addition & 1 deletion flutter/nativebrik_bridge/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ android {
}

dependencies {
implementation 'com.nativebrik:sdk:0.0.6'
implementation 'com.nativebrik:sdk:0.1.0'
implementation 'androidx.activity:activity-compose:1.8.2'
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.mockito:mockito-core:5.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal class NativebrikBridgeManager(private val binaryMessenger: BinaryMessen
}

@Composable
fun Render(channelId: String, modifier: Modifier = Modifier) {
fun Render(channelId: String, arguments: Any?, modifier: Modifier = Modifier) {
if (channelId.isEmpty()) {
return
}
Expand All @@ -84,7 +84,7 @@ internal class NativebrikBridgeManager(private val binaryMessenger: BinaryMessen
MethodChannel(this.binaryMessenger, "Nativebrik/Embedding/$channelId")
}
val data = this.embeddingMap[channelId]
bridgeClient.render(modifier, data, onEvent = { event ->
bridgeClient.render(modifier, arguments, data, onEvent = { event ->
methodChannel.invokeMethod(ON_EVENT_METHOD, mapOf(
"name" to event.name,
"deepLink" to event.deepLink,
Expand Down Expand Up @@ -183,11 +183,12 @@ internal class NativeViewFactory(private val manager: NativebrikBridgeManager):
override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
val creationParams = args as Map<*, *>?
val channelId = creationParams?.get("channelId") as String
return NativeView(context, channelId, manager)
val arguments = creationParams["arguments"]
return NativeView(context, channelId, arguments, manager)
}
}

internal class NativeView(context: Context, channelId: String, manager: NativebrikBridgeManager): PlatformView {
internal class NativeView(context: Context, channelId: String, arguments: Any?, manager: NativebrikBridgeManager): PlatformView {
private val view: ComposeView

override fun getView(): View {
Expand All @@ -200,7 +201,7 @@ internal class NativeView(context: Context, channelId: String, manager: Nativebr
val param = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
view = ComposeView(context).apply {
setContent {
manager.Render(channelId)
manager.Render(channelId, arguments)
}
layoutParams = param
}
Expand Down
8 changes: 4 additions & 4 deletions flutter/nativebrik_bridge/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ PODS:
- Flutter (1.0.0)
- integration_test (0.0.1):
- Flutter
- Nativebrik (0.4.1):
- Nativebrik (0.5.0):
- YogaKit (~> 2.0.0)
- nativebrik_bridge (0.0.1):
- Flutter
- Nativebrik (~> 0.4.1)
- Nativebrik (~> 0.5.0)
- Yoga (2.0.1)
- YogaKit (2.0.1):
- Yoga (~> 2.0.1)
Expand All @@ -33,8 +33,8 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
integration_test: 13825b8a9334a850581300559b8839134b124670
Nativebrik: a404bf96ad818a1bcb2d233b595fbea670d47cbd
nativebrik_bridge: 6fac38eaacecd6e90d1d68a74340b7df20b6088d
Nativebrik: cce9625971439ed9b63b649c60b0d11f3e525e75
nativebrik_bridge: 6256bcf5bdd5ad5d5e4069f45b3d9f444275e244
Yoga: 7ac7f65e2d7120bf97e75fd66436ed4c7bf8e37e
YogaKit: 4751ba673fab49df29e2235e23c4ea7d6d5a339c

Expand Down
4 changes: 2 additions & 2 deletions flutter/nativebrik_bridge/ios/Classes/Embedding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class FLNativeView: NSObject, FlutterPlatformView {
self._view = UIView(frame: frame)
super.init()

guard let args = args as? [String:String] else {
guard let args = args as? [String:Any] else {
return
}
guard let channelId = args["channelId"] else {
guard let channelId = args["channelId"] as? String else {
return
}
guard let entity = manager.getEmbeddingEntity(channelId: channelId) else {
Expand Down
8 changes: 4 additions & 4 deletions flutter/nativebrik_bridge/ios/Classes/Manager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ class NativebrikBridgeManager {
}

// embedding
func connectEmbedding(id: String, channelId: String, messenger: FlutterBinaryMessenger) {
func connectEmbedding(id: String, channelId: String, arguments: Any?, messenger: FlutterBinaryMessenger) {
guard let nativebrikClient = self.nativebrikClient else {
return
}
let channel = FlutterMethodChannel(name: "Nativebrik/Embedding/\(channelId)", binaryMessenger: messenger)
let uiview = nativebrikClient.experiment.embeddingUIView(id, onEvent: { event in
let uiview = nativebrikClient.experiment.embeddingUIView(id, arguments: arguments, onEvent: { event in
channel.invokeMethod(ON_EVENT_METHOD, arguments: [
"name": event.name as Any?,
"deepLink": event.deepLink as Any?,
Expand Down Expand Up @@ -124,15 +124,15 @@ class NativebrikBridgeManager {
self.configMaps[channelId] = nil
}

func connectEmbeddingInRemoteConfigValue(key: String, channelId: String, embeddingChannelId: String, messenger: FlutterBinaryMessenger) {
func connectEmbeddingInRemoteConfigValue(key: String, channelId: String, arguments: Any?, embeddingChannelId: String, messenger: FlutterBinaryMessenger) {
guard let entity = self.configMaps[channelId] else {
return
}
guard let variant = entity.variant else {
return
}
let channel = FlutterMethodChannel(name: "Nativebrik/Embedding/\(embeddingChannelId)", binaryMessenger: messenger)
guard let uiview = variant.getAsUIView(key, onEvent: { event in
guard let uiview = variant.getAsUIView(key, arguments: arguments, onEvent: { event in
channel.invokeMethod(ON_EVENT_METHOD, arguments: [
"name": event.name as Any?,
"deepLink": event.deepLink as Any?,
Expand Down
20 changes: 11 additions & 9 deletions flutter/nativebrik_bridge/ios/Classes/NativebrikBridgePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public class NativebrikBridgePlugin: NSObject, FlutterPlugin {

// embedding
case "connectEmbedding":
let args = call.arguments as! [String:String]
let id = args["id"]!
let channelId = args["channelId"]!
self.manager.connectEmbedding(id: id, channelId: channelId, messenger: self.messenger)
let args = call.arguments as! [String:Any]
let id = args["id"] as! String
let channelId = args["channelId"] as! String
let arguments = args["arguments"] as Any?
self.manager.connectEmbedding(id: id, channelId: channelId, arguments: arguments, messenger: self.messenger)
result("ok")
case "disconnectEmbedding":
let channelId = call.arguments as! String
Expand Down Expand Up @@ -75,11 +76,12 @@ public class NativebrikBridgePlugin: NSObject, FlutterPlugin {
let value = self.manager.getRemoteConfigValue(channelId: channelId, key: key)
result(value)
case "connectEmbeddingInRemoteConfigValue":
let args = call.arguments as! [String:String]
let key = args["key"]!
let channelId = args["channelId"]!
let embeddingChannelId = args["embeddingChannelId"]!
self.manager.connectEmbeddingInRemoteConfigValue(key: key, channelId: channelId, embeddingChannelId: embeddingChannelId, messenger: self.messenger)
let args = call.arguments as! [String:Any]
let key = args["key"] as! String
let channelId = args["channelId"] as! String
let embeddingChannelId = args["embeddingChannelId"] as! String
let arguments = args["arguments"] as Any?
self.manager.connectEmbeddingInRemoteConfigValue(key: key, channelId: channelId, arguments: arguments, embeddingChannelId: embeddingChannelId, messenger: self.messenger)
result("ok")
case "dispatch":
let name = call.arguments as! String
Expand Down
2 changes: 1 addition & 1 deletion flutter/nativebrik_bridge/ios/nativebrik_bridge.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A new Flutter plugin project.
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.dependency 'Nativebrik', '~> 0.4.1'
s.dependency 'Nativebrik', '~> 0.5.0'
s.ios.deployment_target = '14.0'
s.platform = :ios, '14.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ class MethodChannelNativebrikBridge extends NativebrikBridgePlatform {
}

@override
Future<String?> connectEmbedding(String id, String channelId) async {
Future<String?> connectEmbedding(
String id, String channelId, dynamic arguments) async {
final result = await methodChannel.invokeMethod<String>(
'connectEmbedding',
<String, String>{
<String, dynamic>{
'id': id,
'channelId': channelId,
'arguments': arguments,
},
);
return result;
Expand Down Expand Up @@ -91,14 +93,15 @@ class MethodChannelNativebrikBridge extends NativebrikBridgePlatform {
}

@override
Future<String?> connectEmbeddingInRemoteConfigValue(
String key, String channelId, String embeddingChannelId) async {
Future<String?> connectEmbeddingInRemoteConfigValue(String key,
String channelId, String embeddingChannelId, dynamic arguments) async {
final result = await methodChannel.invokeMethod<String>(
'connectEmbeddingInRemoteConfigValue',
<String, String>{
<String, dynamic>{
'key': key,
'channelId': channelId,
'embeddingChannelId': embeddingChannelId,
'arguments': arguments,
},
);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ abstract class NativebrikBridgePlatform extends PlatformInterface {
throw UnimplementedError('connectClient() has not been implemented.');
}

Future<String?> connectEmbedding(String id, String channelId) {
Future<String?> connectEmbedding(
String id, String channelId, dynamic arguments) {
throw UnimplementedError('connectEmbedding() has not been implemented.');
}

Expand All @@ -55,8 +56,8 @@ abstract class NativebrikBridgePlatform extends PlatformInterface {
'getRemoteConfigValue() has not been implemented.');
}

Future<String?> connectEmbeddingInRemoteConfigValue(
String key, String channelId, String embeddingChannelId) {
Future<String?> connectEmbeddingInRemoteConfigValue(String key,
String channelId, String embeddingChannelId, dynamic arguments) {
throw UnimplementedError(
'connectEmbeddingInRemoteConfigValue() has not been implemented.');
}
Expand Down
31 changes: 19 additions & 12 deletions flutter/nativebrik_bridge/lib/embedding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,23 @@ class NativebrikEmbedding extends StatefulWidget {
final String id;
final double? width;
final double? height;
final dynamic arguments;
final EventHandler? onEvent;
final EmbeddingBuilder? builder;

// this is used from remoteconfig.embed
final NativebrikRemoteConfigVariant? variant;

const NativebrikEmbedding(this.id,
{super.key,
this.width,
this.height,
this.variant,
this.onEvent,
this.builder});
const NativebrikEmbedding(
this.id, {
super.key,
this.width,
this.height,
this.arguments,
this.onEvent,
this.variant,
this.builder,
});

@override
// ignore: library_private_types_in_public_api
Expand Down Expand Up @@ -89,9 +93,10 @@ class _EmbeddingState extends State<NativebrikEmbedding> {
final variant = widget.variant;
if (variant != null) {
NativebrikBridgePlatform.instance.connectEmbeddingInRemoteConfigValue(
widget.id, variant.channelId, _channelId);
widget.id, variant.channelId, _channelId, widget.arguments);
} else {
NativebrikBridgePlatform.instance.connectEmbedding(widget.id, _channelId);
NativebrikBridgePlatform.instance
.connectEmbedding(widget.id, _channelId, widget.arguments);
}
}

Expand Down Expand Up @@ -145,8 +150,8 @@ class _EmbeddingState extends State<NativebrikEmbedding> {
return _renderWithBuilder(
context, const Center(child: Text("Embedding not found")));
case EmbeddingPhase.completed:
return _renderWithBuilder(
context, SizedBox(child: _BridgeView(_channelId)));
return _renderWithBuilder(context,
SizedBox(child: _BridgeView(_channelId, widget.arguments)));
}
}

Expand All @@ -160,14 +165,16 @@ class _EmbeddingState extends State<NativebrikEmbedding> {

class _BridgeView extends StatelessWidget {
final String channelId;
final dynamic arguments;

const _BridgeView(this.channelId);
const _BridgeView(this.channelId, this.arguments);

@override
Widget build(BuildContext context) {
const String viewType = "nativebrik-embedding-view";
final Map<String, dynamic> creationParams = <String, dynamic>{
"channelId": channelId,
"arguments": arguments,
};
switch (defaultTargetPlatform) {
case TargetPlatform.iOS:
Expand Down
2 changes: 1 addition & 1 deletion flutter/nativebrik_bridge/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: nativebrik_bridge
description: "Nativebrik bridge sdk for flutter"
version: 0.0.1
version: 0.1.0
homepage: "https://nativebrik.com"
repository: "https://github.com/plaidev/nativebrik-sdk"

Expand Down
7 changes: 4 additions & 3 deletions flutter/nativebrik_bridge/test/nativebrik_bridge_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class MockNativebrikBridgePlatform
}

@override
Future<String?> connectEmbedding(String id, String channelId) {
Future<String?> connectEmbedding(
String id, String channelId, dynamic arguments) {
throw UnimplementedError('connectEmbedding() has not been implemented.');
}

Expand All @@ -44,8 +45,8 @@ class MockNativebrikBridgePlatform
}

@override
Future<String?> connectEmbeddingInRemoteConfigValue(
String key, String channelId, String embeddingChannelId) {
Future<String?> connectEmbeddingInRemoteConfigValue(String key,
String channelId, String embeddingChannelId, dynamic arguments) {
throw UnimplementedError(
'connectEmbeddingInRemoteConfigValue() has not been implemented.');
}
Expand Down

0 comments on commit b39795f

Please sign in to comment.