From 4be91f92f0015f262e8dd86cb2404c6f6f2ddb41 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 7 Jan 2020 23:54:44 +0100 Subject: [PATCH] Note FileName: Save them by default with the title If that title already exists add an _Num at the end. This won't change the default for existing users, as our settings system saves the default value and will just load it the next time. This really needs to be improved. --- lib/core/note.dart | 23 ++++++++++++++--------- lib/core/note_fileName.dart | 28 ++++++++++++++++++++++++++++ lib/settings.dart | 4 +++- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/core/note.dart b/lib/core/note.dart index bb4540ec..76f50500 100644 --- a/lib/core/note.dart +++ b/lib/core/note.dart @@ -35,15 +35,15 @@ class Note with ChangeNotifier implements Comparable { Note.newNote(this.parent) { _created = DateTime.now(); - _filePath = p.join(parent.folderPath, getFileName(this)); } String get filePath { + _filePath ??= p.join(parent.folderPath, getFileName(this)); return _filePath; } String get fileName { - return p.basename(_filePath); + return p.basename(filePath); } DateTime get created { @@ -104,11 +104,14 @@ class Note with ChangeNotifier implements Comparable { } Future load() async { + assert(_filePath != null); + assert(_filePath.isNotEmpty); + if (_loadState == NoteLoadState.Loading) { return _loadState; } - final file = File(filePath); + final file = File(_filePath); if (_loadState == NoteLoadState.Loaded) { var fileLastModified = file.lastModifiedSync(); if (fileLastModified == _fileLastModified) { @@ -134,7 +137,7 @@ class Note with ChangeNotifier implements Comparable { // FIXME: What about error handling? Future save() async { - assert(filePath != null); + assert(_filePath != null); assert(data != null); assert(data.body != null); assert(data.props != null); @@ -146,24 +149,26 @@ class Note with ChangeNotifier implements Comparable { // FIXME: What about error handling? Future remove() async { - var file = File(filePath); + assert(_filePath != null); + + var file = File(_filePath); await file.delete(); } @override - int get hashCode => filePath.hashCode; + int get hashCode => _filePath.hashCode; @override bool operator ==(Object other) => identical(this, other) || other is Note && runtimeType == other.runtimeType && - filePath == other.filePath && + _filePath == other._filePath && _data == other._data; @override String toString() { - return 'Note{filePath: $filePath, created: $created, modified: $modified, data: $_data}'; + return 'Note{filePath: $_filePath, created: $created, modified: $modified, data: $_data}'; } @override @@ -175,7 +180,7 @@ class Note with ChangeNotifier implements Comparable { var dt = modified ?? created ?? _fileLastModified; var otherDt = other.modified ?? other.created ?? other._fileLastModified; if (dt == null || otherDt == null) { - return filePath.compareTo(other.filePath); + return _filePath.compareTo(other._filePath); } return dt.compareTo(otherDt); diff --git a/lib/core/note_fileName.dart b/lib/core/note_fileName.dart index 65e14e96..a34b030d 100644 --- a/lib/core/note_fileName.dart +++ b/lib/core/note_fileName.dart @@ -1,9 +1,19 @@ +import 'dart:io'; + import 'package:gitjournal/core/note.dart'; import 'package:gitjournal/utils/datetime.dart'; import 'package:gitjournal/settings.dart'; +import 'package:path/path.dart' as p; String getFileName(Note note) { switch (Settings.instance.noteFileNameFormat) { + case NoteFileNameFormat.FromTitle: + if (note.title.isNotEmpty) { + return buildTitleFileName(note.parent.folderPath, note.title); + } else { + return toIso8601WithTimezone(note.created) + ".md"; + } + break; case NoteFileNameFormat.Iso8601: return toIso8601(note.created) + ".md"; case NoteFileNameFormat.Iso8601WithTimeZone: @@ -14,3 +24,21 @@ String getFileName(Note note) { return note.created.toString(); } + +String buildTitleFileName(String parentDir, String title) { + var fileName = title + ".md"; + var fullPath = p.join(parentDir, fileName); + var file = File(fullPath); + if (!file.existsSync()) { + return fileName; + } + + for (var i = 1;; i++) { + var fileName = title + "_$i.md"; + var fullPath = p.join(parentDir, fileName); + var file = File(fullPath); + if (!file.existsSync()) { + return fileName; + } + } +} diff --git a/lib/settings.dart b/lib/settings.dart index c2b821f7..c01e8cac 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -143,10 +143,12 @@ class NoteFileNameFormat { static const Iso8601 = NoteFileNameFormat("Iso8601", "Iso8601"); static const Iso8601WithTimeZoneWithoutColon = NoteFileNameFormat( "Iso8601WithTimeZoneWithoutColon", "ISO8601 without Colons"); + static const FromTitle = NoteFileNameFormat("FromTitle", "Title"); - static const Default = Iso8601WithTimeZone; + static const Default = FromTitle; static const options = [ + FromTitle, Iso8601, Iso8601WithTimeZone, Iso8601WithTimeZoneWithoutColon,