Skip to content

Commit

Permalink
Moving from separate callbacks to a single refresh call
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed May 12, 2024
1 parent e0e8f79 commit d8f93fd
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ private static void runRegexReplaceAction(final Editable editable, final List<Re
final int selStartStart = TextViewUtils.getLineStart(text, selStart);

// Number of lines we will be modifying
final int lineCount = TextViewUtils.countChars(text, selStart, selEnd, '\n')[0] + 1;
final int lineCount = GsTextUtils.countChars(text, selStart, selEnd, '\n')[0] + 1;
int lineStart = selStartStart;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.text.Spanned;

import net.gsantner.markor.frontend.textview.TextViewUtils;
import net.gsantner.opoc.format.GsTextUtils;

import java.util.Date;

Expand All @@ -19,7 +20,7 @@ public class TodoTxtAutoTextFormatter implements InputFilter {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
if (start < source.length() && dstart <= dest.length() && TextViewUtils.isNewLine(source, start, end)) {
if (start < source.length() && dstart <= dest.length() && GsTextUtils.isNewLine(source, start, end)) {
return autoIndent(source);
}
} catch (IndexOutOfBoundsException | NullPointerException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ public static GsFileBrowserOptions.Options prepareFsViewerOpts(
opts.contentDescriptionSelected = R.string.selected;
opts.contentDescriptionFile = R.string.file;

opts.sortFolderFirst = appSettings.isFileBrowserSortFolderFirst();
opts.sortByType = appSettings.getFileBrowserSortByType();
opts.sortReverse = appSettings.isFileBrowserSortReverse();
opts.filterShowDotFiles = appSettings.isFileBrowserFilterShowDotFiles();

opts.accentColor = R.color.accent;
opts.primaryColor = R.color.primary;
opts.primaryTextColor = R.color.primary_text;
Expand All @@ -72,13 +67,21 @@ public static GsFileBrowserOptions.Options prepareFsViewerOpts(
opts.fileImage = R.drawable.ic_file_white_24dp;
opts.folderImage = R.drawable.ic_folder_white_24dp;

opts.getRecentFiles = appSettings::getRecentFiles;
opts.getPopularFiles = appSettings::getPopularFiles;
opts.getFavouriteFiles = appSettings::getFavouriteFiles;

opts.titleText = R.string.select;

opts.mountedStorageFolder = cu.getStorageAccessFolder(context);

opts.refresh = () -> {
opts.sortFolderFirst = appSettings.isFileBrowserSortFolderFirst();
opts.sortByType = appSettings.getFileBrowserSortByType();
opts.sortReverse = appSettings.isFileBrowserSortReverse();
opts.filterShowDotFiles = appSettings.isFileBrowserFilterShowDotFiles();
opts.favouriteFiles = appSettings.getFavouriteFiles();
opts.recentFiles = appSettings.getRecentFiles();
opts.popularFiles = appSettings.getPopularFiles();
};
opts.refresh.callback();

return opts;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AutoTextFormatter(FormatPatterns patterns) {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
if (start < source.length() && dstart <= dest.length() && TextViewUtils.isNewLine(source, start, end)) {
if (start < source.length() && dstart <= dest.length() && GsTextUtils.isNewLine(source, start, end)) {
return autoIndent(source, dest, dstart, dend);
}
} catch (IndexOutOfBoundsException | NullPointerException e) {
Expand Down Expand Up @@ -277,7 +277,7 @@ private static OrderedListLine getOrderedListStart(final CharSequence text, int
public static void renumberOrderedList(final Editable edit, final FormatPatterns patterns) {

final int[] sel = TextViewUtils.getSelection(edit);
if (!TextViewUtils.inRange(0, edit.length(), sel)) {
if (!GsTextUtils.inRange(0, edit.length(), sel)) {
return;
}

Expand Down Expand Up @@ -351,7 +351,7 @@ else if (!line.isEmpty) {
chunked.applyChanges();

final int[] newSel = new int[]{sel[0] + shifts[0], sel[1] + shifts[1]};
if (TextViewUtils.inRange(0, edit.length(), newSel)) {
if (GsTextUtils.inRange(0, edit.length(), newSel)) {
Selection.setSelection(edit, newSel[0], newSel[1]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.gsantner.markor.ApplicationObject;
import net.gsantner.markor.activity.MainActivity;
import net.gsantner.markor.model.AppSettings;
import net.gsantner.opoc.format.GsTextUtils;
import net.gsantner.opoc.wrapper.GsCallback;
import net.gsantner.opoc.wrapper.GsTextWatcherAdapter;

Expand Down Expand Up @@ -457,7 +458,7 @@ public int setSelectionExpandWholeLines() {
}

public boolean indexesValid(int... indexes) {
return TextViewUtils.inRange(0, length(), indexes);
return GsTextUtils.inRange(0, length(), indexes);
}

static class LineNumbersDrawer {
Expand All @@ -482,12 +483,12 @@ static class LineNumbersDrawer {
private final GsTextWatcherAdapter _lineTrackingWatcher = new GsTextWatcherAdapter() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
_maxNumber -= TextViewUtils.countChar(s, start, start + count, '\n');
_maxNumber -= GsTextUtils.countChar(s, start, start + count, '\n');
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
_maxNumber += TextViewUtils.countChar(s, start, start + count, '\n');
_maxNumber += GsTextUtils.countChar(s, start, start + count, '\n');
}
};

Expand Down Expand Up @@ -549,7 +550,7 @@ public void startLineTracking() {
_maxNumber = 1;
final CharSequence text = _editor.getText();
if (text != null) {
_maxNumber += TextViewUtils.countChar(text, 0, text.length(), '\n');
_maxNumber += GsTextUtils.countChar(text, 0, text.length(), '\n');
}
_editor.addTextChangedListener(_lineTrackingWatcher);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import androidx.annotation.NonNull;

import net.gsantner.opoc.format.GsTextUtils;
import net.gsantner.opoc.util.GsContextUtils;
import net.gsantner.opoc.wrapper.GsCallback;

Expand All @@ -47,27 +48,13 @@ private TextViewUtils() {
throw new AssertionError();
}

public static boolean isValidIndex(final CharSequence s, final int... indices) {
return s != null && inRange(0, s.length() - 1, indices);
}

// Checks if all values are in [min, max] _inclusive_
public static boolean inRange(final int min, final int max, final int... values) {
for (final int i : values) {
if (i < min || i > max) {
return false;
}
}
return true;
}

public static int getLineStart(CharSequence s, int start) {
return getLineStart(s, start, 0);
}

public static int getLineStart(CharSequence s, int start, int minRange) {
int i = start;
if (isValidIndex(s, start - 1, minRange)) {
if (GsTextUtils.isValidIndex(s, start - 1, minRange)) {
for (; i > minRange; i--) {
if (s.charAt(i - 1) == '\n') {
break;
Expand All @@ -84,7 +71,7 @@ public static int getLineEnd(CharSequence s, int start) {

public static int getLineEnd(CharSequence s, int start, int maxRange) {
int i = start;
if (isValidIndex(s, start, maxRange - 1)) {
if (GsTextUtils.isValidIndex(s, start, maxRange - 1)) {
for (; i < maxRange; i++) {
if (s.charAt(i) == '\n') {
break;
Expand Down Expand Up @@ -218,7 +205,7 @@ public static String getSelectedLines(final CharSequence seq, final int... sel)
*/
public static int[] getLineOffsetFromIndex(final CharSequence s, int p) {
p = Math.min(Math.max(p, 0), s.length());
final int line = countChars(s, 0, p, '\n')[0];
final int line = GsTextUtils.countChars(s, 0, p, '\n')[0];
final int offset = getLineEnd(s, p) - p;

return new int[]{line, offset};
Expand Down Expand Up @@ -259,61 +246,6 @@ public static int getIndexFromLineOffset(final CharSequence s, final int l, fina
return i;
}

public static int[] countChars(final CharSequence s, final char... chars) {
return countChars(s, 0, s.length(), chars);
}

/**
* Count instances of chars between start and end
*
* @param s Sequence to count in
* @param start start of section to count within
* @param end end of section to count within
* @param chars Array of chars to count
* @return number of instances of each char in [start, end)
*/
public static int[] countChars(final CharSequence s, int start, int end, final char... chars) {
// Faster specialization for the common single case
if (chars.length == 1) {
return new int[]{countChar(s, start, end, chars[0])};
}

final int[] counts = new int[chars.length];
start = Math.max(0, start);
end = Math.min(end, s.length());
for (int i = start; i < end; i++) {
final char c = s.charAt(i);
for (int j = 0; j < chars.length; j++) {
if (c == chars[j]) {
counts[j]++;
}
}
}
return counts;
}

public static int countChar(final CharSequence s, final char c) {
return countChar(s, 0, s.length(), c);
}

/**
* Count instances of a single char in a charsequence
*/
public static int countChar(final CharSequence s, int start, int end, final char c) {
start = Math.max(0, start);
end = Math.min(end, s.length());
int count = 0;
for (int i = start; i < end; i++) {
if (s.charAt(i) == c) {
count++;
}
}
return count;
}

public static boolean isNewLine(CharSequence source, int start, int end) {
return isValidIndex(source, start, end - 1) && (source.charAt(start) == '\n' || source.charAt(end - 1) == '\n');
}

public static void selectLines(final EditText edit, final Integer... positions) {
selectLines(edit, Arrays.asList(positions));
Expand Down Expand Up @@ -423,7 +355,7 @@ public static void setSelectionAndShow(final EditText edit, final int... sel) {
final int start = sel[0];
final int end = sel.length > 1 ? sel[1] : start;

if (inRange(0, edit.length(), start, end)) {
if (GsTextUtils.inRange(0, edit.length(), start, end)) {
edit.post(() -> {
if (!edit.hasFocus() && edit.getVisibility() != View.GONE) {
edit.requestFocus();
Expand Down Expand Up @@ -803,7 +735,7 @@ public static boolean checkRange(final CharSequence seq, final int... indices) {
}

public static boolean checkRange(final int length, final int... indices) {
return indices != null && indices.length >= 2 && inRange(0, length, indices) && indices[1] > indices[0];
return indices != null && indices.length >= 2 && GsTextUtils.inRange(0, length, indices) && indices[1] > indices[0];
}

public static boolean isViewVisible(final View view) {
Expand Down
23 changes: 5 additions & 18 deletions app/src/main/java/net/gsantner/markor/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,11 @@ public Document(@NonNull final File file) {

// Set initial format
final String fnlower = _file.getName().toLowerCase();
if (FormatRegistry.CONVERTER_TODOTXT.isFileOutOfThisFormat(fnlower)) {
setFormat(FormatRegistry.FORMAT_TODOTXT);
} else if (FormatRegistry.CONVERTER_KEYVALUE.isFileOutOfThisFormat(fnlower)) {
setFormat(FormatRegistry.FORMAT_KEYVALUE);
} else if (FormatRegistry.CONVERTER_MARKDOWN.isFileOutOfThisFormat(fnlower)) {
setFormat(FormatRegistry.FORMAT_MARKDOWN);
} else if (FormatRegistry.CONVERTER_CSV.isFileOutOfThisFormat(fnlower)) {
setFormat(FormatRegistry.FORMAT_CSV);
} else if (FormatRegistry.CONVERTER_ASCIIDOC.isFileOutOfThisFormat(fnlower)) {
setFormat(FormatRegistry.FORMAT_ASCIIDOC);
} else if (FormatRegistry.CONVERTER_WIKITEXT.isFileOutOfThisFormat(getPath())) {
setFormat(FormatRegistry.FORMAT_WIKITEXT);
} else if (FormatRegistry.CONVERTER_EMBEDBINARY.isFileOutOfThisFormat(getPath())) {
setFormat(FormatRegistry.FORMAT_EMBEDBINARY);
} else if (FormatRegistry.CONVERTER_ORGMODE.isFileOutOfThisFormat(getPath())) {
setFormat(FormatRegistry.FORMAT_ORGMODE);
} else {
setFormat(FormatRegistry.FORMAT_PLAIN);
for (final FormatRegistry.Format format : FormatRegistry.FORMATS) {
if (format.converter.isFileOutOfThisFormat(fnlower)) {
setFormat(format.format);
break;
}
}
}

Expand Down
72 changes: 72 additions & 0 deletions app/src/main/java/net/gsantner/opoc/format/GsTextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package net.gsantner.opoc.format;

import android.util.Base64;
import android.widget.EditText;

import net.gsantner.opoc.wrapper.GsCallback;

Expand Down Expand Up @@ -365,4 +366,75 @@ public static void forEachline(final CharSequence text, GsCallback.b3<Integer, I
}
callback.callback(i, start, text.length());
}


public static int[] countChars(final CharSequence s, final char... chars) {
return countChars(s, 0, s.length(), chars);
}

/**
* Count instances of chars between start and end
*
* @param s Sequence to count in
* @param start start of section to count within
* @param end end of section to count within
* @param chars Array of chars to count
* @return number of instances of each char in [start, end)
*/
public static int[] countChars(final CharSequence s, int start, int end, final char... chars) {
// Faster specialization for the common single case
if (chars.length == 1) {
return new int[]{countChar(s, start, end, chars[0])};
}

final int[] counts = new int[chars.length];
start = Math.max(0, start);
end = Math.min(end, s.length());
for (int i = start; i < end; i++) {
final char c = s.charAt(i);
for (int j = 0; j < chars.length; j++) {
if (c == chars[j]) {
counts[j]++;
}
}
}
return counts;
}

public static int countChar(final CharSequence s, final char c) {
return countChar(s, 0, s.length(), c);
}

/**
* Count instances of a single char in a charsequence
*/
public static int countChar(final CharSequence s, int start, int end, final char c) {
start = Math.max(0, start);
end = Math.min(end, s.length());
int count = 0;
for (int i = start; i < end; i++) {
if (s.charAt(i) == c) {
count++;
}
}
return count;
}

public static boolean isNewLine(CharSequence source, int start, int end) {
return isValidIndex(source, start, end - 1) && (source.charAt(start) == '\n' || source.charAt(end - 1) == '\n');
}

public static boolean isValidIndex(final CharSequence s, final int... indices) {
return s != null && inRange(0, s.length() - 1, indices);
}

// Checks if all values are in [min, max] _inclusive_
public static boolean inRange(final int min, final int max, final int... values) {
for (final int i : values) {
if (i < min || i > max) {
return false;
}
}
return true;
}
}
Loading

0 comments on commit d8f93fd

Please sign in to comment.