diff --git a/lib/app.dart b/lib/app.dart index ccabb61e..bc256f20 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -49,7 +49,10 @@ class JournalApp extends StatelessWidget { appState: appState, child: ChangeNotifierProvider( child: JournalApp(), - create: (_) => appState.notesFolder, + create: (_) { + assert(appState.notesFolder != null); + return appState.notesFolder; + }, ), )); } diff --git a/lib/core/notes_folder.dart b/lib/core/notes_folder.dart index 75d8b3b3..fd039f7e 100644 --- a/lib/core/notes_folder.dart +++ b/lib/core/notes_folder.dart @@ -179,8 +179,17 @@ class NotesFolder with ChangeNotifier { void insert(int index, Note note) { assert(note.parent == this); + assert(index >= 0); note.addListener(_entityChanged); + if (_entities.isEmpty) { + var entity = NoteFSEntity(note: note); + _entities.add(entity); + _entityMap[note.filePath] = entity; + notifyListeners(); + return; + } + for (var i = 0; i < _entities.length; i++) { var e = _entities[i]; if (e is NotesFolder) continue; diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index 31714d77..e7af4112 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -1,14 +1,14 @@ import 'package:flutter/material.dart'; -import 'package:gitjournal/state_container.dart'; +import 'package:provider/provider.dart'; + +import 'package:gitjournal/core/notes_folder.dart'; import 'journal_listing.dart'; class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { - final container = StateContainer.of(context); - final appState = container.appState; - - return JournalListingScreen(notesFolder: appState.notesFolder); + final notesFolder = Provider.of<NotesFolder>(context); + return JournalListingScreen(notesFolder: notesFolder); } } diff --git a/lib/state_container.dart b/lib/state_container.dart index f02eddf9..0a0769c1 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -4,7 +4,6 @@ import 'dart:io'; import 'package:fimber/fimber.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; -import 'package:gitjournal/analytics.dart'; import 'package:gitjournal/apis/git_migration.dart'; import 'package:gitjournal/appstate.dart'; import 'package:gitjournal/core/note.dart'; @@ -68,8 +67,8 @@ class StateContainerState extends State<StateContainer> { removeExistingRemoteClone(); } - _loadNotesFromDisk(); - _syncNotes(); + _loadNotes(); + syncNotes(); } void removeExistingRemoteClone() async { @@ -84,32 +83,10 @@ class StateContainerState extends State<StateContainer> { } Future<void> _loadNotes() async { + // FIXME: We should report the notes that failed to load await appState.notesFolder.loadRecursively(); } - void _loadNotesFromDisk() { - Fimber.d("Loading Notes From Disk"); - _loadNotes().then((void _) { - setState(() { - getAnalytics().logEvent( - name: "notes_loaded", - ); - }); - }).catchError((err, stack) { - setState(() { - Fimber.d("Load Notes From Disk Error: " + err.toString()); - Fimber.d(stack.toString()); - - getAnalytics().logEvent( - name: "notes_loading_failed", - parameters: <String, dynamic>{ - 'error': err.toString(), - }, - ); - }); - }); - } - Future syncNotes() async { if (!appState.remoteGitRepoConfigured) { Fimber.d("Not syncing because RemoteRepo not configured"); @@ -117,37 +94,11 @@ class StateContainerState extends State<StateContainer> { } await _gitRepo.sync(); - - try { - await _loadNotes(); - setState(() { - // TODO: Inform exactly what notes have changed? - }); - } catch (err, stack) { - setState(() { - Fimber.d("Load Notes From Disk Error: " + err.toString()); - Fimber.d(stack.toString()); - }); - } + await _loadNotes(); return true; } - void _syncNotes() { - if (!appState.remoteGitRepoConfigured) { - Fimber.d("Not syncing because RemoteRepo not configured"); - return; - } - - Fimber.d("Starting to syncNotes"); - _gitRepo.sync().then((loaded) { - Fimber.d("NotesRepo Synced: " + loaded.toString()); - _loadNotesFromDisk(); - }).catchError((err) { - Fimber.d("NotesRepo Sync: " + err.toString()); - }); - } - void createFolder(NotesFolder parent, String folderName) async { var newFolderPath = p.join(parent.folderPath, folderName); var newFolder = NotesFolder(parent, newFolderPath); @@ -156,26 +107,19 @@ class StateContainerState extends State<StateContainer> { Fimber.d("Created New Folder: " + newFolderPath); parent.addFolder(newFolder); - setState(() { - // Update the git repo - _gitRepo.addFolder(newFolder).then((NoteRepoResult _) { - _syncNotes(); - }); + _gitRepo.addFolder(newFolder).then((NoteRepoResult _) { + syncNotes(); }); } void renameFolder(NotesFolder folder, String newFolderName) async { var oldFolderPath = folder.folderPath; + folder.rename(newFolderName); - setState(() { - folder.rename(newFolderName); - - // Update the git repo - _gitRepo - .renameFolder(oldFolderPath, folder.folderPath) - .then((NoteRepoResult _) { - _syncNotes(); - }); + _gitRepo + .renameFolder(oldFolderPath, folder.folderPath) + .then((NoteRepoResult _) { + syncNotes(); }); } @@ -184,51 +128,41 @@ class StateContainerState extends State<StateContainer> { } void removeNote(Note note) { - setState(() { - note.parent.remove(note); - _gitRepo.removeNote(note.filePath).then((NoteRepoResult _) async { - // FIXME: Is there a way of figuring this amount dynamically? - // The '4 seconds' is taken from snack_bar.dart -> _kSnackBarDisplayDuration - // We wait an aritfical amount of time, so that the user has a change to undo - // their delete operation, and that commit is not synced with the server, till then. - await Future.delayed(const Duration(seconds: 4)); - _syncNotes(); - }); + note.parent.remove(note); + _gitRepo.removeNote(note.filePath).then((NoteRepoResult _) async { + // FIXME: Is there a way of figuring this amount dynamically? + // The '4 seconds' is taken from snack_bar.dart -> _kSnackBarDisplayDuration + // We wait an aritfical amount of time, so that the user has a change to undo + // their delete operation, and that commit is not synced with the server, till then. + await Future.delayed(const Duration(seconds: 4)); + syncNotes(); }); } void undoRemoveNote(Note note, int index) { - setState(() { - note.parent.insert(index, note); - _gitRepo.resetLastCommit().then((NoteRepoResult _) { - _syncNotes(); - }); + note.parent.insert(index, note); + _gitRepo.resetLastCommit().then((NoteRepoResult _) { + syncNotes(); }); } void insertNote(int index, Note note) { Fimber.d("State Container insertNote " + index.toString()); - setState(() { - if (note.filePath == null || note.filePath.isEmpty) { - var parentPath = note.parent != null - ? note.parent.folderPath - : _gitRepo.notesBasePath; - note.filePath = p.join(parentPath, getFileName(note)); - } - note.parent.insert(index, note); - _gitRepo.addNote(note).then((NoteRepoResult _) { - _syncNotes(); - }); + if (note.filePath == null || note.filePath.isEmpty) { + var parentPath = + note.parent != null ? note.parent.folderPath : _gitRepo.notesBasePath; + note.filePath = p.join(parentPath, getFileName(note)); + } + note.parent.insert(index, note); + _gitRepo.addNote(note).then((NoteRepoResult _) { + syncNotes(); }); } void updateNote(Note note) { Fimber.d("State Container updateNote"); - setState(() { - // Update the git repo - _gitRepo.updateNote(note).then((NoteRepoResult _) { - _syncNotes(); - }); + _gitRepo.updateNote(note).then((NoteRepoResult _) { + syncNotes(); }); } @@ -247,11 +181,11 @@ class StateContainerState extends State<StateContainer> { baseDirectory: appState.gitBaseDirectory, dirName: appState.remoteGitRepoFolderName, ); - appState.notesFolder = NotesFolder(null, _gitRepo.notesBasePath); + appState.notesFolder.folderPath = _gitRepo.notesBasePath; await _persistConfig(); - _loadNotesFromDisk(); - _syncNotes(); + _loadNotes(); + syncNotes(); setState(() {}); }();