From d31628e14609fa5ba5b860179c60f431dfdd0596 Mon Sep 17 00:00:00 2001 From: Harshad Vedartham Date: Thu, 26 Dec 2024 14:50:38 +0530 Subject: [PATCH] Restored option, fixed duplicate calls to loadFolder on resume --- .../markor/activity/MainActivity.java | 3 +- .../markor/format/ActionButtonBase.java | 2 +- .../wikitext/WikitextActionButtons.java | 2 +- .../frontend/textview/AutoTextFormatter.java | 8 ++-- .../gsantner/markor/model/AppSettings.java | 4 ++ .../filebrowser/GsFileBrowserFragment.java | 17 ++++----- .../filebrowser/GsFileBrowserListAdapter.java | 37 ++++++++----------- .../res/values/string-not_translatable.xml | 1 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/preferences_master.xml | 5 +++ 10 files changed, 42 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/net/gsantner/markor/activity/MainActivity.java b/app/src/main/java/net/gsantner/markor/activity/MainActivity.java index d15b39fc60..915a6bfbce 100644 --- a/app/src/main/java/net/gsantner/markor/activity/MainActivity.java +++ b/app/src/main/java/net/gsantner/markor/activity/MainActivity.java @@ -193,10 +193,11 @@ protected void onNewIntent(final Intent intent) { if (_notebook != null && file != null) { _viewPager.setCurrentItem(tabIdToPos(R.id.nav_notebook), false); if (file.isDirectory() || GsFileBrowserListAdapter.isVirtualFolder(file)) { - _notebook.setCurrentFolder(file); + _notebook.getAdapter().setCurrentFolder(file); } else { _notebook.getAdapter().showFile(file); } + _notebook.setReloadRequiredOnResume(false); } } diff --git a/app/src/main/java/net/gsantner/markor/format/ActionButtonBase.java b/app/src/main/java/net/gsantner/markor/format/ActionButtonBase.java index 4591b3eac5..c9266a4a56 100644 --- a/app/src/main/java/net/gsantner/markor/format/ActionButtonBase.java +++ b/app/src/main/java/net/gsantner/markor/format/ActionButtonBase.java @@ -1015,7 +1015,7 @@ public void runJumpBottomTopAction(ActionItem.DisplayMode displayMode) { } public boolean onReceiveKeyPress(final int keyCode, final KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_TAB) { + if (keyCode == KeyEvent.KEYCODE_TAB && _appSettings.isIndentWithTabKey()) { runIndentLines(event.isShiftPressed()); runRenumberOrderedListIfRequired(); return true; diff --git a/app/src/main/java/net/gsantner/markor/format/wikitext/WikitextActionButtons.java b/app/src/main/java/net/gsantner/markor/format/wikitext/WikitextActionButtons.java index eb09028e7a..2416d42f38 100644 --- a/app/src/main/java/net/gsantner/markor/format/wikitext/WikitextActionButtons.java +++ b/app/src/main/java/net/gsantner/markor/format/wikitext/WikitextActionButtons.java @@ -275,7 +275,7 @@ protected void renumberOrderedList() { @Override public boolean onReceiveKeyPress(final int keyCode, final KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_TAB) { + if (keyCode == KeyEvent.KEYCODE_TAB && _appSettings.isIndentWithTabKey()) { if (event.isShiftPressed()) { runRegexReplaceAction(WikitextReplacePatternGenerator.deindentOneTab()); } else { diff --git a/app/src/main/java/net/gsantner/markor/frontend/textview/AutoTextFormatter.java b/app/src/main/java/net/gsantner/markor/frontend/textview/AutoTextFormatter.java index 2e81b4504f..a5413d08eb 100644 --- a/app/src/main/java/net/gsantner/markor/frontend/textview/AutoTextFormatter.java +++ b/app/src/main/java/net/gsantner/markor/frontend/textview/AutoTextFormatter.java @@ -44,7 +44,7 @@ private CharSequence autoIndent(final CharSequence source, final CharSequence de final OrderedListLine oLine = new OrderedListLine(dest, dstart, _patterns); final UnOrderedOrCheckListLine uLine = new UnOrderedOrCheckListLine(dest, dstart, _patterns); - final String indent = oLine.line.substring(0, oLine.indentEnd); + final String indent = source + oLine.line.substring(0, oLine.indentEnd); final String result; if (oLine.isOrderedList && oLine.lineEnd != oLine.groupEnd && dend >= oLine.groupEnd) { @@ -93,9 +93,9 @@ public ListLine(CharSequence text, int position, FormatPatterns patterns) { lineStart = TextViewUtils.getLineStart(text, position); lineEnd = TextViewUtils.getLineEnd(text, position); line = text.subSequence(lineStart, lineEnd).toString(); - isEmpty = line.trim().isEmpty(); - - indentEnd = isEmpty ? 0 : TextViewUtils.getFirstNonWhitespace(line); + final int firstChar = TextViewUtils.getFirstNonWhitespace(line); + isEmpty = firstChar < 0; + indentEnd = Math.max(firstChar, 0); final int[] counts = GsTextUtils.countChars(line, 0, indentEnd, ' ', '\t'); indent = counts[0] + counts[1] * 4; isTopLevel = indent <= patterns.indentSlack; diff --git a/app/src/main/java/net/gsantner/markor/model/AppSettings.java b/app/src/main/java/net/gsantner/markor/model/AppSettings.java index 7e10fd7399..237d735b5e 100644 --- a/app/src/main/java/net/gsantner/markor/model/AppSettings.java +++ b/app/src/main/java/net/gsantner/markor/model/AppSettings.java @@ -852,6 +852,10 @@ public synchronized boolean isExtOpenWithThisApp(String ext) { return _extSettingCache.contains(ext) || _extSettingCache.contains("*"); } + public boolean isIndentWithTabKey() { + return getBool(R.string.pref_key__editor_tab_to_indent, false); + } + public boolean isExperimentalFeaturesEnabled() { return getBool(R.string.pref_key__is_enable_experimental_features, BuildConfig.IS_TEST_BUILD); } diff --git a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java index c9a6f46ebc..69999f3c5c 100644 --- a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java +++ b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java @@ -91,6 +91,7 @@ public static GsFileBrowserFragment newInstance() { private Menu _fragmentMenu; private MarkorContextUtils _cu; private Toolbar _toolbar; + private boolean _reloadRequiredOnResume = true; //######################## //## Methods @@ -123,6 +124,7 @@ public void onViewCreated(@NonNull View root, @Nullable Bundle savedInstanceStat _filesystemViewerAdapter = new GsFileBrowserListAdapter(_dopt, context); _recyclerList.setAdapter(_filesystemViewerAdapter); + setReloadRequiredOnResume(false); // setAdapter will trigger a load onFsViewerDoUiUpdate(_filesystemViewerAdapter); _swipe.setOnRefreshListener(() -> { @@ -315,16 +317,20 @@ public void onViewStateRestored(final Bundle savedInstanceState) { _filesystemViewerAdapter.restoreSavedInstanceState(savedInstanceState); } + public void setReloadRequiredOnResume(boolean reloadRequiredOnResume) { + _reloadRequiredOnResume = reloadRequiredOnResume; + } + @Override public void onResume() { super.onResume(); _dopt.listener.onFsViewerConfig(_dopt); final File folder = getCurrentFolder(); final Activity activity = getActivity(); - if (isVisible() && folder != null && activity != null) { - activity.setTitle(folder.getName()); + if (_reloadRequiredOnResume && isVisible() && folder != null && activity != null) { reloadCurrentFolder(); } + _reloadRequiredOnResume = true; } @Override @@ -435,7 +441,6 @@ public boolean onOptionsItemSelected(final MenuItem item) { case R.id.action_go_to: { final File folder = new File("/storage"); _filesystemViewerAdapter.setCurrentFolder(folder); - Toast.makeText(getContext(), folder.getAbsolutePath(), Toast.LENGTH_SHORT).show(); return true; } case R.id.action_favourite: { @@ -632,12 +637,6 @@ private void importFileToCurrentDirectory(Context context, File sourceFile) { Toast.makeText(context, getString(R.string.import_) + ": " + sourceFile.getName(), Toast.LENGTH_LONG).show(); } - public void setCurrentFolder(final File folder) { - if (folder != null && (folder.canRead() || GsFileBrowserListAdapter.isVirtualFolder(folder)) && _filesystemViewerAdapter != null) { - _filesystemViewerAdapter.setCurrentFolder(folder); - } - } - public GsFileBrowserOptions.Options getOptions() { return _dopt; } diff --git a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java index 8cb5b9bc8f..6e3d7e51a9 100644 --- a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java +++ b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserListAdapter.java @@ -92,6 +92,7 @@ public class GsFileBrowserListAdapter extends RecyclerView.Adapter _currentSelection; private File _fileToShowAfterNextLoad; private File _currentFolder; + private File _goUpFile; private final Context _context; private final StringFilter _filter; private RecyclerView _recyclerView; @@ -200,7 +201,7 @@ public void onBindViewHolder(@NonNull FilesystemViewerViewHolder holder, int pos final File file = GsCollectionUtils.getOrDefault(_virtualMapping, displayFile, displayFile); - final boolean isGoUp = displayFile instanceof GoUpFile; + final boolean isGoUp = displayFile.equals(_goUpFile); final boolean isVirtual = _virtualMapping.containsKey(displayFile); final boolean isSelected = _currentSelection.contains(displayFile); final boolean isFavourite = _dopt.favouriteFiles != null && _dopt.favouriteFiles.contains(displayFile); @@ -418,7 +419,7 @@ public void onClick(View view) { } else { // No pre-selection if (data.file.isDirectory() || _virtualMapping.containsKey(data.file)) { - loadFolder(data.file, _currentFolder); + loadFolder(data.file, data.file.equals(_goUpFile) ? _currentFolder : null); } else if (data.file.isFile()) { _dopt.listener.onFsViewerSelected(_dopt.requestId, data.file, null); } @@ -496,7 +497,7 @@ public boolean toggleSelection(final TagContainer data) { } boolean clickHandled = false; - if (data.file != null && _currentFolder != null && !(data.file instanceof GoUpFile)) { + if (data.file != null && _currentFolder != null && !data.file.equals(_goUpFile)) { if (_currentSelection.contains(data.file)) { // Single selection _currentSelection.remove(data.file); @@ -593,13 +594,9 @@ public void showFile(final File file) { return; } - if (!_adapterDataFiltered.contains(file)) { - final File dir = file.getParentFile(); - if (dir != null) { - loadFolder(dir, file); - } - } else { - scrollToAndFlash(file); + final File dir = file.getParentFile(); + if (dir != null) { + loadFolder(dir, file); } } @@ -718,10 +715,7 @@ private synchronized void _loadFolder(final boolean folderChanged, final @Nullab final long modSum = GsCollectionUtils.accumulate(newData, (f, s) -> s + f.lastModified(), 0L); final boolean modSumChanged = modSum != _prevModSum; - final File parent = getCurrentParent(); - if (parent != null) { - newData.add(0, new GoUpFile(parent)); - } + final File goUp = getCurrentParent(); if (folderChanged || modSumChanged || !newData.equals(_adapterData)) { final ArrayList filteredData = new ArrayList<>(); @@ -729,11 +723,16 @@ private synchronized void _loadFolder(final boolean folderChanged, final @Nullab _recyclerView.post(() -> { // Modify all these values in the UI thread + _goUpFile = goUp; _adapterData.clear(); - _adapterData.addAll(newData); _adapterDataFiltered.clear(); + if (_goUpFile != null) { + _adapterData.add(_goUpFile); + _adapterDataFiltered.add(_goUpFile); + } + _adapterData.addAll(newData); _adapterDataFiltered.addAll(filteredData); - _currentSelection.retainAll(_adapterData); + _currentSelection.retainAll(_adapterDataFiltered); _prevModSum = modSum; if (folderChanged) { @@ -889,10 +888,4 @@ private int getUserId() { return 0; } } - - private static class GoUpFile extends File { - GoUpFile(final File f) { - super(f.getPath()); - } - } } diff --git a/app/src/main/res/values/string-not_translatable.xml b/app/src/main/res/values/string-not_translatable.xml index d30f3f612c..e4f5a6ebd4 100644 --- a/app/src/main/res/values/string-not_translatable.xml +++ b/app/src/main/res/values/string-not_translatable.xml @@ -412,6 +412,7 @@ work. If not, see . pref_key__is_only_first_content_match pref_key__restore_settings pref_key__backup_settings + pref_key__editor_tab_to_indent **** "pref_key__todotxt__additional_projects_contexts" pref_key__todotxt_due_date_offset diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7f8f9c8202..ca34b51a60 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -482,4 +482,5 @@ work. If not, see . Switch Case (Ex: CaSe->cAsE) Capitalize Words (Ex: a note->A Note) Capitalize Sentences (Ex: case->Case) + Indent lines with TAB key diff --git a/app/src/main/res/xml/preferences_master.xml b/app/src/main/res/xml/preferences_master.xml index dbc0522597..5acbb3ca9b 100644 --- a/app/src/main/res/xml/preferences_master.xml +++ b/app/src/main/res/xml/preferences_master.xml @@ -373,6 +373,11 @@ android:key="@string/pref_key__editor_start_editing_on_bottom" android:summary="@string/start_on_bottom_when_loading_document" android:title="@string/start_on_bottom" /> +