From 616a7bed0ff23e9b94ab3395f0f82c95e1fdad7c Mon Sep 17 00:00:00 2001 From: Sarah Zakarias Date: Tue, 14 Mar 2017 09:38:09 +0100 Subject: [PATCH] =?UTF-8?q?Update=20Android=20part=20of=20flutter=5Fview?= =?UTF-8?q?=20to=20use=20the=20new=20platform=20message=20c=E2=80=A6=20(#8?= =?UTF-8?q?729)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * update Android part of flutter_view to use the new platform message channel. * addressed comments * addressed comments --- .../java/com/example/view/MainActivity.java | 23 +++++++++++++------ examples/flutter_view/lib/main.dart | 17 ++++++++------ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/examples/flutter_view/android/app/src/main/java/com/example/view/MainActivity.java b/examples/flutter_view/android/app/src/main/java/com/example/view/MainActivity.java index 200df37021..5714396dd5 100644 --- a/examples/flutter_view/android/app/src/main/java/com/example/view/MainActivity.java +++ b/examples/flutter_view/android/app/src/main/java/com/example/view/MainActivity.java @@ -6,6 +6,10 @@ import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; +import io.flutter.plugin.common.FlutterMessageChannel; +import io.flutter.plugin.common.FlutterMessageChannel.MessageHandler; +import io.flutter.plugin.common.FlutterMessageChannel.Reply; +import io.flutter.plugin.common.StringMessageCodec; import io.flutter.view.FlutterMain; import io.flutter.view.FlutterView; import java.util.ArrayList; @@ -15,6 +19,8 @@ public class MainActivity extends AppCompatActivity { private int counter; private static final String CHANNEL = "increment"; private static final String EMPTY_MESSAGE = ""; + private static final String PING = "ping"; + private FlutterMessageChannel messageChannel; private String[] getArgsFromIntent(Intent intent) { // Before adding more entries to this list, consider that arbitrary @@ -51,11 +57,15 @@ public class MainActivity extends AppCompatActivity { flutterView = (FlutterView) findViewById(R.id.flutter_view); flutterView.runFromBundle(FlutterMain.findAppBundlePath(getApplicationContext()), null); - flutterView.addOnMessageListener(CHANNEL, - new FlutterView.OnMessageListener() { + messageChannel = + new FlutterMessageChannel(flutterView, CHANNEL, StringMessageCodec.INSTANCE); + messageChannel. + setMessageHandler(new MessageHandler() { @Override - public String onMessage(FlutterView view, String message) { - return onFlutterIncrement(); + public void onMessage(String s, Reply reply) { + onFlutterIncrement(); + reply.send(EMPTY_MESSAGE); + } }); @@ -69,15 +79,14 @@ public class MainActivity extends AppCompatActivity { } private void sendAndroidIncrement() { - flutterView.sendToFlutter(CHANNEL, EMPTY_MESSAGE, null); + messageChannel.send(PING); } - private String onFlutterIncrement() { + private void onFlutterIncrement() { counter++; TextView textView = (TextView) findViewById(R.id.button_tap); String value = "Flutter button tapped " + counter + (counter == 1 ? " time" : " times"); textView.setText(value); - return EMPTY_MESSAGE; } @Override diff --git a/examples/flutter_view/lib/main.dart b/examples/flutter_view/lib/main.dart index e6bf6b42c8..1fc7a69561 100644 --- a/examples/flutter_view/lib/main.dart +++ b/examples/flutter_view/lib/main.dart @@ -31,29 +31,32 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { static const String _channel = "increment"; + static const String _pong = "pong"; static const String _emptyMessage = ""; + static const PlatformMessageChannel platform = + const PlatformMessageChannel(_channel, const StringCodec()); int _counter = 0; - Future handlePlatformIncrement(String message) async { - _incrementCounter(); - return _emptyMessage; + @override + void initState() { + super.initState(); + platform.setMessageHandler(_handlePlatformIncrement); } - void _incrementCounter() { + Future _handlePlatformIncrement(String message) async { setState(() { _counter++; }); + return _emptyMessage; } void _sendFlutterIncrement() { - PlatformMessages.sendString(_channel, _emptyMessage); + platform.send(_pong); } @override Widget build(BuildContext context) { - PlatformMessages.setStringMessageHandler(_channel, - handlePlatformIncrement); return new Scaffold( body: new Column( crossAxisAlignment: CrossAxisAlignment.start,