nc-photos/lib/pref.dart

89 lines
3.2 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'dart:convert';
import 'package:nc_photos/account.dart';
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-04-10 06:28:12 +02:00
final jsonObjs = _pref.getStringList("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();
return _pref.setStringList("accounts", jsons);
}
2021-07-23 22:05:57 +02:00
int? getCurrentAccountIndex() => _pref.getInt("currentAccountIndex");
int getCurrentAccountIndexOr(int def) => getCurrentAccountIndex() ?? def;
2021-04-10 06:28:12 +02:00
Future<bool> setCurrentAccountIndex(int value) =>
_pref.setInt("currentAccountIndex", value);
2021-07-23 22:05:57 +02:00
int? getHomePhotosZoomLevel() => _pref.getInt("homePhotosZoomLevel");
int getHomePhotosZoomLevelOr(int def) => getHomePhotosZoomLevel() ?? def;
2021-04-10 06:28:12 +02:00
Future<bool> setHomePhotosZoomLevel(int value) =>
_pref.setInt("homePhotosZoomLevel", value);
int? getAlbumBrowserZoomLevel() => _pref.getInt("albumViewerZoomLevel");
int getAlbumBrowserZoomLevelOr(int def) => getAlbumBrowserZoomLevel() ?? def;
Future<bool> setAlbumBrowserZoomLevel(int value) =>
2021-04-10 06:28:12 +02:00
_pref.setInt("albumViewerZoomLevel", value);
2021-07-23 22:05:57 +02:00
bool? isEnableExif() => _pref.getBool("isEnableExif");
bool isEnableExifOr([bool def = true]) => isEnableExif() ?? def;
2021-04-10 06:28:12 +02:00
Future<bool> setEnableExif(bool value) =>
_pref.setBool("isEnableExif", value);
2021-07-23 22:05:57 +02:00
int? getSetupProgress() => _pref.getInt("setupProgress");
int getSetupProgressOr([int def = 0]) => getSetupProgress() ?? def;
2021-04-10 06:28:12 +02:00
Future<bool> setSetupProgress(int value) =>
_pref.setInt("setupProgress", value);
2021-04-16 10:45:17 +02:00
/// Return the version number when the app last ran
2021-07-23 22:05:57 +02:00
int? getLastVersion() => _pref.getInt("lastVersion");
int getLastVersionOr(int def) => getLastVersion() ?? def;
2021-04-16 10:45:17 +02:00
Future<bool> setLastVersion(int value) => _pref.setInt("lastVersion", value);
2021-07-23 22:05:57 +02:00
bool? isDarkTheme() => _pref.getBool("isDarkTheme");
bool isDarkThemeOr(bool def) => isDarkTheme() ?? def;
2021-04-17 11:04:46 +02:00
Future<bool> setDarkTheme(bool value) => _pref.setBool("isDarkTheme", value);
2021-07-23 22:05:57 +02:00
int? getLanguage() => _pref.getInt("language");
int getLanguageOr(int def) => getLanguage() ?? def;
2021-06-23 10:15:25 +02:00
Future<bool> setLanguage(int value) => _pref.setInt("language", value);
2021-08-08 14:09:21 +02:00
bool? hasNewSharedAlbum() => _pref.getBool("hasNewSharedAlbum");
bool hasNewSharedAlbumOr(bool def) => hasNewSharedAlbum() ?? def;
Future<bool> setNewSharedAlbum(bool value) =>
_pref.setBool("hasNewSharedAlbum", value);
2021-08-08 20:46:16 +02:00
bool? isLabEnableSharedAlbum() => _pref.getBool("isLabEnableSharedAlbum");
bool isLabEnableSharedAlbumOr(bool def) => isLabEnableSharedAlbum() ?? def;
Future<bool> setLabEnableSharedAlbum(bool value) =>
_pref.setBool("isLabEnableSharedAlbum", value);
2021-04-10 06:28:12 +02:00
Pref._();
static final _inst = Pref._();
2021-07-23 22:05:57 +02:00
late SharedPreferences _pref;
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;
}
}
}