Make naming consistent across channel APIs (#9270)
This commit is contained in:
committed by
GitHub
parent
752d6096a6
commit
945cfc3ee2
@@ -6,9 +6,9 @@ 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.BasicMessageChannel;
|
||||
import io.flutter.plugin.common.BasicMessageChannel.MessageHandler;
|
||||
import io.flutter.plugin.common.BasicMessageChannel.Reply;
|
||||
import io.flutter.plugin.common.StringCodec;
|
||||
import io.flutter.view.FlutterMain;
|
||||
import io.flutter.view.FlutterView;
|
||||
@@ -20,7 +20,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
private static final String CHANNEL = "increment";
|
||||
private static final String EMPTY_MESSAGE = "";
|
||||
private static final String PING = "ping";
|
||||
private FlutterMessageChannel messageChannel;
|
||||
private BasicMessageChannel messageChannel;
|
||||
|
||||
private String[] getArgsFromIntent(Intent intent) {
|
||||
// Before adding more entries to this list, consider that arbitrary
|
||||
@@ -57,15 +57,13 @@ public class MainActivity extends AppCompatActivity {
|
||||
flutterView = (FlutterView) findViewById(R.id.flutter_view);
|
||||
flutterView.runFromBundle(FlutterMain.findAppBundlePath(getApplicationContext()), null);
|
||||
|
||||
messageChannel =
|
||||
new FlutterMessageChannel<String>(flutterView, CHANNEL, StringCodec.INSTANCE);
|
||||
messageChannel = new BasicMessageChannel<>(flutterView, CHANNEL, StringCodec.INSTANCE);
|
||||
messageChannel.
|
||||
setMessageHandler(new MessageHandler<String>() {
|
||||
@Override
|
||||
public void onMessage(String s, Reply<String> reply) {
|
||||
onFlutterIncrement();
|
||||
reply.send(EMPTY_MESSAGE);
|
||||
|
||||
reply.reply(EMPTY_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -108,4 +106,4 @@ public class MainActivity extends AppCompatActivity {
|
||||
super.onPostResume();
|
||||
flutterView.onPostResume();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
@property (nonatomic) NativeViewController* nativeViewController;
|
||||
@property (nonatomic) FlutterViewController* flutterViewController;
|
||||
@property (nonatomic) FlutterMessageChannel* messageChannel;
|
||||
@property (nonatomic) FlutterBasicMessageChannel* messageChannel;
|
||||
@end
|
||||
|
||||
static NSString* const emptyString = @"";
|
||||
@@ -35,20 +35,20 @@ static NSString* const channel = @"increment";
|
||||
if ([segue.identifier isEqualToString:@"FlutterViewControllerSegue"]) {
|
||||
self.flutterViewController = segue.destinationViewController;
|
||||
|
||||
self.messageChannel = [FlutterMessageChannel messageChannelWithName:channel
|
||||
binaryMessenger:self.flutterViewController
|
||||
codec:[FlutterStringCodec sharedInstance]];
|
||||
self.messageChannel = [FlutterBasicMessageChannel messageChannelWithName:channel
|
||||
binaryMessenger:self.flutterViewController
|
||||
codec:[FlutterStringCodec sharedInstance]];
|
||||
|
||||
MainViewController* __weak weakSelf = self;
|
||||
[self.messageChannel setMessageHandler:^(id message, FlutterReplyHandler replyHandler) {
|
||||
[self.messageChannel setMessageHandler:^(id message, FlutterReply reply) {
|
||||
[weakSelf.nativeViewController didReceiveIncrement];
|
||||
replyHandler(emptyString);
|
||||
reply(emptyString);
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)didTapIncrementButton {
|
||||
[self.messageChannel sendMessage:ping replyHandler:nil];
|
||||
[self.messageChannel sendMessage:ping];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -29,8 +29,8 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
static const String _channel = "increment";
|
||||
static const String _pong = "pong";
|
||||
static const String _emptyMessage = "";
|
||||
static const PlatformMessageChannel<String> platform =
|
||||
const PlatformMessageChannel<String>(_channel, const StringCodec());
|
||||
static const BasicMessageChannel<String> platform =
|
||||
const BasicMessageChannel<String>(_channel, const StringCodec());
|
||||
|
||||
int _counter = 0;
|
||||
|
||||
|
||||
@@ -15,12 +15,12 @@ import android.os.Build.VERSION_CODES;
|
||||
import android.os.Bundle;
|
||||
|
||||
import io.flutter.app.FlutterActivity;
|
||||
import io.flutter.plugin.common.FlutterEventChannel;
|
||||
import io.flutter.plugin.common.FlutterEventChannel.EventSink;
|
||||
import io.flutter.plugin.common.FlutterEventChannel.StreamHandler;
|
||||
import io.flutter.plugin.common.FlutterMethodChannel;
|
||||
import io.flutter.plugin.common.FlutterMethodChannel.MethodCallHandler;
|
||||
import io.flutter.plugin.common.FlutterMethodChannel.Response;
|
||||
import io.flutter.plugin.common.EventChannel;
|
||||
import io.flutter.plugin.common.EventChannel.EventSink;
|
||||
import io.flutter.plugin.common.EventChannel.StreamHandler;
|
||||
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.MethodCall;
|
||||
|
||||
public class MainActivity extends FlutterActivity {
|
||||
@@ -31,7 +31,7 @@ public class MainActivity extends FlutterActivity {
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
new FlutterEventChannel(getFlutterView(), CHARGING_CHANNEL).setStreamHandler(
|
||||
new EventChannel(getFlutterView(), CHARGING_CHANNEL).setStreamHandler(
|
||||
new StreamHandler() {
|
||||
private BroadcastReceiver chargingStateChangeReceiver;
|
||||
@Override
|
||||
@@ -49,20 +49,20 @@ public class MainActivity extends FlutterActivity {
|
||||
}
|
||||
);
|
||||
|
||||
new FlutterMethodChannel(getFlutterView(), BATTERY_CHANNEL).setMethodCallHandler(
|
||||
new MethodChannel(getFlutterView(), BATTERY_CHANNEL).setMethodCallHandler(
|
||||
new MethodCallHandler() {
|
||||
@Override
|
||||
public void onMethodCall(MethodCall call, Response response) {
|
||||
public void onMethodCall(MethodCall call, Result result) {
|
||||
if (call.method.equals("getBatteryLevel")) {
|
||||
int batteryLevel = getBatteryLevel();
|
||||
|
||||
if (batteryLevel != -1) {
|
||||
response.success(batteryLevel);
|
||||
result.success(batteryLevel);
|
||||
} else {
|
||||
response.error("UNAVAILABLE", "Battery level not available.", null);
|
||||
result.error("UNAVAILABLE", "Battery level not available.", null);
|
||||
}
|
||||
} else {
|
||||
response.notImplemented();
|
||||
result.notImplemented();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
|
||||
@implementation AppDelegate {
|
||||
FlutterEventReceiver _eventReceiver;
|
||||
FlutterEventSink _eventSink;
|
||||
}
|
||||
|
||||
- (BOOL)application:(UIApplication*)application
|
||||
@@ -19,7 +19,7 @@
|
||||
methodChannelWithName:@"samples.flutter.io/battery"
|
||||
binaryMessenger:controller];
|
||||
[batteryChannel setMethodCallHandler:^(FlutterMethodCall* call,
|
||||
FlutterResultReceiver result) {
|
||||
FlutterResult result) {
|
||||
if ([@"getBatteryLevel" isEqualToString:call.method]) {
|
||||
int batteryLevel = [self getBatteryLevel];
|
||||
if (batteryLevel == -1) {
|
||||
@@ -52,8 +52,8 @@
|
||||
}
|
||||
|
||||
- (FlutterError*)onListenWithArguments:(id)arguments
|
||||
eventReceiver:(FlutterEventReceiver)eventReceiver {
|
||||
_eventReceiver = eventReceiver;
|
||||
eventSink:(FlutterEventSink)eventSink {
|
||||
_eventSink = eventSink;
|
||||
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
|
||||
[self sendBatteryStateEvent];
|
||||
[[NSNotificationCenter defaultCenter]
|
||||
@@ -74,22 +74,22 @@
|
||||
switch (state) {
|
||||
case UIDeviceBatteryStateFull:
|
||||
case UIDeviceBatteryStateCharging:
|
||||
_eventReceiver(@"charging");
|
||||
_eventSink(@"charging");
|
||||
break;
|
||||
case UIDeviceBatteryStateUnplugged:
|
||||
_eventReceiver(@"discharging");
|
||||
_eventSink(@"discharging");
|
||||
break;
|
||||
default:
|
||||
_eventReceiver([FlutterError errorWithCode:@"UNAVAILABLE"
|
||||
message:@"Charging status unavailable"
|
||||
details:nil]);
|
||||
_eventSink([FlutterError errorWithCode:@"UNAVAILABLE"
|
||||
message:@"Charging status unavailable"
|
||||
details:nil]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
- (FlutterError*)onCancelWithArguments:(id)arguments {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
_eventReceiver = nil;
|
||||
_eventSink = nil;
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,10 +13,10 @@ class PlatformChannel extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _PlatformChannelState extends State<PlatformChannel> {
|
||||
static const PlatformMethodChannel methodChannel =
|
||||
const PlatformMethodChannel('samples.flutter.io/battery');
|
||||
static const PlatformEventChannel eventChannel =
|
||||
const PlatformEventChannel('samples.flutter.io/charging');
|
||||
static const MethodChannel methodChannel =
|
||||
const MethodChannel('samples.flutter.io/battery');
|
||||
static const EventChannel eventChannel =
|
||||
const EventChannel('samples.flutter.io/charging');
|
||||
|
||||
String _batteryLevel = 'Battery level: unknown.';
|
||||
String _chargingStatus = 'Battery status: unknown.';
|
||||
|
||||
@@ -7,7 +7,7 @@ import Flutter
|
||||
|
||||
@UIApplicationMain
|
||||
@objc class AppDelegate: FlutterAppDelegate, FlutterStreamHandler {
|
||||
private var eventReceiver: FlutterEventReceiver?;
|
||||
private var eventSink: FlutterEventSink?;
|
||||
|
||||
override func application(
|
||||
_ application: UIApplication,
|
||||
@@ -16,14 +16,13 @@ import Flutter
|
||||
let batteryChannel = FlutterMethodChannel.init(name: "samples.flutter.io/battery",
|
||||
binaryMessenger: controller);
|
||||
batteryChannel.setMethodCallHandler({
|
||||
(call: FlutterMethodCall, result: FlutterResultReceiver) -> Void in
|
||||
(call: FlutterMethodCall, result: FlutterResult) -> Void in
|
||||
if ("getBatteryLevel" == call.method) {
|
||||
self.receiveBatteryLevel(result: result);
|
||||
} else {
|
||||
result(FlutterMethodNotImplemented);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
let chargingChannel = FlutterEventChannel.init(name: "samples.flutter.io/charging",
|
||||
binaryMessenger: controller);
|
||||
@@ -31,7 +30,7 @@ import Flutter
|
||||
return true
|
||||
}
|
||||
|
||||
private func receiveBatteryLevel(result: FlutterResultReceiver) {
|
||||
private func receiveBatteryLevel(result: FlutterResult) {
|
||||
let device = UIDevice.current;
|
||||
device.isBatteryMonitoringEnabled = true;
|
||||
if (device.batteryState == UIDeviceBatteryState.unknown) {
|
||||
@@ -44,8 +43,8 @@ import Flutter
|
||||
}
|
||||
|
||||
public func onListen(withArguments arguments: Any?,
|
||||
eventReceiver: @escaping FlutterEventReceiver) -> FlutterError? {
|
||||
self.eventReceiver = eventReceiver;
|
||||
eventSink: @escaping FlutterEventSink) -> FlutterError? {
|
||||
self.eventSink = eventSink;
|
||||
UIDevice.current.isBatteryMonitoringEnabled = true;
|
||||
self.sendBatteryStateEvent();
|
||||
NotificationCenter.default.addObserver(
|
||||
@@ -61,32 +60,32 @@ import Flutter
|
||||
}
|
||||
|
||||
private func sendBatteryStateEvent() {
|
||||
if (eventReceiver == nil) {
|
||||
if (eventSink == nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
let state = UIDevice.current.batteryState;
|
||||
switch state {
|
||||
case UIDeviceBatteryState.full:
|
||||
eventReceiver!("charging");
|
||||
eventSink!("charging");
|
||||
break;
|
||||
case UIDeviceBatteryState.charging:
|
||||
eventReceiver!("charging");
|
||||
eventSink!("charging");
|
||||
break;
|
||||
case UIDeviceBatteryState.unplugged:
|
||||
eventReceiver!("discharging");
|
||||
eventSink!("discharging");
|
||||
break;
|
||||
default:
|
||||
eventReceiver!(FlutterError.init(code: "UNAVAILABLE",
|
||||
message: "Charging status unavailable",
|
||||
details: nil));
|
||||
eventSink!(FlutterError.init(code: "UNAVAILABLE",
|
||||
message: "Charging status unavailable",
|
||||
details: nil));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
NotificationCenter.default.removeObserver(self);
|
||||
eventReceiver = nil;
|
||||
eventSink = nil;
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,10 +13,10 @@ class PlatformChannel extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _PlatformChannelState extends State<PlatformChannel> {
|
||||
static const PlatformMethodChannel methodChannel =
|
||||
const PlatformMethodChannel('samples.flutter.io/battery');
|
||||
static const PlatformEventChannel eventChannel =
|
||||
const PlatformEventChannel('samples.flutter.io/charging');
|
||||
static const MethodChannel methodChannel =
|
||||
const MethodChannel('samples.flutter.io/battery');
|
||||
static const EventChannel eventChannel =
|
||||
const EventChannel('samples.flutter.io/charging');
|
||||
|
||||
String _batteryLevel = 'Battery level: unknown.';
|
||||
String _chargingStatus = 'Battery status: unknown.';
|
||||
|
||||
Reference in New Issue
Block a user