settings_manager.dart 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import 'package:shared_preferences/shared_preferences.dart';
  2. import '../game/audio/audio_manager.dart';
  3. import 'theme_manager.dart';
  4. class SettingsManager {
  5. static const String _keyMusicEnabled = 'music_enabled';
  6. static const String _keyHapticsEnabled = 'haptics_enabled';
  7. static const String _keyTutorialShown = 'tutorial_shown';
  8. static const String _keyBgmVolume = 'bgm_volume';
  9. static const String _keySfxVolume = 'sfx_volume';
  10. static const String _keySelectedTheme = 'selected_theme';
  11. static SharedPreferences? _prefs;
  12. static Future<void> init() async {
  13. _prefs = await SharedPreferences.getInstance();
  14. // Initialize theme manager
  15. await ThemeManager.init();
  16. }
  17. // Music Settings
  18. static bool get isMusicEnabled => _prefs?.getBool(_keyMusicEnabled) ?? true;
  19. static Future<void> setMusicEnabled(bool enabled) async {
  20. await _prefs?.setBool(_keyMusicEnabled, enabled);
  21. }
  22. // Haptics Settings
  23. static bool get isHapticsEnabled => _prefs?.getBool(_keyHapticsEnabled) ?? true;
  24. static Future<void> setHapticsEnabled(bool enabled) async {
  25. await _prefs?.setBool(_keyHapticsEnabled, enabled);
  26. }
  27. // BGM Volume Settings
  28. static double get bgmVolume => _prefs?.getDouble(_keyBgmVolume) ?? 0.4;
  29. static Future<void> setBgmVolume(double volume) async {
  30. await _prefs?.setDouble(_keyBgmVolume, volume);
  31. }
  32. // SFX Volume Settings
  33. static double get sfxVolume => _prefs?.getDouble(_keySfxVolume) ?? 0.6;
  34. static Future<void> setSfxVolume(double volume) async {
  35. await _prefs?.setDouble(_keySfxVolume, volume);
  36. }
  37. // Apply BGM volume to AudioManager
  38. static void applyBgmVolume() {
  39. AudioManager().setBgmVolume(bgmVolume);
  40. }
  41. // Apply SFX volume to AudioManager
  42. static void applySfxVolume() {
  43. AudioManager().setSfxVolume(sfxVolume);
  44. }
  45. // Tutorial Settings
  46. static bool get isTutorialShown => _prefs?.getBool(_keyTutorialShown) ?? false;
  47. static Future<void> setTutorialShown(bool shown) async {
  48. await _prefs?.setBool(_keyTutorialShown, shown);
  49. }
  50. // Theme Settings
  51. static SeasonalTheme get selectedTheme {
  52. final themeId = _prefs?.getString(_keySelectedTheme) ?? 'default';
  53. return SeasonalTheme.fromId(themeId);
  54. }
  55. static Future<void> setSelectedTheme(SeasonalTheme theme) async {
  56. await _prefs?.setString(_keySelectedTheme, theme.id);
  57. await ThemeManager.setTheme(theme);
  58. }
  59. }