From d137fb83cf95953f18a476f66d4b6c203f122f83 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 18 Feb 2020 22:26:09 +0100 Subject: [PATCH] Add experimental support for not saving the yaml header Fixes #19 This is experimental as it changes the whole concept of a Note - We have till now always assumed that a Note can have attached metadata. With this, there is no longer any metadata. So other parts of the application need to start reacting accordingly. For example the Mardkown Editor should not allow you to modify the title. This also makes it easier to implement '.txt' file support. --- lib/core/note.dart | 13 ++++++++++++- lib/core/note_fileName.dart | 12 +++++++----- lib/screens/settings_note_metadata.dart | 21 ++++++++++++++++++--- lib/settings.dart | 4 ++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/lib/core/note.dart b/lib/core/note.dart index 263b7e24..75ab8239 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:gitjournal/settings.dart'; import 'package:gitjournal/utils/markdown.dart'; import 'package:path/path.dart' as p; @@ -38,7 +39,7 @@ class Note with ChangeNotifier implements Comparable { Note(this.parent, this._filePath); Note.newNote(this.parent) { - _created = DateTime.now(); + created = DateTime.now(); } String get filePath { @@ -55,6 +56,8 @@ class Note with ChangeNotifier implements Comparable { } set created(DateTime dt) { + if (!canHaveMetadata) return; + _created = dt; notifyListeners(); } @@ -64,6 +67,8 @@ class Note with ChangeNotifier implements Comparable { } set modified(DateTime dt) { + if (!canHaveMetadata) return; + _modified = dt; notifyListeners(); } @@ -88,10 +93,16 @@ class Note with ChangeNotifier implements Comparable { } set title(String title) { + if (!canHaveMetadata) return; + _title = title; notifyListeners(); } + bool get canHaveMetadata { + return Settings.instance.yamlHeaderEnabled; + } + MdYamlDoc get data { noteSerializer.encode(this, _data); return _data; diff --git a/lib/core/note_fileName.dart b/lib/core/note_fileName.dart index a34b030d..8ec1743a 100644 --- a/lib/core/note_fileName.dart +++ b/lib/core/note_fileName.dart @@ -6,23 +6,25 @@ import 'package:gitjournal/settings.dart'; import 'package:path/path.dart' as p; String getFileName(Note note) { + var date = + note.created ?? note.modified ?? note.fileLastModified ?? DateTime.now(); switch (Settings.instance.noteFileNameFormat) { case NoteFileNameFormat.FromTitle: if (note.title.isNotEmpty) { return buildTitleFileName(note.parent.folderPath, note.title); } else { - return toIso8601WithTimezone(note.created) + ".md"; + return toIso8601WithTimezone(date) + ".md"; } break; case NoteFileNameFormat.Iso8601: - return toIso8601(note.created) + ".md"; + return toIso8601(date) + ".md"; case NoteFileNameFormat.Iso8601WithTimeZone: - return toIso8601WithTimezone(note.created) + ".md"; + return toIso8601WithTimezone(date) + ".md"; case NoteFileNameFormat.Iso8601WithTimeZoneWithoutColon: - return toIso8601WithTimezone(note.created).replaceAll(":", "_") + ".md"; + return toIso8601WithTimezone(date).replaceAll(":", "_") + ".md"; } - return note.created.toString(); + return date.toString(); } String buildTitleFileName(String parentDir, String title) { diff --git a/lib/screens/settings_note_metadata.dart b/lib/screens/settings_note_metadata.dart index 57430df7..7b1d65fd 100644 --- a/lib/screens/settings_note_metadata.dart +++ b/lib/screens/settings_note_metadata.dart @@ -15,6 +15,11 @@ class _NoteMetadataSettingsScreenState @override Widget build(BuildContext context) { var textTheme = Theme.of(context).textTheme; + String yamlHeader = " \n"; + if (Settings.instance.yamlHeaderEnabled) { + var map = _buildMap(); + yamlHeader = MarkdownYAMLCodec.toYamlHeader(map).trim(); + } var body = Column( children: [ @@ -26,9 +31,19 @@ class _NoteMetadataSettingsScreenState ), ), const SizedBox(height: 16.0), - NoteMetaDataExample(_buildMap()), + NoteMetaDataExample(yamlHeader), const SizedBox(height: 16.0), const Divider(), + SwitchListTile( + title: const Text("Enable YAML Header"), + value: Settings.instance.yamlHeaderEnabled, + onChanged: (bool newVal) { + setState(() { + Settings.instance.yamlHeaderEnabled = newVal; + Settings.instance.save(); + }); + }, + ), ListPreference( title: "Modified Field", options: [ @@ -44,6 +59,7 @@ class _NoteMetadataSettingsScreenState Settings.instance.save(); }); }, + enabled: Settings.instance.yamlHeaderEnabled, ), ], ); @@ -75,8 +91,7 @@ class _NoteMetadataSettingsScreenState class NoteMetaDataExample extends StatelessWidget { final String yamlHeader; - NoteMetaDataExample(Map data) - : yamlHeader = MarkdownYAMLCodec.toYamlHeader(data); + NoteMetaDataExample(this.yamlHeader); @override Widget build(BuildContext context) { diff --git a/lib/settings.dart b/lib/settings.dart index 8d5c71a1..69ad7261 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -18,6 +18,7 @@ class Settings { bool collectCrashReports = true; String yamlModifiedKey = "modified"; + bool yamlHeaderEnabled = true; String defaultNewNoteFolder = "journal"; RemoteSyncFrequency remoteSyncFrequency = RemoteSyncFrequency.Default; @@ -38,6 +39,7 @@ class Settings { pref.getBool("collectCrashReports") ?? collectCrashReports; yamlModifiedKey = pref.getString("yamlModifiedKey") ?? yamlModifiedKey; + yamlHeaderEnabled = pref.getBool("yamlHeaderEnabled") ?? yamlHeaderEnabled; defaultNewNoteFolder = pref.getString("defaultNewNoteFolder") ?? defaultNewNoteFolder; @@ -59,6 +61,7 @@ class Settings { pref.setBool("collectUsageStatistics", collectUsageStatistics); pref.setBool("collectCrashReports", collectCrashReports); pref.setString("yamlModifiedKey", yamlModifiedKey); + pref.setBool("yamlHeaderEnabled", yamlHeaderEnabled); pref.setString("defaultNewNoteFolder", defaultNewNoteFolder); pref.setString( "remoteSyncFrequency", remoteSyncFrequency.toInternalString()); @@ -80,6 +83,7 @@ class Settings { "collectUsageStatistics": collectUsageStatistics, "collectCrashReports": collectCrashReports, "yamlModifiedKey": yamlModifiedKey, + "yamlHeaderEnabled": yamlHeaderEnabled, "defaultNewNoteFolder": defaultNewNoteFolder, "defaultEditor": defaultEditor.toInternalString(), "version": version,