40 lines
1.0 KiB
Dart
40 lines
1.0 KiB
Dart
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
/* Copyright (C) 2024 Marco Groß <mgross@sw-gross.de> */
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:calorimeter/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);
|
|
}
|
|
}
|