2024-06-11 17:05:42 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-09-06 17:00:25 +00:00
|
|
|
import 'package:calorimeter/storage/storage.dart';
|
2024-06-11 17:05:42 +00:00
|
|
|
|
|
|
|
class SettingsDataBloc extends Bloc<SettingsEvent, SettingsState> {
|
2024-09-04 20:47:32 +00:00
|
|
|
final FoodStorage storage;
|
2024-06-11 17:05:42 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|