Split Settings into MarkdownRendererSettings

We don't need one huge monolight of all the settings.
This commit is contained in:
Vishesh Handa
2021-07-26 15:35:23 +02:00
parent a090a38991
commit 95f6bceebc
9 changed files with 393 additions and 337 deletions

View File

@ -24,6 +24,7 @@ import 'package:gitjournal/repository.dart';
import 'package:gitjournal/repository_manager.dart'; import 'package:gitjournal/repository_manager.dart';
import 'package:gitjournal/settings/app_settings.dart'; import 'package:gitjournal/settings/app_settings.dart';
import 'package:gitjournal/settings/settings.dart'; import 'package:gitjournal/settings/settings.dart';
import 'package:gitjournal/settings/settings_markdown_renderer.dart';
import 'package:gitjournal/themes.dart'; import 'package:gitjournal/themes.dart';
import 'package:gitjournal/utils/logger.dart'; import 'package:gitjournal/utils/logger.dart';
@ -63,6 +64,7 @@ class JournalApp extends StatefulWidget {
child: GitJournalChangeNotifiers( child: GitJournalChangeNotifiers(
repoManager: repoManager, repoManager: repoManager,
appSettings: appSettings, appSettings: appSettings,
pref: pref,
child: JournalApp(), child: JournalApp(),
), ),
supportedLocales: [ supportedLocales: [
@ -350,11 +352,13 @@ class _JournalAppState extends State<JournalApp> {
class GitJournalChangeNotifiers extends StatelessWidget { class GitJournalChangeNotifiers extends StatelessWidget {
final RepositoryManager repoManager; final RepositoryManager repoManager;
final AppSettings appSettings; final AppSettings appSettings;
final SharedPreferences pref;
final Widget child; final Widget child;
GitJournalChangeNotifiers({ GitJournalChangeNotifiers({
required this.repoManager, required this.repoManager,
required this.appSettings, required this.appSettings,
required this.pref,
required this.child, required this.child,
Key? key, Key? key,
}) : super(key: key); }) : super(key: key);
@ -364,16 +368,18 @@ class GitJournalChangeNotifiers extends StatelessWidget {
var app = ChangeNotifierProvider.value( var app = ChangeNotifierProvider.value(
value: repoManager, value: repoManager,
child: Consumer<RepositoryManager>( child: Consumer<RepositoryManager>(
builder: (_, repoManager, __) => ChangeNotifierProvider.value( builder: (_, repoManager, __) => buildMarkdownSettings(
value: repoManager.currentRepo, child: ChangeNotifierProvider.value(
child: Consumer<GitJournalRepo>( value: repoManager.currentRepo,
builder: (_, repo, __) => ChangeNotifierProvider<Settings>.value( child: Consumer<GitJournalRepo>(
value: repo.settings, builder: (_, repo, __) => ChangeNotifierProvider<Settings>.value(
child: Consumer<GitJournalRepo>( value: repo.settings,
builder: (_, repo, __) => child: Consumer<GitJournalRepo>(
ChangeNotifierProvider<NotesFolderFS>.value( builder: (_, repo, __) =>
value: repo.notesFolder, ChangeNotifierProvider<NotesFolderFS>.value(
child: child, value: repo.notesFolder,
child: child,
),
), ),
), ),
), ),
@ -387,4 +393,15 @@ class GitJournalChangeNotifiers extends StatelessWidget {
child: app, child: app,
); );
} }
Widget buildMarkdownSettings({required Widget child}) {
return Consumer<RepositoryManager>(
builder: (_, repoManager, __) {
var markdown = MarkdownRendererSettings(repoManager.currentId);
markdown.load(pref);
return ChangeNotifierProvider.value(value: markdown, child: child);
},
);
}
} }

View File

@ -42,9 +42,13 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
String folderName = "journal"; String folderName = "journal";
// Properties // Git Settings
String gitAuthor = "GitJournal"; String gitAuthor = "GitJournal";
String gitAuthorEmail = "app@gitjournal.io"; String gitAuthorEmail = "app@gitjournal.io";
String sshPublicKey = "";
String sshPrivateKey = "";
String sshPassword = "";
NoteFileNameFormat noteFileNameFormat = NoteFileNameFormat.Default; NoteFileNameFormat noteFileNameFormat = NoteFileNameFormat.Default;
NoteFileNameFormat journalNoteFileNameFormat = NoteFileNameFormat.Default; NoteFileNameFormat journalNoteFileNameFormat = NoteFileNameFormat.Default;
@ -71,32 +75,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
SettingsHomeScreen homeScreen = SettingsHomeScreen.Default; SettingsHomeScreen homeScreen = SettingsHomeScreen.Default;
SettingsTheme theme = SettingsTheme.Default; SettingsTheme theme = SettingsTheme.Default;
// Display - Image
bool rotateImageGestures = false;
double maxImageZoom = 10;
// Display - Image - Theming
bool themeRasterGraphics = false;
SettingsImageTextType themeOverrideTagLocation =
SettingsImageTextType.Default;
Set<String> doNotThemeTags = {"notheme", "!nt"};
Set<String> 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<String> doNotCaptionTags = {"nocaption", "!nc"};
Set<String> doCaptionTags = {"docaption", "!dc"};
SettingsMarkdownDefaultView markdownDefaultView = SettingsMarkdownDefaultView markdownDefaultView =
SettingsMarkdownDefaultView.Default; SettingsMarkdownDefaultView.Default;
SettingsMarkdownDefaultView markdownLastUsedView = SettingsMarkdownDefaultView markdownLastUsedView =
@ -118,13 +96,12 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
bool storeInternally = true; bool storeInternally = true;
String storageLocation = ""; String storageLocation = "";
String sshPublicKey = "";
String sshPrivateKey = "";
String sshPassword = "";
void load(SharedPreferences pref) { void load(SharedPreferences pref) {
gitAuthor = getString(pref, "gitAuthor") ?? gitAuthor; gitAuthor = getString(pref, "gitAuthor") ?? gitAuthor;
gitAuthorEmail = getString(pref, "gitAuthorEmail") ?? gitAuthorEmail; gitAuthorEmail = getString(pref, "gitAuthorEmail") ?? gitAuthorEmail;
sshPublicKey = getString(pref, "sshPublicKey") ?? sshPublicKey;
sshPrivateKey = getString(pref, "sshPrivateKey") ?? sshPrivateKey;
sshPassword = getString(pref, "sshPassword") ?? sshPassword;
noteFileNameFormat = NoteFileNameFormat.fromInternalString( noteFileNameFormat = NoteFileNameFormat.fromInternalString(
getString(pref, "noteFileNameFormat")); getString(pref, "noteFileNameFormat"));
@ -176,39 +153,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
SettingsHomeScreen.fromInternalString(getString(pref, "homeScreen")); SettingsHomeScreen.fromInternalString(getString(pref, "homeScreen"));
theme = SettingsTheme.fromInternalString(getString(pref, "theme")); 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 = imageLocationSpec =
getString(pref, "imageLocationSpec") ?? imageLocationSpec; getString(pref, "imageLocationSpec") ?? imageLocationSpec;
@ -223,10 +167,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
// From AppState // From AppState
folderName = getString(pref, FOLDER_NAME_KEY) ?? folderName; 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; bottomMenuBar = getBool(pref, "bottomMenuBar") ?? bottomMenuBar;
confirmDelete = getBool(pref, "confirmDelete") ?? confirmDelete; confirmDelete = getBool(pref, "confirmDelete") ?? confirmDelete;
storeInternally = getBool(pref, "storeInternally") ?? storeInternally; 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, "gitAuthor", gitAuthor, defaultSet.gitAuthor);
await setString( await setString(
pref, "gitAuthorEmail", gitAuthorEmail, defaultSet.gitAuthorEmail); 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( await setString(
pref, pref,
"noteFileNameFormat", "noteFileNameFormat",
@ -303,54 +249,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
await setString(pref, "theme", theme.toInternalString(), await setString(pref, "theme", theme.toInternalString(),
defaultSet.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, await setString(pref, "imageLocationSpec", imageLocationSpec,
defaultSet.imageLocationSpec); defaultSet.imageLocationSpec);
await setBool(pref, "zenMode", zenMode, defaultSet.zenMode); await setBool(pref, "zenMode", zenMode, defaultSet.zenMode);
@ -369,12 +267,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
await setString( await setString(
pref, "storageLocation", storageLocation, defaultSet.storageLocation); 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 setInt(pref, "settingsVersion", version, defaultSet.version);
await setString(pref, FOLDER_NAME_KEY, folderName, defaultSet.folderName); await setString(pref, FOLDER_NAME_KEY, folderName, defaultSet.folderName);
@ -388,6 +280,8 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
return <String, String>{ return <String, String>{
"gitAuthor": gitAuthor.isNotEmpty.toString(), "gitAuthor": gitAuthor.isNotEmpty.toString(),
"gitAuthorEmail": gitAuthorEmail.isNotEmpty.toString(), "gitAuthorEmail": gitAuthorEmail.isNotEmpty.toString(),
'sshPublicKey': sshPublicKey.isNotEmpty.toString(),
'sshPrivateKey': sshPrivateKey.isNotEmpty.toString(),
"noteFileNameFormat": noteFileNameFormat.toInternalString(), "noteFileNameFormat": noteFileNameFormat.toInternalString(),
"journalNoteFileNameFormat": journalNoteFileNameFormat.toInternalString(), "journalNoteFileNameFormat": journalNoteFileNameFormat.toInternalString(),
"yamlModifiedKey": yamlModifiedKey, "yamlModifiedKey": yamlModifiedKey,
@ -412,28 +306,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
'markdownLastUsedView': markdownLastUsedView.toInternalString(), 'markdownLastUsedView': markdownLastUsedView.toInternalString(),
'homeScreen': homeScreen.toInternalString(), 'homeScreen': homeScreen.toInternalString(),
'theme': theme.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, 'imageLocationSpec': imageLocationSpec,
'zenMode': zenMode.toString(), 'zenMode': zenMode.toString(),
'titleSettings': titleSettings.toInternalString(), 'titleSettings': titleSettings.toInternalString(),
@ -445,8 +317,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
'confirmDelete': confirmDelete.toString(), 'confirmDelete': confirmDelete.toString(),
'storeInternally': storeInternally.toString(), 'storeInternally': storeInternally.toString(),
'storageLocation': storageLocation, '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 = <SettingsImageTextType>[
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 = <SettingsThemeVectorGraphics>[
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 = <SettingsVectorGraphicsAdjustColors>[
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() { String generateRandomId() {
return const Uuid().v4().substring(0, 8); return const Uuid().v4().substring(0, 8);
} }
@ -1151,7 +858,3 @@ Set<String> parseTags(String tags) {
.where((e) => e.isNotEmpty) .where((e) => e.isNotEmpty)
.toSet(); .toSet();
} }
String csvTags(Set<String> tags) {
return tags.join(", ");
}

View File

@ -21,9 +21,9 @@ import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:provider/provider.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_caption.dart';
import 'package:gitjournal/settings/settings_display_images_theming.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'; import 'package:gitjournal/settings/settings_screen.dart';
class SettingsDisplayImagesScreen extends StatefulWidget { class SettingsDisplayImagesScreen extends StatefulWidget {
@ -36,7 +36,7 @@ class SettingsDisplayImagesScreenState
extends State<SettingsDisplayImagesScreen> { extends State<SettingsDisplayImagesScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final settings = Provider.of<Settings>(context); final settings = Provider.of<MarkdownRendererSettings>(context);
final theme = Theme.of(context); final theme = Theme.of(context);
final body = ListView(children: <Widget>[ final body = ListView(children: <Widget>[

View File

@ -20,6 +20,7 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:gitjournal/settings/settings.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_screen.dart';
import 'package:gitjournal/settings/settings_widgets.dart'; import 'package:gitjournal/settings/settings_widgets.dart';
@ -35,7 +36,7 @@ class SettingsDisplayImagesCaptionScreenState
final doCaptionTagsKey = GlobalKey<FormFieldState<String>>(); final doCaptionTagsKey = GlobalKey<FormFieldState<String>>();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var settings = Provider.of<Settings>(context); var settings = Provider.of<MarkdownRendererSettings>(context);
var saveDoNotCaptionTag = (String? doNotCaptionTags) { var saveDoNotCaptionTag = (String? doNotCaptionTags) {
if (doNotCaptionTags == null) { if (doNotCaptionTags == null) {
return; return;

View File

@ -20,6 +20,7 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:gitjournal/settings/settings.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_screen.dart';
import 'package:gitjournal/settings/settings_widgets.dart'; import 'package:gitjournal/settings/settings_widgets.dart';
@ -36,7 +37,7 @@ class SettingsDisplayImagesThemingScreenState
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var settings = Provider.of<Settings>(context); var settings = Provider.of<MarkdownRendererSettings>(context);
var saveDoNotThemeTag = (String? doNotThemeTags) { var saveDoNotThemeTag = (String? doNotThemeTags) {
settings.doNotThemeTags = parseTags(doNotThemeTags!); settings.doNotThemeTags = parseTags(doNotThemeTags!);

View File

@ -0,0 +1,334 @@
/*
Copyright 2020-2021 Vishesh Handa <me@vhanda.in>
Roland Fredenhagen <important@van-fredenhagen.de>
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<void> 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<String, String> toLoggableMap() {
return <String, String>{
// 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<String> 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 = <SettingsThemeVectorGraphics>[
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 = <SettingsVectorGraphicsAdjustColors>[
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 = <SettingsImageTextType>[
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 "";
}
}

View File

@ -21,7 +21,7 @@ import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:provider/provider.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'; import 'package:gitjournal/utils/hero_dialog.dart';
class ImageCaption extends StatelessWidget { class ImageCaption extends StatelessWidget {
@ -33,7 +33,7 @@ class ImageCaption extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = Theme.of(context); final theme = Theme.of(context);
final settings = Provider.of<Settings>(context); final settings = Provider.of<MarkdownRendererSettings>(context);
final text = captionText(context, altText, tooltip); 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) { String captionText(BuildContext context, String altText, String tooltip) {
final settings = Provider.of<Settings>(context); final settings = Provider.of<MarkdownRendererSettings>(context);
bool altTextCaption = bool altTextCaption =
settings.useAsCaption == SettingsImageTextType.AltTool || settings.useAsCaption == SettingsImageTextType.AltTool ||
@ -152,7 +152,7 @@ String captionText(BuildContext context, String altText, String tooltip) {
} }
String _cleanCaption(BuildContext context, String caption) { String _cleanCaption(BuildContext context, String caption) {
final settings = Provider.of<Settings>(context); final settings = Provider.of<MarkdownRendererSettings>(context);
final tags = [ final tags = [
...settings.doThemeTags, ...settings.doThemeTags,
...settings.doNotThemeTags, ...settings.doNotThemeTags,
@ -171,7 +171,7 @@ String _cleanCaption(BuildContext context, String caption) {
} }
Color _overlayBackgroundColor(context) { Color _overlayBackgroundColor(context) {
final settings = Provider.of<Settings>(context); final settings = Provider.of<MarkdownRendererSettings>(context);
final theme = Theme.of(context); final theme = Theme.of(context);
return settings.transparentCaption return settings.transparentCaption
? (theme.brightness == Brightness.dark ? Colors.black : Colors.white) ? (theme.brightness == Brightness.dark ? Colors.black : Colors.white)

View File

@ -19,7 +19,7 @@ import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart'; import 'package:photo_view/photo_view.dart';
import 'package:provider/provider.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/markdown_image.dart';
import 'package:gitjournal/widgets/images/themable_image.dart'; import 'package:gitjournal/widgets/images/themable_image.dart';
@ -38,7 +38,7 @@ class _ImageDetailsState extends State<ImageDetails> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = Theme.of(context); final theme = Theme.of(context);
final settings = Provider.of<Settings>(context); final settings = Provider.of<MarkdownRendererSettings>(context);
final bg = final bg =
theme.brightness == Brightness.dark ? Colors.black : Colors.white; theme.brightness == Brightness.dark ? Colors.black : Colors.white;
final overlayColor = getOverlayBackgroundColor(context, final overlayColor = getOverlayBackgroundColor(context,

View File

@ -26,7 +26,7 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:provider/provider.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/utils/logger.dart';
import 'package:gitjournal/widgets/images/image_caption.dart'; import 'package:gitjournal/widgets/images/image_caption.dart';
import 'package:gitjournal/widgets/images/image_details.dart'; import 'package:gitjournal/widgets/images/image_details.dart';
@ -61,7 +61,7 @@ class MarkdownImage extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final settings = Provider.of<Settings>(context); final settings = Provider.of<MarkdownRendererSettings>(context);
final theme = Theme.of(context); final theme = Theme.of(context);
final dark = theme.brightness == Brightness.dark; final dark = theme.brightness == Brightness.dark;
@ -217,7 +217,7 @@ class MarkdownImage extends StatelessWidget {
Color getOverlayBackgroundColor(BuildContext context, Color getOverlayBackgroundColor(BuildContext context,
{Color? light, Color? dark}) { {Color? light, Color? dark}) {
final settings = Provider.of<Settings>(context); final settings = Provider.of<MarkdownRendererSettings>(context);
final theme = Theme.of(context); final theme = Theme.of(context);
return theme.brightness == Brightness.dark return theme.brightness == Brightness.dark
? settings.transparentCaption ? settings.transparentCaption