Skip to content

Commit

Permalink
Code review fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
martawoldanska committed Jan 21, 2020
1 parent 544e483 commit a8021d9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 37 deletions.
2 changes: 2 additions & 0 deletions example/lib/adapter/ble_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class BleAdapter {

Future<List<BleService>> discoverAndGetServicesCharacteristics(
String peripheralId) async {
// TODO remove connect() call when connectivity handling is implemented
await _scannedPeripherals[peripheralId].connect();
await _scannedPeripherals[peripheralId]
.discoverAllServicesAndCharacteristics();
Expand All @@ -108,6 +109,7 @@ class BleAdapter {
bleServices.add(BleService(service.uuid, bleCharacteristics));
}

// TODO remove when connectivity handling is implemented
_scannedPeripherals[peripheralId].disconnectOrCancelConnection();

return bleServices;
Expand Down
22 changes: 11 additions & 11 deletions example/lib/example_peripherals/generic_peripheral.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,42 @@ class GenericPeripheral extends SimulatedPeripheral {
SimulatedCharacteristic(
uuid: 'F000AA10-0001-4000-B000-000000000000',
value: Uint8List.fromList([0]),
convenienceName: 'Generic characteristic'),
convenienceName: 'Generic characteristic 1'),
],
convenienceName: 'Generic service',
convenienceName: 'Generic service 1',
),
SimulatedService(
uuid: 'F000AA01-0001-4000-B000-000000000000',
isAdvertised: true,
characteristics: [
SimulatedCharacteristic(
uuid: 'F000AA10-0001-4000-B000-000000000000',
uuid: 'F000AA11-0001-4000-B000-000000000000',
value: Uint8List.fromList([0]),
convenienceName: 'Generic characteristic'),
convenienceName: 'Generic characteristic 2'),
],
convenienceName: 'Generic service',
convenienceName: 'Generic service 2',
),
SimulatedService(
uuid: 'F000AA02-0001-4000-B000-000000000000',
isAdvertised: true,
characteristics: [
SimulatedCharacteristic(
uuid: 'F000AA10-0001-4000-B000-000000000000',
uuid: 'F000AA12-0001-4000-B000-000000000000',
value: Uint8List.fromList([0]),
convenienceName: 'Generic characteristic'),
convenienceName: 'Generic characteristic 3'),
],
convenienceName: 'Generic service',
convenienceName: 'Generic service 3',
),
SimulatedService(
uuid: 'F000AA03-0001-4000-B000-000000000000',
isAdvertised: true,
characteristics: [
SimulatedCharacteristic(
uuid: 'F000AA10-0001-4000-B000-000000000000',
uuid: 'F000AA13-0001-4000-B000-000000000000',
value: Uint8List.fromList([0]),
convenienceName: 'Generic characteristic'),
convenienceName: 'Generic characteristic 4'),
],
convenienceName: 'Generic service',
convenienceName: 'Generic service 4',
),
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ class PeripheralDetailsView extends StatelessWidget {
child: ListView.builder(
shrinkWrap: true,
itemCount: state.bleServiceStates.length,
itemBuilder: (context, index) =>
_createServiceTileView(context, state.bleServiceStates[index]),
itemBuilder: (context, index) => _createServiceTileView(
context, state.bleServiceStates[index], index),
),
);
}

Widget _createServiceTileView(
BuildContext context,
BleServiceState serviceState,
int index,
) {
// ignore: close_sinks
final PeripheralDetailsBloc bloc =
Expand All @@ -73,22 +74,20 @@ class PeripheralDetailsView extends StatelessWidget {
icon: Icon(
serviceState.expanded ? Icons.unfold_less : Icons.unfold_more),
onPressed: () => bloc.add(ServiceViewExpandedEvent(
serviceState,
!serviceState.expanded,
index,
)),
),
),
serviceState.expanded
? Padding(
padding: EdgeInsets.only(left: 16.0),
child: ListView.builder(
itemCount: serviceState.service.characteristics.length,
itemBuilder: (context, index) => _buildCharacteristicCard(
context, serviceState.service.characteristics[index]),
shrinkWrap: true,
),
)
: Column(),
if (serviceState.expanded)
Padding(
padding: EdgeInsets.only(left: 16.0),
child: ListView.builder(
itemCount: serviceState.service.characteristics.length,
itemBuilder: (context, index) => _buildCharacteristicCard(
context, serviceState.service.characteristics[index]),
shrinkWrap: true,
),
),
],
);
}
Expand Down Expand Up @@ -121,7 +120,7 @@ class PeripheralDetailsView extends StatelessWidget {
List<String> _getCharacteristicProperties(BleCharacteristic characteristic) {
List<String> properties = new List<String>();

if (characteristic.isWritableWithoutResponse ||
if (characteristic.isWritableWithResponse ||
characteristic.isWritableWithoutResponse) {
properties.add("write");
}
Expand Down
5 changes: 2 additions & 3 deletions example/lib/peripheral_details/peripheral_details_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ class PeripheralDetailsBloc
List<BleServiceState> newBleServiceStates =
List.from(state.bleServiceStates);

int serviceIndex = newBleServiceStates.indexOf(event.serviceStateToChange);
newBleServiceStates[serviceIndex] =
BleServiceState(event.serviceStateToChange.service, event.expanded);
newBleServiceStates[event.index] =
BleServiceState(state.bleServiceStates[event.index].service, !state.bleServiceStates[event.index].expanded);

return PeripheralDetailsState(
peripheral: state.peripheral,
Expand Down
8 changes: 3 additions & 5 deletions example/lib/peripheral_details/peripheral_details_event.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:blemulator_example/model/ble_service.dart';
import 'package:blemulator_example/peripheral_details/peripheral_details_state.dart';
import 'package:equatable/equatable.dart';

abstract class PeripheralDetailsEvent extends Equatable {
Expand All @@ -17,10 +16,9 @@ class ServicesFetchedEvent extends PeripheralDetailsEvent {

class ServiceViewExpandedEvent extends PeripheralDetailsEvent {
@override
List<Object> get props => [serviceStateToChange, expanded];
List<Object> get props => [index];

final BleServiceState serviceStateToChange;
final bool expanded;
final int index;

ServiceViewExpandedEvent(this.serviceStateToChange, this.expanded);
ServiceViewExpandedEvent(this.index);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ void main() {
test('should map ServiceViewExpandedEvent to PeripheralDetailsState', () async {
// given
SampleBleService service = SampleBleService();
BleServiceState bleServiceState = BleServiceState(service, false);
BleServiceState newBleServiceState = BleServiceState(service, true);

peripheralDetailsBloc.add(ServicesFetchedEvent([service]));
Expand All @@ -65,7 +64,7 @@ void main() {
peripheral: peripheral, bleServiceStates: [newBleServiceState]);

// when
peripheralDetailsBloc.add(ServiceViewExpandedEvent(bleServiceState, true));
peripheralDetailsBloc.add(ServiceViewExpandedEvent(0));

// then
await expectLater(
Expand Down

0 comments on commit a8021d9

Please sign in to comment.