Add charging status to Swift version of Platform Channel example (#9436)
This commit is contained in:
@@ -11,5 +11,6 @@
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Dart SDK" level="project" />
|
||||
<orderEntry type="library" name="Dart Packages" level="project" />
|
||||
<orderEntry type="library" name="Dart SDK" level="application" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -6,35 +6,82 @@ import UIKit
|
||||
import Flutter
|
||||
|
||||
@UIApplicationMain
|
||||
@objc class AppDelegate: FlutterAppDelegate {
|
||||
@objc class AppDelegate: FlutterAppDelegate, FlutterStreamHandler {
|
||||
private var eventReceiver: FlutterEventReceiver?;
|
||||
|
||||
override func application(
|
||||
_ application: UIApplication,
|
||||
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
|
||||
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController;
|
||||
let batteryChannel = FlutterMethodChannel.init(name: "samples.flutter.io/battery",
|
||||
binaryMessenger: controller);
|
||||
batteryChannel.setMethodCallHandler({
|
||||
(call: FlutterMethodCall, result: FlutterResultReceiver) -> Void in
|
||||
if ("getBatteryLevel" == call.method) {
|
||||
receiveBatteryLevel(result: result);
|
||||
} else {
|
||||
result(FlutterMethodNotImplemented);
|
||||
}
|
||||
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController;
|
||||
let batteryChannel = FlutterMethodChannel.init(name: "samples.flutter.io/battery",
|
||||
binaryMessenger: controller);
|
||||
batteryChannel.setMethodCallHandler({
|
||||
(call: FlutterMethodCall, result: FlutterResultReceiver) -> Void in
|
||||
if ("getBatteryLevel" == call.method) {
|
||||
self.receiveBatteryLevel(result: result);
|
||||
} else {
|
||||
result(FlutterMethodNotImplemented);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let chargingChannel = FlutterEventChannel.init(name: "samples.flutter.io/charging",
|
||||
binaryMessenger: controller);
|
||||
chargingChannel.setStreamHandler(self);
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
private func receiveBatteryLevel(result: FlutterResultReceiver) {
|
||||
let device = UIDevice.current;
|
||||
device.isBatteryMonitoringEnabled = true;
|
||||
if (device.batteryState == UIDeviceBatteryState.unknown) {
|
||||
result(FlutterError.init(code: "UNAVAILABLE",
|
||||
message: "Battery info unavailable",
|
||||
details: nil));
|
||||
} else {
|
||||
result(Int(device.batteryLevel * 100));
|
||||
private func receiveBatteryLevel(result: FlutterResultReceiver) {
|
||||
let device = UIDevice.current;
|
||||
device.isBatteryMonitoringEnabled = true;
|
||||
if (device.batteryState == UIDeviceBatteryState.unknown) {
|
||||
result(FlutterError.init(code: "UNAVAILABLE",
|
||||
message: "Battery info unavailable",
|
||||
details: nil));
|
||||
} else {
|
||||
result(Int(device.batteryLevel * 100));
|
||||
}
|
||||
}
|
||||
|
||||
public func onListen(withArguments arguments: Any?,
|
||||
eventReceiver: @escaping FlutterEventReceiver) -> FlutterError? {
|
||||
self.eventReceiver = eventReceiver;
|
||||
UIDevice.current.isBatteryMonitoringEnabled = true;
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(onBatteryStateDidChange),
|
||||
name: NSNotification.Name.UIDeviceBatteryStateDidChange,
|
||||
object: nil)
|
||||
return nil;
|
||||
}
|
||||
|
||||
@objc private func onBatteryStateDidChange(notification: NSNotification) {
|
||||
if (eventReceiver == nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
let state = UIDevice.current.batteryState;
|
||||
switch state {
|
||||
case UIDeviceBatteryState.full:
|
||||
eventReceiver!("charging");
|
||||
break;
|
||||
case UIDeviceBatteryState.charging:
|
||||
eventReceiver!("charging");
|
||||
break;
|
||||
case UIDeviceBatteryState.unplugged:
|
||||
eventReceiver!("discharging");
|
||||
break;
|
||||
default:
|
||||
eventReceiver!(FlutterError.init(code: "UNAVAILABLE",
|
||||
message: "Charging status unavailable",
|
||||
details: nil));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
NotificationCenter.default.removeObserver(self);
|
||||
eventReceiver = nil;
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,36 +13,67 @@ class PlatformChannel extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _PlatformChannelState extends State<PlatformChannel> {
|
||||
static const PlatformMethodChannel platform = const PlatformMethodChannel('samples.flutter.io/battery');
|
||||
String _batteryLevel = '';
|
||||
static const PlatformMethodChannel methodChannel =
|
||||
const PlatformMethodChannel('samples.flutter.io/battery');
|
||||
static const PlatformEventChannel eventChannel =
|
||||
const PlatformEventChannel('samples.flutter.io/charging');
|
||||
|
||||
String _batteryLevel = 'Battery level: unknown.';
|
||||
String _chargingStatus = 'Battery status: unknown.';
|
||||
|
||||
Future<Null> _getBatteryLevel() async {
|
||||
String batteryLevel;
|
||||
try {
|
||||
final int result = await platform.invokeMethod('getBatteryLevel');
|
||||
batteryLevel = 'Battery level at $result % .';
|
||||
} on PlatformException catch (e) {
|
||||
batteryLevel = "Failed to get battery level: '${e.message}'.";
|
||||
}
|
||||
final int result = await methodChannel.invokeMethod('getBatteryLevel');
|
||||
batteryLevel = 'Battery level: $result%.';
|
||||
} on PlatformException {
|
||||
batteryLevel = "Failed to get battery level.";
|
||||
}
|
||||
setState(() {
|
||||
_batteryLevel = batteryLevel;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
eventChannel.receiveBroadcastStream().listen(_onEvent, onError: _onError);
|
||||
}
|
||||
|
||||
void _onEvent(String event) {
|
||||
setState(() {
|
||||
_chargingStatus =
|
||||
"Battery status: ${event == 'charging' ? '' : 'dis'}charging.";
|
||||
});
|
||||
}
|
||||
|
||||
void _onError(PlatformException error) {
|
||||
setState(() {
|
||||
_chargingStatus = "Battery status: unknown.";
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new Material(
|
||||
child: new Center(
|
||||
child: new Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: <Widget>[
|
||||
new RaisedButton(
|
||||
child: const Text('Get Battery Level'),
|
||||
onPressed: _getBatteryLevel,
|
||||
),
|
||||
new Text(_batteryLevel, key: const Key('Battery level label')),
|
||||
],
|
||||
),
|
||||
child: new Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: <Widget>[
|
||||
new Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
new Text(_batteryLevel, key: const Key('Battery level label')),
|
||||
new Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: new RaisedButton(
|
||||
child: const Text('Refresh'),
|
||||
onPressed: _getBatteryLevel,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
new Text(_chargingStatus),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
</content>
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Dart SDK" level="project" />
|
||||
<orderEntry type="library" name="Dart SDK" level="application" />
|
||||
<orderEntry type="library" name="Dart Packages" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
Reference in New Issue
Block a user