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

View File

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

View File

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

View File

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

View File

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

View File

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