Skip to content

Commit

Permalink
add priority long press behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
wshoy committed Aug 11, 2024
1 parent 0cc1963 commit 1a1881d
Showing 1 changed file with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,7 @@ public boolean onActionClick(final @StringRes int action) {
}
case R.string.abid_todotxt_priority: {
MarkorDialogFactory.showPriorityDialog(getActivity(), selTasks.get(0).getPriority(), (priority) -> {
ArrayList<ReplacePattern> patterns = new ArrayList<>();
if (priority.length() > 1) {
patterns.add(new ReplacePattern(TodoTxtTask.PATTERN_PRIORITY_ANY, ""));
} else if (priority.length() == 1) {
final String _priority = String.format("(%c) ", priority.charAt(0));
patterns.add(new ReplacePattern(TodoTxtTask.PATTERN_PRIORITY_ANY, _priority));
patterns.add(new ReplacePattern("^\\s*", _priority));
}
runRegexReplaceAction(patterns);
trimLeadingWhiteSpace();
setPriority(priority.length() == 1 ? priority.charAt(0) : TodoTxtTask.PRIORITY_NONE);
});
return true;
}
Expand Down Expand Up @@ -208,6 +199,35 @@ public boolean onActionLongClick(final @StringRes int action) {
}
return true;
}
case R.string.abid_todotxt_priority: {
final Editable text = _hlEditor.getText();
final int[] sel = TextViewUtils.getSelection(_hlEditor);
final int linesStart = TextViewUtils.getLineStart(text, sel[0]);
if (linesStart == 0) {
setPriority(TodoTxtTask.PRIORITY_NONE);
return true;
}
final int prevLineStart = TextViewUtils.getLineStart(text, linesStart - 1);
final int prevLineEnd = TextViewUtils.getLineEnd(text, prevLineStart);
final String prevLine = text.subSequence(prevLineStart, prevLineEnd).toString();
final char prevPriority = new TodoTxtTask(prevLine).getPriority();

final List<TodoTxtTask> tasks = TodoTxtTask.getTasks(text, sel[0], sel[1]);
boolean areAllSamePriority = true;
for (TodoTxtTask task : tasks) {
final char commonPriority = tasks.get(0).getPriority();
if (task.getPriority() != commonPriority) {
areAllSamePriority = false;
break;
}
}
if (areAllSamePriority && prevPriority == tasks.get(0).getPriority()) {
setPriority(TodoTxtTask.PRIORITY_NONE);
} else {
setPriority(prevPriority);
}
return true;
}
case R.string.abid_todotxt_due_date: {
setDate();
return true;
Expand Down Expand Up @@ -303,6 +323,19 @@ private static void insertInline(final Editable editable, String thing) {
editable.replace(sel[0], sel[1], thing);
}

private void setPriority(char priority) {
ArrayList<ReplacePattern> patterns = new ArrayList<>();
if (priority == TodoTxtTask.PRIORITY_NONE) {
patterns.add(new ReplacePattern(TodoTxtTask.PATTERN_PRIORITY_ANY, ""));
} else {
final String _priority = String.format("(%c) ", priority);
patterns.add(new ReplacePattern(TodoTxtTask.PATTERN_PRIORITY_ANY, _priority));
patterns.add(new ReplacePattern("^\\s*", _priority));
}
runRegexReplaceAction(patterns);
trimLeadingWhiteSpace();
}

private static Calendar parseDateString(final String dateString, final Calendar fallback) {
if (dateString == null || dateString.length() != TodoTxtTask.DATEF_YYYY_MM_DD_LEN) {
return fallback;
Expand Down

0 comments on commit 1a1881d

Please sign in to comment.