From 186f6b0af3cb14bc48dcac7cf8347606d7e3bfb5 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 26 Sep 2019 17:22:52 +0200 Subject: [PATCH] Refactor: Remove FileStorage With this, the application compiles again. And note editing is working again. This is was the first of some small refactors in order to implement both async note loading and a support for directories. --- gj_common/gitjournal.c | 1 + lib/note.dart | 4 +- lib/state_container.dart | 2 +- lib/storage/file_storage.dart | 108 ---------------------------------- lib/storage/git_storage.dart | 61 ++++++++++--------- pubspec.yaml | 2 +- 6 files changed, 38 insertions(+), 140 deletions(-) delete mode 100644 lib/storage/file_storage.dart diff --git a/gj_common/gitjournal.c b/gj_common/gitjournal.c index 26f44dc9..b5800090 100644 --- a/gj_common/gitjournal.c +++ b/gj_common/gitjournal.c @@ -100,6 +100,7 @@ int gj_git_rm(const char *git_base_path, const char *pattern) char *paths[] = {(char *)pattern}; git_strarray pathspec = {paths, 1}; + gj_log_internal("Calling git rm with pathspec %s", pattern); err = git_index_remove_all(index, &pathspec, rm_match_cb, (void *)git_base_path); if (err < 0) goto cleanup; diff --git a/lib/note.dart b/lib/note.dart index b1645b96..2b5bb64f 100644 --- a/lib/note.dart +++ b/lib/note.dart @@ -25,8 +25,6 @@ class Note implements Comparable { created = created ?? DateTime(0, 0, 0, 0, 0, 0, 0, 0); props = props ?? {}; body = body ?? ""; - - assert(filePath != null); } bool hasValidDate() { @@ -80,6 +78,8 @@ class Note implements Comparable { // FIXME: What about error handling? Future save() async { + assert(filePath != null); + if (hasValidDate()) { props['created'] = toIso8601WithTimezone(created); } diff --git a/lib/state_container.dart b/lib/state_container.dart index 172a3cd3..b50e40f7 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -217,7 +217,7 @@ class StateContainerState extends State { Fimber.d("State Container insertNote"); setState(() { if (note.filePath == null || note.filePath.isEmpty) { - note.filePath = getFileName(note); + note.filePath = p.join(noteRepo.notesBasePath, getFileName(note)); } appState.notes.insert(index, note); appState.hasJournalEntries = true; diff --git a/lib/storage/file_storage.dart b/lib/storage/file_storage.dart deleted file mode 100644 index e163c474..00000000 --- a/lib/storage/file_storage.dart +++ /dev/null @@ -1,108 +0,0 @@ -import 'dart:async'; -import 'dart:io'; - -import 'package:fimber/fimber.dart'; -import 'package:flutter/foundation.dart'; -import 'package:journal/note.dart'; -import 'package:journal/storage/notes_repository.dart'; -import 'package:journal/storage/serializers.dart'; -import 'package:path/path.dart' as p; - -/// Each Note is saved in a different file -/// Each note must have a fileName which ends in a .md -class FileStorage implements NoteRepository { - final String baseDirectory; - final NoteSerializer noteSerializer; - - FileStorage({ - @required this.baseDirectory, - @required this.noteSerializer, - }) { - assert(baseDirectory != null); - assert(baseDirectory.isNotEmpty); - Fimber.d("FileStorage Directory: " + baseDirectory); - } - - @override - Future> listNotes() async { - final dir = Directory(baseDirectory); - - var notes = []; - var lister = dir.list(recursive: false); - await for (var fileEntity in lister) { - Note note = await _loadNote(fileEntity); - if (note == null) { - continue; - } - if (!note.filePath.toLowerCase().endsWith('.md')) { - continue; - } - notes.add(note); - } - - // Reverse sort - notes.sort((a, b) => b.compareTo(a)); - return notes; - } - - Future _loadNote(FileSystemEntity entity) async { - if (entity is! File) { - return null; - } - var file = entity as File; - final string = await file.readAsString(); - - var note = noteSerializer.decode(string); - note.filePath = p.basename(entity.path); - return note; - } - - @override - Future addNote(Note note) async { - var filePath = p.join(baseDirectory, note.filePath); - Fimber.d("FileStorage: Adding note in " + filePath); - - var file = File(filePath); - if (file == null) { - return NoteRepoResult(error: true); - } - var contents = noteSerializer.encode(note); - await file.writeAsString(contents); - - return NoteRepoResult(noteFilePath: filePath, error: false); - } - - @override - Future removeNote(Note note) async { - var filePath = p.join(baseDirectory, note.filePath); - - var file = File(filePath); - await file.delete(); - - return NoteRepoResult(noteFilePath: filePath, error: false); - } - - @override - Future updateNote(Note note) async { - return addNote(note); - } - - @override - Future sync() async { - return false; - } - - Future saveNotes(List notes) async { - final dir = Directory(baseDirectory); - - for (var note in notes) { - var filePath = p.join(dir.path, note.filePath); - - var file = File(filePath); - var contents = noteSerializer.encode(note); - await file.writeAsString(contents); - } - - return dir; - } -} diff --git a/lib/storage/git_storage.dart b/lib/storage/git_storage.dart index 50bb79d9..800a0ae9 100644 --- a/lib/storage/git_storage.dart +++ b/lib/storage/git_storage.dart @@ -1,37 +1,37 @@ import 'dart:async'; +import 'dart:io'; import 'package:fimber/fimber.dart'; import 'package:flutter/foundation.dart'; import 'package:journal/apis/git.dart'; import 'package:journal/note.dart'; import 'package:journal/settings.dart'; -import 'package:journal/storage/file_storage.dart'; import 'package:journal/storage/notes_repository.dart'; -import 'package:journal/storage/serializers.dart'; import 'package:path/path.dart' as p; class GitNoteRepository implements NoteRepository { - final FileStorage _fileStorage; final String dirName; final String subDirName; + final String baseDirectory; + String notesBasePath; final GitRepo _gitRepo; bool cloned = false; bool checkForCloned = false; + // vHanda: This no longer needs to be so complex. It will only ever take the baseDirectory + dirName + // The directory should already exist! GitNoteRepository({ @required this.dirName, @required this.subDirName, - @required String baseDirectory, - }) : _fileStorage = FileStorage( - noteSerializer: MarkdownYAMLSerializer(), - baseDirectory: p.join(baseDirectory, dirName, subDirName), - ), - _gitRepo = GitRepo( + @required this.baseDirectory, + }) : _gitRepo = GitRepo( folderName: dirName, authorEmail: Settings.instance.gitAuthorEmail, authorName: Settings.instance.gitAuthor, - ); + ) { + notesBasePath = p.join(baseDirectory, dirName, subDirName); + } @override Future addNote(Note note) async { @@ -39,36 +39,27 @@ class GitNoteRepository implements NoteRepository { } Future _addNote(Note note, String commitMessage) async { - var result = await _fileStorage.addNote(note); - if (result.error) { - return result; - } - + await note.save(); await _gitRepo.add("."); await _gitRepo.commit( message: commitMessage, ); - return result; + return NoteRepoResult(noteFilePath: note.filePath, error: false); } @override Future removeNote(Note note) async { - var result = await _fileStorage.addNote(note); - if (result.error) { - return result; - } + var gitDir = p.join(baseDirectory, dirName); + var pathSpec = note.filePath.replaceFirst(gitDir, "").substring(1); - // FIXME: '/' is not valid on all platforms - var baseDir = _fileStorage.baseDirectory; - var filePath = result.noteFilePath.replaceFirst(baseDir + "/", ""); - - await _gitRepo.rm(filePath); + // We are not calling note.remove() as gitRm will also remove the file + await _gitRepo.rm(pathSpec); await _gitRepo.commit( message: "Removed Journal entry", ); - return result; + return NoteRepoResult(noteFilePath: note.filePath, error: false); } Future resetLastCommit() async { @@ -82,8 +73,22 @@ class GitNoteRepository implements NoteRepository { } @override - Future> listNotes() { - return _fileStorage.listNotes(); + Future> listNotes() async { + final dir = Directory(notesBasePath); + + var notes = []; + var lister = dir.list(recursive: false); + await for (var fileEntity in lister) { + var note = Note(filePath: fileEntity.path); + if (!note.filePath.toLowerCase().endsWith('.md')) { + continue; + } + notes.add(note); + } + + // Reverse sort + notes.sort((a, b) => b.compareTo(a)); + return notes; } @override diff --git a/pubspec.yaml b/pubspec.yaml index efa359c8..a1aa5f1b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: journal description: A Journaling App Built on top of Git -version: 1.1.12+10 +version: 1.1.13+10 dependencies: flutter: