local_settings.dart 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import 'package:photos/core/constants.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. enum AlbumSortKey {
  4. albumName,
  5. newestPhoto,
  6. lastUpdated,
  7. }
  8. class LocalSettings {
  9. LocalSettings._privateConstructor();
  10. static final LocalSettings instance = LocalSettings._privateConstructor();
  11. static const kCollectionSortPref = "collection_sort_pref";
  12. static const kPhotoGridSize = "photo_grid_size";
  13. static const kEnableMagicSearch = "enable_magic_search";
  14. late SharedPreferences _prefs;
  15. void init(SharedPreferences preferences) {
  16. _prefs = preferences;
  17. }
  18. AlbumSortKey albumSortKey() {
  19. return AlbumSortKey.values[_prefs.getInt(kCollectionSortPref) ?? 0];
  20. }
  21. Future<bool> setAlbumSortKey(AlbumSortKey key) {
  22. return _prefs.setInt(kCollectionSortPref, key.index);
  23. }
  24. int getPhotoGridSize() {
  25. if (_prefs.containsKey(kPhotoGridSize)) {
  26. return _prefs.getInt(kPhotoGridSize)!;
  27. } else {
  28. return photoGridSizeDefault;
  29. }
  30. }
  31. Future<void> setPhotoGridSize(int value) async {
  32. await _prefs.setInt(kPhotoGridSize, value);
  33. }
  34. bool hasEnabledMagicSearch() {
  35. if (_prefs.containsKey(kEnableMagicSearch)) {
  36. return _prefs.getBool(kEnableMagicSearch)!;
  37. }
  38. return false;
  39. }
  40. Future<void> setShouldEnableMagicSearch(bool value) async {
  41. await _prefs.setBool(kEnableMagicSearch, value);
  42. }
  43. }