2024-05-29 22:58:26 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-06-11 17:32:25 +00:00
|
|
|
import 'package:calodiary/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-06-09 12:42:17 +00:00
|
|
|
class FoodEntryBloc extends Bloc<FoodEvent, FoodEntryState> {
|
2024-06-11 17:05:42 +00:00
|
|
|
final FoodEntryState initialState;
|
2024-06-09 17:06:10 +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-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-05-29 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
2024-06-09 17:06:10 +00:00
|
|
|
void addFoodEntry(FoodEntryEvent event, Emitter<FoodEntryState> emit) async {
|
2024-05-29 22:58:26 +00:00
|
|
|
FoodEntryState newState = FoodEntryState.from(state);
|
|
|
|
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-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-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
|
|
|
|
2024-06-11 17:05:42 +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);
|
2024-06-11 17:05:42 +00:00
|
|
|
var newState = FoodEntryState(foodEntries: entries);
|
2024-06-09 17:06:10 +00:00
|
|
|
emit(newState);
|
|
|
|
}
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
2024-06-09 12:42:17 +00:00
|
|
|
class FoodEvent {}
|
|
|
|
|
|
|
|
class FoodEntryEvent extends FoodEvent {
|
2024-05-29 22:58:26 +00:00
|
|
|
final FoodEntry 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-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
|
|
|
}
|
|
|
|
|
|
|
|
class PageChangedEvent extends FoodEvent {
|
|
|
|
final DateTime changedToDate;
|
|
|
|
|
|
|
|
PageChangedEvent({required this.changedToDate});
|
2024-06-09 12:42:17 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 22:58:26 +00:00
|
|
|
class FoodEntryState {
|
|
|
|
final List<FoodEntry> foodEntries;
|
|
|
|
|
2024-06-11 17:05:42 +00:00
|
|
|
FoodEntryState({required this.foodEntries});
|
2024-05-29 22:58:26 +00:00
|
|
|
|
|
|
|
factory FoodEntryState.init() {
|
2024-06-11 17:05:42 +00:00
|
|
|
return FoodEntryState(foodEntries: []);
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static from(FoodEntryState state) {
|
|
|
|
List<FoodEntry> newList = List.from(state.foodEntries);
|
2024-06-11 17:05:42 +00:00
|
|
|
return FoodEntryState(foodEntries: newList);
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2024-05-29 22:58:26 +00:00
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
2024-05-29 22:58:26 +00:00
|
|
|
}
|