49 lines
1.3 KiB
Dart
49 lines
1.3 KiB
Dart
import 'package:barcode_scan2/barcode_scan2.dart';
|
|
import 'package:calorimeter/food_entry/food_entry_bloc.dart';
|
|
import 'package:calorimeter/food_scan/food_fact_lookup.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class ScanFoodNotifier with ChangeNotifier {
|
|
void handleBarcodeScannedEvent(BarcodeScanned barcode) async {
|
|
var client = FoodFactLookupClient();
|
|
var scanResult = await barcode.scanResultFuture;
|
|
|
|
if (scanResult.type == ResultType.Cancelled) {
|
|
return;
|
|
}
|
|
if (scanResult.type == ResultType.Error) {
|
|
return;
|
|
}
|
|
var responseFuture = client.retrieveFoodInfo(scanResult.rawContent);
|
|
|
|
var newEntryWaiting = FoodEntryState(
|
|
kcalPer100: 0,
|
|
name: "",
|
|
mass: 0,
|
|
waitingForNetwork: true,
|
|
isSelected: false,
|
|
);
|
|
|
|
await responseFuture.then((response) async {
|
|
if (response.status ==
|
|
FoodFactResponseStatus.foodFactServerNotReachable) {
|
|
return;
|
|
}
|
|
|
|
var newEntryFinishedWaiting = FoodEntryState(
|
|
name: response.food?.name ?? "",
|
|
mass: response.food?.mass ?? 0,
|
|
kcalPer100: response.food?.kcalPer100g ?? 0,
|
|
waitingForNetwork: false,
|
|
isSelected: false,
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
class BarcodeScanned {
|
|
final Future<ScanResult> scanResultFuture;
|
|
|
|
BarcodeScanned({required this.scanResultFuture});
|
|
}
|