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.
This commit is contained in:
Vishesh Handa
2020-01-07 23:54:44 +01:00
parent 28f626719b
commit 4be91f92f0
3 changed files with 45 additions and 10 deletions

View File

@ -35,15 +35,15 @@ class Note with ChangeNotifier implements Comparable<Note> {
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<Note> {
}
Future<NoteLoadState> 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<Note> {
// FIXME: What about error handling?
Future<void> 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<Note> {
// FIXME: What about error handling?
Future<void> 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<Note> {
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);

View File

@ -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;
}
}
}

View File

@ -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 = <NoteFileNameFormat>[
FromTitle,
Iso8601,
Iso8601WithTimeZone,
Iso8601WithTimeZoneWithoutColon,