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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,16 +12,18 @@ import 'package:path/path.dart' as p;
class GitNoteRepository implements NoteRepository { class GitNoteRepository implements NoteRepository {
final FileStorage _fileStorage; final FileStorage _fileStorage;
final String dirName; final String dirName;
final String subDirName;
bool cloned = false; bool cloned = false;
bool checkForCloned = false; bool checkForCloned = false;
GitNoteRepository({ GitNoteRepository({
@required this.dirName, @required this.dirName,
@required this.subDirName,
@required String baseDirectory, @required String baseDirectory,
}) : _fileStorage = FileStorage( }) : _fileStorage = FileStorage(
noteSerializer: MarkdownYAMLSerializer(), noteSerializer: MarkdownYAMLSerializer(),
baseDirectory: p.join(baseDirectory, dirName), baseDirectory: p.join(baseDirectory, dirName, subDirName),
); );
@override @override
@ -83,29 +85,6 @@ class GitNoteRepository implements NoteRepository {
@override @override
Future<bool> sync() async { 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 { try {
await gitPull(this.dirName); await gitPull(this.dirName);
} on GitException catch (ex) { } on GitException catch (ex) {