Note: Rename 'fileName' to 'filePath'

A note can reside in a subfolder, so lets just use the path as an
identifier instead of using just the fileName.
This commit is contained in:
Vishesh Handa
2019-02-14 12:07:23 +01:00
parent 882c4eb024
commit 8345f1424e
6 changed files with 24 additions and 24 deletions

View File

@ -1,13 +1,13 @@
import 'package:journal/datetime_utils.dart';
class Note implements Comparable<Note> {
String fileName;
String filePath;
DateTime created;
String body;
Map<String, dynamic> extraProperties = Map<String, dynamic>();
Note({this.created, this.body, this.fileName, this.extraProperties}) {
Note({this.created, this.body, this.filePath, this.extraProperties}) {
if (created == null) {
created = DateTime(0, 0, 0, 0, 0, 0, 0, 0);
}
@ -17,10 +17,10 @@ class Note implements Comparable<Note> {
}
factory Note.fromJson(Map<String, dynamic> json) {
String fileName = "";
if (json.containsKey("fileName")) {
fileName = json["fileName"].toString();
json.remove("fileName");
String filePath = "";
if (json.containsKey("filePath")) {
filePath = json["filePath"].toString();
json.remove("filePath");
}
DateTime created;
@ -56,7 +56,7 @@ class Note implements Comparable<Note> {
}
return Note(
fileName: fileName,
filePath: filePath,
created: created,
body: body,
extraProperties: json,
@ -70,14 +70,14 @@ class Note implements Comparable<Note> {
json['created'] = createdStr;
}
json['body'] = body;
json['fileName'] = fileName;
json['filePath'] = filePath;
return json;
}
@override
int get hashCode =>
fileName.hashCode ^
filePath.hashCode ^
created.hashCode ^
body.hashCode ^
extraProperties.hashCode;
@ -87,7 +87,7 @@ class Note implements Comparable<Note> {
identical(this, other) ||
other is Note &&
runtimeType == other.runtimeType &&
fileName == other.fileName &&
filePath == other.filePath &&
body == other.body &&
created == other.created &&
_equalMaps(extraProperties, other.extraProperties);
@ -100,7 +100,7 @@ class Note implements Comparable<Note> {
@override
String toString() {
return 'Note{fileName: $fileName, body: $body, created: $created, extraProperties: $extraProperties}';
return 'Note{filePath: $filePath, body: $body, created: $created, extraProperties: $extraProperties}';
}
@override
@ -108,7 +108,7 @@ class Note implements Comparable<Note> {
if (other == null) {
return -1;
}
if (created == other.created) return fileName.compareTo(other.fileName);
if (created == other.created) return filePath.compareTo(other.filePath);
return created.compareTo(other.created);
}
}

View File

@ -169,8 +169,8 @@ class StateContainerState extends State<StateContainer> {
void insertNote(int index, Note note) {
print("State Container insertNote");
setState(() {
if (note.fileName == null || note.fileName.isEmpty) {
note.fileName = toIso8601WithTimezone(note.created) + '.md';
if (note.filePath == null || note.filePath.isEmpty) {
note.filePath = toIso8601WithTimezone(note.created) + '.md';
}
appState.notes.insert(index, note);
noteRepo.addNote(note).then((NoteRepoResult _) {

View File

@ -35,7 +35,7 @@ class FileStorage implements NoteRepository {
if (note == null) {
continue;
}
if (!note.fileName.toLowerCase().endsWith('.md')) {
if (!note.filePath.toLowerCase().endsWith('.md')) {
continue;
}
notes.add(note);
@ -54,13 +54,13 @@ class FileStorage implements NoteRepository {
final string = await file.readAsString();
var note = noteSerializer.decode(string);
note.fileName = p.basename(entity.path);
note.filePath = p.basename(entity.path);
return note;
}
@override
Future<NoteRepoResult> addNote(Note note) async {
var filePath = p.join(baseDirectory, note.fileName);
var filePath = p.join(baseDirectory, note.filePath);
print("FileStorage: Adding note in " + filePath);
var file = File(filePath);
@ -75,7 +75,7 @@ class FileStorage implements NoteRepository {
@override
Future<NoteRepoResult> removeNote(Note note) async {
var filePath = p.join(baseDirectory, note.fileName);
var filePath = p.join(baseDirectory, note.filePath);
var file = File(filePath);
await file.delete();
@ -97,7 +97,7 @@ class FileStorage implements NoteRepository {
final dir = Directory(baseDirectory);
for (var note in notes) {
var filePath = p.join(dir.path, note.fileName);
var filePath = p.join(dir.path, note.filePath);
var file = File(filePath);
var contents = noteSerializer.encode(note);

View File

@ -27,7 +27,7 @@ class JournalList extends StatelessWidget {
var note = notes[i];
return Dismissible(
key: Key(note.fileName),
key: Key(note.filePath),
child: _buildRow(context, note, i),
background: Container(color: Theme.of(context).accentColor),
onDismissed: (direction) {

View File

@ -14,8 +14,8 @@ DateTime nowWithoutMicro() {
void main() {
group('FileStorage', () {
var notes = [
Note(fileName: "1.md", body: "test", created: nowWithoutMicro()),
Note(fileName: "2.md", body: "test2", created: nowWithoutMicro()),
Note(filePath: "1.md", body: "test", created: nowWithoutMicro()),
Note(filePath: "2.md", body: "test2", created: nowWithoutMicro()),
];
Directory tempDir;

View File

@ -10,7 +10,7 @@ DateTime nowWithoutMicro() {
void main() {
group('Serializers', () {
var note = Note(
fileName: "2", body: "This is the body", created: nowWithoutMicro());
filePath: "2", body: "This is the body", created: nowWithoutMicro());
test('JSON Serializer', () {
var serializer = JsonNoteSerializer();
@ -26,7 +26,7 @@ void main() {
var note2 = serializer.decode(str);
// The YAML seriazlier loses the fileName by design
note2.fileName = note.fileName;
note2.filePath = note.filePath;
expect(note2, note);
});