Simplify Settings/Config classes

This commit is contained in:
Vishesh Handa
2021-07-29 12:22:50 +02:00
parent c66eaf1052
commit c6665fa63e
23 changed files with 298 additions and 309 deletions

View File

@ -414,8 +414,8 @@ class GitJournalChangeNotifiers extends StatelessWidget {
Widget buildMarkdownSettings({required Widget child}) { Widget buildMarkdownSettings({required Widget child}) {
return Consumer<RepositoryManager>( return Consumer<RepositoryManager>(
builder: (_, repoManager, __) { builder: (_, repoManager, __) {
var markdown = MarkdownRendererConfig(repoManager.currentId); var markdown = MarkdownRendererConfig(repoManager.currentId, pref);
markdown.load(pref); markdown.load();
return ChangeNotifierProvider.value(value: markdown, child: child); return ChangeNotifierProvider.value(value: markdown, child: child);
}, },

0
lib/core/file.dart Normal file
View File

View File

@ -8,11 +8,14 @@ import 'package:gitjournal/settings/settings.dart';
import 'package:gitjournal/settings/settings_sharedpref.dart'; import 'package:gitjournal/settings/settings_sharedpref.dart';
class NotesFolderConfig extends ChangeNotifier with SettingsSharedPref { class NotesFolderConfig extends ChangeNotifier with SettingsSharedPref {
NotesFolderConfig(this.id); NotesFolderConfig(this.id, this.pref);
@override @override
final String id; final String id;
@override
final SharedPreferences pref;
var sortingField = SortingField.Default; var sortingField = SortingField.Default;
var sortingOrder = SortingOrder.Default; var sortingOrder = SortingOrder.Default;
@ -34,83 +37,71 @@ class NotesFolderConfig extends ChangeNotifier with SettingsSharedPref {
var inlineTagPrefixes = {'#'}; var inlineTagPrefixes = {'#'};
var imageLocationSpec = "."; // . means the same folder var imageLocationSpec = "."; // . means the same folder
void load(SharedPreferences pref) { void load() {
fileNameFormat = NoteFileNameFormat.fromInternalString( fileNameFormat =
getString(pref, "noteFileNameFormat")); NoteFileNameFormat.fromInternalString(getString("noteFileNameFormat"));
journalFileNameFormat = NoteFileNameFormat.fromInternalString( journalFileNameFormat = NoteFileNameFormat.fromInternalString(
getString(pref, "journalNoteFileNameFormat")); getString("journalNoteFileNameFormat"));
yamlModifiedKey = getString(pref, "yamlModifiedKey") ?? yamlModifiedKey; yamlModifiedKey = getString("yamlModifiedKey") ?? yamlModifiedKey;
yamlCreatedKey = getString(pref, "yamlCreatedKey") ?? yamlCreatedKey; yamlCreatedKey = getString("yamlCreatedKey") ?? yamlCreatedKey;
yamlTagsKey = getString(pref, "yamlTagsKey") ?? yamlTagsKey; yamlTagsKey = getString("yamlTagsKey") ?? yamlTagsKey;
yamlHeaderEnabled = getBool(pref, "yamlHeaderEnabled") ?? yamlHeaderEnabled; yamlHeaderEnabled = getBool("yamlHeaderEnabled") ?? yamlHeaderEnabled;
sortingField = sortingField = SortingField.fromInternalString(getString("sortingField"));
SortingField.fromInternalString(getString(pref, "sortingField")); sortingOrder = SortingOrder.fromInternalString(getString("sortingOrder"));
sortingOrder =
SortingOrder.fromInternalString(getString(pref, "sortingOrder"));
defaultEditor = defaultEditor =
SettingsEditorType.fromInternalString(getString(pref, "defaultEditor")); SettingsEditorType.fromInternalString(getString("defaultEditor"));
defaultView = SettingsFolderViewType.fromInternalString( defaultView =
getString(pref, "defaultView")); SettingsFolderViewType.fromInternalString(getString("defaultView"));
showNoteSummary = getBool(pref, "showNoteSummary") ?? showNoteSummary; showNoteSummary = getBool("showNoteSummary") ?? showNoteSummary;
folderViewHeaderType = folderViewHeaderType =
getString(pref, "folderViewHeaderType") ?? folderViewHeaderType; getString("folderViewHeaderType") ?? folderViewHeaderType;
imageLocationSpec = imageLocationSpec = getString("imageLocationSpec") ?? imageLocationSpec;
getString(pref, "imageLocationSpec") ?? imageLocationSpec;
titleSettings = titleSettings =
SettingsTitle.fromInternalString(getString(pref, "titleSettings")); SettingsTitle.fromInternalString(getString("titleSettings"));
inlineTagPrefixes = inlineTagPrefixes = getStringSet("inlineTagPrefixes") ?? inlineTagPrefixes;
getStringSet(pref, "inlineTagPrefixes") ?? inlineTagPrefixes;
} }
Future<void> save() async { Future<void> save() async {
var pref = await SharedPreferences.getInstance(); var def = NotesFolderConfig(id, pref);
var defaultSet = NotesFolderConfig(id);
await setString("noteFileNameFormat", fileNameFormat.toInternalString(),
def.fileNameFormat.toInternalString());
await setString( await setString(
pref,
"noteFileNameFormat",
fileNameFormat.toInternalString(),
defaultSet.fileNameFormat.toInternalString());
await setString(
pref,
"journalNoteFileNameFormat", "journalNoteFileNameFormat",
journalFileNameFormat.toInternalString(), journalFileNameFormat.toInternalString(),
defaultSet.journalFileNameFormat.toInternalString()); def.journalFileNameFormat.toInternalString());
await setString("sortingField", sortingField.toInternalString(),
def.sortingField.toInternalString());
await setString("sortingOrder", sortingOrder.toInternalString(),
def.sortingOrder.toInternalString());
await setString("defaultEditor", defaultEditor.toInternalString(),
def.defaultEditor.toInternalString());
await setString("defaultView", defaultView.toInternalString(),
def.defaultView.toInternalString());
await setBool("showNoteSummary", showNoteSummary, def.showNoteSummary);
await setString(
"folderViewHeaderType", folderViewHeaderType, def.folderViewHeaderType);
await setString(pref, "sortingField", sortingField.toInternalString(),
defaultSet.sortingField.toInternalString());
await setString(pref, "sortingOrder", sortingOrder.toInternalString(),
defaultSet.sortingOrder.toInternalString());
await setString(pref, "defaultEditor", defaultEditor.toInternalString(),
defaultSet.defaultEditor.toInternalString());
await setString(pref, "defaultView", defaultView.toInternalString(),
defaultSet.defaultView.toInternalString());
await setBool( await setBool(
pref, "showNoteSummary", showNoteSummary, defaultSet.showNoteSummary); "yamlHeaderEnabled", yamlHeaderEnabled, def.yamlHeaderEnabled);
await setString(pref, "folderViewHeaderType", folderViewHeaderType, await setString("yamlModifiedKey", yamlModifiedKey, def.yamlModifiedKey);
defaultSet.folderViewHeaderType); await setString("yamlCreatedKey", yamlCreatedKey, def.yamlCreatedKey);
await setString("yamlTagsKey", yamlTagsKey, def.yamlTagsKey);
await setString("titleSettings", titleSettings.toInternalString(),
def.titleSettings.toInternalString());
await setBool(pref, "yamlHeaderEnabled", yamlHeaderEnabled, await setStringSet(
defaultSet.yamlHeaderEnabled); "inlineTagPrefixes", inlineTagPrefixes, def.inlineTagPrefixes);
await setString( await setString(
pref, "yamlModifiedKey", yamlModifiedKey, defaultSet.yamlModifiedKey); "imageLocationSpec", imageLocationSpec, def.imageLocationSpec);
await setString(
pref, "yamlCreatedKey", yamlCreatedKey, defaultSet.yamlCreatedKey);
await setString(pref, "yamlTagsKey", yamlTagsKey, defaultSet.yamlTagsKey);
await setString(pref, "titleSettings", titleSettings.toInternalString(),
defaultSet.titleSettings.toInternalString());
await setStringSet(pref, "inlineTagPrefixes", inlineTagPrefixes,
defaultSet.inlineTagPrefixes);
await setString(pref, "imageLocationSpec", imageLocationSpec,
defaultSet.imageLocationSpec);
notifyListeners(); notifyListeners();
} }

View File

@ -1,13 +1,12 @@
import 'package:gitjournal/settings/settings.dart';
import 'note.dart'; import 'note.dart';
import 'notes_folder.dart'; import 'notes_folder.dart';
import 'notes_folder_notifier.dart'; import 'notes_folder_notifier.dart';
class VirtualNotesFolder with NotesFolderNotifier implements NotesFolder { class VirtualNotesFolder with NotesFolderNotifier implements NotesFolder {
final List<Note> _notes; final List<Note> _notes;
final Settings settings; final NotesFolderConfig _config;
VirtualNotesFolder(this._notes, this.settings); VirtualNotesFolder(this._notes, this._config);
@override @override
List<Note> get notes => _notes; List<Note> get notes => _notes;
@ -39,8 +38,5 @@ class VirtualNotesFolder with NotesFolderNotifier implements NotesFolder {
} }
@override @override
NotesFolderConfig get config { NotesFolderConfig get config => _config;
// FIXME: This isn't expecting null!
return NotesFolderConfig('_');
}
} }

View File

@ -75,17 +75,17 @@ class GitJournalRepo with ChangeNotifier {
}) async { }) async {
await migrateSettings(id, pref, gitBaseDir); await migrateSettings(id, pref, gitBaseDir);
var storageConfig = StorageConfig(id); var storageConfig = StorageConfig(id, pref);
storageConfig.load(pref); storageConfig.load();
var folderConfig = NotesFolderConfig(id); var folderConfig = NotesFolderConfig(id, pref);
folderConfig.load(pref); folderConfig.load();
var gitConfig = GitConfig(id); var gitConfig = GitConfig(id, pref);
gitConfig.load(pref); gitConfig.load();
var settings = Settings(id); var settings = Settings(id, pref);
settings.load(pref); settings.load();
// logEvent(Event.Settings, parameters: settings.toLoggableMap()); // logEvent(Event.Settings, parameters: settings.toLoggableMap());

View File

@ -5,37 +5,36 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:gitjournal/settings/settings_sharedpref.dart'; import 'package:gitjournal/settings/settings_sharedpref.dart';
class GitConfig extends ChangeNotifier with SettingsSharedPref { class GitConfig extends ChangeNotifier with SettingsSharedPref {
GitConfig(this.id); GitConfig(this.id, this.pref);
@override @override
final String id; final String id;
@override
final SharedPreferences pref;
var gitAuthor = "GitJournal"; var gitAuthor = "GitJournal";
var gitAuthorEmail = "app@gitjournal.io"; var gitAuthorEmail = "app@gitjournal.io";
var sshPublicKey = ""; var sshPublicKey = "";
var sshPrivateKey = ""; var sshPrivateKey = "";
var sshPassword = ""; var sshPassword = "";
void load(SharedPreferences pref) { void load() {
gitAuthor = getString(pref, "gitAuthor") ?? gitAuthor; gitAuthor = getString("gitAuthor") ?? gitAuthor;
gitAuthorEmail = getString(pref, "gitAuthorEmail") ?? gitAuthorEmail; gitAuthorEmail = getString("gitAuthorEmail") ?? gitAuthorEmail;
sshPublicKey = getString(pref, "sshPublicKey") ?? sshPublicKey; sshPublicKey = getString("sshPublicKey") ?? sshPublicKey;
sshPrivateKey = getString(pref, "sshPrivateKey") ?? sshPrivateKey; sshPrivateKey = getString("sshPrivateKey") ?? sshPrivateKey;
sshPassword = getString(pref, "sshPassword") ?? sshPassword; sshPassword = getString("sshPassword") ?? sshPassword;
} }
Future<void> save() async { Future<void> save() async {
var pref = await SharedPreferences.getInstance(); var def = GitConfig(id, pref);
var defaultSet = GitConfig(id);
await setString(pref, "gitAuthor", gitAuthor, defaultSet.gitAuthor); await setString("gitAuthor", gitAuthor, def.gitAuthor);
await setString( await setString("gitAuthorEmail", gitAuthorEmail, def.gitAuthorEmail);
pref, "gitAuthorEmail", gitAuthorEmail, defaultSet.gitAuthorEmail); await setString("sshPublicKey", sshPublicKey, def.sshPublicKey);
await setString( await setString("sshPrivateKey", sshPrivateKey, def.sshPrivateKey);
pref, "sshPublicKey", sshPublicKey, defaultSet.sshPublicKey); await setString("sshPassword", sshPassword, def.sshPassword);
await setString(
pref, "sshPrivateKey", sshPrivateKey, defaultSet.sshPrivateKey);
await setString(pref, "sshPassword", sshPassword, defaultSet.sshPassword);
notifyListeners(); notifyListeners();
} }

View File

@ -23,11 +23,14 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:gitjournal/settings/settings_sharedpref.dart'; import 'package:gitjournal/settings/settings_sharedpref.dart';
class MarkdownRendererConfig extends ChangeNotifier with SettingsSharedPref { class MarkdownRendererConfig extends ChangeNotifier with SettingsSharedPref {
MarkdownRendererConfig(this.id); MarkdownRendererConfig(this.id, this.pref);
@override @override
final String id; final String id;
@override
final SharedPreferences pref;
// Display - Image // Display - Image
bool rotateImageGestures = false; bool rotateImageGestures = false;
double maxImageZoom = 10; double maxImageZoom = 10;
@ -51,92 +54,78 @@ class MarkdownRendererConfig extends ChangeNotifier with SettingsSharedPref {
var doNotCaptionTags = {"nocaption", "!nc"}; var doNotCaptionTags = {"nocaption", "!nc"};
var doCaptionTags = {"docaption", "!dc"}; var doCaptionTags = {"docaption", "!dc"};
void load(SharedPreferences pref) { void load() {
// Display - Image // Display - Image
rotateImageGestures = rotateImageGestures = getBool("rotateImageGestures") ?? rotateImageGestures;
getBool(pref, "rotateImageGestures") ?? rotateImageGestures; maxImageZoom = getDouble("maxImageZoom") ?? maxImageZoom;
maxImageZoom = getDouble(pref, "maxImageZoom") ?? maxImageZoom;
// Display - Image - Theming // Display - Image - Theming
themeRasterGraphics = themeRasterGraphics = getBool("themeRasterGraphics") ?? themeRasterGraphics;
getBool(pref, "themeRasterGraphics") ?? themeRasterGraphics;
themeOverrideTagLocation = SettingsImageTextType.fromInternalString( themeOverrideTagLocation = SettingsImageTextType.fromInternalString(
getString(pref, "themeOverrideTagLocation")); getString("themeOverrideTagLocation"));
doNotThemeTags = getStringSet(pref, "doNotThemeTags") ?? doNotThemeTags; doNotThemeTags = getStringSet("doNotThemeTags") ?? doNotThemeTags;
doThemeTags = getStringSet(pref, "doThemeTags") ?? doThemeTags; doThemeTags = getStringSet("doThemeTags") ?? doThemeTags;
themeVectorGraphics = SettingsThemeVectorGraphics.fromInternalString( themeVectorGraphics = SettingsThemeVectorGraphics.fromInternalString(
getString(pref, "themeVectorGraphics")); getString("themeVectorGraphics"));
themeSvgWithBackground = themeSvgWithBackground =
getBool(pref, "themeSvgWithBackground") ?? themeSvgWithBackground; getBool("themeSvgWithBackground") ?? themeSvgWithBackground;
matchCanvasColor = getBool(pref, "matchCanvasColor") ?? matchCanvasColor; matchCanvasColor = getBool("matchCanvasColor") ?? matchCanvasColor;
vectorGraphicsAdjustColors = vectorGraphicsAdjustColors =
SettingsVectorGraphicsAdjustColors.fromInternalString( SettingsVectorGraphicsAdjustColors.fromInternalString(
getString(pref, "vectorGraphicsAdjustColors")); getString("vectorGraphicsAdjustColors"));
// Display - Image - Caption // Display - Image - Caption
overlayCaption = getBool(pref, "overlayCaption") ?? overlayCaption; overlayCaption = getBool("overlayCaption") ?? overlayCaption;
transparentCaption = transparentCaption = getBool("transparentCaption") ?? transparentCaption;
getBool(pref, "transparentCaption") ?? transparentCaption; blurBehindCaption = getBool("blurBehindCaption") ?? blurBehindCaption;
blurBehindCaption = getBool(pref, "blurBehindCaption") ?? blurBehindCaption; tooltipFirst = getBool("tooltipFirst") ?? tooltipFirst;
tooltipFirst = getBool(pref, "tooltipFirst") ?? tooltipFirst; useAsCaption =
useAsCaption = SettingsImageTextType.fromInternalString( SettingsImageTextType.fromInternalString(getString("useAsCaption"));
getString(pref, "useAsCaption")); doNotCaptionTags = getStringSet("doNotCaptionTag") ?? doNotCaptionTags;
doNotCaptionTags = doCaptionTags = getStringSet("doCaptionTag") ?? doCaptionTags;
getStringSet(pref, "doNotCaptionTag") ?? doNotCaptionTags;
doCaptionTags = getStringSet(pref, "doCaptionTag") ?? doCaptionTags;
} }
Future<void> save() async { Future<void> save() async {
var pref = await SharedPreferences.getInstance(); var def = MarkdownRendererConfig(id, pref);
var defaultSet = MarkdownRendererConfig(id);
// Display - Image // Display - Image
await setBool(pref, "rotateImageGestures", rotateImageGestures, await setBool(
defaultSet.rotateImageGestures); "rotateImageGestures", rotateImageGestures, def.rotateImageGestures);
await setDouble( await setDouble("maxImageZoom", maxImageZoom, def.maxImageZoom);
pref, "maxImageZoom", maxImageZoom, defaultSet.maxImageZoom);
// Display - Image - Theme // Display - Image - Theme
await setBool(pref, "themeRasterGraphics", themeRasterGraphics, await setBool(
defaultSet.themeRasterGraphics); "themeRasterGraphics", themeRasterGraphics, def.themeRasterGraphics);
await setString( await setString(
pref,
"themeOverrideTagLocation", "themeOverrideTagLocation",
themeOverrideTagLocation.toInternalString(), themeOverrideTagLocation.toInternalString(),
defaultSet.themeOverrideTagLocation.toInternalString()); def.themeOverrideTagLocation.toInternalString());
await setStringSet( await setStringSet("doNotThemeTags", doNotThemeTags, def.doNotThemeTags);
pref, "doNotThemeTags", doNotThemeTags, defaultSet.doNotThemeTags); await setStringSet("doThemeTags", doThemeTags, def.doThemeTags);
await setStringSet(
pref, "doThemeTags", doThemeTags, defaultSet.doThemeTags);
await setString( await setString(
pref,
"themeVectorGraphics", "themeVectorGraphics",
themeVectorGraphics.toInternalString(), themeVectorGraphics.toInternalString(),
defaultSet.themeVectorGraphics.toInternalString()); def.themeVectorGraphics.toInternalString());
await setBool(pref, "themeSvgWithBackground", themeSvgWithBackground, await setBool("themeSvgWithBackground", themeSvgWithBackground,
defaultSet.themeSvgWithBackground); def.themeSvgWithBackground);
await setBool(pref, "matchCanvasColor", matchCanvasColor, await setBool("matchCanvasColor", matchCanvasColor, def.matchCanvasColor);
defaultSet.matchCanvasColor);
await setString( await setString(
pref,
"vectorGraphicsAdjustColors", "vectorGraphicsAdjustColors",
vectorGraphicsAdjustColors.toInternalString(), vectorGraphicsAdjustColors.toInternalString(),
defaultSet.vectorGraphicsAdjustColors.toInternalString()); def.vectorGraphicsAdjustColors.toInternalString());
// Display - Image - Caption // Display - Image - Caption
await setBool("overlayCaption", overlayCaption, def.overlayCaption);
await setBool( await setBool(
pref, "overlayCaption", overlayCaption, defaultSet.overlayCaption); "transparentCaption", transparentCaption, def.transparentCaption);
await setBool(pref, "transparentCaption", transparentCaption, await setBool(
defaultSet.transparentCaption); "blurBehindCaption", blurBehindCaption, def.blurBehindCaption);
await setBool(pref, "blurBehindCaption", blurBehindCaption, await setBool("tooltipFirst", tooltipFirst, def.tooltipFirst);
defaultSet.blurBehindCaption); await setString("useAsCaption", useAsCaption.toInternalString(),
await setBool(pref, "tooltipFirst", tooltipFirst, defaultSet.tooltipFirst); def.useAsCaption.toInternalString());
await setString(pref, "useAsCaption", useAsCaption.toInternalString(),
defaultSet.useAsCaption.toInternalString());
await setStringSet( await setStringSet(
pref, "doNotCaptionTag", doNotCaptionTags, defaultSet.doNotCaptionTags); "doNotCaptionTag", doNotCaptionTags, def.doNotCaptionTags);
await setStringSet( await setStringSet("doCaptionTag", doCaptionTags, def.doCaptionTags);
pref, "doCaptionTag", doCaptionTags, defaultSet.doCaptionTags);
notifyListeners(); notifyListeners();
} }

View File

@ -0,0 +1,8 @@
import 'package:flutter/material.dart';
import 'package:gitjournal/settings/settings_screen.dart';
// Need - Settings, AppSettings, Repo, RepoManager
// It won't work otherwise!
//
Widget settingsScreen() => SettingsScreen();

View File

@ -29,10 +29,14 @@ const DEFAULT_ID = "0";
const SETTINGS_VERSION = 3; const SETTINGS_VERSION = 3;
class Settings extends ChangeNotifier with SettingsSharedPref { class Settings extends ChangeNotifier with SettingsSharedPref {
Settings(this.id); Settings(this.id, this.pref);
@override @override
final String id; final String id;
@override
final SharedPreferences pref;
String customMetaData = ""; String customMetaData = "";
String defaultNewNoteFolderSpec = ""; String defaultNewNoteFolderSpec = "";
@ -60,88 +64,80 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
bool confirmDelete = true; bool confirmDelete = true;
bool hardWrap = false; bool hardWrap = false;
void load(SharedPreferences pref) { void load() {
defaultNewNoteFolderSpec = defaultNewNoteFolderSpec =
getString(pref, "defaultNewNoteFolderSpec") ?? defaultNewNoteFolderSpec; getString("defaultNewNoteFolderSpec") ?? defaultNewNoteFolderSpec;
journalEditordefaultNewNoteFolderSpec = journalEditordefaultNewNoteFolderSpec =
getString(pref, "journalEditordefaultNewNoteFolderSpec") ?? getString("journalEditordefaultNewNoteFolderSpec") ??
journalEditordefaultNewNoteFolderSpec; journalEditordefaultNewNoteFolderSpec;
journalEditorSingleNote = journalEditorSingleNote =
getBool(pref, "journalEditorSingleNote") ?? journalEditorSingleNote; getBool("journalEditorSingleNote") ?? journalEditorSingleNote;
remoteSyncFrequency = RemoteSyncFrequency.fromInternalString( remoteSyncFrequency = RemoteSyncFrequency.fromInternalString(
getString(pref, "remoteSyncFrequency")); getString("remoteSyncFrequency"));
markdownDefaultView = SettingsMarkdownDefaultView.fromInternalString( markdownDefaultView = SettingsMarkdownDefaultView.fromInternalString(
getString(pref, "markdownDefaultView")); getString("markdownDefaultView"));
markdownLastUsedView = SettingsMarkdownDefaultView.fromInternalString( markdownLastUsedView = SettingsMarkdownDefaultView.fromInternalString(
getString(pref, "markdownLastUsedView")); getString("markdownLastUsedView"));
if (markdownLastUsedView == SettingsMarkdownDefaultView.LastUsed) { if (markdownLastUsedView == SettingsMarkdownDefaultView.LastUsed) {
markdownLastUsedView = SettingsMarkdownDefaultView.Edit; markdownLastUsedView = SettingsMarkdownDefaultView.Edit;
} }
version = getInt(pref, "settingsVersion") ?? version; version = getInt("settingsVersion") ?? version;
emojiParser = getBool(pref, "emojiParser") ?? emojiParser; emojiParser = getBool("emojiParser") ?? emojiParser;
homeScreen = homeScreen = SettingsHomeScreen.fromInternalString(getString("homeScreen"));
SettingsHomeScreen.fromInternalString(getString(pref, "homeScreen")); theme = SettingsTheme.fromInternalString(getString("theme"));
theme = SettingsTheme.fromInternalString(getString(pref, "theme"));
zenMode = getBool(pref, "zenMode") ?? zenMode; zenMode = getBool("zenMode") ?? zenMode;
swipeToDelete = getBool(pref, "swipeToDelete") ?? swipeToDelete; swipeToDelete = getBool("swipeToDelete") ?? swipeToDelete;
// From AppState // From AppState
bottomMenuBar = getBool(pref, "bottomMenuBar") ?? bottomMenuBar; bottomMenuBar = getBool("bottomMenuBar") ?? bottomMenuBar;
confirmDelete = getBool(pref, "confirmDelete") ?? confirmDelete; confirmDelete = getBool("confirmDelete") ?? confirmDelete;
hardWrap = getBool(pref, "hardWrap") ?? hardWrap; hardWrap = getBool("hardWrap") ?? hardWrap;
} }
Future<void> save() async { Future<void> save() async {
var pref = await SharedPreferences.getInstance(); var pref = await SharedPreferences.getInstance();
var defaultSet = Settings(id); var def = Settings(id, pref);
await setString(pref, "defaultNewNoteFolderSpec", defaultNewNoteFolderSpec, await setString("defaultNewNoteFolderSpec", defaultNewNoteFolderSpec,
defaultSet.defaultNewNoteFolderSpec); def.defaultNewNoteFolderSpec);
await setString( await setString(
pref,
"journalEditordefaultNewNoteFolderSpec", "journalEditordefaultNewNoteFolderSpec",
journalEditordefaultNewNoteFolderSpec, journalEditordefaultNewNoteFolderSpec,
defaultSet.journalEditordefaultNewNoteFolderSpec); def.journalEditordefaultNewNoteFolderSpec);
await setBool(pref, "journalEditorSingleNote", journalEditorSingleNote, await setBool("journalEditorSingleNote", journalEditorSingleNote,
defaultSet.journalEditorSingleNote); def.journalEditorSingleNote);
await setString( await setString(
pref,
"remoteSyncFrequency", "remoteSyncFrequency",
remoteSyncFrequency.toInternalString(), remoteSyncFrequency.toInternalString(),
defaultSet.remoteSyncFrequency.toInternalString()); def.remoteSyncFrequency.toInternalString());
await setString( await setString(
pref,
"markdownDefaultView", "markdownDefaultView",
markdownDefaultView.toInternalString(), markdownDefaultView.toInternalString(),
defaultSet.markdownDefaultView.toInternalString()); def.markdownDefaultView.toInternalString());
await setString( await setString(
pref,
"markdownLastUsedView", "markdownLastUsedView",
markdownLastUsedView.toInternalString(), markdownLastUsedView.toInternalString(),
defaultSet.markdownLastUsedView.toInternalString()); def.markdownLastUsedView.toInternalString());
await setBool(pref, "emojiParser", emojiParser, defaultSet.emojiParser); await setBool("emojiParser", emojiParser, def.emojiParser);
await setString(pref, "homeScreen", homeScreen.toInternalString(), await setString("homeScreen", homeScreen.toInternalString(),
defaultSet.homeScreen.toInternalString()); def.homeScreen.toInternalString());
await setString(pref, "theme", theme.toInternalString(), await setString(
defaultSet.theme.toInternalString()); "theme", theme.toInternalString(), def.theme.toInternalString());
await setBool(pref, "zenMode", zenMode, defaultSet.zenMode); await setBool("zenMode", zenMode, def.zenMode);
await setBool( await setBool("swipeToDelete", swipeToDelete, def.swipeToDelete);
pref, "swipeToDelete", swipeToDelete, defaultSet.swipeToDelete); await setBool("bottomMenuBar", bottomMenuBar, def.bottomMenuBar);
await setBool( await setBool("confirmDelete", confirmDelete, def.confirmDelete);
pref, "bottomMenuBar", bottomMenuBar, defaultSet.bottomMenuBar);
await setBool(
pref, "confirmDelete", confirmDelete, defaultSet.confirmDelete);
await setInt(pref, "settingsVersion", version, defaultSet.version); await setInt("settingsVersion", version, def.version);
await setBool(pref, "hardWrap", hardWrap, defaultSet.hardWrap); await setBool("hardWrap", hardWrap, def.hardWrap);
notifyListeners(); notifyListeners();
} }

View File

@ -7,42 +7,40 @@ abstract class SettingsSharedPref {
String get id; String get id;
@protected @protected
String? getString(SharedPreferences pref, String key) { SharedPreferences get pref;
@protected
String? getString(String key) {
return pref.getString(id + '_' + key); return pref.getString(id + '_' + key);
} }
@protected @protected
bool? getBool(SharedPreferences pref, String key) { bool? getBool(String key) {
return pref.getBool(id + '_' + key); return pref.getBool(id + '_' + key);
} }
@protected @protected
List<String>? getStringList(SharedPreferences pref, String key) { List<String>? getStringList(String key) {
return pref.getStringList(id + '_' + key); return pref.getStringList(id + '_' + key);
} }
@protected @protected
Set<String>? getStringSet(SharedPreferences pref, String key) { Set<String>? getStringSet(String key) {
return getStringList(pref, key)?.toSet(); return getStringList(key)?.toSet();
} }
@protected @protected
int? getInt(SharedPreferences pref, String key) { int? getInt(String key) {
return pref.getInt(id + '_' + key); return pref.getInt(id + '_' + key);
} }
@protected @protected
double? getDouble(SharedPreferences pref, String key) { double? getDouble(String key) {
return pref.getDouble(id + '_' + key); return pref.getDouble(id + '_' + key);
} }
@protected @protected
Future<void> setString( Future<void> setString(String key, String value, String? defaultValue) async {
SharedPreferences pref,
String key,
String value,
String? defaultValue,
) async {
key = id + '_' + key; key = id + '_' + key;
if (value == defaultValue) { if (value == defaultValue) {
await pref.remove(key); await pref.remove(key);
@ -52,12 +50,7 @@ abstract class SettingsSharedPref {
} }
@protected @protected
Future<void> setBool( Future<void> setBool(String key, bool value, bool defaultValue) async {
SharedPreferences pref,
String key,
bool value,
bool defaultValue,
) async {
key = id + '_' + key; key = id + '_' + key;
if (value == defaultValue) { if (value == defaultValue) {
await pref.remove(key); await pref.remove(key);
@ -67,12 +60,7 @@ abstract class SettingsSharedPref {
} }
@protected @protected
Future<void> setInt( Future<void> setInt(String key, int value, int defaultValue) async {
SharedPreferences pref,
String key,
int value,
int defaultValue,
) async {
key = id + '_' + key; key = id + '_' + key;
if (value == defaultValue) { if (value == defaultValue) {
await pref.remove(key); await pref.remove(key);
@ -82,12 +70,7 @@ abstract class SettingsSharedPref {
} }
@protected @protected
Future<void> setDouble( Future<void> setDouble(String key, double value, double defaultValue) async {
SharedPreferences pref,
String key,
double value,
double defaultValue,
) async {
key = id + '_' + key; key = id + '_' + key;
if (value == defaultValue) { if (value == defaultValue) {
await pref.remove(key); await pref.remove(key);
@ -98,11 +81,7 @@ abstract class SettingsSharedPref {
@protected @protected
Future<void> setStringSet( Future<void> setStringSet(
SharedPreferences pref, String key, Set<String> value, Set<String> defaultValue) async {
String key,
Set<String> value,
Set<String> defaultValue,
) async {
key = id + '_' + key; key = id + '_' + key;
final bool Function(Set<dynamic>, Set<dynamic>) eq = final bool Function(Set<dynamic>, Set<dynamic>) eq =

View File

@ -11,30 +11,30 @@ import 'package:gitjournal/settings/settings_sharedpref.dart';
const FOLDER_NAME_KEY = "remoteGitRepoPath"; const FOLDER_NAME_KEY = "remoteGitRepoPath";
class StorageConfig extends ChangeNotifier with SettingsSharedPref { class StorageConfig extends ChangeNotifier with SettingsSharedPref {
StorageConfig(this.id); StorageConfig(this.id, this.pref);
@override @override
final String id; final String id;
@override
final SharedPreferences pref;
var folderName = "journal"; var folderName = "journal";
var storeInternally = true; var storeInternally = true;
var storageLocation = ""; var storageLocation = "";
void load(SharedPreferences pref) { void load() {
folderName = getString(pref, FOLDER_NAME_KEY) ?? folderName; folderName = getString(FOLDER_NAME_KEY) ?? folderName;
storeInternally = getBool(pref, "storeInternally") ?? storeInternally; storeInternally = getBool("storeInternally") ?? storeInternally;
storageLocation = getString(pref, "storageLocation") ?? ""; storageLocation = getString("storageLocation") ?? "";
} }
Future<void> save() async { Future<void> save() async {
var pref = await SharedPreferences.getInstance(); var def = StorageConfig(id, pref);
var defaultSet = StorageConfig(id);
await setString(pref, FOLDER_NAME_KEY, folderName, defaultSet.folderName); await setString(FOLDER_NAME_KEY, folderName, def.folderName);
await setBool( await setBool("storeInternally", storeInternally, def.storeInternally);
pref, "storeInternally", storeInternally, defaultSet.storeInternally); await setString("storageLocation", storageLocation, def.storageLocation);
await setString(
pref, "storageLocation", storageLocation, defaultSet.storageLocation);
notifyListeners(); notifyListeners();
} }

View File

@ -4,10 +4,10 @@ import 'package:easy_localization/easy_localization.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/core/note.dart';
import 'package:gitjournal/core/notes_folder.dart';
import 'package:gitjournal/core/virtual_notes_folder.dart'; import 'package:gitjournal/core/virtual_notes_folder.dart';
import 'package:gitjournal/folder_views/common.dart'; import 'package:gitjournal/folder_views/common.dart';
import 'package:gitjournal/folder_views/standard_view.dart'; import 'package:gitjournal/folder_views/standard_view.dart';
import 'package:gitjournal/settings/settings.dart';
import 'package:gitjournal/themes.dart'; import 'package:gitjournal/themes.dart';
class NoteSearchDelegate extends SearchDelegate<Note?> { class NoteSearchDelegate extends SearchDelegate<Note?> {
@ -86,8 +86,8 @@ class NoteSearchDelegate extends SearchDelegate<Note?> {
return note.body.toLowerCase().contains(q); return note.body.toLowerCase().contains(q);
}).toList(); }).toList();
var settings = Provider.of<Settings>(context); var folderConfig = Provider.of<NotesFolderConfig>(context);
var folder = VirtualNotesFolder(filteredNotes, settings); var folder = VirtualNotesFolder(filteredNotes, folderConfig);
var emptyText = tr('widgets.FolderView.searchFailed'); var emptyText = tr('widgets.FolderView.searchFailed');
return buildFolderView( return buildFolderView(

View File

@ -1,6 +1,7 @@
import 'dart:io'; import 'dart:io';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/checklist.dart'; import 'package:gitjournal/core/checklist.dart';
@ -11,9 +12,12 @@ import 'package:gitjournal/core/notes_folder_fs.dart';
void main() { void main() {
group('Note', () { group('Note', () {
late Directory tempDir; late Directory tempDir;
late NotesFolderConfig config;
setUpAll(() async { setUpAll(() async {
tempDir = await Directory.systemTemp.createTemp('__notes_test__'); tempDir = await Directory.systemTemp.createTemp('__notes_test__');
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
}); });
tearDownAll(() async { tearDownAll(() async {
@ -41,8 +45,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note.md"); var notePath = p.join(tempDir.path, "note.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -106,8 +109,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note2.md"); var notePath = p.join(tempDir.path, "note2.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -122,8 +124,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note3.md"); var notePath = p.join(tempDir.path, "note3.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -144,8 +145,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note13.md"); var notePath = p.join(tempDir.path, "note13.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -165,8 +165,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note4.md"); var notePath = p.join(tempDir.path, "note4.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -186,8 +185,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note4.md"); var notePath = p.join(tempDir.path, "note4.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -203,8 +201,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note449.md"); var notePath = p.join(tempDir.path, "note449.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -222,8 +219,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note448.md"); var notePath = p.join(tempDir.path, "note448.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -239,8 +235,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note448.md"); var notePath = p.join(tempDir.path, "note448.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -256,8 +251,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note449.md"); var notePath = p.join(tempDir.path, "note449.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -274,8 +268,7 @@ Booga Wooga
var notePath = p.join(tempDir.path, "note429.md"); var notePath = p.join(tempDir.path, "note429.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();

View File

@ -2,6 +2,7 @@ import 'dart:io';
import 'dart:math'; import 'dart:math';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/flattened_notes_folder.dart'; import 'package:gitjournal/core/flattened_notes_folder.dart';
@ -13,18 +14,20 @@ void main() {
group('Flattened Notes Folder Large Test', () { group('Flattened Notes Folder Large Test', () {
late Directory tempDir; late Directory tempDir;
late NotesFolderFS rootFolder; late NotesFolderFS rootFolder;
late NotesFolderConfig config;
setUp(() async { setUp(() async {
tempDir = await Directory.systemTemp.createTemp('__flat_folder_test__'); tempDir = await Directory.systemTemp.createTemp('__flat_folder_test__');
// print("TempDir: ${tempDir.path}"); SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
var random = Random(); var random = Random();
for (var i = 0; i < 300; i++) { for (var i = 0; i < 300; i++) {
// print("Building Note $i"); // print("Building Note $i");
await _writeRandomNote(random, tempDir.path); await _writeRandomNote(random, tempDir.path, config);
} }
rootFolder = NotesFolderFS(null, tempDir.path, NotesFolderConfig('')); rootFolder = NotesFolderFS(null, tempDir.path, config);
await rootFolder.loadRecursively(); await rootFolder.loadRecursively();
}); });
@ -38,7 +41,7 @@ void main() {
expect(f.notes.length, 300); expect(f.notes.length, 300);
var tempDir = await Directory.systemTemp.createTemp('_test_'); var tempDir = await Directory.systemTemp.createTemp('_test_');
await _writeRandomNote(Random(), tempDir.path); await _writeRandomNote(Random(), tempDir.path, config);
rootFolder.reset(tempDir.path); rootFolder.reset(tempDir.path);
await rootFolder.loadRecursively(); await rootFolder.loadRecursively();
@ -47,7 +50,8 @@ void main() {
}); });
} }
Future<void> _writeRandomNote(Random random, String dirPath) async { Future<void> _writeRandomNote(
Random random, String dirPath, NotesFolderConfig config) async {
String path; String path;
while (true) { while (true) {
path = p.join(dirPath, "${random.nextInt(10000)}.md"); path = p.join(dirPath, "${random.nextInt(10000)}.md");
@ -56,7 +60,7 @@ Future<void> _writeRandomNote(Random random, String dirPath) async {
} }
} }
var note = Note(NotesFolderFS(null, dirPath, NotesFolderConfig('')), path); var note = Note(NotesFolderFS(null, dirPath, config), path);
note.modified = DateTime(2014, 1, 1 + (random.nextInt(2000))); note.modified = DateTime(2014, 1, 1 + (random.nextInt(2000)));
note.body = "p1"; note.body = "p1";
await note.save(); await note.save();

View File

@ -2,6 +2,7 @@ import 'dart:io';
import 'dart:math'; import 'dart:math';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/flattened_notes_folder.dart'; import 'package:gitjournal/core/flattened_notes_folder.dart';
@ -26,11 +27,14 @@ void main() {
group('Flattened Notes Folder Test', () { group('Flattened Notes Folder Test', () {
late Directory tempDir; late Directory tempDir;
late NotesFolderFS rootFolder; late NotesFolderFS rootFolder;
late NotesFolderConfig config;
setUp(() async { setUp(() async {
tempDir = await Directory.systemTemp.createTemp('__sorted_folder_test__'); tempDir = await Directory.systemTemp.createTemp('__sorted_folder_test__');
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
rootFolder = NotesFolderFS(null, tempDir.path, NotesFolderConfig('')); rootFolder = NotesFolderFS(null, tempDir.path, config);
for (var i = 0; i < 3; i++) { for (var i = 0; i < 3; i++) {
var note = Note(rootFolder, _getRandomFilePath(rootFolder.folderPath)); var note = Note(rootFolder, _getRandomFilePath(rootFolder.folderPath));
@ -43,8 +47,8 @@ void main() {
Directory(p.join(tempDir.path, "sub1", "p1")).createSync(); Directory(p.join(tempDir.path, "sub1", "p1")).createSync();
Directory(p.join(tempDir.path, "sub2")).createSync(); Directory(p.join(tempDir.path, "sub2")).createSync();
var sub1Folder = NotesFolderFS( var sub1Folder =
rootFolder, p.join(tempDir.path, "sub1"), NotesFolderConfig('')); NotesFolderFS(rootFolder, p.join(tempDir.path, "sub1"), config);
for (var i = 0; i < 2; i++) { for (var i = 0; i < 2; i++) {
var note = Note( var note = Note(
sub1Folder, sub1Folder,
@ -55,8 +59,8 @@ void main() {
await note.save(); await note.save();
} }
var sub2Folder = NotesFolderFS( var sub2Folder =
rootFolder, p.join(tempDir.path, "sub2"), NotesFolderConfig('')); NotesFolderFS(rootFolder, p.join(tempDir.path, "sub2"), config);
for (var i = 0; i < 2; i++) { for (var i = 0; i < 2; i++) {
var note = Note( var note = Note(
sub2Folder, sub2Folder,
@ -67,8 +71,8 @@ void main() {
await note.save(); await note.save();
} }
var p1Folder = NotesFolderFS(sub1Folder, var p1Folder =
p.join(tempDir.path, "sub1", "p1"), NotesFolderConfig('')); NotesFolderFS(sub1Folder, p.join(tempDir.path, "sub1", "p1"), config);
for (var i = 0; i < 2; i++) { for (var i = 0; i < 2; i++) {
var note = Note( var note = Note(
p1Folder, p1Folder,

View File

@ -1,6 +1,7 @@
import 'dart:io'; import 'dart:io';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/link.dart'; import 'package:gitjournal/core/link.dart';
@ -11,11 +12,14 @@ import 'package:gitjournal/utils/link_resolver.dart';
void main() { void main() {
late Directory tempDir; late Directory tempDir;
late NotesFolderFS rootFolder; late NotesFolderFS rootFolder;
late NotesFolderConfig config;
setUpAll(() async { setUpAll(() async {
tempDir = await Directory.systemTemp.createTemp('__link_resolver__'); tempDir = await Directory.systemTemp.createTemp('__link_resolver__');
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
rootFolder = NotesFolderFS(null, tempDir.path, NotesFolderConfig('')); rootFolder = NotesFolderFS(null, tempDir.path, config);
await generateNote(tempDir.path, "Hello.md"); await generateNote(tempDir.path, "Hello.md");
await generateNote(tempDir.path, "Fire.md"); await generateNote(tempDir.path, "Fire.md");

View File

@ -1,5 +1,6 @@
import 'dart:collection'; import 'dart:collection';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/md_yaml_doc.dart'; import 'package:gitjournal/core/md_yaml_doc.dart';
@ -11,7 +12,14 @@ import 'package:gitjournal/settings/settings.dart';
void main() { void main() {
group('Note Serializer Test', () { group('Note Serializer Test', () {
var parent = NotesFolderFS(null, '/tmp', NotesFolderConfig('')); late NotesFolderConfig config;
late NotesFolderFS parent;
setUpAll(() async {
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
parent = NotesFolderFS(null, '/tmp', config);
});
test('Test emojis', () { test('Test emojis', () {
var props = LinkedHashMap<String, dynamic>.from( var props = LinkedHashMap<String, dynamic>.from(

View File

@ -2,6 +2,7 @@ import 'dart:collection';
import 'dart:io'; import 'dart:io';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/md_yaml_doc.dart'; import 'package:gitjournal/core/md_yaml_doc.dart';
@ -16,9 +17,12 @@ void main() {
late String n1Path; late String n1Path;
late String n2Path; late String n2Path;
late Directory tempDir; late Directory tempDir;
late NotesFolderConfig config;
setUpAll(() async { setUpAll(() async {
tempDir = await Directory.systemTemp.createTemp('__storage_test__'); tempDir = await Directory.systemTemp.createTemp('__storage_test__');
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
var dt = DateTime(2019, 12, 2, 5, 4, 2); var dt = DateTime(2019, 12, 2, 5, 4, 2);
// ignore: prefer_collection_literals // ignore: prefer_collection_literals
@ -28,7 +32,7 @@ void main() {
n1Path = p.join(tempDir.path, "1.md"); n1Path = p.join(tempDir.path, "1.md");
n2Path = p.join(tempDir.path, "2.md"); n2Path = p.join(tempDir.path, "2.md");
var parent = NotesFolderFS(null, tempDir.path, NotesFolderConfig('')); var parent = NotesFolderFS(null, tempDir.path, config);
var n1 = Note(parent, n1Path); var n1 = Note(parent, n1Path);
n1.body = "test\n"; n1.body = "test\n";
n1.created = dt; n1.created = dt;
@ -52,7 +56,7 @@ void main() {
expect(File(n2Path).existsSync(), isTrue); expect(File(n2Path).existsSync(), isTrue);
var loadedNotes = <Note>[]; var loadedNotes = <Note>[];
var parent = NotesFolderFS(null, tempDir.path, NotesFolderConfig('')); var parent = NotesFolderFS(null, tempDir.path, config);
await Future.forEach(notes, (Note origNote) async { await Future.forEach(notes, (Note origNote) async {
var note = Note(parent, origNote.filePath); var note = Note(parent, origNote.filePath);

View File

@ -1,6 +1,7 @@
import 'dart:io'; import 'dart:io';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/core/note.dart';
@ -10,9 +11,12 @@ import 'package:gitjournal/core/notes_folder_fs.dart';
void main() { void main() {
group('Note', () { group('Note', () {
late Directory tempDir; late Directory tempDir;
late NotesFolderConfig config;
setUpAll(() async { setUpAll(() async {
tempDir = await Directory.systemTemp.createTemp('__notes_test__'); tempDir = await Directory.systemTemp.createTemp('__notes_test__');
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
}); });
tearDownAll(() async { tearDownAll(() async {
@ -31,8 +35,7 @@ Hello
var notePath = p.join(tempDir.path, "note.md"); var notePath = p.join(tempDir.path, "note.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -64,8 +67,7 @@ Hello
var notePath = p.join(tempDir.path, "note.md"); var notePath = p.join(tempDir.path, "note.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -97,8 +99,7 @@ Hello
var notePath = p.join(tempDir.path, "note5.md"); var notePath = p.join(tempDir.path, "note5.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -137,8 +138,7 @@ bar: Foo
var notePath = p.join(tempDir.path, "note6.md"); var notePath = p.join(tempDir.path, "note6.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -159,8 +159,7 @@ bar: Foo
var notePath = p.join(tempDir.path, "note63.md"); var notePath = p.join(tempDir.path, "note63.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -186,8 +185,7 @@ Gee
var notePath = p.join(tempDir.path, "note16.md"); var notePath = p.join(tempDir.path, "note16.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();
@ -209,8 +207,7 @@ Gee
}); });
test('New Notes have a file extension', () async { test('New Notes have a file extension', () async {
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note.newNote(parentFolder); var note = Note.newNote(parentFolder);
var path = note.filePath; var path = note.filePath;
expect(path.endsWith('.md'), true); expect(path.endsWith('.md'), true);
@ -224,8 +221,7 @@ Gee
var txtNotePath = p.join(tempDir.path, "note163.txt"); var txtNotePath = p.join(tempDir.path, "note163.txt");
await File(txtNotePath).writeAsString(content); await File(txtNotePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var txtNote = Note(parentFolder, txtNotePath); var txtNote = Note(parentFolder, txtNotePath);
await txtNote.load(); await txtNote.load();
@ -248,8 +244,7 @@ Hello
var notePath = p.join(tempDir.path, "note.md"); var notePath = p.join(tempDir.path, "note.md");
await File(notePath).writeAsString(content); await File(notePath).writeAsString(content);
var parentFolder = var parentFolder = NotesFolderFS(null, tempDir.path, config);
NotesFolderFS(null, tempDir.path, NotesFolderConfig(''));
var note = Note(parentFolder, notePath); var note = Note(parentFolder, notePath);
await note.load(); await note.load();

View File

@ -1,6 +1,7 @@
import 'dart:io'; import 'dart:io';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/notes_cache.dart'; import 'package:gitjournal/core/notes_cache.dart';
@ -18,14 +19,17 @@ void main() {
'/base/d1/file.md', '/base/d1/file.md',
]; ];
late NotesCache cache; late NotesCache cache;
late NotesFolderConfig config;
setUp(() async { setUp(() async {
tempDir = await Directory.systemTemp.createTemp('__notes_test__'); tempDir = await Directory.systemTemp.createTemp('__notes_test__');
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
cacheFilePath = p.join(tempDir.path, "cache.raw"); cacheFilePath = p.join(tempDir.path, "cache.raw");
cache = NotesCache( cache = NotesCache(
filePath: cacheFilePath, filePath: cacheFilePath,
notesBasePath: '/base', notesBasePath: '/base',
folderConfig: NotesFolderConfig(''), folderConfig: config,
); );
}); });
@ -45,7 +49,7 @@ void main() {
test('Should create directory structure accurately', () async { test('Should create directory structure accurately', () async {
await cache.saveToDisk(fileList); await cache.saveToDisk(fileList);
var rootFolder = NotesFolderFS(null, '/base', NotesFolderConfig('')); var rootFolder = NotesFolderFS(null, '/base', config);
await cache.load(rootFolder); await cache.load(rootFolder);
expect(rootFolder.subFolders.length, 2); expect(rootFolder.subFolders.length, 2);

View File

@ -1,3 +1,4 @@
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/notes_folder_config.dart'; import 'package:gitjournal/core/notes_folder_config.dart';
@ -5,11 +6,13 @@ import 'package:gitjournal/core/notes_folder_fs.dart';
import 'package:gitjournal/core/processors/wiki_links_auto_add.dart'; import 'package:gitjournal/core/processors/wiki_links_auto_add.dart';
void main() { void main() {
test('Should process body', () { test('Should process body', () async {
var body = var body =
"GitJournal is the best? And it works quite well with Foam, Foam and Obsidian."; "GitJournal is the best? And it works quite well with Foam, Foam and Obsidian.";
var folder = NotesFolderFS(null, '/', NotesFolderConfig('')); SharedPreferences.setMockInitialValues({});
var config = NotesFolderConfig('', await SharedPreferences.getInstance());
var folder = NotesFolderFS(null, '/', config);
var p = WikiLinksAutoAddProcessor(folder); var p = WikiLinksAutoAddProcessor(folder);
var newBody = p.processBody(body, ['GitJournal', 'Foam', 'Obsidian']); var newBody = p.processBody(body, ['GitJournal', 'Foam', 'Obsidian']);
var expectedBody = var expectedBody =

View File

@ -2,6 +2,7 @@ import 'dart:io';
import 'dart:math'; import 'dart:math';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/core/note.dart';
@ -14,11 +15,14 @@ void main() {
group('Sorted Notes Folder Test', () { group('Sorted Notes Folder Test', () {
late Directory tempDir; late Directory tempDir;
late NotesFolderFS folder; late NotesFolderFS folder;
late NotesFolderConfig config;
setUp(() async { setUp(() async {
tempDir = await Directory.systemTemp.createTemp('__sorted_folder_test__'); tempDir = await Directory.systemTemp.createTemp('__sorted_folder_test__');
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
folder = NotesFolderFS(null, tempDir.path, NotesFolderConfig('')); folder = NotesFolderFS(null, tempDir.path, config);
var random = Random(); var random = Random();
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
@ -122,7 +126,7 @@ void main() {
}); });
test('If still sorted while loading the notes', () async { test('If still sorted while loading the notes', () async {
var folder = NotesFolderFS(null, tempDir.path, NotesFolderConfig('')); var folder = NotesFolderFS(null, tempDir.path, config);
var sf = SortedNotesFolder( var sf = SortedNotesFolder(
folder: folder, folder: folder,
sortingMode: sortingMode:

View File

@ -1,3 +1,4 @@
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/core/note.dart';
@ -7,8 +8,15 @@ import 'package:gitjournal/core/sorting_mode.dart';
void main() { void main() {
group('Sorting Mode', () { group('Sorting Mode', () {
late NotesFolderConfig config;
setUpAll(() async {
SharedPreferences.setMockInitialValues({});
config = NotesFolderConfig('', await SharedPreferences.getInstance());
});
test('Created', () async { test('Created', () async {
var folder = NotesFolderFS(null, '/tmp/', NotesFolderConfig('')); var folder = NotesFolderFS(null, '/tmp/', config);
var n1 = Note(folder, '/tmp/1.md'); var n1 = Note(folder, '/tmp/1.md');
n1.created = DateTime(2020, 10, 01); n1.created = DateTime(2020, 10, 01);
@ -33,7 +41,7 @@ void main() {
}); });
test('Modified', () async { test('Modified', () async {
var folder = NotesFolderFS(null, '/tmp/', NotesFolderConfig('')); var folder = NotesFolderFS(null, '/tmp/', config);
var n1 = Note(folder, '/tmp/1.md'); var n1 = Note(folder, '/tmp/1.md');
n1.modified = DateTime(2020, 10, 01); n1.modified = DateTime(2020, 10, 01);
@ -58,7 +66,7 @@ void main() {
}); });
test('Title', () async { test('Title', () async {
var folder = NotesFolderFS(null, '/tmp/', NotesFolderConfig('')); var folder = NotesFolderFS(null, '/tmp/', config);
var n1 = Note(folder, '/tmp/1.md'); var n1 = Note(folder, '/tmp/1.md');
n1.title = "alpha"; n1.title = "alpha";