nc-photos/lib/pref.dart

230 lines
8 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'dart:convert';
2021-09-11 14:03:40 +02:00
import 'package:event_bus/event_bus.dart';
import 'package:kiwi/kiwi.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/account.dart';
2021-09-11 14:03:40 +02:00
import 'package:nc_photos/event/event.dart';
2021-04-10 06:28:12 +02:00
import 'package:shared_preferences/shared_preferences.dart';
class Pref {
static Future<void> init() async {
return SharedPreferences.getInstance().then((pref) {
_inst._pref = pref;
});
}
factory Pref.inst() => _inst;
2021-07-23 22:05:57 +02:00
List<Account>? getAccounts() {
2021-09-11 14:03:40 +02:00
final jsonObjs = _pref.getStringList(_toKey(PrefKey.accounts));
2021-07-23 22:05:57 +02:00
return jsonObjs?.map((e) => Account.fromJson(jsonDecode(e))).toList();
2021-04-10 06:28:12 +02:00
}
2021-07-23 22:05:57 +02:00
List<Account> getAccountsOr(List<Account> def) => getAccounts() ?? def;
2021-04-10 06:28:12 +02:00
Future<bool> setAccounts(List<Account> value) {
final jsons = value.map((e) => jsonEncode(e.toJson())).toList();
2021-09-11 14:03:40 +02:00
return _setStringList(PrefKey.accounts, jsons);
2021-04-10 06:28:12 +02:00
}
2021-09-11 14:03:40 +02:00
int? getCurrentAccountIndex() =>
_pref.getInt(_toKey(PrefKey.currentAccountIndex));
2021-07-23 22:05:57 +02:00
int getCurrentAccountIndexOr(int def) => getCurrentAccountIndex() ?? def;
2021-04-10 06:28:12 +02:00
Future<bool> setCurrentAccountIndex(int value) =>
2021-09-11 14:03:40 +02:00
_setInt(PrefKey.currentAccountIndex, value);
2021-04-10 06:28:12 +02:00
2021-09-11 14:03:40 +02:00
int? getHomePhotosZoomLevel() =>
_pref.getInt(_toKey(PrefKey.homePhotosZoomLevel));
2021-07-23 22:05:57 +02:00
int getHomePhotosZoomLevelOr(int def) => getHomePhotosZoomLevel() ?? def;
2021-04-10 06:28:12 +02:00
Future<bool> setHomePhotosZoomLevel(int value) =>
2021-09-11 14:03:40 +02:00
_setInt(PrefKey.homePhotosZoomLevel, value);
2021-04-10 06:28:12 +02:00
2021-09-11 14:03:40 +02:00
int? getAlbumBrowserZoomLevel() =>
_pref.getInt(_toKey(PrefKey.albumBrowserZoomLevel));
int getAlbumBrowserZoomLevelOr(int def) => getAlbumBrowserZoomLevel() ?? def;
Future<bool> setAlbumBrowserZoomLevel(int value) =>
2021-09-11 14:03:40 +02:00
_setInt(PrefKey.albumBrowserZoomLevel, value);
2021-04-10 06:28:12 +02:00
2021-09-11 14:03:40 +02:00
int? getHomeAlbumsSort() => _pref.getInt(_toKey(PrefKey.homeAlbumsSort));
2021-09-08 18:23:42 +02:00
int getHomeAlbumsSortOr(int def) => getHomeAlbumsSort() ?? def;
Future<bool> setHomeAlbumsSort(int value) =>
2021-09-11 14:03:40 +02:00
_setInt(PrefKey.homeAlbumsSort, value);
2021-09-08 18:23:42 +02:00
2021-09-11 14:03:40 +02:00
bool? isEnableExif() => _pref.getBool(_toKey(PrefKey.enableExif));
2021-07-23 22:05:57 +02:00
bool isEnableExifOr([bool def = true]) => isEnableExif() ?? def;
2021-09-11 14:03:40 +02:00
Future<bool> setEnableExif(bool value) => _setBool(PrefKey.enableExif, value);
2021-04-10 06:28:12 +02:00
2021-09-11 14:03:40 +02:00
int? getViewerScreenBrightness() =>
_pref.getInt(_toKey(PrefKey.viewerScreenBrightness));
int getViewerScreenBrightnessOr([int def = -1]) =>
getViewerScreenBrightness() ?? def;
Future<bool> setViewerScreenBrightness(int value) =>
2021-09-11 14:03:40 +02:00
_setInt(PrefKey.viewerScreenBrightness, value);
2021-09-11 14:03:40 +02:00
bool? isViewerForceRotation() =>
_pref.getBool(_toKey(PrefKey.viewerForceRotation));
bool isViewerForceRotationOr([bool def = false]) =>
isViewerForceRotation() ?? def;
Future<bool> setViewerForceRotation(bool value) =>
2021-09-11 14:03:40 +02:00
_setBool(PrefKey.viewerForceRotation, value);
2021-09-11 14:03:40 +02:00
int? getSetupProgress() => _pref.getInt(_toKey(PrefKey.setupProgress));
2021-07-23 22:05:57 +02:00
int getSetupProgressOr([int def = 0]) => getSetupProgress() ?? def;
2021-04-10 06:28:12 +02:00
Future<bool> setSetupProgress(int value) =>
2021-09-11 14:03:40 +02:00
_setInt(PrefKey.setupProgress, value);
2021-04-10 06:28:12 +02:00
2021-04-16 10:45:17 +02:00
/// Return the version number when the app last ran
2021-09-11 14:03:40 +02:00
int? getLastVersion() => _pref.getInt(_toKey(PrefKey.lastVersion));
2021-07-23 22:05:57 +02:00
int getLastVersionOr(int def) => getLastVersion() ?? def;
2021-09-11 14:03:40 +02:00
Future<bool> setLastVersion(int value) => _setInt(PrefKey.lastVersion, value);
2021-04-16 10:45:17 +02:00
2021-09-11 14:03:40 +02:00
bool? isDarkTheme() => _pref.getBool(_toKey(PrefKey.darkTheme));
2021-07-23 22:05:57 +02:00
bool isDarkThemeOr(bool def) => isDarkTheme() ?? def;
2021-09-11 14:03:40 +02:00
Future<bool> setDarkTheme(bool value) => _setBool(PrefKey.darkTheme, value);
2021-04-17 11:04:46 +02:00
2021-09-11 14:03:40 +02:00
bool? isFollowSystemTheme() =>
_pref.getBool(_toKey(PrefKey.followSystemTheme));
bool isFollowSystemThemeOr(bool def) => isFollowSystemTheme() ?? def;
Future<bool> setFollowSystemTheme(bool value) =>
2021-09-11 14:03:40 +02:00
_setBool(PrefKey.followSystemTheme, value);
2021-09-11 14:03:40 +02:00
bool? isUseBlackInDarkTheme() =>
_pref.getBool(_toKey(PrefKey.useBlackInDarkTheme));
bool isUseBlackInDarkThemeOr(bool def) => isUseBlackInDarkTheme() ?? def;
Future<bool> setUseBlackInDarkTheme(bool value) =>
2021-09-11 14:03:40 +02:00
_setBool(PrefKey.useBlackInDarkTheme, value);
2021-09-11 14:03:40 +02:00
int? getLanguage() => _pref.getInt(_toKey(PrefKey.language));
2021-07-23 22:05:57 +02:00
int getLanguageOr(int def) => getLanguage() ?? def;
2021-09-11 14:03:40 +02:00
Future<bool> setLanguage(int value) => _setInt(PrefKey.language, value);
2021-06-23 10:15:25 +02:00
2021-09-14 23:00:24 +02:00
int? getSlideshowDuration() =>
_pref.getInt(_toKey(PrefKey.slideshowDuration));
int getSlideshowDurationOr(int def) => getSlideshowDuration() ?? def;
Future<bool> setSlideshowDuration(int value) =>
_setInt(PrefKey.slideshowDuration, value);
bool? isSlideshowShuffle() =>
_pref.getBool(_toKey(PrefKey.isSlideshowShuffle));
bool isSlideshowShuffleOr(bool def) => isSlideshowShuffle() ?? def;
Future<bool> setSlideshowShuffle(bool value) =>
_setBool(PrefKey.isSlideshowShuffle, value);
bool? isSlideshowRepeat() => _pref.getBool(_toKey(PrefKey.isSlideshowRepeat));
bool isSlideshowRepeatOr(bool def) => isSlideshowRepeat() ?? def;
Future<bool> setSlideshowRepeat(bool value) =>
_setBool(PrefKey.isSlideshowRepeat, value);
2021-09-11 14:03:40 +02:00
bool? hasNewSharedAlbum() => _pref.getBool(_toKey(PrefKey.newSharedAlbum));
2021-08-08 14:09:21 +02:00
bool hasNewSharedAlbumOr(bool def) => hasNewSharedAlbum() ?? def;
Future<bool> setNewSharedAlbum(bool value) =>
2021-09-11 14:03:40 +02:00
_setBool(PrefKey.newSharedAlbum, value);
2021-08-08 14:09:21 +02:00
2021-09-11 14:03:40 +02:00
bool? isLabEnableSharedAlbum() =>
_pref.getBool(_toKey(PrefKey.labEnableSharedAlbum));
2021-08-08 20:46:16 +02:00
bool isLabEnableSharedAlbumOr(bool def) => isLabEnableSharedAlbum() ?? def;
Future<bool> setLabEnableSharedAlbum(bool value) =>
2021-09-11 14:03:40 +02:00
_setBool(PrefKey.labEnableSharedAlbum, value);
2021-08-08 20:46:16 +02:00
2021-04-10 06:28:12 +02:00
Pref._();
2021-09-11 14:03:40 +02:00
Future<bool> _setBool(PrefKey key, bool value) async {
return _onPostSet(await _pref.setBool(_toKey(key), value), key, value);
}
Future<bool> _setInt(PrefKey key, int value) async {
return _onPostSet(await _pref.setInt(_toKey(key), value), key, value);
}
Future<bool> _setStringList(PrefKey key, List<String> value) async {
return _onPostSet(
await _pref.setStringList(_toKey(key), value), key, value);
}
bool _onPostSet(bool result, PrefKey key, dynamic value) {
if (result) {
KiwiContainer().resolve<EventBus>().fire(PrefUpdatedEvent(key, value));
return true;
} else {
return false;
}
}
String _toKey(PrefKey key) {
switch (key) {
case PrefKey.accounts:
return "accounts";
case PrefKey.currentAccountIndex:
return "currentAccountIndex";
case PrefKey.homePhotosZoomLevel:
return "homePhotosZoomLevel";
case PrefKey.albumBrowserZoomLevel:
return "albumViewerZoomLevel";
case PrefKey.homeAlbumsSort:
return "homeAlbumsSort";
case PrefKey.enableExif:
return "isEnableExif";
case PrefKey.viewerScreenBrightness:
return "viewerScreenBrightness";
case PrefKey.viewerForceRotation:
return "viewerForceRotation";
case PrefKey.setupProgress:
return "setupProgress";
case PrefKey.lastVersion:
return "lastVersion";
case PrefKey.darkTheme:
return "isDarkTheme";
case PrefKey.followSystemTheme:
return "isFollowSystemTheme";
case PrefKey.useBlackInDarkTheme:
return "isUseBlackInDarkTheme";
case PrefKey.language:
return "language";
case PrefKey.newSharedAlbum:
return "hasNewSharedAlbum";
case PrefKey.labEnableSharedAlbum:
return "isLabEnableSharedAlbum";
2021-09-14 23:00:24 +02:00
case PrefKey.slideshowDuration:
return "slideshowDuration";
case PrefKey.isSlideshowShuffle:
return "isSlideshowShuffle";
case PrefKey.isSlideshowRepeat:
return "isSlideshowRepeat";
2021-09-11 14:03:40 +02:00
}
}
2021-04-10 06:28:12 +02:00
static final _inst = Pref._();
2021-07-23 22:05:57 +02:00
late SharedPreferences _pref;
2021-04-10 06:28:12 +02:00
}
2021-09-11 14:03:40 +02:00
enum PrefKey {
accounts,
currentAccountIndex,
homePhotosZoomLevel,
albumBrowserZoomLevel,
homeAlbumsSort,
enableExif,
viewerScreenBrightness,
viewerForceRotation,
setupProgress,
lastVersion,
darkTheme,
followSystemTheme,
useBlackInDarkTheme,
language,
newSharedAlbum,
labEnableSharedAlbum,
2021-09-14 23:00:24 +02:00
slideshowDuration,
isSlideshowShuffle,
isSlideshowRepeat,
2021-09-11 14:03:40 +02:00
}
2021-04-10 06:28:12 +02:00
extension PrefExtension on Pref {
2021-07-23 22:05:57 +02:00
Account? getCurrentAccount() {
2021-04-10 06:28:12 +02:00
try {
2021-07-23 22:05:57 +02:00
return Pref.inst().getAccounts()![Pref.inst().getCurrentAccountIndex()!];
2021-04-10 06:28:12 +02:00
} catch (_) {
return null;
}
}
}