Skip to content

Commit

Permalink
Restored option, fixed duplicate calls to loadFolder on resume
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Dec 26, 2024
1 parent aba8578 commit d31628e
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/net/gsantner/markor/model/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public static GsFileBrowserFragment newInstance() {
private Menu _fragmentMenu;
private MarkorContextUtils _cu;
private Toolbar _toolbar;
private boolean _reloadRequiredOnResume = true;

//########################
//## Methods
Expand Down Expand Up @@ -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(() -> {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public class GsFileBrowserListAdapter extends RecyclerView.Adapter<GsFileBrowser
private final Set<File> _currentSelection;
private File _fileToShowAfterNextLoad;
private File _currentFolder;
private File _goUpFile;
private final Context _context;
private final StringFilter _filter;
private RecyclerView _recyclerView;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -718,22 +715,24 @@ 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<File> filteredData = new ArrayList<>();
_filter._filter(newData, filteredData);

_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) {
Expand Down Expand Up @@ -889,10 +888,4 @@ private int getUserId() {
return 0;
}
}

private static class GoUpFile extends File {
GoUpFile(final File f) {
super(f.getPath());
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/string-not_translatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
<string name="pref_key__is_only_first_content_match" translatable="false">pref_key__is_only_first_content_match</string>
<string name="pref_key__restore_settings" translatable="false">pref_key__restore_settings</string>
<string name="pref_key__backup_settings" translatable="false">pref_key__backup_settings</string>
<string name="pref_key__editor_tab_to_indent" translatable="false">pref_key__editor_tab_to_indent</string>
<string name="hidden_password" translatable="false">****</string>
<string name="pref_key__todotxt__additional_projects_contexts" translatable="false">"pref_key__todotxt__additional_projects_contexts"</string>
<string name="pref_key__todotxt__due_date_offset" translatable="false">pref_key__todotxt_due_date_offset</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -482,4 +482,5 @@ work. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
<string name="switch_case">Switch Case (Ex: CaSe->cAsE)</string>
<string name="capitalize_words">Capitalize Words (Ex: a note->A Note)</string>
<string name="capitalize_sentences">Capitalize Sentences (Ex: case->Case)</string>
<string name="use_tab_to_indent">Indent lines with TAB key</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/preferences_master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
<CheckBoxPreference
android:defaultValue="true"
android:icon="@drawable/ic_keyboard_tab_black_24dp"
android:key="@string/pref_key__editor_tab_to_indent"
android:title="@string/use_tab_to_indent" />
<ListPreference
android:defaultValue="-"
android:dialogTitle="@string/theme"
Expand Down

0 comments on commit d31628e

Please sign in to comment.