calorimeter/lib/food_entry_bloc.dart

77 lines
1.8 KiB
Dart
Raw Normal View History

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:quiver/core.dart';
class FoodEntryBloc extends Bloc<FoodEntryEvent, FoodEntryState> {
FoodEntryBloc(super.initialState) {
on<FoodEntryEvent>(addFoodEntryToState);
}
void addFoodEntryToState(FoodEntryEvent event, Emitter<FoodEntryState> emit) {
FoodEntryState newState = FoodEntryState.from(state);
newState.addEntry(event.entry);
emit(newState);
}
}
class FoodEntryEvent {
final FoodEntry entry;
FoodEntryEvent({required this.entry});
}
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);
}
@override
int get hashCode {
return hashObjects(foodEntries);
}
@override
operator ==(other) {
if (other.runtimeType != FoodEntryState) return false;
other = other as FoodEntryState;
if (foodEntries.length != other.foodEntries.length) return false;
for (var i = 0; i < foodEntries.length; i += 1) {
if (foodEntries[i].name != other.foodEntries[i].name ||
foodEntries[i].mass != other.foodEntries[i].mass ||
foodEntries[i].kcalPerMass != other.foodEntries[i].kcalPerMass ||
foodEntries[i].kcal != other.foodEntries[i].kcal) {
return false;
}
}
return true;
}
}
class FoodEntry {
final String name;
final double mass;
final double kcalPerMass;
final double kcal;
FoodEntry({
required this.name,
required this.mass,
required this.kcalPerMass,
required this.kcal,
});
}