From f4051db677c2a1c0bba00532e157e548b22a4b10 Mon Sep 17 00:00:00 2001 From: Dariusz Seweryn Date: Fri, 17 Jul 2020 15:29:27 +0200 Subject: [PATCH] Added pedantic lib for better static analysis (#494) * Added pedantic lib for better static analysis Fixed (what I hope) are the biggest static analysis issues on pub.dev. * Fixed device_connection_mixin. * Code review updates. * Format fix. --- analysis_options.yaml | 1 + lib/ble_manager.dart | 4 +- lib/peripheral.dart | 2 +- lib/scan_result.dart | 2 - lib/src/bridge/device_connection_mixin.dart | 67 ++++++++++++++------- pubspec.yaml | 1 + 6 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 analysis_options.yaml diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 00000000..d4fcc1ad --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1 @@ +include: package:pedantic/analysis_options.yaml \ No newline at end of file diff --git a/lib/ble_manager.dart b/lib/ble_manager.dart index 34fb4734..5b0def9b 100644 --- a/lib/ble_manager.dart +++ b/lib/ble_manager.dart @@ -34,9 +34,7 @@ abstract class BleManager { static BleManager _instance; factory BleManager() { - if (_instance == null) { - _instance = InternalBleManager(); - } + _instance ??= InternalBleManager(); return _instance; } diff --git a/lib/peripheral.dart b/lib/peripheral.dart index c8298878..4c22fd4c 100644 --- a/lib/peripheral.dart +++ b/lib/peripheral.dart @@ -15,7 +15,7 @@ abstract class _PeripheralMetadata { /// [disconnectOrCancelConnection()] can be used if peripheral is not connected. class Peripheral { static const int NO_MTU_NEGOTIATION = 0; - ManagerForPeripheral _manager; + final ManagerForPeripheral _manager; String name; String identifier; diff --git a/lib/scan_result.dart b/lib/scan_result.dart index 112b3c65..3ae7ce6d 100644 --- a/lib/scan_result.dart +++ b/lib/scan_result.dart @@ -1,8 +1,6 @@ part of flutter_ble_lib; abstract class _ScanResultMetadata { - static const String id = "id"; - static const String name = "name"; static const String rssi = "rssi"; static const String manufacturerData = "manufacturerData"; static const String serviceData = "serviceData"; diff --git a/lib/src/bridge/device_connection_mixin.dart b/lib/src/bridge/device_connection_mixin.dart index 55aa17b9..5b28044c 100644 --- a/lib/src/bridge/device_connection_mixin.dart +++ b/lib/src/bridge/device_connection_mixin.dart @@ -7,20 +7,41 @@ mixin DeviceConnectionMixin on FlutterBLE { Future connectToPeripheral(String deviceIdentifier, bool isAutoConnect, int requestMtu, bool refreshGatt, Duration timeout) async { - return await _methodChannel - .invokeMethod(MethodName.connectToDevice, { - ArgumentName.deviceIdentifier: deviceIdentifier, - ArgumentName.isAutoConnect: isAutoConnect, - ArgumentName.requestMtu: requestMtu, - ArgumentName.refreshGatt: refreshGatt, - ArgumentName.timeoutMillis: timeout?.inMilliseconds - }).catchError((errorJson) => - Future.error(BleError.fromJson(jsonDecode(errorJson.details)))); + return await _methodChannel.invokeMethod( + MethodName.connectToDevice, + { + ArgumentName.deviceIdentifier: deviceIdentifier, + ArgumentName.isAutoConnect: isAutoConnect, + ArgumentName.requestMtu: requestMtu, + ArgumentName.refreshGatt: refreshGatt, + ArgumentName.timeoutMillis: timeout?.inMilliseconds + }, + ).catchError( + (errorJson) => Future.error( + BleError.fromJson(jsonDecode(errorJson.details)), + ), + ); } Stream observePeripheralConnectionState( - String identifier, bool emitCurrentValue) async* { - yield* _peripheralConnectionStateChanges + String identifier, bool emitCurrentValue) { + var controller = StreamController( + onListen: () => _methodChannel.invokeMethod( + MethodName.observeConnectionState, + { + ArgumentName.deviceIdentifier: identifier, + ArgumentName.emitCurrentValue: emitCurrentValue, + }, + ).catchError( + (errorJson) => throw BleError.fromJson(jsonDecode(errorJson.details)), + ), + ); + + controller + .addStream(_peripheralConnectionStateChanges) + .then((value) => controller?.close()); + + return controller.stream .map((jsonString) => ConnectionStateContainer.fromJson(jsonDecode(jsonString))) .where((connectionStateContainer) => @@ -39,24 +60,21 @@ mixin DeviceConnectionMixin on FlutterBLE { return PeripheralConnectionState.disconnecting; default: throw FormatException( - "Unrecognized value of device connection state. Value: $connectionStateString"); + 'Unrecognized value of device connection state. Value: $connectionStateString', + ); } }); - - _methodChannel.invokeMethod( - MethodName.observeConnectionState, { - ArgumentName.deviceIdentifier: identifier, - ArgumentName.emitCurrentValue: emitCurrentValue, - }).catchError( - (errorJson) => throw BleError.fromJson(jsonDecode(errorJson.details))); } Future isPeripheralConnected(String peripheralIdentifier) async { return await _methodChannel .invokeMethod(MethodName.isDeviceConnected, { ArgumentName.deviceIdentifier: peripheralIdentifier, - }).catchError((errorJson) => - Future.error(BleError.fromJson(jsonDecode(errorJson.details)))); + }).catchError( + (errorJson) => Future.error( + BleError.fromJson(jsonDecode(errorJson.details)), + ), + ); } Future disconnectOrCancelPeripheralConnection( @@ -64,7 +82,10 @@ mixin DeviceConnectionMixin on FlutterBLE { return await _methodChannel .invokeMethod(MethodName.cancelConnection, { ArgumentName.deviceIdentifier: peripheralIdentifier, - }).catchError((errorJson) => - Future.error(BleError.fromJson(jsonDecode(errorJson.details)))); + }).catchError( + (errorJson) => Future.error( + BleError.fromJson(jsonDecode(errorJson.details)), + ), + ); } } diff --git a/pubspec.yaml b/pubspec.yaml index beb0c30c..cae815a4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,6 +17,7 @@ dependencies: dev_dependencies: test: ^1.5.3 mockito: ^4.0.0 + pedantic: ^1.9.0 flutter_test: sdk: flutter