Allow notes to be stored inside a sub-folder in a git repo

This commit is contained in:
Vishesh Handa
2019-02-14 12:59:43 +01:00
parent 8345f1424e
commit 03e46ce8cd
7 changed files with 54 additions and 50 deletions

View File

@ -6,6 +6,9 @@ import 'package:flutter/services.dart';
const _platform = const MethodChannel('gitjournal.io/git');
///
/// This gives us the directory where all the git repos will be stored
///
Future<Directory> getGitBaseDirectory() async {
final String path = await _platform.invokeMethod('getBaseDirectory');
if (path == null) {
@ -14,6 +17,9 @@ Future<Directory> getGitBaseDirectory() async {
return Directory(path);
}
///
/// It will be clone in gitBaseDirectory/folderName
///
Future<void> gitClone(String cloneUrl, String folderName) async {
print("Going to git clone");
try {
@ -93,7 +99,7 @@ GitException createGitException(String msg) {
}
Future gitPull(String folderName) async {
print("Going to git pull");
print("Going to git pull: $folderName");
try {
await _platform.invokeMethod('gitPull', {
'folderName': folderName,

View File

@ -12,11 +12,14 @@ import 'package:path/path.dart' as p;
Future migrateGitRepo({
@required String gitBasePath,
@required String fromGitBasePath,
@required String toGitBasePath,
@required String toGitBaseFolder,
@required String toGitBaseSubFolder,
}) async {
print("migrateGitRepo " + fromGitBasePath + " " + toGitBasePath);
print(
"migrateGitRepo $fromGitBasePath $toGitBaseFolder / $toGitBaseSubFolder");
var fromBasePath = p.join(gitBasePath, fromGitBasePath);
var toBasePath = p.join(gitBasePath, toGitBasePath);
var toGitRepoPath = p.join(gitBasePath, toGitBaseFolder, toGitBaseSubFolder);
print("toGitRemotePath $toGitRepoPath");
final dir = Directory(fromBasePath);
var lister = dir.list(recursive: false);
@ -26,14 +29,14 @@ Future migrateGitRepo({
}
File file = fileEntity;
var fileName = p.basename(file.path);
var toPath = p.join(toBasePath, fileName);
var toPath = p.join(toGitRepoPath, fileName);
print("Migrating " + file.path + " --> " + toPath);
await file.copy(toPath);
await gitAdd(toGitBasePath, fileName);
await gitAdd(toGitBaseFolder, fileName);
await gitCommit(
gitFolder: toGitBasePath,
gitFolder: toGitBaseFolder,
authorEmail: Settings.instance.gitAuthorEmail,
authorName: Settings.instance.gitAuthor,
message: "Migrated Journal Entry",

View File

@ -6,7 +6,8 @@ class AppState {
bool localGitRepoConfigured = false;
// FIXME: Rename from 'path' to folderName
String remoteGitRepoPath = "";
String remoteGitRepoFolderName = "";
String remoteGitRepoSubFolder = "";
bool remoteGitRepoConfigured = false;
bool hasJournalEntries = false;

View File

@ -37,7 +37,8 @@ Future runJournalApp() async {
var remoteGitRepoConfigured =
pref.getBool("remoteGitRepoConfigured") ?? false;
var localGitRepoPath = pref.getString("localGitRepoPath") ?? "";
var remoteGitRepoPath = pref.getString("remoteGitRepoPath") ?? "";
var remoteGitRepoFolderName = pref.getString("remoteGitRepoPath") ?? "";
var remoteGitRepoSubFolder = pref.getString("remoteGitRepoSubFolder") ?? "";
var onBoardingCompleted = pref.getBool("onBoardingCompleted") ?? false;
if (JournalApp.isInDebugMode) {
@ -65,7 +66,8 @@ Future runJournalApp() async {
localGitRepoConfigured: localGitRepoConfigured,
remoteGitRepoConfigured: remoteGitRepoConfigured,
localGitRepoPath: localGitRepoPath,
remoteGitRepoPath: remoteGitRepoPath,
remoteGitRepoFolderName: remoteGitRepoFolderName,
remoteGitRepoSubFolder: remoteGitRepoSubFolder,
gitBaseDirectory: dir.path,
onBoardingCompleted: onBoardingCompleted,
child: JournalApp(),

View File

@ -459,16 +459,17 @@ class GitHostSetupScreenState extends State<GitHostSetupScreen> {
}
void _finish() {
String subFolder = "";
_subFolderSelected(subFolder);
}
void _subFolderSelected(String folder) {
getAnalytics().logEvent(
name: "onboarding_complete",
parameters: <String, dynamic>{},
);
Navigator.pop(context);
this.widget.onCompletedFunction();
}
void _subFolderSelected(String folder) {
_finish();
this.widget.onCompletedFunction(folder);
}
}

View File

@ -17,7 +17,9 @@ class StateContainer extends StatefulWidget {
final bool localGitRepoConfigured;
final bool remoteGitRepoConfigured;
final String localGitRepoPath;
final String remoteGitRepoPath;
final String remoteGitRepoFolderName;
final String remoteGitRepoSubFolder;
final String gitBaseDirectory;
final bool onBoardingCompleted;
@ -25,7 +27,8 @@ class StateContainer extends StatefulWidget {
@required this.localGitRepoConfigured,
@required this.remoteGitRepoConfigured,
@required this.localGitRepoPath,
@required this.remoteGitRepoPath,
@required this.remoteGitRepoFolderName,
@required this.remoteGitRepoSubFolder,
@required this.gitBaseDirectory,
@required this.onBoardingCompleted,
@required this.child,
@ -43,7 +46,8 @@ class StateContainer extends StatefulWidget {
st.appState.localGitRepoConfigured = localGitRepoConfigured;
st.appState.remoteGitRepoConfigured = remoteGitRepoConfigured;
st.appState.localGitRepoPath = localGitRepoPath;
st.appState.remoteGitRepoPath = remoteGitRepoPath;
st.appState.remoteGitRepoFolderName = remoteGitRepoFolderName;
st.appState.remoteGitRepoSubFolder = remoteGitRepoSubFolder;
st.appState.gitBaseDirectory = gitBaseDirectory;
st.appState.onBoardingCompleted = onBoardingCompleted;
@ -53,7 +57,7 @@ class StateContainer extends StatefulWidget {
class StateContainerState extends State<StateContainer> {
AppState appState = AppState();
NoteRepository noteRepo;
GitNoteRepository noteRepo;
@override
void initState() {
@ -64,12 +68,14 @@ class StateContainerState extends State<StateContainer> {
if (appState.remoteGitRepoConfigured) {
noteRepo = GitNoteRepository(
baseDirectory: appState.gitBaseDirectory,
dirName: appState.remoteGitRepoPath,
dirName: appState.remoteGitRepoFolderName,
subDirName: appState.remoteGitRepoSubFolder,
);
} else if (appState.localGitRepoConfigured) {
noteRepo = GitNoteRepository(
baseDirectory: appState.gitBaseDirectory,
dirName: appState.localGitRepoPath,
subDirName: "",
);
}
@ -84,7 +90,7 @@ class StateContainerState extends State<StateContainer> {
void removeExistingRemoteClone() async {
var remoteGitDir = Directory(
p.join(appState.gitBaseDirectory, appState.remoteGitRepoPath));
p.join(appState.gitBaseDirectory, appState.remoteGitRepoFolderName));
var dotGitDir = Directory(p.join(remoteGitDir.path, ".git"));
bool exists = await dotGitDir.exists();
@ -173,6 +179,7 @@ class StateContainerState extends State<StateContainer> {
note.filePath = toIso8601WithTimezone(note.created) + '.md';
}
appState.notes.insert(index, note);
appState.hasJournalEntries = true;
noteRepo.addNote(note).then((NoteRepoResult _) {
_syncNotes();
});
@ -188,20 +195,23 @@ class StateContainerState extends State<StateContainer> {
});
}
void completeGitHostSetup() {
void completeGitHostSetup(String subFolder) {
setState(() async {
this.appState.remoteGitRepoConfigured = true;
this.appState.remoteGitRepoPath = "journal";
this.appState.remoteGitRepoFolderName = "journal";
this.appState.remoteGitRepoSubFolder = subFolder;
await migrateGitRepo(
fromGitBasePath: appState.localGitRepoPath,
toGitBasePath: appState.remoteGitRepoPath,
toGitBaseFolder: appState.remoteGitRepoFolderName,
toGitBaseSubFolder: appState.remoteGitRepoSubFolder,
gitBasePath: appState.gitBaseDirectory,
);
noteRepo = GitNoteRepository(
baseDirectory: appState.gitBaseDirectory,
dirName: appState.remoteGitRepoPath,
dirName: appState.remoteGitRepoFolderName,
subDirName: appState.remoteGitRepoSubFolder,
);
await _persistConfig();
@ -221,7 +231,9 @@ class StateContainerState extends State<StateContainer> {
var pref = await SharedPreferences.getInstance();
await pref.setBool(
"remoteGitRepoConfigured", appState.remoteGitRepoConfigured);
await pref.setString("remoteGitRepoPath", appState.remoteGitRepoPath);
await pref.setString("remoteGitRepoPath", appState.remoteGitRepoFolderName);
await pref.setString(
"remoteGitRepoSubFolder", appState.remoteGitRepoSubFolder);
await pref.setBool("onBoardingCompleted", appState.onBoardingCompleted);
}

View File

@ -12,16 +12,18 @@ import 'package:path/path.dart' as p;
class GitNoteRepository implements NoteRepository {
final FileStorage _fileStorage;
final String dirName;
final String subDirName;
bool cloned = false;
bool checkForCloned = false;
GitNoteRepository({
@required this.dirName,
@required this.subDirName,
@required String baseDirectory,
}) : _fileStorage = FileStorage(
noteSerializer: MarkdownYAMLSerializer(),
baseDirectory: p.join(baseDirectory, dirName),
baseDirectory: p.join(baseDirectory, dirName, subDirName),
);
@override
@ -83,29 +85,6 @@ class GitNoteRepository implements NoteRepository {
@override
Future<bool> sync() async {
/*
Disable git clone for now - The repo should have already been cloned!
if (gitCloneUrl == null || gitCloneUrl.isEmpty) {
print("Cannot sync because of lack of clone url");
return false;
}
if (!checkForCloned) {
var baseDir = Directory(_fileStorage.baseDirectory);
var dotGitDir = Directory(p.join(baseDir.path, ".git"));
cloned = await dotGitDir.exists();
checkForCloned = true;
}
// FIXME: If we are calling sync, it should always be cloned!
assert(cloned == true);
if (!cloned) {
await gitClone(this.gitCloneUrl, this.dirName);
cloned = true;
return true;
}
*/
try {
await gitPull(this.dirName);
} on GitException catch (ex) {