Skip to content

Commit

Permalink
feat: Scanner improvements (#1781)
Browse files Browse the repository at this point in the history
* All barcode detections are now computed in an Isolate

* Add a setStateSafe method

* Only accept 1D barcodes

* Between each decoding a window is passed

* Let's try to reduce a little bit the quality of the camera

* Improve a little bit the documentation

* Fix a typo

* Fix some typos

* Fixes issues highlighted by MonsieurTanuki in the PR

* Remove irrelevant `mount` check

* Fix wrong import
  • Loading branch information
g123k authored May 11, 2022
1 parent 3894610 commit ca1fa29
Show file tree
Hide file tree
Showing 7 changed files with 590 additions and 117 deletions.
12 changes: 7 additions & 5 deletions packages/smooth_app/lib/data_models/continuous_scan_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,25 @@ class ContinuousScanModel with ChangeNotifier {

Product getProduct(final String barcode) => _productList.getProduct(barcode);

Future<void> onScan(String? code) async {
/// Adds a barcode
/// Will return [true] if this barcode is successfully added
Future<bool> onScan(String? code) async {
if (code == null) {
return;
return false;
}

if (_barcodeTrustCheck != code) {
_barcodeTrustCheck = code;
return;
return false;
}
if (_latestScannedBarcode == code || _barcodes.contains(code)) {
lastConsultedBarcode = code;
return;
return false;
}
AnalyticsHelper.trackScannedProduct(barcode: code);

_latestScannedBarcode = code;
_addBarcode(code);
return _addBarcode(code);
}

Future<bool> onCreateProduct(String? barcode) async {
Expand Down
50 changes: 50 additions & 0 deletions packages/smooth_app/lib/helpers/collections_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'dart:collection';

import 'package:collection/collection.dart';

/// List of [num] with a max length of [_maxCapacity], where we can easily
/// compute the average value of all elements.
class AverageList<T extends num> with ListMixin<T> {
static const int _maxCapacity = 10;
final List<T> _elements = <T>[];

int average(int defaultValueIfEmpty) {
if (_elements.isEmpty) {
return defaultValueIfEmpty;
} else {
return _elements.average.floor();
}
}

@override
int get length => _elements.length;

@override
T operator [](int index) => throw UnsupportedError(
'Please only use the "add" method',
);

@override
void operator []=(int index, T value) {
if (index > _maxCapacity) {
throw UnsupportedError('The index is above the capacity!');
} else {
_elements[index] = value;
}
}

@override
void add(T element) {
// The first element is always the latest added
_elements.insert(0, element);

if (_elements.length >= _maxCapacity) {
_elements.removeLast();
}
}

@override
set length(int newLength) {
throw UnimplementedError('This list has a fixed size of $_maxCapacity');
}
}
Loading

0 comments on commit ca1fa29

Please sign in to comment.