Unverified Commit f6c154c3 authored by James D. Lin's avatar James D. Lin Committed by GitHub

Improve Swift usage in platform_channel_swift some more (#22318)

Improve Swift usage in platform_channel_swift some more

Make a bunch of changes that willlarche requested in
https://github.com/flutter/flutter/pull/21712:

* Use `static let` within `enum` namespaces for String constants.
* Use `guard` for early exits and to avoid forced unwrapping.
* Explicitly qualify a selector.
* Fix a retain cycle by using a weak reference.
parent 5225f3d2
...@@ -5,6 +5,20 @@ ...@@ -5,6 +5,20 @@
import UIKit import UIKit
import Flutter import Flutter
enum ChannelName {
static let battery = "samples.flutter.io/battery"
static let charging = "samples.flutter.io/charging"
}
enum BatteryState {
static let charging = "charging"
static let discharging = "discharging"
}
enum MyFlutterErrorCode {
static let unavailable = "UNAVAILABLE"
}
@UIApplicationMain @UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, FlutterStreamHandler { @objc class AppDelegate: FlutterAppDelegate, FlutterStreamHandler {
private var eventSink: FlutterEventSink? private var eventSink: FlutterEventSink?
...@@ -13,19 +27,21 @@ import Flutter ...@@ -13,19 +27,21 @@ import Flutter
_ application: UIApplication, _ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
GeneratedPluginRegistrant.register(with: self) GeneratedPluginRegistrant.register(with: self)
let controller: FlutterViewController = window?.rootViewController as! FlutterViewController guard let controller = window?.rootViewController as? FlutterViewController else {
let batteryChannel = FlutterMethodChannel(name: "samples.flutter.io/battery", fatalError("rootViewController is not type FlutterViewController")
}
let batteryChannel = FlutterMethodChannel(name: ChannelName.battery,
binaryMessenger: controller) binaryMessenger: controller)
batteryChannel.setMethodCallHandler({ batteryChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: FlutterResult) -> Void in [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
if "getBatteryLevel" == call.method { guard call.method == "getBatteryLevel" else {
self.receiveBatteryLevel(result: result)
} else {
result(FlutterMethodNotImplemented) result(FlutterMethodNotImplemented)
return
} }
self?.receiveBatteryLevel(result: result)
}) })
let chargingChannel = FlutterEventChannel(name: "samples.flutter.io/charging", let chargingChannel = FlutterEventChannel(name: ChannelName.charging,
binaryMessenger: controller) binaryMessenger: controller)
chargingChannel.setStreamHandler(self) chargingChannel.setStreamHandler(self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions) return super.application(application, didFinishLaunchingWithOptions: launchOptions)
...@@ -34,13 +50,13 @@ import Flutter ...@@ -34,13 +50,13 @@ import Flutter
private func receiveBatteryLevel(result: FlutterResult) { private func receiveBatteryLevel(result: FlutterResult) {
let device = UIDevice.current let device = UIDevice.current
device.isBatteryMonitoringEnabled = true device.isBatteryMonitoringEnabled = true
if device.batteryState == UIDeviceBatteryState.unknown { guard device.batteryState != .unknown else {
result(FlutterError(code: "UNAVAILABLE", result(FlutterError(code: MyFlutterErrorCode.unavailable,
message: "Battery info unavailable", message: "Battery info unavailable",
details: nil)) details: nil))
} else { return
result(Int(device.batteryLevel * 100))
} }
result(Int(device.batteryLevel * 100))
} }
public func onListen(withArguments arguments: Any?, public func onListen(withArguments arguments: Any?,
...@@ -50,7 +66,7 @@ import Flutter ...@@ -50,7 +66,7 @@ import Flutter
sendBatteryStateEvent() sendBatteryStateEvent()
NotificationCenter.default.addObserver( NotificationCenter.default.addObserver(
self, self,
selector: #selector(onBatteryStateDidChange), selector: #selector(AppDelegate.onBatteryStateDidChange),
name: NSNotification.Name.UIDeviceBatteryStateDidChange, name: NSNotification.Name.UIDeviceBatteryStateDidChange,
object: nil) object: nil)
return nil return nil
...@@ -61,20 +77,19 @@ import Flutter ...@@ -61,20 +77,19 @@ import Flutter
} }
private func sendBatteryStateEvent() { private func sendBatteryStateEvent() {
if eventSink == nil { guard let eventSink = eventSink else {
return return
} }
let state = UIDevice.current.batteryState switch UIDevice.current.batteryState {
switch state { case .full:
case UIDeviceBatteryState.full: eventSink(BatteryState.charging)
eventSink!("charging") case .charging:
case UIDeviceBatteryState.charging: eventSink(BatteryState.charging)
eventSink!("charging") case .unplugged:
case UIDeviceBatteryState.unplugged: eventSink(BatteryState.discharging)
eventSink!("discharging")
default: default:
eventSink!(FlutterError(code: "UNAVAILABLE", eventSink(FlutterError(code: MyFlutterErrorCode.unavailable,
message: "Charging status unavailable", message: "Charging status unavailable",
details: nil)) details: nil))
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment