From 72b09bc5a5a3e83858cea6d5cb30a9839df603b7 Mon Sep 17 00:00:00 2001 From: Aaron Madlon-Kay Date: Sun, 24 Dec 2023 17:54:19 +0900 Subject: [PATCH] Add DecryptPolicy settings See #33 --- lib/src/components/view_settings.dart | 15 +++++++++++++ lib/src/preferences.dart | 31 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lib/src/components/view_settings.dart b/lib/src/components/view_settings.dart index d91c654d..efdca8d0 100644 --- a/lib/src/components/view_settings.dart +++ b/lib/src/components/view_settings.dart @@ -104,6 +104,14 @@ class InheritedViewSettings extends InheritedWidget { _update(data.copyWith(saveChangesPolicy: value)); } + DecryptPolicy get decryptPolicy => data.decryptPolicy; + void setDecryptPolicy(DecryptPolicy value, {bool persist = false}) { + if (persist) { + _prefs.setDecryptPolicy(value); + } + _update(data.copyWith(decryptPolicy: value)); + } + bool get fullWidth => data.fullWidth; set fullWidth(bool value) => _update(data.copyWith(fullWidth: value)); @@ -124,6 +132,7 @@ class ViewSettingsData { prefs.remoteImagesPolicy ?? kDefaultRemoteImagesPolicy, localLinksPolicy: prefs.localLinksPolicy ?? kDefaultLocalLinksPolicy, saveChangesPolicy: prefs.saveChangesPolicy ?? kDefaultSaveChangesPolicy, + decryptPolicy: prefs.decryptPolicy ?? kDefaultDecryptPolicy, fullWidth: prefs.fullWidth ?? kDefaultFullWidth, ); } @@ -136,6 +145,7 @@ class ViewSettingsData { required this.remoteImagesPolicy, required this.localLinksPolicy, required this.saveChangesPolicy, + required this.decryptPolicy, required this.fullWidth, }); @@ -146,6 +156,7 @@ class ViewSettingsData { final RemoteImagesPolicy remoteImagesPolicy; final LocalLinksPolicy localLinksPolicy; final SaveChangesPolicy saveChangesPolicy; + final DecryptPolicy decryptPolicy; final bool fullWidth; ViewSettingsData copyWith({ @@ -156,6 +167,7 @@ class ViewSettingsData { RemoteImagesPolicy? remoteImagesPolicy, LocalLinksPolicy? localLinksPolicy, SaveChangesPolicy? saveChangesPolicy, + DecryptPolicy? decryptPolicy, bool? fullWidth, }) => ViewSettingsData( @@ -166,6 +178,7 @@ class ViewSettingsData { remoteImagesPolicy: remoteImagesPolicy ?? this.remoteImagesPolicy, localLinksPolicy: localLinksPolicy ?? this.localLinksPolicy, saveChangesPolicy: saveChangesPolicy ?? this.saveChangesPolicy, + decryptPolicy: decryptPolicy ?? this.decryptPolicy, fullWidth: fullWidth ?? this.fullWidth, ); @@ -179,6 +192,7 @@ class ViewSettingsData { remoteImagesPolicy == other.remoteImagesPolicy && localLinksPolicy == other.localLinksPolicy && saveChangesPolicy == other.saveChangesPolicy && + decryptPolicy == other.decryptPolicy && fullWidth == other.fullWidth; @override @@ -190,6 +204,7 @@ class ViewSettingsData { remoteImagesPolicy, localLinksPolicy, saveChangesPolicy, + decryptPolicy, fullWidth, ); } diff --git a/lib/src/preferences.dart b/lib/src/preferences.dart index 5e09cbbc..ce782312 100644 --- a/lib/src/preferences.dart +++ b/lib/src/preferences.dart @@ -10,6 +10,8 @@ enum LocalLinksPolicy { deny, ask } enum SaveChangesPolicy { allow, deny, ask } +enum DecryptPolicy { deny, ask } + const kDefaultFontFamily = 'Fira Code'; const kDefaultTextScale = 1.0; const String? kDefaultQueryString = null; @@ -17,6 +19,7 @@ const kDefaultReaderMode = false; const kDefaultRemoteImagesPolicy = RemoteImagesPolicy.ask; const kDefaultLocalLinksPolicy = LocalLinksPolicy.ask; const kDefaultSaveChangesPolicy = SaveChangesPolicy.ask; +const kDefaultDecryptPolicy = DecryptPolicy.ask; const kDefaultFullWidth = false; const kMaxRecentFiles = 10; @@ -27,6 +30,7 @@ const _kReaderModeKey = 'reader_mode'; const _kRemoteImagesPolicyKey = 'remote_images_policy'; const _kLocalLinksPolicyKey = 'local_links_policy'; const _kSaveChangesPolicyKey = 'save_changes_policy'; +const _kDecryptPolicyKey = 'decrypt_policy'; const _kRecentFilesJsonKey = 'recent_files_json'; const _kAccessibleDirectoriesKey = 'accessible_directories_json'; const _kFullWidthKey = 'full_width'; @@ -90,6 +94,12 @@ class Preferences extends InheritedWidget { Future setSaveChangesPolicy(SaveChangesPolicy? value) => _setOrRemove(_kSaveChangesPolicyKey, value.persistableString); + DecryptPolicy? get decryptPolicy => + DecryptPolicyPersistence.fromString(_prefs.getString(_kDecryptPolicyKey)); + + Future setDecryptPolicy(DecryptPolicy? value) => + _setOrRemove(_kDecryptPolicyKey, value.persistableString); + /// List of identifiers List get accessibleDirs => _prefs.getStringList(_kAccessibleDirectoriesKey) ?? []; @@ -228,3 +238,24 @@ extension SaveChangesPolicyPersistence on SaveChangesPolicy? { const _kSaveChangesPolicyAllow = 'save_changes_policy_allow'; const _kSaveChangesPolicyDeny = 'save_changes_policy_deny'; const _kSaveChangesPolicyAsk = 'save_changes_policy_ask'; + +extension DecryptPolicyPersistence on DecryptPolicy? { + static DecryptPolicy? fromString(String? value) { + switch (value) { + case _kDecryptPolicyDeny: + return DecryptPolicy.deny; + case _kDecryptPolicyAsk: + return DecryptPolicy.ask; + } + return null; + } + + String? get persistableString => switch (this) { + DecryptPolicy.deny => _kDecryptPolicyDeny, + DecryptPolicy.ask => _kDecryptPolicyAsk, + null => null, + }; +} + +const _kDecryptPolicyDeny = 'decrypt_policy_deny'; +const _kDecryptPolicyAsk = 'decrypt_policy_ask';