settings_controller.dart 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2022, the Flutter project authors. Please see the AUTHORS file
  2. // for details. All rights reserved. Use of this source code is governed by a
  3. // BSD-style license that can be found in the LICENSE file.
  4. import 'dart:async';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:image_puzzle/persistence/persistence.dart';
  7. /// An class that holds settings like [playerName] or [musicOn],
  8. /// and saves them to an injected persistence store.
  9. class SettingsController {
  10. final Persistence _persistence;
  11. /// Whether or not the sound is on at all. This overrides both music
  12. /// and sound.
  13. ValueNotifier<bool> sound = ValueNotifier(true);
  14. ValueNotifier<bool> music = ValueNotifier(true);
  15. ValueNotifier<bool> vibrate = ValueNotifier(true);
  16. ValueNotifier<int> skin = ValueNotifier(0);
  17. /// Creates a new instance of [SettingsController] backed by [persistence].
  18. SettingsController({required Persistence persistence}) : _persistence = persistence;
  19. /// Asynchronously loads values from the injected persistence store.
  20. Future<void> loadStateFromPersistence() async {
  21. sound.value = _persistence.sound;
  22. music.value = _persistence.music;
  23. vibrate.value = _persistence.vibrate;
  24. skin.value = _persistence.skin;
  25. }
  26. void toggleSound() {
  27. sound.value = !sound.value;
  28. _persistence.sound = sound.value;
  29. }
  30. void toggleMusic() {
  31. music.value = !music.value;
  32. _persistence.music = music.value;
  33. }
  34. void toggleVibrate() {
  35. vibrate.value = !vibrate.value;
  36. _persistence.vibrate = vibrate.value;
  37. }
  38. void setSkin(int value) {
  39. skin.value = value;
  40. _persistence.skin = value;
  41. }
  42. }