Add a lock between all git operations

This way in between a git add + git commit, no other git operation will
run. This rarely occurs but it was a posibility depending on the speed
of the users actions.
This commit is contained in:
Vishesh Handa
2020-03-21 00:44:14 +01:00
parent 86ae88bae0
commit eb7d7dc2e7

View File

@ -14,10 +14,13 @@ import 'package:gitjournal/settings.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_crashlytics/flutter_crashlytics.dart'; import 'package:flutter_crashlytics/flutter_crashlytics.dart';
import 'package:synchronized/synchronized.dart';
class StateContainer with ChangeNotifier { class StateContainer with ChangeNotifier {
final AppState appState; final AppState appState;
final _opLock = Lock();
// FIXME: The gitRepo should never be changed once it has been setup // FIXME: The gitRepo should never be changed once it has been setup
// We should always just be modifying the 'git remotes' // We should always just be modifying the 'git remotes'
// With that, the StateContainer can be a StatelessWidget // With that, the StateContainer can be a StatelessWidget
@ -118,6 +121,7 @@ class StateContainer with ChangeNotifier {
} }
void createFolder(NotesFolderFS parent, String folderName) async { void createFolder(NotesFolderFS parent, String folderName) async {
return _opLock.synchronized(() async {
var newFolderPath = p.join(parent.folderPath, folderName); var newFolderPath = p.join(parent.folderPath, folderName);
var newFolder = NotesFolderFS(parent, newFolderPath); var newFolder = NotesFolderFS(parent, newFolderPath);
newFolder.create(); newFolder.create();
@ -128,18 +132,22 @@ class StateContainer with ChangeNotifier {
_gitRepo.addFolder(newFolder).then((NoteRepoResult _) { _gitRepo.addFolder(newFolder).then((NoteRepoResult _) {
_syncNotes(); _syncNotes();
}); });
});
} }
void removeFolder(NotesFolderFS folder) { void removeFolder(NotesFolderFS folder) async {
return _opLock.synchronized(() async {
Fimber.d("Removing Folder: " + folder.folderPath); Fimber.d("Removing Folder: " + folder.folderPath);
folder.parentFS.removeFolder(folder); folder.parentFS.removeFolder(folder);
_gitRepo.removeFolder(folder.folderPath).then((NoteRepoResult _) { _gitRepo.removeFolder(folder.folderPath).then((NoteRepoResult _) {
_syncNotes(); _syncNotes();
}); });
});
} }
void renameFolder(NotesFolderFS folder, String newFolderName) { void renameFolder(NotesFolderFS folder, String newFolderName) async {
return _opLock.synchronized(() async {
var oldFolderPath = folder.folderPath; var oldFolderPath = folder.folderPath;
folder.rename(newFolderName); folder.rename(newFolderName);
@ -148,39 +156,47 @@ class StateContainer with ChangeNotifier {
.then((NoteRepoResult _) { .then((NoteRepoResult _) {
_syncNotes(); _syncNotes();
}); });
});
} }
void renameNote(Note note, String newFileName) { void renameNote(Note note, String newFileName) async {
return _opLock.synchronized(() async {
var oldNotePath = note.filePath; var oldNotePath = note.filePath;
note.rename(newFileName); note.rename(newFileName);
_gitRepo.renameNote(oldNotePath, note.filePath).then((NoteRepoResult _) { _gitRepo.renameNote(oldNotePath, note.filePath).then((NoteRepoResult _) {
_syncNotes(); _syncNotes();
}); });
});
} }
void moveNote(Note note, NotesFolderFS destFolder) { void moveNote(Note note, NotesFolderFS destFolder) async {
if (destFolder.folderPath == note.parent.folderPath) { if (destFolder.folderPath == note.parent.folderPath) {
return; return;
} }
return _opLock.synchronized(() async {
var oldNotePath = note.filePath; var oldNotePath = note.filePath;
note.move(destFolder); note.move(destFolder);
_gitRepo.moveNote(oldNotePath, note.filePath).then((NoteRepoResult _) { _gitRepo.moveNote(oldNotePath, note.filePath).then((NoteRepoResult _) {
_syncNotes(); _syncNotes();
}); });
});
} }
void addNote(Note note) { void addNote(Note note) async {
return _opLock.synchronized(() async {
Fimber.d("State Container addNote"); Fimber.d("State Container addNote");
note.parent.insert(0, note); note.parent.insert(0, note);
note.updateModified(); note.updateModified();
_gitRepo.addNote(note).then((NoteRepoResult _) { _gitRepo.addNote(note).then((NoteRepoResult _) {
_syncNotes(); _syncNotes();
}); });
});
} }
void removeNote(Note note) { void removeNote(Note note) async {
return _opLock.synchronized(() async {
// FIXME: What if the Note hasn't yet been saved? // FIXME: What if the Note hasn't yet been saved?
note.parent.remove(note); note.parent.remove(note);
_gitRepo.removeNote(note.filePath).then((NoteRepoResult _) async { _gitRepo.removeNote(note.filePath).then((NoteRepoResult _) async {
@ -191,21 +207,26 @@ class StateContainer with ChangeNotifier {
await Future.delayed(const Duration(seconds: 4)); await Future.delayed(const Duration(seconds: 4));
_syncNotes(); _syncNotes();
}); });
});
} }
void undoRemoveNote(Note note) { void undoRemoveNote(Note note) async {
return _opLock.synchronized(() async {
note.parent.insert(0, note); note.parent.insert(0, note);
_gitRepo.resetLastCommit().then((NoteRepoResult _) { _gitRepo.resetLastCommit().then((NoteRepoResult _) {
_syncNotes(); _syncNotes();
}); });
});
} }
void updateNote(Note note) { void updateNote(Note note) async {
return _opLock.synchronized(() async {
Fimber.d("State Container updateNote"); Fimber.d("State Container updateNote");
note.updateModified(); note.updateModified();
_gitRepo.updateNote(note).then((NoteRepoResult _) { _gitRepo.updateNote(note).then((NoteRepoResult _) {
_syncNotes(); _syncNotes();
}); });
});
} }
void completeGitHostSetup() { void completeGitHostSetup() {