40 lines
995 B
Dart
40 lines
995 B
Dart
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
/* Copyright (C) 2024 Marco Groß <mgross@sw-gross.de> */
|
|
import 'package:calorimeter/storage/storage.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
class ThemeDataBloc extends Bloc<ThemeToggleEvent, ThemeState> {
|
|
final FoodStorage storage;
|
|
|
|
ThemeDataBloc(super.initialState, {required this.storage}) {
|
|
on<ThemeToggleEvent>(switchTheme);
|
|
}
|
|
|
|
void switchTheme(ThemeToggleEvent event, Emitter<ThemeState> emit) async {
|
|
String newBrightness = 'light';
|
|
if (state.brightness == 'light') newBrightness = 'dark';
|
|
|
|
persistTheme(newBrightness);
|
|
|
|
emit(ThemeState(brightness: newBrightness));
|
|
}
|
|
|
|
void persistTheme(String brightness) async {
|
|
storage.writeBrightness(brightness);
|
|
}
|
|
|
|
Future<String> getPersistedTheme() async {
|
|
return storage.readBrightness();
|
|
}
|
|
}
|
|
|
|
class ThemeToggleEvent {
|
|
ThemeToggleEvent();
|
|
}
|
|
|
|
class ThemeState {
|
|
final String brightness;
|
|
|
|
ThemeState({required this.brightness});
|
|
}
|