import 'dart:convert'; import 'package:nc_photos/account.dart'; import 'package:shared_preferences/shared_preferences.dart'; class Pref { static Future init() async { return SharedPreferences.getInstance().then((pref) { _inst._pref = pref; }); } factory Pref.inst() => _inst; List? getAccounts() { final jsonObjs = _pref.getStringList("accounts"); return jsonObjs?.map((e) => Account.fromJson(jsonDecode(e))).toList(); } List getAccountsOr(List def) => getAccounts() ?? def; Future setAccounts(List value) { final jsons = value.map((e) => jsonEncode(e.toJson())).toList(); return _pref.setStringList("accounts", jsons); } int? getCurrentAccountIndex() => _pref.getInt("currentAccountIndex"); int getCurrentAccountIndexOr(int def) => getCurrentAccountIndex() ?? def; Future setCurrentAccountIndex(int value) => _pref.setInt("currentAccountIndex", value); int? getHomePhotosZoomLevel() => _pref.getInt("homePhotosZoomLevel"); int getHomePhotosZoomLevelOr(int def) => getHomePhotosZoomLevel() ?? def; Future setHomePhotosZoomLevel(int value) => _pref.setInt("homePhotosZoomLevel", value); int? getAlbumBrowserZoomLevel() => _pref.getInt("albumViewerZoomLevel"); int getAlbumBrowserZoomLevelOr(int def) => getAlbumBrowserZoomLevel() ?? def; Future setAlbumBrowserZoomLevel(int value) => _pref.setInt("albumViewerZoomLevel", value); bool? isEnableExif() => _pref.getBool("isEnableExif"); bool isEnableExifOr([bool def = true]) => isEnableExif() ?? def; Future setEnableExif(bool value) => _pref.setBool("isEnableExif", value); int? getSetupProgress() => _pref.getInt("setupProgress"); int getSetupProgressOr([int def = 0]) => getSetupProgress() ?? def; Future setSetupProgress(int value) => _pref.setInt("setupProgress", value); /// Return the version number when the app last ran int? getLastVersion() => _pref.getInt("lastVersion"); int getLastVersionOr(int def) => getLastVersion() ?? def; Future setLastVersion(int value) => _pref.setInt("lastVersion", value); bool? isDarkTheme() => _pref.getBool("isDarkTheme"); bool isDarkThemeOr(bool def) => isDarkTheme() ?? def; Future setDarkTheme(bool value) => _pref.setBool("isDarkTheme", value); int? getLanguage() => _pref.getInt("language"); int getLanguageOr(int def) => getLanguage() ?? def; Future setLanguage(int value) => _pref.setInt("language", value); bool? hasNewSharedAlbum() => _pref.getBool("hasNewSharedAlbum"); bool hasNewSharedAlbumOr(bool def) => hasNewSharedAlbum() ?? def; Future setNewSharedAlbum(bool value) => _pref.setBool("hasNewSharedAlbum", value); bool? isLabEnableSharedAlbum() => _pref.getBool("isLabEnableSharedAlbum"); bool isLabEnableSharedAlbumOr(bool def) => isLabEnableSharedAlbum() ?? def; Future setLabEnableSharedAlbum(bool value) => _pref.setBool("isLabEnableSharedAlbum", value); Pref._(); static final _inst = Pref._(); late SharedPreferences _pref; } extension PrefExtension on Pref { Account? getCurrentAccount() { try { return Pref.inst().getAccounts()![Pref.inst().getCurrentAccountIndex()!]; } catch (_) { return null; } } }