calorimeter/lib/food_entry/food_entry_bloc.dart

105 lines
2.6 KiB
Dart
Raw Permalink Normal View History

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-06-09 12:42:17 +00:00
class FoodEntryBloc extends Bloc<FoodEvent, FoodEntryState> {
final FoodEntryState 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-06-09 12:42:17 +00:00
on<FoodDeletionEvent>(deleteFood);
2024-06-09 17:06:10 +00:00
on<PageChangedEvent>(updateEntries);
}
void handleFoodEntryEvent(
FoodEntryEvent event, Emitter<FoodEntryState> emit) async {
FoodEntryState newState = FoodEntryState.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-06-09 17:06:10 +00:00
void deleteFood(FoodDeletionEvent event, Emitter<FoodEntryState> 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
2024-06-09 12:42:17 +00:00
emit(FoodEntryState.from(state));
}
2024-06-09 17:06:10 +00:00
void updateEntries(
PageChangedEvent event, Emitter<FoodEntryState> emit) async {
var entries = await storage.getEntriesForDate(event.changedToDate);
var newState = FoodEntryState(foodEntries: entries);
2024-06-09 17:06:10 +00:00
emit(newState);
}
}
2024-06-09 12:42:17 +00:00
class FoodEvent {}
class FoodEntryEvent extends FoodEvent {
final FoodEntry entry;
2024-06-09 17:06:10 +00:00
FoodEntryEvent({required this.entry});
}
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 PageChangedEvent extends FoodEvent {
final DateTime changedToDate;
PageChangedEvent({required this.changedToDate});
2024-06-09 12:42:17 +00:00
}
class FoodEntryState {
final List<FoodEntry> foodEntries;
FoodEntryState({required this.foodEntries});
factory FoodEntryState.init() {
return FoodEntryState(foodEntries: []);
}
static from(FoodEntryState state) {
List<FoodEntry> newList = List.from(state.foodEntries);
return FoodEntryState(foodEntries: newList);
}
void addEntry(FoodEntry entry) {
foodEntries.add(entry);
}
}
class FoodEntry {
final String name;
final double mass;
final double kcalPerMass;
2024-06-09 12:42:17 +00:00
final String id;
FoodEntry({
required this.name,
required this.mass,
required this.kcalPerMass,
2024-06-09 12:42:17 +00:00
}) : id = const Uuid().v1();
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
return '$id,"$name",$mass,$kcalPerMass';
2024-06-09 17:06:10 +00:00
}
}