Skip to content

Commit ee556a3

Browse files
committed
Refactor code to improve cancellation handling and remove unused outputs in CI workflow
1 parent 51c6df2 commit ee556a3

10 files changed

Lines changed: 31 additions & 21 deletions

File tree

.github/workflows/flutter-ci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ jobs:
1919
defaults:
2020
run:
2121
working-directory: client
22-
outputs:
23-
flutter-version: ${{ steps.setup-flutter.outputs.flutter-version }}
22+
# outputs removed as setup-flutter does not define flutter-version output
2423

2524
steps:
2625
- name: Checkout code

client/lib/devices/borneo/lyfi/view_models/settings_view_model.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:borneo_app/devices/borneo/lyfi/view_models/base_lyfi_device_view
22
import 'package:borneo_app/infrastructure/timezone.dart';
33
import 'package:borneo_app/services/i_app_notification_service.dart';
44
import 'package:borneo_common/exceptions.dart' as bo_ex;
5-
import 'package:timezone/timezone.dart' as tz;
5+
import 'package:cancellation_token/cancellation_token.dart';
66
import 'package:geolocator/geolocator.dart';
77

88
import 'package:borneo_kernel/drivers/borneo/borneo_device_api.dart';
@@ -83,6 +83,7 @@ class SettingsViewModel extends BaseLyfiDeviceViewModel {
8383
});
8484
}
8585

86+
/*
8687
Duration _getTimeDifference(String timezone1, String timezone2) {
8788
final location1 = tz.getLocation(timezone1);
8889
final location2 = tz.getLocation(timezone2);
@@ -91,22 +92,22 @@ class SettingsViewModel extends BaseLyfiDeviceViewModel {
9192
final offset2 = tz.TZDateTime.now(location2).timeZoneOffset;
9293
return offset1 - offset2;
9394
}
95+
*/
9496

95-
Future<Position> getLocation() async {
96-
// TODO cancellable
97+
Future<Position> getLocation({CancellationToken? cancel}) async {
9798
bool serviceEnabled;
9899
LocationPermission permission;
99100

100101
// Check if location services are enabled
101-
serviceEnabled = await Geolocator.isLocationServiceEnabled();
102+
serviceEnabled = await Geolocator.isLocationServiceEnabled().asCancellable(cancel);
102103
if (!serviceEnabled) {
103104
throw bo_ex.InvalidOperationException(message: 'Please enable location services');
104105
}
105106

106107
// Check permissions
107-
permission = await Geolocator.checkPermission();
108+
permission = await Geolocator.checkPermission().asCancellable(cancel);
108109
if (permission == LocationPermission.denied) {
109-
permission = await Geolocator.requestPermission();
110+
permission = await Geolocator.requestPermission().asCancellable(cancel);
110111
if (permission == LocationPermission.denied) {
111112
throw bo_ex.PermissionDeniedException(message: 'Location permissions are denied');
112113
}
@@ -123,7 +124,7 @@ class SettingsViewModel extends BaseLyfiDeviceViewModel {
123124
timeLimit: Duration(seconds: 30),
124125
distanceFilter: 100,
125126
),
126-
);
127+
).asCancellable(cancel);
127128

128129
return position;
129130
}

client/lib/services/device_manager.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,18 @@ final class DeviceManager implements IDisposable {
146146
deviceEvents.fire(DeviceEntityUpdatedEvent(oldEntity, updatedEntity));
147147
}
148148

149+
Future<bool> _groupExists(Transaction tx, String groupID) async {
150+
final groupStore = stringMapStoreFactory.store(StoreNames.groups);
151+
final groupRecord = await groupStore.record(groupID).get(tx);
152+
return groupRecord != null;
153+
}
154+
149155
Future<void> moveToGroup(String id, String newGroupID) async {
150156
return await _db.transaction((tx) async {
151-
// TODO ensure newGroupID exists
157+
final exists = await _groupExists(tx, newGroupID);
158+
if (!exists) {
159+
throw KeyNotFoundException(message: 'Cannot find group with ID `$newGroupID`');
160+
}
152161
return await _update(id, tx: tx, groupID: newGroupID);
153162
});
154163
}

client/lib/view_models/abstract_screen_view_model.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ abstract class AbstractScreenViewModel extends BaseViewModel with ViewModelEvent
44
bool _isInitialized = false;
55
bool get isInitialized => _isInitialized;
66

7-
AbstractScreenViewModel();
7+
AbstractScreenViewModel({super.logger});
88

99
Future<void> initialize() async {
1010
if (_isInitialized) {

client/lib/view_models/scenes/scene_edit_view_model.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'package:borneo_app/models/scene_entity.dart';
22
import 'package:borneo_app/services/scene_manager.dart';
33
import 'package:borneo_app/view_models/abstract_screen_view_model.dart';
44
import 'package:event_bus/event_bus.dart';
5-
import 'package:logger/logger.dart';
65

76
import '../base_view_model.dart';
87

@@ -14,7 +13,6 @@ final class SceneEditArguments {
1413
}
1514

1615
class SceneEditViewModel extends AbstractScreenViewModel with ViewModelEventBusMixin {
17-
final Logger? logger;
1816
final SceneManager _sceneManager;
1917
final bool isCreation;
2018
late final String? id;
@@ -29,7 +27,7 @@ class SceneEditViewModel extends AbstractScreenViewModel with ViewModelEventBusM
2927
this._sceneManager, {
3028
required this.isCreation,
3129
SceneEntity? model,
32-
this.logger,
30+
super.logger,
3331
}) : _deletionAvailable = !isCreation {
3432
super.globalEventBus = globalEventBus;
3533
if (isCreation) {

client/lib/widgets/icon_progress.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ class IconProgressBar extends StatefulWidget {
99
final Duration? duration;
1010

1111
const IconProgressBar({
12-
Key? key,
12+
super.key,
1313
required this.icon,
1414
this.size = 50.0,
1515
required this.progress,
1616
this.backgroundColor = Colors.grey,
1717
this.progressColor = Colors.blue,
1818
this.duration,
19-
}) : super(key: key);
19+
});
2020

2121
@override
2222
_IconProgressBarState createState() => _IconProgressBarState();

client/lib/widgets/map_location_picker.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_map/flutter_map.dart';
3-
import 'package:geocoding/geocoding.dart';
43
import 'package:geolocator/geolocator.dart';
54
import 'package:latlong2/latlong.dart';
65
import 'package:lat_lng_to_timezone/lat_lng_to_timezone.dart';
@@ -20,7 +19,7 @@ class _MapLocationPickerState extends State<MapLocationPicker> {
2019
LatLng? _userLocation;
2120
String? _currentTimeZone;
2221

23-
final TextEditingController _searchController = TextEditingController();
22+
// final TextEditingController _searchController = TextEditingController();
2423

2524
@override
2625
void initState() {
@@ -72,6 +71,7 @@ class _MapLocationPickerState extends State<MapLocationPicker> {
7271
});
7372
}
7473

74+
/*
7575
Future<List<String>> _getSuggestions(String query) async {
7676
if (query.isEmpty) return [];
7777
try {
@@ -105,6 +105,7 @@ class _MapLocationPickerState extends State<MapLocationPicker> {
105105
// Handle error
106106
}
107107
}
108+
*/
108109

109110
bool _isSameLocation(LatLng? a, LatLng? b, {double epsilon = 1e-6}) {
110111
if (a == null || b == null) return false;

client/packages/borneo_common/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ dependencies:
1313
cancellation_token: ^2.0.1
1414

1515
dev_dependencies:
16-
test: ^1.25.0
16+
test: ^1.25.0
17+
lints: ^6.0.0

client/packages/borneo_kernel/lib/drivers/borneo/lyfi/lyfi_driver.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@ class BorneoLyfiDriver
408408
@override
409409
Future<bool> heartbeat(Device dev, {CancellationToken? cancelToken}) async {
410410
try {
411-
await getOnOff(dev).asCancellable(cancelToken); // FIXME
412-
return true;
411+
final dd = dev.driverData as LyfiDriverData;
412+
return await dd.coap.ping().asCancellable(cancelToken);
413413
} catch (e) {
414414
return false;
415415
}

client/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ dev_dependencies:
101101
sdk: flutter
102102
test: ^1.25.0
103103
build_runner: ^2.4.13
104+
lints: ^6.0.0
104105

105106
# The "flutter_lints" package below contains a set of recommended lints to
106107
# encourage good coding practices. The lint set provided by the package is

0 commit comments

Comments
 (0)