calorimeter/lib/settings_bloc.dart
Marco b83f547f6b Implement food entry lookup on entering a food name.
Now, an on-the-fly food lookup is created from existing entries on startup. Those entries are used to make suggestions when the user is typing to enter new food entries.
2024-09-04 22:47:37 +02:00

38 lines
933 B
Dart

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:calodiary/storage/storage.dart';
class SettingsDataBloc extends Bloc<SettingsEvent, SettingsState> {
final FoodStorage storage;
SettingsDataBloc(super.initialState, {required this.storage}) {
on<DailyKcalLimitUpdated>(persistDailyLimit);
}
void persistDailyLimit(
DailyKcalLimitUpdated event, Emitter<SettingsState> emit) async {
await storage.updateLimit(event.kcal);
emit(SettingsState(kcalLimit: event.kcal));
}
}
class SettingsEvent {}
class DailyKcalLimitUpdated extends SettingsEvent {
final double kcal;
DailyKcalLimitUpdated({required this.kcal});
}
class SettingsState {
final double kcalLimit;
SettingsState({required this.kcalLimit});
factory SettingsState.init() {
return SettingsState(kcalLimit: 2000);
}
static from(SettingsState state) {
return SettingsState(kcalLimit: state.kcalLimit);
}
}