Files
GitJournal/lib/core/note_fileName.dart
Vishesh Handa 4be91f92f0 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.
2020-01-07 23:54:44 +01:00

45 lines
1.3 KiB
Dart

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:
return toIso8601WithTimezone(note.created) + ".md";
case NoteFileNameFormat.Iso8601WithTimeZoneWithoutColon:
return toIso8601WithTimezone(note.created).replaceAll(":", "_") + ".md";
}
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;
}
}
}