calorimeter/lib/food_entry/food_entry_bloc.dart

236 lines
6.2 KiB
Dart
Raw Normal View History

import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:calorimeter/food_scan/food_fact_lookup.dart';
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';
class FoodEntryBloc extends Bloc<FoodEvent, PageState> {
final PageState initialState;
final FoodStorage storage;
final DateTime forDate;
2024-06-09 17:06:10 +00:00
FoodEntryBloc(
{required this.initialState,
required this.forDate,
required this.storage})
: super(initialState) {
on<FoodEntryEvent>(handleFoodEntryEvent);
2024-09-24 15:23:01 +00:00
on<FoodChangedEvent>(handleFoodChangedEvent);
on<FoodDeletionEvent>(handleDeleteFoodEvent);
on<BarcodeScanned>(handleBarcodeScannedEvent);
on<FoodEntryTapped>(handleFoodEntryTapped);
}
void handleFoodEntryEvent(
FoodEntryEvent event, Emitter<PageState> emit) async {
PageState newState = PageState.from(state);
newState.addEntry(event.entry);
2024-06-09 17:06:10 +00:00
await storage.writeEntriesForDate(forDate, newState.foodEntries);
storage.addFoodEntryToLookupDatabase(event.entry);
2024-06-09 17:06:10 +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));
}
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
await storage.writeEntriesForDate(forDate, state.foodEntries);
2024-06-09 17:06:10 +00:00
emit(PageState.from(state));
2024-06-09 12:42:17 +00:00
}
2024-06-09 17:06:10 +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(
kcalPer100: 0,
name: "",
mass: 0,
waitingForNetwork: true,
isSelected: false,
);
newList.add(newEntryWaiting);
emit(PageState(foodEntries: newList));
await responseFuture.then((response) {
var index = newList
.indexWhere((entryState) => entryState.id == newEntryWaiting.id);
// element not found (was deleted previously)
if (index == -1) {
return;
}
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,
waitingForNetwork: false,
isSelected: false,
);
newList.removeAt(index);
newList.insert(index, newEntryFinishedWaiting);
emit(PageState(foodEntries: newList));
});
2024-06-09 17:06:10 +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-06-09 12:42:17 +00:00
class FoodEvent {}
class FoodEntryEvent extends FoodEvent {
final FoodEntryState entry;
2024-06-09 17:06:10 +00:00
FoodEntryEvent({required this.entry});
}
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
FoodDeletionEvent({required this.entryID});
2024-06-09 17:06:10 +00:00
}
class BarcodeScanned extends FoodEvent {
final Future<ScanResult> scanResultFuture;
2024-06-09 17:06:10 +00:00
BarcodeScanned({required this.scanResultFuture});
2024-06-09 12:42:17 +00:00
}
class FoodEntryTapped extends FoodEvent {
final String entryID;
FoodEntryTapped({required this.entryID});
}
/// This is the state for one date/page
class PageState {
final List<FoodEntryState> foodEntries;
final String? errorString;
PageState({required this.foodEntries, this.errorString});
factory PageState.init() {
return PageState(foodEntries: []);
}
static from(PageState state) {
return PageState(foodEntries: state.foodEntries);
}
void addEntry(FoodEntryState entry) {
foodEntries.add(entry);
}
}
class FoodEntryState {
final String name;
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;
final bool waitingForNetwork;
bool isSelected;
2024-09-24 15:23:01 +00:00
factory FoodEntryState({
required name,
required mass,
required kcalPer100,
required waitingForNetwork,
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,
isSelected: isSelected,
2024-09-24 15:23:01 +00:00
);
}
FoodEntryState.withID({
required this.id,
required this.name,
required this.mass,
2024-09-24 15:23:01 +00:00
required this.kcalPer100,
required this.waitingForNetwork,
required this.isSelected,
2024-09-24 15:23:01 +00:00
});
2024-06-09 17:06:10 +00:00
set selected(bool selected) => isSelected = selected;
bool get selected => isSelected;
2024-06-09 17:06:10 +00:00
@override
String toString() {
//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
}
}