nc-photos/lib/pref.dart

384 lines
12 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-10-19 21:49:41 +02:00
import 'package:nc_photos/type.dart';
import 'package:nc_photos/use_case/compat/v32.dart';
2021-04-10 06:28:12 +02:00
import 'package:shared_preferences/shared_preferences.dart';
class Pref {
factory Pref() {
_inst ??= Pref.scoped(PrefMemoryProvider());
return _inst!;
}
2021-10-27 22:40:54 +02:00
Pref.scoped(this.provider);
2021-10-27 22:40:54 +02:00
/// Set the global [Pref] instance returned by the default constructor
static void setGlobalInstance(Pref pref) {
assert(_inst == null);
_inst = pref;
2021-04-10 06:28:12 +02:00
}
2021-10-19 21:49:41 +02:00
List<PrefAccount>? getAccounts2() {
final jsonObjs = provider.getStringList(PrefKey.accounts2);
2021-10-19 21:49:41 +02:00
return jsonObjs?.map((e) => PrefAccount.fromJson(jsonDecode(e))).toList();
2021-04-10 06:28:12 +02:00
}
2021-10-19 21:49:41 +02:00
List<PrefAccount> getAccounts2Or(List<PrefAccount> def) =>
getAccounts2() ?? def;
Future<bool> setAccounts2(List<PrefAccount> value) {
2021-04-10 06:28:12 +02:00
final jsons = value.map((e) => jsonEncode(e.toJson())).toList();
return provider.setStringList(PrefKey.accounts2, jsons);
2021-04-10 06:28:12 +02:00
}
int? getCurrentAccountIndex() => provider.getInt(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) =>
provider.setInt(PrefKey.currentAccountIndex, value);
2021-04-10 06:28:12 +02:00
int? getHomePhotosZoomLevel() => provider.getInt(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) =>
provider.setInt(PrefKey.homePhotosZoomLevel, value);
2021-04-10 06:28:12 +02:00
2021-09-11 14:03:40 +02:00
int? getAlbumBrowserZoomLevel() =>
provider.getInt(PrefKey.albumBrowserZoomLevel);
int getAlbumBrowserZoomLevelOr(int def) => getAlbumBrowserZoomLevel() ?? def;
Future<bool> setAlbumBrowserZoomLevel(int value) =>
provider.setInt(PrefKey.albumBrowserZoomLevel, value);
2021-04-10 06:28:12 +02:00
int? getHomeAlbumsSort() => provider.getInt(PrefKey.homeAlbumsSort);
2021-09-08 18:23:42 +02:00
int getHomeAlbumsSortOr(int def) => getHomeAlbumsSort() ?? def;
Future<bool> setHomeAlbumsSort(int value) =>
provider.setInt(PrefKey.homeAlbumsSort, value);
2021-09-08 18:23:42 +02:00
bool? isEnableExif() => provider.getBool(PrefKey.enableExif);
2021-07-23 22:05:57 +02:00
bool isEnableExifOr([bool def = true]) => isEnableExif() ?? def;
Future<bool> setEnableExif(bool value) =>
provider.setBool(PrefKey.enableExif, value);
2021-04-10 06:28:12 +02:00
2021-09-11 14:03:40 +02:00
int? getViewerScreenBrightness() =>
provider.getInt(PrefKey.viewerScreenBrightness);
int getViewerScreenBrightnessOr([int def = -1]) =>
getViewerScreenBrightness() ?? def;
Future<bool> setViewerScreenBrightness(int value) =>
provider.setInt(PrefKey.viewerScreenBrightness, value);
2021-09-11 14:03:40 +02:00
bool? isViewerForceRotation() =>
provider.getBool(PrefKey.viewerForceRotation);
bool isViewerForceRotationOr([bool def = false]) =>
isViewerForceRotation() ?? def;
Future<bool> setViewerForceRotation(bool value) =>
provider.setBool(PrefKey.viewerForceRotation, value);
int? getSetupProgress() => provider.getInt(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) =>
provider.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
int? getLastVersion() => provider.getInt(PrefKey.lastVersion);
2021-07-23 22:05:57 +02:00
int getLastVersionOr(int def) => getLastVersion() ?? def;
Future<bool> setLastVersion(int value) =>
provider.setInt(PrefKey.lastVersion, value);
2021-04-16 10:45:17 +02:00
bool? isDarkTheme() => provider.getBool(PrefKey.darkTheme);
2021-07-23 22:05:57 +02:00
bool isDarkThemeOr(bool def) => isDarkTheme() ?? def;
Future<bool> setDarkTheme(bool value) =>
provider.setBool(PrefKey.darkTheme, value);
2021-04-17 11:04:46 +02:00
bool? isFollowSystemTheme() => provider.getBool(PrefKey.followSystemTheme);
bool isFollowSystemThemeOr(bool def) => isFollowSystemTheme() ?? def;
Future<bool> setFollowSystemTheme(bool value) =>
provider.setBool(PrefKey.followSystemTheme, value);
2021-09-11 14:03:40 +02:00
bool? isUseBlackInDarkTheme() =>
provider.getBool(PrefKey.useBlackInDarkTheme);
bool isUseBlackInDarkThemeOr(bool def) => isUseBlackInDarkTheme() ?? def;
Future<bool> setUseBlackInDarkTheme(bool value) =>
provider.setBool(PrefKey.useBlackInDarkTheme, value);
int? getLanguage() => provider.getInt(PrefKey.language);
2021-07-23 22:05:57 +02:00
int getLanguageOr(int def) => getLanguage() ?? def;
Future<bool> setLanguage(int value) =>
provider.setInt(PrefKey.language, value);
2021-06-23 10:15:25 +02:00
int? getSlideshowDuration() => provider.getInt(PrefKey.slideshowDuration);
2021-09-14 23:00:24 +02:00
int getSlideshowDurationOr(int def) => getSlideshowDuration() ?? def;
Future<bool> setSlideshowDuration(int value) =>
provider.setInt(PrefKey.slideshowDuration, value);
2021-09-14 23:00:24 +02:00
bool? isSlideshowShuffle() => provider.getBool(PrefKey.isSlideshowShuffle);
2021-09-14 23:00:24 +02:00
bool isSlideshowShuffleOr(bool def) => isSlideshowShuffle() ?? def;
Future<bool> setSlideshowShuffle(bool value) =>
provider.setBool(PrefKey.isSlideshowShuffle, value);
2021-09-14 23:00:24 +02:00
bool? isSlideshowRepeat() => provider.getBool(PrefKey.isSlideshowRepeat);
2021-09-14 23:00:24 +02:00
bool isSlideshowRepeatOr(bool def) => isSlideshowRepeat() ?? def;
Future<bool> setSlideshowRepeat(bool value) =>
provider.setBool(PrefKey.isSlideshowRepeat, value);
2021-09-14 23:00:24 +02:00
2021-10-04 15:53:03 +02:00
bool? isAlbumBrowserShowDate() =>
provider.getBool(PrefKey.isAlbumBrowserShowDate);
2021-10-04 15:53:03 +02:00
bool isAlbumBrowserShowDateOr([bool def = false]) =>
isAlbumBrowserShowDate() ?? def;
Future<bool> setAlbumBrowserShowDate(bool value) =>
provider.setBool(PrefKey.isAlbumBrowserShowDate, value);
2021-10-04 15:53:03 +02:00
bool? hasNewSharedAlbum() => provider.getBool(PrefKey.newSharedAlbum);
2021-08-08 14:09:21 +02:00
bool hasNewSharedAlbumOr(bool def) => hasNewSharedAlbum() ?? def;
Future<bool> setNewSharedAlbum(bool value) =>
provider.setBool(PrefKey.newSharedAlbum, value);
2021-08-08 14:09:21 +02:00
2021-11-17 15:41:11 +01:00
int? getGpsMapProvider() => provider.getInt(PrefKey.gpsMapProvider);
int getGpsMapProviderOr(int def) => getGpsMapProvider() ?? def;
Future<bool> setGpsMapProvider(int value) =>
provider.setInt(PrefKey.gpsMapProvider, value);
2021-09-11 14:03:40 +02:00
bool? isLabEnableSharedAlbum() =>
provider.getBool(PrefKey.labEnableSharedAlbum);
2021-08-08 20:46:16 +02:00
bool isLabEnableSharedAlbumOr(bool def) => isLabEnableSharedAlbum() ?? def;
Future<bool> setLabEnableSharedAlbum(bool value) =>
provider.setBool(PrefKey.labEnableSharedAlbum, value);
2021-08-08 20:46:16 +02:00
final PrefProvider provider;
2021-09-11 14:03:40 +02:00
static Pref? _inst;
}
2021-09-11 14:03:40 +02:00
/// Provide the data for [Pref]
abstract class PrefProvider {
bool? getBool(PrefKey key);
Future<bool> setBool(PrefKey key, bool value);
int? getInt(PrefKey key);
Future<bool> setInt(PrefKey key, int value);
List<String>? getStringList(PrefKey key);
Future<bool> setStringList(PrefKey key, List<String> value);
2021-09-11 14:03:40 +02:00
bool _onPostSet(bool result, PrefKey key, dynamic value) {
if (result) {
KiwiContainer().resolve<EventBus>().fire(PrefUpdatedEvent(key, value));
return true;
} else {
return false;
}
}
}
2021-09-11 14:03:40 +02:00
/// [Pref] stored with [SharedPreferences] lib
class PrefSharedPreferencesProvider extends PrefProvider {
Future<void> init() async {
if (await CompatV32.isPrefNeedMigration()) {
await CompatV32.migratePref();
2021-09-11 14:03:40 +02:00
}
return SharedPreferences.getInstance().then((pref) {
_pref = pref;
});
}
@override
getBool(PrefKey key) => _pref.getBool(key.toStringKey());
@override
setBool(PrefKey key, bool value) async {
return _onPostSet(
await _pref.setBool(key.toStringKey(), value), key, value);
}
@override
getInt(PrefKey key) => _pref.getInt(key.toStringKey());
@override
setInt(PrefKey key, int value) async {
return _onPostSet(await _pref.setInt(key.toStringKey(), value), key, value);
}
@override
getStringList(PrefKey key) => _pref.getStringList(key.toStringKey());
@override
setStringList(PrefKey key, List<String> value) async {
return _onPostSet(
await _pref.setStringList(key.toStringKey(), value), key, value);
2021-09-11 14:03:40 +02:00
}
2021-07-23 22:05:57 +02:00
late SharedPreferences _pref;
2021-04-10 06:28:12 +02:00
}
/// [Pref] stored in memory
class PrefMemoryProvider extends PrefProvider {
PrefMemoryProvider([
Map<String, dynamic> initialData = const <String, dynamic>{},
]) : _data = Map.of(initialData);
@override
getBool(PrefKey key) => _data[key.toStringKey()];
@override
setBool(PrefKey key, bool value) async {
return _onPostSet(() {
_data[key.toStringKey()] = value;
return true;
}(), key, value);
}
@override
getInt(PrefKey key) => _data[key.toStringKey()];
@override
setInt(PrefKey key, int value) async {
return _onPostSet(() {
_data[key.toStringKey()] = value;
return true;
}(), key, value);
}
@override
getStringList(PrefKey key) => _data[key.toStringKey()];
@override
setStringList(PrefKey key, List<String> value) async {
return _onPostSet(() {
_data[key.toStringKey()] = value;
return true;
}(), key, value);
}
final Map<String, dynamic> _data;
}
2021-10-19 21:49:41 +02:00
class PrefAccount {
const PrefAccount(
this.account, [
this.settings = const AccountSettings(),
]);
factory PrefAccount.fromJson(JsonObj json) {
return PrefAccount(
Account.fromJson(json["account"].cast<String, dynamic>()),
AccountSettings.fromJson(json["settings"].cast<String, dynamic>()),
);
}
JsonObj toJson() => {
"account": account.toJson(),
"settings": settings.toJson(),
};
PrefAccount copyWith({
Account? account,
AccountSettings? settings,
}) {
return PrefAccount(
account ?? this.account,
settings ?? this.settings,
);
}
@override
toString() {
return "$runtimeType {"
"account: $account, "
"settings: $settings, "
"}";
}
final Account account;
final AccountSettings settings;
}
2021-09-11 14:03:40 +02:00
enum PrefKey {
2021-10-19 21:49:41 +02:00
accounts2,
2021-09-11 14:03:40 +02:00
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-10-04 15:53:03 +02:00
isAlbumBrowserShowDate,
2021-11-17 15:41:11 +01:00
gpsMapProvider,
2021-09-11 14:03:40 +02:00
}
extension on PrefKey {
String toStringKey() {
switch (this) {
case PrefKey.accounts2:
return "accounts2";
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";
case PrefKey.slideshowDuration:
return "slideshowDuration";
case PrefKey.isSlideshowShuffle:
return "isSlideshowShuffle";
case PrefKey.isSlideshowRepeat:
return "isSlideshowRepeat";
case PrefKey.isAlbumBrowserShowDate:
return "isAlbumBrowserShowDate";
2021-11-17 15:41:11 +01:00
case PrefKey.gpsMapProvider:
return "gpsMapProvider";
}
}
}
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-10-27 22:40:54 +02:00
return Pref().getAccounts2()![Pref().getCurrentAccountIndex()!].account;
2021-04-10 06:28:12 +02:00
} catch (_) {
return null;
}
}
AccountSettings getAccountSettings(Account account) {
2021-10-27 22:40:54 +02:00
return Pref()
.getAccounts2()!
.firstWhere((element) => element.account == account)
.settings;
}
2021-04-10 06:28:12 +02:00
}