selfprivacy.org.app/lib/logic/cubit/app_settings/app_settings_cubit.dart

67 lines
2.1 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
2022-05-24 18:55:39 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2020-12-08 19:26:51 +00:00
import 'package:equatable/equatable.dart';
2021-01-06 17:35:57 +00:00
import 'package:hive/hive.dart';
import 'package:material_color_utilities/material_color_utilities.dart'
as color_utils;
import 'package:selfprivacy/config/brand_colors.dart';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/config/hive_config.dart';
import 'package:selfprivacy/theming/factory/app_theme_factory.dart';
2020-12-08 19:26:51 +00:00
export 'package:provider/provider.dart';
part 'app_settings_state.dart';
class AppSettingsCubit extends Cubit<AppSettingsState> {
AppSettingsCubit({
2022-06-05 19:36:32 +00:00
required final bool isDarkModeOn,
2023-02-23 14:41:45 +00:00
required final bool isAutoDarkModeOn,
2022-06-05 19:36:32 +00:00
required final bool isOnboardingShowing,
2020-12-08 19:26:51 +00:00
}) : super(
2021-01-06 17:35:57 +00:00
AppSettingsState(
isDarkModeOn: isDarkModeOn,
2023-02-23 14:41:45 +00:00
isAutoDarkModeOn: isAutoDarkModeOn,
isOnboardingShowing: isOnboardingShowing,
2021-01-06 17:35:57 +00:00
),
2020-12-08 19:26:51 +00:00
);
Box box = Hive.box(BNames.appSettingsBox);
2021-01-06 17:35:57 +00:00
void load() async {
2022-06-05 19:36:32 +00:00
final bool? isDarkModeOn = box.get(BNames.isDarkModeOn);
2023-02-23 14:41:45 +00:00
final bool? isAutoDarkModeOn = box.get(BNames.isAutoDarkModeOn);
2022-06-05 19:36:32 +00:00
final bool? isOnboardingShowing = box.get(BNames.isOnboardingShowing);
emit(
state.copyWith(
isDarkModeOn: isDarkModeOn,
2023-02-23 14:41:45 +00:00
isAutoDarkModeOn: isAutoDarkModeOn,
isOnboardingShowing: isOnboardingShowing,
),
);
WidgetsFlutterBinding.ensureInitialized();
final color_utils.CorePalette? colorPalette =
await AppThemeFactory.getCorePalette();
emit(
state.copyWith(
corePalette: colorPalette,
),
);
2021-01-06 17:35:57 +00:00
}
2022-06-05 19:36:32 +00:00
void updateDarkMode({required final bool isDarkModeOn}) {
2021-01-06 17:35:57 +00:00
box.put(BNames.isDarkModeOn, isDarkModeOn);
emit(state.copyWith(isDarkModeOn: isDarkModeOn));
}
2023-02-23 14:41:45 +00:00
void updateAutoDarkMode({required final bool isAutoDarkModeOn}) {
box.put(BNames.isAutoDarkModeOn, isAutoDarkModeOn);
emit(state.copyWith(isAutoDarkModeOn: isAutoDarkModeOn));
}
void turnOffOnboarding({final bool isOnboardingShowing = false}) {
box.put(BNames.isOnboardingShowing, isOnboardingShowing);
2021-01-06 17:35:57 +00:00
2023-02-23 14:41:45 +00:00
emit(state.copyWith(isOnboardingShowing: isOnboardingShowing));
2020-12-08 19:26:51 +00:00
}
}