calorimeter/lib/food_entry_bloc.dart

101 lines
2.4 KiB
Dart
Raw Normal View History

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:calodiary/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;
2024-06-12 12:42:29 +00:00
final AppStorage storage;
final DateTime forDate;
2024-06-09 17:06:10 +00:00
FoodEntryBloc(
{required this.initialState,
required this.forDate,
required this.storage})
: super(initialState) {
2024-06-09 17:06:10 +00:00
on<FoodEntryEvent>(addFoodEntry);
2024-06-09 12:42:17 +00:00
on<FoodDeletionEvent>(deleteFood);
2024-06-09 17:06:10 +00:00
on<PageChangedEvent>(updateEntries);
}
2024-06-09 17:06:10 +00:00
void addFoodEntry(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);
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() {
return '$id,$name,$mass,$kcalPerMass';
}
}