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.
This commit is contained in:
Vishesh Handa
2020-02-18 22:26:09 +01:00
parent 27ebd9686b
commit d137fb83cf
4 changed files with 41 additions and 9 deletions

View File

@ -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> {
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<Note> {
}
set created(DateTime dt) {
if (!canHaveMetadata) return;
_created = dt;
notifyListeners();
}
@ -64,6 +67,8 @@ class Note with ChangeNotifier implements Comparable<Note> {
}
set modified(DateTime dt) {
if (!canHaveMetadata) return;
_modified = dt;
notifyListeners();
}
@ -88,10 +93,16 @@ class Note with ChangeNotifier implements Comparable<Note> {
}
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;

View File

@ -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) {

View File

@ -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: <Widget>[
@ -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<String, dynamic> data)
: yamlHeader = MarkdownYAMLCodec.toYamlHeader(data);
NoteMetaDataExample(this.yamlHeader);
@override
Widget build(BuildContext context) {

View File

@ -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,