local_settings.dart 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. late SharedPreferences _prefs;
  14. void init(SharedPreferences preferences) {
  15. _prefs = preferences;
  16. }
  17. AlbumSortKey albumSortKey() {
  18. return AlbumSortKey.values[_prefs.getInt(kCollectionSortPref) ?? 0];
  19. }
  20. Future<bool> setAlbumSortKey(AlbumSortKey key) {
  21. return _prefs.setInt(kCollectionSortPref, key.index);
  22. }
  23. int getPhotoGridSize() {
  24. if (_prefs.containsKey(kPhotoGridSize)) {
  25. return _prefs.getInt(kPhotoGridSize)!;
  26. } else {
  27. return photoGridSizeDefault;
  28. }
  29. }
  30. Future<void> setPhotoGridSize(int value) async {
  31. await _prefs.setInt(kPhotoGridSize, value);
  32. }
  33. }