calorimeter/lib/food_entry_bloc.dart

114 lines
3.0 KiB
Dart
Raw Normal View History

import 'package:flutter_bloc/flutter_bloc.dart';
2024-06-09 17:06:10 +00:00
import 'package:kalodings/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> {
2024-06-09 17:06:10 +00:00
final FoodStorage storage;
FoodEntryBloc(super.initialState, {required this.storage}) {
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);
on<DailyKcalLimitUpdated>(persistDailyLimit);
}
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(event.date, newState.foodEntries);
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(event.date, state.foodEntries);
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 limit = await storage.readLimit();
var newState = FoodEntryState(foodEntries: entries, kcalLimit: limit);
2024-06-09 17:06:10 +00:00
emit(newState);
}
void persistDailyLimit(
DailyKcalLimitUpdated event, Emitter<FoodEntryState> emit) async {
await storage.updateLimit(event.kcal);
emit(FoodEntryState(foodEntries: state.foodEntries, kcalLimit: event.kcal));
}
void getDailyLimit(
DailyKcalLimitUpdated event, Emitter<FoodEntryState> emit) async {}
}
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
final DateTime date;
FoodEntryEvent({required this.entry, required this.date});
}
2024-06-09 12:42:17 +00:00
class FoodDeletionEvent extends FoodEvent {
final String entryID;
2024-06-09 17:06:10 +00:00
final DateTime date;
FoodDeletionEvent({required this.entryID, required this.date});
}
class PageChangedEvent extends FoodEvent {
final DateTime changedToDate;
PageChangedEvent({required this.changedToDate});
2024-06-09 12:42:17 +00:00
}
class DailyKcalLimitUpdated extends FoodEvent {
final double kcal;
DailyKcalLimitUpdated({required this.kcal});
}
class FoodEntryState {
final List<FoodEntry> foodEntries;
final double kcalLimit;
FoodEntryState({required this.foodEntries, required this.kcalLimit});
factory FoodEntryState.init() {
return FoodEntryState(foodEntries: [], kcalLimit: 0);
}
static from(FoodEntryState state) {
List<FoodEntry> newList = List.from(state.foodEntries);
return FoodEntryState(foodEntries: newList, kcalLimit: state.kcalLimit);
}
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';
}
}