From e80fff575cafc1a2d479c0a941bc0e6abeac28c7 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Wed, 3 Feb 2021 11:02:19 +0100 Subject: [PATCH] Move Editor Heuristics to be self contained This way it can easily be used in other editors --- lib/editors/common.dart | 12 ++++++++++++ lib/editors/heuristics.dart | 15 +++++++++++++++ lib/editors/markdown_editor.dart | 21 +++++++++------------ lib/repository.dart | 18 ++++++++++++++++++ 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/lib/editors/common.dart b/lib/editors/common.dart index ab822693..8cfc3be4 100644 --- a/lib/editors/common.dart +++ b/lib/editors/common.dart @@ -32,6 +32,18 @@ class TextEditorState { int cursorPos; TextEditorState(this.text, this.cursorPos); + + TextEditorState.fromValue(TextEditingValue val) { + text = val.text; + cursorPos = val.selection.baseOffset; + } + + TextEditingValue toValue() { + return TextEditingValue( + text: text, + selection: TextSelection.collapsed(offset: cursorPos), + ); + } } class EditorAppBar extends StatelessWidget implements PreferredSizeWidget { diff --git a/lib/editors/heuristics.dart b/lib/editors/heuristics.dart index 76c91bd8..0d0ad454 100644 --- a/lib/editors/heuristics.dart +++ b/lib/editors/heuristics.dart @@ -69,3 +69,18 @@ TextEditorState autoAddBulletList( return TextEditorState(text, newCursorPos); } + +class EditorHeuristics { + EditorHeuristics({String text = ''}) { + _lastState = TextEditorState(text, 0); + } + + TextEditorState _lastState; + + TextEditorState textChanged(TextEditorState es) { + var lastState = _lastState; + _lastState = es; + + return autoAddBulletList(lastState.text, es.text, es.cursorPos); + } +} diff --git a/lib/editors/markdown_editor.dart b/lib/editors/markdown_editor.dart index 7f78aa3d..f515390a 100644 --- a/lib/editors/markdown_editor.dart +++ b/lib/editors/markdown_editor.dart @@ -63,20 +63,20 @@ class MarkdownEditorState extends State TextEditingController _textController = TextEditingController(); TextEditingController _titleTextController = TextEditingController(); - String _oldText; + EditorHeuristics _heuristics; bool _noteModified; MarkdownEditorState(this.note) { _textController = TextEditingController(text: note.body); _titleTextController = TextEditingController(text: note.title); - _oldText = note.body; } @override void initState() { super.initState(); _noteModified = widget.noteModified; + _heuristics = EditorHeuristics(text: note.body); } @override @@ -192,21 +192,18 @@ class MarkdownEditorState extends State void _applyHeuristics() { var selection = _textController.selection; + var editState = TextEditorState.fromValue(_textController.value); + + // vHanda: Why does this happen? if (selection.baseOffset != selection.extentOffset) { - _oldText = _textController.text; + _heuristics.textChanged(editState); return; } - var r = - autoAddBulletList(_oldText, _textController.text, selection.baseOffset); - _oldText = _textController.text; - - if (r == null) { - return; + var r = _heuristics.textChanged(editState); + if (r != null) { + _textController.value = r.toValue(); } - - _textController.text = r.text; - _textController.selection = TextSelection.collapsed(offset: r.cursorPos); } @override diff --git a/lib/repository.dart b/lib/repository.dart index 86c0efb3..a69b44a4 100644 --- a/lib/repository.dart +++ b/lib/repository.dart @@ -22,6 +22,7 @@ import 'package:gitjournal/error_reporting.dart'; import 'package:gitjournal/features.dart'; import 'package:gitjournal/settings.dart'; import 'package:gitjournal/settings_migrations.dart'; +import 'package:gitjournal/setup/clone.dart'; import 'package:gitjournal/utils/logger.dart'; enum SyncStatus { @@ -89,6 +90,23 @@ class Repository with ChangeNotifier { var repo = await GitRepository.load(repoPath); var remoteConfigured = repo.config.remotes.isNotEmpty; + if (remoteConfigured) { + // Code path for 'branch is null' exception + var branches = await repo.branches(); + if (branches.isEmpty) { + var remoteConfig = repo.config.remotes[0]; + await cloneRemote( + repoPath: repoPath, + remoteName: remoteConfig.name, + cloneUrl: remoteConfig.url, + authorName: settings.gitAuthor, + authorEmail: settings.gitAuthorEmail, + sshPublicKey: settings.sshPublicKey, + sshPrivateKey: settings.sshPrivateKey, + sshPassword: settings.sshPassword, + ); + } + } return Repository._internal( repoPath: repoPath,