Skip to content

Commit

Permalink
Added pedantic lib for better static analysis (#494)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
dariuszseweryn authored Jul 17, 2020
1 parent cedaee1 commit f4051db
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 29 deletions.
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:pedantic/analysis_options.yaml
4 changes: 1 addition & 3 deletions lib/ble_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ abstract class BleManager {
static BleManager _instance;

factory BleManager() {
if (_instance == null) {
_instance = InternalBleManager();
}
_instance ??= InternalBleManager();

return _instance;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/peripheral.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions lib/scan_result.dart
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
67 changes: 44 additions & 23 deletions lib/src/bridge/device_connection_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,41 @@ mixin DeviceConnectionMixin on FlutterBLE {

Future<void> connectToPeripheral(String deviceIdentifier, bool isAutoConnect,
int requestMtu, bool refreshGatt, Duration timeout) async {
return await _methodChannel
.invokeMethod(MethodName.connectToDevice, <String, dynamic>{
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,
<String, dynamic>{
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<PeripheralConnectionState> observePeripheralConnectionState(
String identifier, bool emitCurrentValue) async* {
yield* _peripheralConnectionStateChanges
String identifier, bool emitCurrentValue) {
var controller = StreamController<String>(
onListen: () => _methodChannel.invokeMethod(
MethodName.observeConnectionState,
<String, dynamic>{
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) =>
Expand All @@ -39,32 +60,32 @@ 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, <String, dynamic>{
ArgumentName.deviceIdentifier: identifier,
ArgumentName.emitCurrentValue: emitCurrentValue,
}).catchError(
(errorJson) => throw BleError.fromJson(jsonDecode(errorJson.details)));
}

Future<bool> isPeripheralConnected(String peripheralIdentifier) async {
return await _methodChannel
.invokeMethod(MethodName.isDeviceConnected, <String, dynamic>{
ArgumentName.deviceIdentifier: peripheralIdentifier,
}).catchError((errorJson) =>
Future.error(BleError.fromJson(jsonDecode(errorJson.details))));
}).catchError(
(errorJson) => Future.error(
BleError.fromJson(jsonDecode(errorJson.details)),
),
);
}

Future<void> disconnectOrCancelPeripheralConnection(
String peripheralIdentifier) async {
return await _methodChannel
.invokeMethod(MethodName.cancelConnection, <String, dynamic>{
ArgumentName.deviceIdentifier: peripheralIdentifier,
}).catchError((errorJson) =>
Future.error(BleError.fromJson(jsonDecode(errorJson.details))));
}).catchError(
(errorJson) => Future.error(
BleError.fromJson(jsonDecode(errorJson.details)),
),
);
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies:
dev_dependencies:
test: ^1.5.3
mockito: ^4.0.0
pedantic: ^1.9.0

flutter_test:
sdk: flutter
Expand Down

0 comments on commit f4051db

Please sign in to comment.