From 95f6bceebcdba865b7f1007580734e1982259fa7 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 26 Jul 2021 15:35:23 +0200 Subject: [PATCH] Split Settings into MarkdownRendererSettings We don't need one huge monolight of all the settings. --- lib/app.dart | 37 +- lib/settings/settings.dart | 329 +---------------- lib/settings/settings_display_images.dart | 4 +- .../settings_display_images_caption.dart | 3 +- .../settings_display_images_theming.dart | 3 +- lib/settings/settings_markdown_renderer.dart | 334 ++++++++++++++++++ lib/widgets/images/image_caption.dart | 10 +- lib/widgets/images/image_details.dart | 4 +- lib/widgets/images/markdown_image.dart | 6 +- 9 files changed, 393 insertions(+), 337 deletions(-) create mode 100644 lib/settings/settings_markdown_renderer.dart diff --git a/lib/app.dart b/lib/app.dart index 0ed3b900..8fecfb5b 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -24,6 +24,7 @@ import 'package:gitjournal/repository.dart'; import 'package:gitjournal/repository_manager.dart'; import 'package:gitjournal/settings/app_settings.dart'; import 'package:gitjournal/settings/settings.dart'; +import 'package:gitjournal/settings/settings_markdown_renderer.dart'; import 'package:gitjournal/themes.dart'; import 'package:gitjournal/utils/logger.dart'; @@ -63,6 +64,7 @@ class JournalApp extends StatefulWidget { child: GitJournalChangeNotifiers( repoManager: repoManager, appSettings: appSettings, + pref: pref, child: JournalApp(), ), supportedLocales: [ @@ -350,11 +352,13 @@ class _JournalAppState extends State { class GitJournalChangeNotifiers extends StatelessWidget { final RepositoryManager repoManager; final AppSettings appSettings; + final SharedPreferences pref; final Widget child; GitJournalChangeNotifiers({ required this.repoManager, required this.appSettings, + required this.pref, required this.child, Key? key, }) : super(key: key); @@ -364,16 +368,18 @@ class GitJournalChangeNotifiers extends StatelessWidget { var app = ChangeNotifierProvider.value( value: repoManager, child: Consumer( - builder: (_, repoManager, __) => ChangeNotifierProvider.value( - value: repoManager.currentRepo, - child: Consumer( - builder: (_, repo, __) => ChangeNotifierProvider.value( - value: repo.settings, - child: Consumer( - builder: (_, repo, __) => - ChangeNotifierProvider.value( - value: repo.notesFolder, - child: child, + builder: (_, repoManager, __) => buildMarkdownSettings( + child: ChangeNotifierProvider.value( + value: repoManager.currentRepo, + child: Consumer( + builder: (_, repo, __) => ChangeNotifierProvider.value( + value: repo.settings, + child: Consumer( + builder: (_, repo, __) => + ChangeNotifierProvider.value( + value: repo.notesFolder, + child: child, + ), ), ), ), @@ -387,4 +393,15 @@ class GitJournalChangeNotifiers extends StatelessWidget { child: app, ); } + + Widget buildMarkdownSettings({required Widget child}) { + return Consumer( + builder: (_, repoManager, __) { + var markdown = MarkdownRendererSettings(repoManager.currentId); + markdown.load(pref); + + return ChangeNotifierProvider.value(value: markdown, child: child); + }, + ); + } } diff --git a/lib/settings/settings.dart b/lib/settings/settings.dart index 850ad879..726e5752 100644 --- a/lib/settings/settings.dart +++ b/lib/settings/settings.dart @@ -42,9 +42,13 @@ class Settings extends ChangeNotifier with SettingsSharedPref { String folderName = "journal"; - // Properties + // Git Settings String gitAuthor = "GitJournal"; String gitAuthorEmail = "app@gitjournal.io"; + String sshPublicKey = ""; + String sshPrivateKey = ""; + String sshPassword = ""; + NoteFileNameFormat noteFileNameFormat = NoteFileNameFormat.Default; NoteFileNameFormat journalNoteFileNameFormat = NoteFileNameFormat.Default; @@ -71,32 +75,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref { SettingsHomeScreen homeScreen = SettingsHomeScreen.Default; SettingsTheme theme = SettingsTheme.Default; - // Display - Image - bool rotateImageGestures = false; - double maxImageZoom = 10; - - // Display - Image - Theming - bool themeRasterGraphics = false; - SettingsImageTextType themeOverrideTagLocation = - SettingsImageTextType.Default; - Set doNotThemeTags = {"notheme", "!nt"}; - Set doThemeTags = {"dotheme", "!dt"}; - SettingsThemeVectorGraphics themeVectorGraphics = - SettingsThemeVectorGraphics.Default; - bool themeSvgWithBackground = false; - bool matchCanvasColor = true; - SettingsVectorGraphicsAdjustColors vectorGraphicsAdjustColors = - SettingsVectorGraphicsAdjustColors.Default; - - // Display - Image - Caption - bool overlayCaption = true; - bool transparentCaption = true; - bool blurBehindCaption = true; - bool tooltipFirst = false; - SettingsImageTextType useAsCaption = SettingsImageTextType.Default; - Set doNotCaptionTags = {"nocaption", "!nc"}; - Set doCaptionTags = {"docaption", "!dc"}; - SettingsMarkdownDefaultView markdownDefaultView = SettingsMarkdownDefaultView.Default; SettingsMarkdownDefaultView markdownLastUsedView = @@ -118,13 +96,12 @@ class Settings extends ChangeNotifier with SettingsSharedPref { bool storeInternally = true; String storageLocation = ""; - String sshPublicKey = ""; - String sshPrivateKey = ""; - String sshPassword = ""; - void load(SharedPreferences pref) { gitAuthor = getString(pref, "gitAuthor") ?? gitAuthor; gitAuthorEmail = getString(pref, "gitAuthorEmail") ?? gitAuthorEmail; + sshPublicKey = getString(pref, "sshPublicKey") ?? sshPublicKey; + sshPrivateKey = getString(pref, "sshPrivateKey") ?? sshPrivateKey; + sshPassword = getString(pref, "sshPassword") ?? sshPassword; noteFileNameFormat = NoteFileNameFormat.fromInternalString( getString(pref, "noteFileNameFormat")); @@ -176,39 +153,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref { SettingsHomeScreen.fromInternalString(getString(pref, "homeScreen")); theme = SettingsTheme.fromInternalString(getString(pref, "theme")); - // Display - Image - rotateImageGestures = - getBool(pref, "rotateImageGestures") ?? rotateImageGestures; - maxImageZoom = getDouble(pref, "maxImageZoom") ?? maxImageZoom; - - // Display - Image - Theming - themeRasterGraphics = - getBool(pref, "themeRasterGraphics") ?? themeRasterGraphics; - themeOverrideTagLocation = SettingsImageTextType.fromInternalString( - getString(pref, "themeOverrideTagLocation")); - doNotThemeTags = getStringSet(pref, "doNotThemeTags") ?? doNotThemeTags; - doThemeTags = getStringSet(pref, "doThemeTags") ?? doThemeTags; - themeVectorGraphics = SettingsThemeVectorGraphics.fromInternalString( - getString(pref, "themeVectorGraphics")); - themeSvgWithBackground = - getBool(pref, "themeSvgWithBackground") ?? themeSvgWithBackground; - matchCanvasColor = getBool(pref, "matchCanvasColor") ?? matchCanvasColor; - vectorGraphicsAdjustColors = - SettingsVectorGraphicsAdjustColors.fromInternalString( - getString(pref, "vectorGraphicsAdjustColors")); - - // Display - Image - Caption - overlayCaption = getBool(pref, "overlayCaption") ?? overlayCaption; - transparentCaption = - getBool(pref, "transparentCaption") ?? transparentCaption; - blurBehindCaption = getBool(pref, "blurBehindCaption") ?? blurBehindCaption; - tooltipFirst = getBool(pref, "tooltipFirst") ?? tooltipFirst; - useAsCaption = SettingsImageTextType.fromInternalString( - getString(pref, "useAsCaption")); - doNotCaptionTags = - getStringSet(pref, "doNotCaptionTag") ?? doNotCaptionTags; - doCaptionTags = getStringSet(pref, "doCaptionTag") ?? doCaptionTags; - imageLocationSpec = getString(pref, "imageLocationSpec") ?? imageLocationSpec; @@ -223,10 +167,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref { // From AppState folderName = getString(pref, FOLDER_NAME_KEY) ?? folderName; - sshPublicKey = getString(pref, "sshPublicKey") ?? sshPublicKey; - sshPrivateKey = getString(pref, "sshPrivateKey") ?? sshPrivateKey; - sshPassword = getString(pref, "sshPassword") ?? sshPassword; - bottomMenuBar = getBool(pref, "bottomMenuBar") ?? bottomMenuBar; confirmDelete = getBool(pref, "confirmDelete") ?? confirmDelete; storeInternally = getBool(pref, "storeInternally") ?? storeInternally; @@ -242,6 +182,12 @@ class Settings extends ChangeNotifier with SettingsSharedPref { await setString(pref, "gitAuthor", gitAuthor, defaultSet.gitAuthor); await setString( pref, "gitAuthorEmail", gitAuthorEmail, defaultSet.gitAuthorEmail); + await setString( + pref, "sshPublicKey", sshPublicKey, defaultSet.sshPublicKey); + await setString( + pref, "sshPrivateKey", sshPrivateKey, defaultSet.sshPrivateKey); + await setString(pref, "sshPassword", sshPassword, defaultSet.sshPassword); + await setString( pref, "noteFileNameFormat", @@ -303,54 +249,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref { await setString(pref, "theme", theme.toInternalString(), defaultSet.theme.toInternalString()); - // Display - Image - await setBool(pref, "rotateImageGestures", rotateImageGestures, - defaultSet.rotateImageGestures); - await setDouble( - pref, "maxImageZoom", maxImageZoom, defaultSet.maxImageZoom); - - // Display - Image - Theme - await setBool(pref, "themeRasterGraphics", themeRasterGraphics, - defaultSet.themeRasterGraphics); - await setString( - pref, - "themeOverrideTagLocation", - themeOverrideTagLocation.toInternalString(), - defaultSet.themeOverrideTagLocation.toInternalString()); - await setStringSet( - pref, "doNotThemeTags", doNotThemeTags, defaultSet.doNotThemeTags); - await setStringSet( - pref, "doThemeTags", doThemeTags, defaultSet.doThemeTags); - await setString( - pref, - "themeVectorGraphics", - themeVectorGraphics.toInternalString(), - defaultSet.themeVectorGraphics.toInternalString()); - await setBool(pref, "themeSvgWithBackground", themeSvgWithBackground, - defaultSet.themeSvgWithBackground); - await setBool(pref, "matchCanvasColor", matchCanvasColor, - defaultSet.matchCanvasColor); - await setString( - pref, - "vectorGraphicsAdjustColors", - vectorGraphicsAdjustColors.toInternalString(), - defaultSet.vectorGraphicsAdjustColors.toInternalString()); - - // Display - Image - Caption - await setBool( - pref, "overlayCaption", overlayCaption, defaultSet.overlayCaption); - await setBool(pref, "transparentCaption", transparentCaption, - defaultSet.transparentCaption); - await setBool(pref, "blurBehindCaption", blurBehindCaption, - defaultSet.blurBehindCaption); - await setBool(pref, "tooltipFirst", tooltipFirst, defaultSet.tooltipFirst); - await setString(pref, "useAsCaption", useAsCaption.toInternalString(), - defaultSet.useAsCaption.toInternalString()); - await setStringSet( - pref, "doNotCaptionTag", doNotCaptionTags, defaultSet.doNotCaptionTags); - await setStringSet( - pref, "doCaptionTag", doCaptionTags, defaultSet.doCaptionTags); - await setString(pref, "imageLocationSpec", imageLocationSpec, defaultSet.imageLocationSpec); await setBool(pref, "zenMode", zenMode, defaultSet.zenMode); @@ -369,12 +267,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref { await setString( pref, "storageLocation", storageLocation, defaultSet.storageLocation); - await setString( - pref, "sshPublicKey", sshPublicKey, defaultSet.sshPublicKey); - await setString( - pref, "sshPrivateKey", sshPrivateKey, defaultSet.sshPrivateKey); - await setString(pref, "sshPassword", sshPassword, defaultSet.sshPassword); - await setInt(pref, "settingsVersion", version, defaultSet.version); await setString(pref, FOLDER_NAME_KEY, folderName, defaultSet.folderName); @@ -388,6 +280,8 @@ class Settings extends ChangeNotifier with SettingsSharedPref { return { "gitAuthor": gitAuthor.isNotEmpty.toString(), "gitAuthorEmail": gitAuthorEmail.isNotEmpty.toString(), + 'sshPublicKey': sshPublicKey.isNotEmpty.toString(), + 'sshPrivateKey': sshPrivateKey.isNotEmpty.toString(), "noteFileNameFormat": noteFileNameFormat.toInternalString(), "journalNoteFileNameFormat": journalNoteFileNameFormat.toInternalString(), "yamlModifiedKey": yamlModifiedKey, @@ -412,28 +306,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref { 'markdownLastUsedView': markdownLastUsedView.toInternalString(), 'homeScreen': homeScreen.toInternalString(), 'theme': theme.toInternalString(), - // Display - Image - 'rotateImageGestures': rotateImageGestures.toString(), - 'maxImageZoom': maxImageZoom.toString(), - // Display - Image - Theming - 'themeRasterGraphics': themeRasterGraphics.toString(), - 'themeOverrideTagLocation': themeOverrideTagLocation.toInternalString(), - 'doNotThemeTags': csvTags(doNotThemeTags), - 'doThemeTags': csvTags(doThemeTags), - 'themeVectorGraphics': themeVectorGraphics.toInternalString(), - 'themeSvgWithBackground': themeSvgWithBackground.toString(), - 'matchCanvasColor': matchCanvasColor.toString(), - 'vectorGraphicsAdjustColors': - vectorGraphicsAdjustColors.toInternalString(), - // Display - Image - Caption - 'overlayCaption': overlayCaption.toString(), - 'transparentCaption': transparentCaption.toString(), - 'blurBehindCaption': blurBehindCaption.toString(), - 'tooltipFirst': tooltipFirst.toString(), - 'useAsCaption': useAsCaption.toInternalString(), - 'doNotCaptionTag': csvTags(doNotCaptionTags), - 'doCaptionTag': csvTags(doCaptionTags), - // 'imageLocationSpec': imageLocationSpec, 'zenMode': zenMode.toString(), 'titleSettings': titleSettings.toInternalString(), @@ -445,8 +317,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref { 'confirmDelete': confirmDelete.toString(), 'storeInternally': storeInternally.toString(), 'storageLocation': storageLocation, - 'sshPublicKey': sshPublicKey.isNotEmpty.toString(), - 'sshPrivateKey': sshPrivateKey.isNotEmpty.toString(), }; } @@ -863,169 +733,6 @@ class SettingsHomeScreen { } } -class SettingsImageTextType { - static const AltTool = SettingsImageTextType( - "settings.display.images.imageTextType.altAndTooltip", "alt_and_tooltip"); - static const Tooltip = SettingsImageTextType( - "settings.display.images.imageTextType.tooltip", "tooltip"); - static const Alt = - SettingsImageTextType("settings.display.images.imageTextType.alt", "alt"); - static const None = SettingsImageTextType( - "settings.display.images.imageTextType.none", "none"); - static const Default = AltTool; - - final String _str; - final String _publicString; - const SettingsImageTextType(this._publicString, this._str); - - String toInternalString() { - return _str; - } - - String toPublicString() { - return tr(_publicString); - } - - static const options = [ - AltTool, - Tooltip, - Alt, - None, - ]; - - static SettingsImageTextType fromInternalString(String? str) { - for (var opt in options) { - if (opt.toInternalString() == str) { - return opt; - } - } - return Default; - } - - static SettingsImageTextType fromPublicString(String str) { - for (var opt in options) { - if (opt.toPublicString() == str) { - return opt; - } - } - return Default; - } - - @override - String toString() { - assert(false, - "SettingsThemeOverrideTagLocation toString should never be called"); - return ""; - } -} - -class SettingsThemeVectorGraphics { - static const On = SettingsThemeVectorGraphics( - "settings.display.images.theming.themeVectorGraphics.on", "on"); - static const Off = SettingsThemeVectorGraphics( - "settings.display.images.theming.themeVectorGraphics.off", "off"); - static const Filter = SettingsThemeVectorGraphics( - "settings.display.images.theming.themeVectorGraphics.filter", "filter"); - static const Default = On; - - final String _str; - final String _publicString; - const SettingsThemeVectorGraphics(this._publicString, this._str); - - String toInternalString() { - return _str; - } - - String toPublicString() { - return tr(_publicString); - } - - static const options = [ - On, - Off, - Filter, - ]; - - static SettingsThemeVectorGraphics fromInternalString(String? str) { - for (var opt in options) { - if (opt.toInternalString() == str) { - return opt; - } - } - return Default; - } - - static SettingsThemeVectorGraphics fromPublicString(String str) { - for (var opt in options) { - if (opt.toPublicString() == str) { - return opt; - } - } - return Default; - } - - @override - String toString() { - assert( - false, "SettingsThemeVectorGraphics toString should never be called"); - return ""; - } -} - -class SettingsVectorGraphicsAdjustColors { - static const All = SettingsVectorGraphicsAdjustColors( - "settings.display.images.theming.adjustColors.all", "all"); - static const BnW = SettingsVectorGraphicsAdjustColors( - "settings.display.images.theming.adjustColors.blackAndWhite", - "black_and_white"); - static const Grays = SettingsVectorGraphicsAdjustColors( - "settings.display.images.theming.adjustColors.grays", "grays"); - static const Default = All; - - final String _str; - final String _publicString; - const SettingsVectorGraphicsAdjustColors(this._publicString, this._str); - - String toInternalString() { - return _str; - } - - String toPublicString() { - return tr(_publicString); - } - - static const options = [ - BnW, - Grays, - All, - ]; - - static SettingsVectorGraphicsAdjustColors fromInternalString(String? str) { - for (var opt in options) { - if (opt.toInternalString() == str) { - return opt; - } - } - return Default; - } - - static SettingsVectorGraphicsAdjustColors fromPublicString(String str) { - for (var opt in options) { - if (opt.toPublicString() == str) { - return opt; - } - } - return Default; - } - - @override - String toString() { - assert(false, - "SettingsVectorGraphicsAdjustColors toString should never be called"); - return ""; - } -} - String generateRandomId() { return const Uuid().v4().substring(0, 8); } @@ -1151,7 +858,3 @@ Set parseTags(String tags) { .where((e) => e.isNotEmpty) .toSet(); } - -String csvTags(Set tags) { - return tags.join(", "); -} diff --git a/lib/settings/settings_display_images.dart b/lib/settings/settings_display_images.dart index 10aee513..5e65483b 100644 --- a/lib/settings/settings_display_images.dart +++ b/lib/settings/settings_display_images.dart @@ -21,9 +21,9 @@ import 'package:flutter/material.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:provider/provider.dart'; -import 'package:gitjournal/settings/settings.dart'; import 'package:gitjournal/settings/settings_display_images_caption.dart'; import 'package:gitjournal/settings/settings_display_images_theming.dart'; +import 'package:gitjournal/settings/settings_markdown_renderer.dart'; import 'package:gitjournal/settings/settings_screen.dart'; class SettingsDisplayImagesScreen extends StatefulWidget { @@ -36,7 +36,7 @@ class SettingsDisplayImagesScreenState extends State { @override Widget build(BuildContext context) { - final settings = Provider.of(context); + final settings = Provider.of(context); final theme = Theme.of(context); final body = ListView(children: [ diff --git a/lib/settings/settings_display_images_caption.dart b/lib/settings/settings_display_images_caption.dart index 635a386c..7c2a6634 100644 --- a/lib/settings/settings_display_images_caption.dart +++ b/lib/settings/settings_display_images_caption.dart @@ -20,6 +20,7 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:provider/provider.dart'; import 'package:gitjournal/settings/settings.dart'; +import 'package:gitjournal/settings/settings_markdown_renderer.dart'; import 'package:gitjournal/settings/settings_screen.dart'; import 'package:gitjournal/settings/settings_widgets.dart'; @@ -35,7 +36,7 @@ class SettingsDisplayImagesCaptionScreenState final doCaptionTagsKey = GlobalKey>(); @override Widget build(BuildContext context) { - var settings = Provider.of(context); + var settings = Provider.of(context); var saveDoNotCaptionTag = (String? doNotCaptionTags) { if (doNotCaptionTags == null) { return; diff --git a/lib/settings/settings_display_images_theming.dart b/lib/settings/settings_display_images_theming.dart index 8b633d2a..a09e9020 100644 --- a/lib/settings/settings_display_images_theming.dart +++ b/lib/settings/settings_display_images_theming.dart @@ -20,6 +20,7 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:provider/provider.dart'; import 'package:gitjournal/settings/settings.dart'; +import 'package:gitjournal/settings/settings_markdown_renderer.dart'; import 'package:gitjournal/settings/settings_screen.dart'; import 'package:gitjournal/settings/settings_widgets.dart'; @@ -36,7 +37,7 @@ class SettingsDisplayImagesThemingScreenState @override Widget build(BuildContext context) { - var settings = Provider.of(context); + var settings = Provider.of(context); var saveDoNotThemeTag = (String? doNotThemeTags) { settings.doNotThemeTags = parseTags(doNotThemeTags!); diff --git a/lib/settings/settings_markdown_renderer.dart b/lib/settings/settings_markdown_renderer.dart new file mode 100644 index 00000000..3f9c38a5 --- /dev/null +++ b/lib/settings/settings_markdown_renderer.dart @@ -0,0 +1,334 @@ +/* +Copyright 2020-2021 Vishesh Handa + Roland Fredenhagen + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import 'package:flutter/foundation.dart'; + +import 'package:easy_localization/easy_localization.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'package:gitjournal/settings/settings_sharedpref.dart'; + +class MarkdownRendererSettings extends ChangeNotifier with SettingsSharedPref { + MarkdownRendererSettings(this.id); + + @override + final String id; + + // Display - Image + bool rotateImageGestures = false; + double maxImageZoom = 10; + + // Display - Image - Theming + var themeRasterGraphics = false; + var themeOverrideTagLocation = SettingsImageTextType.Default; + var doNotThemeTags = {"notheme", "!nt"}; + var doThemeTags = {"dotheme", "!dt"}; + var themeVectorGraphics = SettingsThemeVectorGraphics.Default; + var themeSvgWithBackground = false; + var matchCanvasColor = true; + var vectorGraphicsAdjustColors = SettingsVectorGraphicsAdjustColors.Default; + + // Display - Image - Caption + var overlayCaption = true; + var transparentCaption = true; + var blurBehindCaption = true; + var tooltipFirst = false; + var useAsCaption = SettingsImageTextType.Default; + var doNotCaptionTags = {"nocaption", "!nc"}; + var doCaptionTags = {"docaption", "!dc"}; + + void load(SharedPreferences pref) { + // Display - Image + rotateImageGestures = + getBool(pref, "rotateImageGestures") ?? rotateImageGestures; + maxImageZoom = getDouble(pref, "maxImageZoom") ?? maxImageZoom; + + // Display - Image - Theming + themeRasterGraphics = + getBool(pref, "themeRasterGraphics") ?? themeRasterGraphics; + themeOverrideTagLocation = SettingsImageTextType.fromInternalString( + getString(pref, "themeOverrideTagLocation")); + doNotThemeTags = getStringSet(pref, "doNotThemeTags") ?? doNotThemeTags; + doThemeTags = getStringSet(pref, "doThemeTags") ?? doThemeTags; + themeVectorGraphics = SettingsThemeVectorGraphics.fromInternalString( + getString(pref, "themeVectorGraphics")); + themeSvgWithBackground = + getBool(pref, "themeSvgWithBackground") ?? themeSvgWithBackground; + matchCanvasColor = getBool(pref, "matchCanvasColor") ?? matchCanvasColor; + vectorGraphicsAdjustColors = + SettingsVectorGraphicsAdjustColors.fromInternalString( + getString(pref, "vectorGraphicsAdjustColors")); + + // Display - Image - Caption + overlayCaption = getBool(pref, "overlayCaption") ?? overlayCaption; + transparentCaption = + getBool(pref, "transparentCaption") ?? transparentCaption; + blurBehindCaption = getBool(pref, "blurBehindCaption") ?? blurBehindCaption; + tooltipFirst = getBool(pref, "tooltipFirst") ?? tooltipFirst; + useAsCaption = SettingsImageTextType.fromInternalString( + getString(pref, "useAsCaption")); + doNotCaptionTags = + getStringSet(pref, "doNotCaptionTag") ?? doNotCaptionTags; + doCaptionTags = getStringSet(pref, "doCaptionTag") ?? doCaptionTags; + } + + Future save() async { + var pref = await SharedPreferences.getInstance(); + var defaultSet = MarkdownRendererSettings(id); + + // Display - Image + await setBool(pref, "rotateImageGestures", rotateImageGestures, + defaultSet.rotateImageGestures); + await setDouble( + pref, "maxImageZoom", maxImageZoom, defaultSet.maxImageZoom); + + // Display - Image - Theme + await setBool(pref, "themeRasterGraphics", themeRasterGraphics, + defaultSet.themeRasterGraphics); + await setString( + pref, + "themeOverrideTagLocation", + themeOverrideTagLocation.toInternalString(), + defaultSet.themeOverrideTagLocation.toInternalString()); + await setStringSet( + pref, "doNotThemeTags", doNotThemeTags, defaultSet.doNotThemeTags); + await setStringSet( + pref, "doThemeTags", doThemeTags, defaultSet.doThemeTags); + await setString( + pref, + "themeVectorGraphics", + themeVectorGraphics.toInternalString(), + defaultSet.themeVectorGraphics.toInternalString()); + await setBool(pref, "themeSvgWithBackground", themeSvgWithBackground, + defaultSet.themeSvgWithBackground); + await setBool(pref, "matchCanvasColor", matchCanvasColor, + defaultSet.matchCanvasColor); + await setString( + pref, + "vectorGraphicsAdjustColors", + vectorGraphicsAdjustColors.toInternalString(), + defaultSet.vectorGraphicsAdjustColors.toInternalString()); + + // Display - Image - Caption + await setBool( + pref, "overlayCaption", overlayCaption, defaultSet.overlayCaption); + await setBool(pref, "transparentCaption", transparentCaption, + defaultSet.transparentCaption); + await setBool(pref, "blurBehindCaption", blurBehindCaption, + defaultSet.blurBehindCaption); + await setBool(pref, "tooltipFirst", tooltipFirst, defaultSet.tooltipFirst); + await setString(pref, "useAsCaption", useAsCaption.toInternalString(), + defaultSet.useAsCaption.toInternalString()); + await setStringSet( + pref, "doNotCaptionTag", doNotCaptionTags, defaultSet.doNotCaptionTags); + await setStringSet( + pref, "doCaptionTag", doCaptionTags, defaultSet.doCaptionTags); + } + + Map toLoggableMap() { + return { + // Display - Image + 'rotateImageGestures': rotateImageGestures.toString(), + 'maxImageZoom': maxImageZoom.toString(), + // Display - Image - Theming + 'themeRasterGraphics': themeRasterGraphics.toString(), + 'themeOverrideTagLocation': themeOverrideTagLocation.toInternalString(), + 'doNotThemeTags': csvTags(doNotThemeTags), + 'doThemeTags': csvTags(doThemeTags), + 'themeVectorGraphics': themeVectorGraphics.toInternalString(), + 'themeSvgWithBackground': themeSvgWithBackground.toString(), + 'matchCanvasColor': matchCanvasColor.toString(), + 'vectorGraphicsAdjustColors': + vectorGraphicsAdjustColors.toInternalString(), + // Display - Image - Caption + 'overlayCaption': overlayCaption.toString(), + 'transparentCaption': transparentCaption.toString(), + 'blurBehindCaption': blurBehindCaption.toString(), + 'tooltipFirst': tooltipFirst.toString(), + 'useAsCaption': useAsCaption.toInternalString(), + 'doNotCaptionTag': csvTags(doNotCaptionTags), + 'doCaptionTag': csvTags(doCaptionTags), + }; + } +} + +String csvTags(Set tags) { + return tags.join(", "); +} + +class SettingsThemeVectorGraphics { + static const On = SettingsThemeVectorGraphics( + "settings.display.images.theming.themeVectorGraphics.on", "on"); + static const Off = SettingsThemeVectorGraphics( + "settings.display.images.theming.themeVectorGraphics.off", "off"); + static const Filter = SettingsThemeVectorGraphics( + "settings.display.images.theming.themeVectorGraphics.filter", "filter"); + static const Default = On; + + final String _str; + final String _publicString; + const SettingsThemeVectorGraphics(this._publicString, this._str); + + String toInternalString() { + return _str; + } + + String toPublicString() { + return tr(_publicString); + } + + static const options = [ + On, + Off, + Filter, + ]; + + static SettingsThemeVectorGraphics fromInternalString(String? str) { + for (var opt in options) { + if (opt.toInternalString() == str) { + return opt; + } + } + return Default; + } + + static SettingsThemeVectorGraphics fromPublicString(String str) { + for (var opt in options) { + if (opt.toPublicString() == str) { + return opt; + } + } + return Default; + } + + @override + String toString() { + assert( + false, "SettingsThemeVectorGraphics toString should never be called"); + return ""; + } +} + +class SettingsVectorGraphicsAdjustColors { + static const All = SettingsVectorGraphicsAdjustColors( + "settings.display.images.theming.adjustColors.all", "all"); + static const BnW = SettingsVectorGraphicsAdjustColors( + "settings.display.images.theming.adjustColors.blackAndWhite", + "black_and_white"); + static const Grays = SettingsVectorGraphicsAdjustColors( + "settings.display.images.theming.adjustColors.grays", "grays"); + static const Default = All; + + final String _str; + final String _publicString; + const SettingsVectorGraphicsAdjustColors(this._publicString, this._str); + + String toInternalString() { + return _str; + } + + String toPublicString() { + return tr(_publicString); + } + + static const options = [ + BnW, + Grays, + All, + ]; + + static SettingsVectorGraphicsAdjustColors fromInternalString(String? str) { + for (var opt in options) { + if (opt.toInternalString() == str) { + return opt; + } + } + return Default; + } + + static SettingsVectorGraphicsAdjustColors fromPublicString(String str) { + for (var opt in options) { + if (opt.toPublicString() == str) { + return opt; + } + } + return Default; + } + + @override + String toString() { + assert(false, + "SettingsVectorGraphicsAdjustColors toString should never be called"); + return ""; + } +} + +class SettingsImageTextType { + static const AltTool = SettingsImageTextType( + "settings.display.images.imageTextType.altAndTooltip", "alt_and_tooltip"); + static const Tooltip = SettingsImageTextType( + "settings.display.images.imageTextType.tooltip", "tooltip"); + static const Alt = + SettingsImageTextType("settings.display.images.imageTextType.alt", "alt"); + static const None = SettingsImageTextType( + "settings.display.images.imageTextType.none", "none"); + static const Default = AltTool; + + final String _str; + final String _publicString; + const SettingsImageTextType(this._publicString, this._str); + + String toInternalString() { + return _str; + } + + String toPublicString() { + return tr(_publicString); + } + + static const options = [ + AltTool, + Tooltip, + Alt, + None, + ]; + + static SettingsImageTextType fromInternalString(String? str) { + for (var opt in options) { + if (opt.toInternalString() == str) { + return opt; + } + } + return Default; + } + + static SettingsImageTextType fromPublicString(String str) { + for (var opt in options) { + if (opt.toPublicString() == str) { + return opt; + } + } + return Default; + } + + @override + String toString() { + assert(false, + "SettingsThemeOverrideTagLocation toString should never be called"); + return ""; + } +} diff --git a/lib/widgets/images/image_caption.dart b/lib/widgets/images/image_caption.dart index 917acb28..2f0eaf05 100644 --- a/lib/widgets/images/image_caption.dart +++ b/lib/widgets/images/image_caption.dart @@ -21,7 +21,7 @@ import 'package:flutter/material.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:provider/provider.dart'; -import 'package:gitjournal/settings/settings.dart'; +import 'package:gitjournal/settings/settings_markdown_renderer.dart'; import 'package:gitjournal/utils/hero_dialog.dart'; class ImageCaption extends StatelessWidget { @@ -33,7 +33,7 @@ class ImageCaption extends StatelessWidget { @override Widget build(BuildContext context) { final theme = Theme.of(context); - final settings = Provider.of(context); + final settings = Provider.of(context); final text = captionText(context, altText, tooltip); @@ -116,7 +116,7 @@ bool shouldCaption(BuildContext context, String altText, String tooltip) { } String captionText(BuildContext context, String altText, String tooltip) { - final settings = Provider.of(context); + final settings = Provider.of(context); bool altTextCaption = settings.useAsCaption == SettingsImageTextType.AltTool || @@ -152,7 +152,7 @@ String captionText(BuildContext context, String altText, String tooltip) { } String _cleanCaption(BuildContext context, String caption) { - final settings = Provider.of(context); + final settings = Provider.of(context); final tags = [ ...settings.doThemeTags, ...settings.doNotThemeTags, @@ -171,7 +171,7 @@ String _cleanCaption(BuildContext context, String caption) { } Color _overlayBackgroundColor(context) { - final settings = Provider.of(context); + final settings = Provider.of(context); final theme = Theme.of(context); return settings.transparentCaption ? (theme.brightness == Brightness.dark ? Colors.black : Colors.white) diff --git a/lib/widgets/images/image_details.dart b/lib/widgets/images/image_details.dart index 65af58ec..a25fb90f 100644 --- a/lib/widgets/images/image_details.dart +++ b/lib/widgets/images/image_details.dart @@ -19,7 +19,7 @@ import 'package:flutter/material.dart'; import 'package:photo_view/photo_view.dart'; import 'package:provider/provider.dart'; -import 'package:gitjournal/settings/settings.dart'; +import 'package:gitjournal/settings/settings_markdown_renderer.dart'; import 'package:gitjournal/widgets/images/markdown_image.dart'; import 'package:gitjournal/widgets/images/themable_image.dart'; @@ -38,7 +38,7 @@ class _ImageDetailsState extends State { @override Widget build(BuildContext context) { final theme = Theme.of(context); - final settings = Provider.of(context); + final settings = Provider.of(context); final bg = theme.brightness == Brightness.dark ? Colors.black : Colors.white; final overlayColor = getOverlayBackgroundColor(context, diff --git a/lib/widgets/images/markdown_image.dart b/lib/widgets/images/markdown_image.dart index 7c42a437..56f8b348 100644 --- a/lib/widgets/images/markdown_image.dart +++ b/lib/widgets/images/markdown_image.dart @@ -26,7 +26,7 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:provider/provider.dart'; -import 'package:gitjournal/settings/settings.dart'; +import 'package:gitjournal/settings/settings_markdown_renderer.dart'; import 'package:gitjournal/utils/logger.dart'; import 'package:gitjournal/widgets/images/image_caption.dart'; import 'package:gitjournal/widgets/images/image_details.dart'; @@ -61,7 +61,7 @@ class MarkdownImage extends StatelessWidget { @override Widget build(BuildContext context) { - final settings = Provider.of(context); + final settings = Provider.of(context); final theme = Theme.of(context); final dark = theme.brightness == Brightness.dark; @@ -217,7 +217,7 @@ class MarkdownImage extends StatelessWidget { Color getOverlayBackgroundColor(BuildContext context, {Color? light, Color? dark}) { - final settings = Provider.of(context); + final settings = Provider.of(context); final theme = Theme.of(context); return theme.brightness == Brightness.dark ? settings.transparentCaption