2024-09-09 20:41:48 +00:00
|
|
|
import 'package:barcode_scan2/barcode_scan2.dart';
|
|
|
|
import 'package:calorimeter/food_scan/food_fact_lookup.dart';
|
2024-05-29 22:58:26 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/storage/storage.dart';
|
2024-06-09 12:42:17 +00:00
|
|
|
import 'package:uuid/uuid.dart';
|
2024-05-29 22:58:26 +00:00
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
class FoodEntryBloc extends Bloc<FoodEvent, PageState> {
|
|
|
|
final PageState initialState;
|
2024-09-04 20:47:32 +00:00
|
|
|
final FoodStorage storage;
|
2024-06-11 17:05:42 +00:00
|
|
|
final DateTime forDate;
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-06-11 17:05:42 +00:00
|
|
|
FoodEntryBloc(
|
|
|
|
{required this.initialState,
|
|
|
|
required this.forDate,
|
|
|
|
required this.storage})
|
|
|
|
: super(initialState) {
|
2024-09-04 20:47:32 +00:00
|
|
|
on<FoodEntryEvent>(handleFoodEntryEvent);
|
2024-09-24 15:23:01 +00:00
|
|
|
on<FoodChangedEvent>(handleFoodChangedEvent);
|
2024-09-09 20:41:48 +00:00
|
|
|
on<FoodDeletionEvent>(handleDeleteFoodEvent);
|
|
|
|
on<BarcodeScanned>(handleBarcodeScannedEvent);
|
2024-09-25 15:33:40 +00:00
|
|
|
on<FoodEntryTapped>(handleFoodEntryTapped);
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
2024-09-04 20:47:32 +00:00
|
|
|
void handleFoodEntryEvent(
|
2024-09-09 20:41:48 +00:00
|
|
|
FoodEntryEvent event, Emitter<PageState> emit) async {
|
|
|
|
PageState newState = PageState.from(state);
|
2024-05-29 22:58:26 +00:00
|
|
|
newState.addEntry(event.entry);
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-06-11 17:05:42 +00:00
|
|
|
await storage.writeEntriesForDate(forDate, newState.foodEntries);
|
2024-09-04 20:47:32 +00:00
|
|
|
storage.addFoodEntryToLookupDatabase(event.entry);
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-05-29 22:58:26 +00:00
|
|
|
emit(newState);
|
|
|
|
}
|
2024-06-09 12:42:17 +00:00
|
|
|
|
2024-09-24 15:23:01 +00:00
|
|
|
void handleFoodChangedEvent(
|
|
|
|
FoodChangedEvent event, Emitter<PageState> emit) async {
|
|
|
|
var entries = state.foodEntries;
|
|
|
|
var index = entries.indexWhere((entry) {
|
|
|
|
return entry.id == event.newEntry.id;
|
|
|
|
});
|
|
|
|
|
|
|
|
entries.removeAt(index);
|
|
|
|
entries.insert(index, event.newEntry);
|
|
|
|
|
|
|
|
await storage.writeEntriesForDate(forDate, entries);
|
|
|
|
storage.addFoodEntryToLookupDatabase(event.newEntry);
|
|
|
|
|
|
|
|
emit(PageState(foodEntries: entries));
|
|
|
|
}
|
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
void handleDeleteFoodEvent(
|
|
|
|
FoodDeletionEvent event, Emitter<PageState> emit) async {
|
2024-06-09 12:42:17 +00:00
|
|
|
state.foodEntries.removeWhere((entry) => entry.id == event.entryID);
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-06-11 17:05:42 +00:00
|
|
|
await storage.writeEntriesForDate(forDate, state.foodEntries);
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
emit(PageState.from(state));
|
2024-06-09 12:42:17 +00:00
|
|
|
}
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
void handleBarcodeScannedEvent(
|
|
|
|
BarcodeScanned event, Emitter<PageState> emit) async {
|
|
|
|
var client = FoodFactLookupClient();
|
|
|
|
var scanResult = await event.scanResultFuture;
|
|
|
|
|
|
|
|
if (scanResult.type == ResultType.Cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (scanResult.type == ResultType.Error) {
|
|
|
|
emit(PageState(
|
|
|
|
foodEntries: state.foodEntries,
|
|
|
|
errorString: "Fehler beim Scannen des Barcodes"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var responseFuture = client.retrieveFoodInfo(scanResult.rawContent);
|
|
|
|
|
|
|
|
List<FoodEntryState> newList = List.from(state.foodEntries);
|
|
|
|
var newEntryWaiting = FoodEntryState(
|
2024-09-25 15:33:40 +00:00
|
|
|
kcalPer100: 0,
|
|
|
|
name: "",
|
|
|
|
mass: 0,
|
|
|
|
waitingForNetwork: true,
|
|
|
|
isSelected: false,
|
|
|
|
);
|
2024-09-09 20:41:48 +00:00
|
|
|
newList.add(newEntryWaiting);
|
|
|
|
emit(PageState(foodEntries: newList));
|
|
|
|
|
|
|
|
await responseFuture.then((response) {
|
2024-09-25 15:33:40 +00:00
|
|
|
var index = newList
|
|
|
|
.indexWhere((entryState) => entryState.id == newEntryWaiting.id);
|
|
|
|
|
|
|
|
// element not found (was deleted previously)
|
|
|
|
if (index == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2024-09-09 20:41:48 +00:00
|
|
|
|
|
|
|
if (response.status == FoodFactResponseStatus.barcodeNotFound) {
|
|
|
|
emit(PageState(
|
|
|
|
foodEntries: state.foodEntries,
|
|
|
|
errorString: "Barcode konnte nicht gefunden werden."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (response.status ==
|
|
|
|
FoodFactResponseStatus.foodFactServerNotReachable) {
|
|
|
|
emit(PageState(
|
|
|
|
foodEntries: state.foodEntries,
|
|
|
|
errorString: "OpenFoodFacts-Server konnte nicht erreicht werden."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var newEntryFinishedWaiting = FoodEntryState(
|
|
|
|
name: response.food?.name ?? "",
|
|
|
|
mass: response.food?.mass ?? 0,
|
2024-09-24 15:23:01 +00:00
|
|
|
kcalPer100: response.food?.kcalPer100g ?? 0,
|
2024-09-09 20:41:48 +00:00
|
|
|
waitingForNetwork: false,
|
2024-09-25 15:33:40 +00:00
|
|
|
isSelected: false,
|
2024-09-09 20:41:48 +00:00
|
|
|
);
|
2024-09-25 15:33:40 +00:00
|
|
|
|
|
|
|
newList.removeAt(index);
|
|
|
|
newList.insert(index, newEntryFinishedWaiting);
|
2024-09-09 20:41:48 +00:00
|
|
|
emit(PageState(foodEntries: newList));
|
|
|
|
});
|
2024-06-09 17:06:10 +00:00
|
|
|
}
|
2024-09-25 15:33:40 +00:00
|
|
|
|
|
|
|
void handleFoodEntryTapped(
|
|
|
|
FoodEntryTapped event, Emitter<PageState> emit) async {
|
|
|
|
for (var entry in state.foodEntries) {
|
|
|
|
entry.isSelected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var selectedEntry = state.foodEntries.firstWhere((entry) {
|
|
|
|
return entry.id == event.entryID;
|
|
|
|
});
|
|
|
|
|
|
|
|
selectedEntry.selected = !selectedEntry.selected;
|
|
|
|
|
|
|
|
emit(PageState(foodEntries: state.foodEntries));
|
|
|
|
}
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
2024-06-09 12:42:17 +00:00
|
|
|
class FoodEvent {}
|
|
|
|
|
|
|
|
class FoodEntryEvent extends FoodEvent {
|
2024-09-09 20:41:48 +00:00
|
|
|
final FoodEntryState entry;
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-06-11 17:05:42 +00:00
|
|
|
FoodEntryEvent({required this.entry});
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
2024-09-24 15:23:01 +00:00
|
|
|
class FoodChangedEvent extends FoodEvent {
|
|
|
|
final FoodEntryState newEntry;
|
|
|
|
|
|
|
|
FoodChangedEvent({required this.newEntry});
|
|
|
|
}
|
|
|
|
|
2024-06-09 12:42:17 +00:00
|
|
|
class FoodDeletionEvent extends FoodEvent {
|
|
|
|
final String entryID;
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-06-11 17:05:42 +00:00
|
|
|
FoodDeletionEvent({required this.entryID});
|
2024-06-09 17:06:10 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
class BarcodeScanned extends FoodEvent {
|
|
|
|
final Future<ScanResult> scanResultFuture;
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
BarcodeScanned({required this.scanResultFuture});
|
2024-06-09 12:42:17 +00:00
|
|
|
}
|
|
|
|
|
2024-09-25 15:33:40 +00:00
|
|
|
class FoodEntryTapped extends FoodEvent {
|
|
|
|
final String entryID;
|
|
|
|
|
|
|
|
FoodEntryTapped({required this.entryID});
|
|
|
|
}
|
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
/// This is the state for one date/page
|
|
|
|
class PageState {
|
|
|
|
final List<FoodEntryState> foodEntries;
|
|
|
|
final String? errorString;
|
2024-05-29 22:58:26 +00:00
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
PageState({required this.foodEntries, this.errorString});
|
2024-05-29 22:58:26 +00:00
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
factory PageState.init() {
|
|
|
|
return PageState(foodEntries: []);
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
static from(PageState state) {
|
|
|
|
return PageState(foodEntries: state.foodEntries);
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
void addEntry(FoodEntryState entry) {
|
2024-05-29 22:58:26 +00:00
|
|
|
foodEntries.add(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 20:41:48 +00:00
|
|
|
class FoodEntryState {
|
2024-05-29 22:58:26 +00:00
|
|
|
final String name;
|
2024-09-09 20:41:48 +00:00
|
|
|
final int mass;
|
2024-09-24 15:23:01 +00:00
|
|
|
final int kcalPer100;
|
2024-06-09 12:42:17 +00:00
|
|
|
final String id;
|
2024-09-09 20:41:48 +00:00
|
|
|
final bool waitingForNetwork;
|
2024-09-25 15:33:40 +00:00
|
|
|
bool isSelected;
|
2024-05-29 22:58:26 +00:00
|
|
|
|
2024-09-24 15:23:01 +00:00
|
|
|
factory FoodEntryState({
|
|
|
|
required name,
|
|
|
|
required mass,
|
|
|
|
required kcalPer100,
|
|
|
|
required waitingForNetwork,
|
2024-09-25 15:33:40 +00:00
|
|
|
required isSelected,
|
2024-09-24 15:23:01 +00:00
|
|
|
}) {
|
|
|
|
return FoodEntryState.withID(
|
|
|
|
id: const Uuid().v1(),
|
|
|
|
name: name,
|
|
|
|
mass: mass,
|
|
|
|
kcalPer100: kcalPer100,
|
|
|
|
waitingForNetwork: waitingForNetwork,
|
2024-09-25 15:33:40 +00:00
|
|
|
isSelected: isSelected,
|
2024-09-24 15:23:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
FoodEntryState.withID({
|
|
|
|
required this.id,
|
2024-05-29 22:58:26 +00:00
|
|
|
required this.name,
|
|
|
|
required this.mass,
|
2024-09-24 15:23:01 +00:00
|
|
|
required this.kcalPer100,
|
2024-09-09 20:41:48 +00:00
|
|
|
required this.waitingForNetwork,
|
2024-09-25 15:33:40 +00:00
|
|
|
required this.isSelected,
|
2024-09-24 15:23:01 +00:00
|
|
|
});
|
2024-06-09 17:06:10 +00:00
|
|
|
|
2024-09-25 15:33:40 +00:00
|
|
|
set selected(bool selected) => isSelected = selected;
|
|
|
|
bool get selected => isSelected;
|
|
|
|
|
2024-06-09 17:06:10 +00:00
|
|
|
@override
|
|
|
|
String toString() {
|
2024-09-13 20:35:32 +00:00
|
|
|
//we use quotation marks around the name because the name might contain
|
|
|
|
//commas and we want to store it in a csv file
|
2024-09-24 15:23:01 +00:00
|
|
|
return '$id,"$name",$mass,$kcalPer100';
|
2024-06-09 17:06:10 +00:00
|
|
|
}
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|