Skip to content

Commit 4be91f9

Browse files
committed
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.
1 parent 28f6267 commit 4be91f9

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

lib/core/note.dart

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ class Note with ChangeNotifier implements Comparable<Note> {
3535

3636
Note.newNote(this.parent) {
3737
_created = DateTime.now();
38-
_filePath = p.join(parent.folderPath, getFileName(this));
3938
}
4039

4140
String get filePath {
41+
_filePath ??= p.join(parent.folderPath, getFileName(this));
4242
return _filePath;
4343
}
4444

4545
String get fileName {
46-
return p.basename(_filePath);
46+
return p.basename(filePath);
4747
}
4848

4949
DateTime get created {
@@ -104,11 +104,14 @@ class Note with ChangeNotifier implements Comparable<Note> {
104104
}
105105

106106
Future<NoteLoadState> load() async {
107+
assert(_filePath != null);
108+
assert(_filePath.isNotEmpty);
109+
107110
if (_loadState == NoteLoadState.Loading) {
108111
return _loadState;
109112
}
110113

111-
final file = File(filePath);
114+
final file = File(_filePath);
112115
if (_loadState == NoteLoadState.Loaded) {
113116
var fileLastModified = file.lastModifiedSync();
114117
if (fileLastModified == _fileLastModified) {
@@ -134,7 +137,7 @@ class Note with ChangeNotifier implements Comparable<Note> {
134137

135138
// FIXME: What about error handling?
136139
Future<void> save() async {
137-
assert(filePath != null);
140+
assert(_filePath != null);
138141
assert(data != null);
139142
assert(data.body != null);
140143
assert(data.props != null);
@@ -146,24 +149,26 @@ class Note with ChangeNotifier implements Comparable<Note> {
146149

147150
// FIXME: What about error handling?
148151
Future<void> remove() async {
149-
var file = File(filePath);
152+
assert(_filePath != null);
153+
154+
var file = File(_filePath);
150155
await file.delete();
151156
}
152157

153158
@override
154-
int get hashCode => filePath.hashCode;
159+
int get hashCode => _filePath.hashCode;
155160

156161
@override
157162
bool operator ==(Object other) =>
158163
identical(this, other) ||
159164
other is Note &&
160165
runtimeType == other.runtimeType &&
161-
filePath == other.filePath &&
166+
_filePath == other._filePath &&
162167
_data == other._data;
163168

164169
@override
165170
String toString() {
166-
return 'Note{filePath: $filePath, created: $created, modified: $modified, data: $_data}';
171+
return 'Note{filePath: $_filePath, created: $created, modified: $modified, data: $_data}';
167172
}
168173

169174
@override
@@ -175,7 +180,7 @@ class Note with ChangeNotifier implements Comparable<Note> {
175180
var dt = modified ?? created ?? _fileLastModified;
176181
var otherDt = other.modified ?? other.created ?? other._fileLastModified;
177182
if (dt == null || otherDt == null) {
178-
return filePath.compareTo(other.filePath);
183+
return _filePath.compareTo(other._filePath);
179184
}
180185

181186
return dt.compareTo(otherDt);

lib/core/note_fileName.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import 'dart:io';
2+
13
import 'package:gitjournal/core/note.dart';
24
import 'package:gitjournal/utils/datetime.dart';
35
import 'package:gitjournal/settings.dart';
6+
import 'package:path/path.dart' as p;
47

58
String getFileName(Note note) {
69
switch (Settings.instance.noteFileNameFormat) {
10+
case NoteFileNameFormat.FromTitle:
11+
if (note.title.isNotEmpty) {
12+
return buildTitleFileName(note.parent.folderPath, note.title);
13+
} else {
14+
return toIso8601WithTimezone(note.created) + ".md";
15+
}
16+
break;
717
case NoteFileNameFormat.Iso8601:
818
return toIso8601(note.created) + ".md";
919
case NoteFileNameFormat.Iso8601WithTimeZone:
@@ -14,3 +24,21 @@ String getFileName(Note note) {
1424

1525
return note.created.toString();
1626
}
27+
28+
String buildTitleFileName(String parentDir, String title) {
29+
var fileName = title + ".md";
30+
var fullPath = p.join(parentDir, fileName);
31+
var file = File(fullPath);
32+
if (!file.existsSync()) {
33+
return fileName;
34+
}
35+
36+
for (var i = 1;; i++) {
37+
var fileName = title + "_$i.md";
38+
var fullPath = p.join(parentDir, fileName);
39+
var file = File(fullPath);
40+
if (!file.existsSync()) {
41+
return fileName;
42+
}
43+
}
44+
}

lib/settings.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,12 @@ class NoteFileNameFormat {
143143
static const Iso8601 = NoteFileNameFormat("Iso8601", "Iso8601");
144144
static const Iso8601WithTimeZoneWithoutColon = NoteFileNameFormat(
145145
"Iso8601WithTimeZoneWithoutColon", "ISO8601 without Colons");
146+
static const FromTitle = NoteFileNameFormat("FromTitle", "Title");
146147

147-
static const Default = Iso8601WithTimeZone;
148+
static const Default = FromTitle;
148149

149150
static const options = <NoteFileNameFormat>[
151+
FromTitle,
150152
Iso8601,
151153
Iso8601WithTimeZone,
152154
Iso8601WithTimeZoneWithoutColon,

0 commit comments

Comments
 (0)