Adjust FormattingHelpActivity

This commit is contained in:
Stefan Niedermann 2021-01-04 09:33:10 +01:00
parent 6eb99e9434
commit 25e2634460
3 changed files with 14 additions and 76 deletions

View file

@ -1,38 +1,23 @@
package it.niedermann.owncloud.notes;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.TypedValue;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
import com.yydcdut.markdown.MarkdownProcessor;
import com.yydcdut.markdown.syntax.text.TextFactory;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedActivity;
import it.niedermann.owncloud.notes.databinding.ActivityFormattingHelpBinding;
import static it.niedermann.owncloud.notes.shared.util.MarkDownUtil.CHECKBOX_CHECKED_MINUS;
import static it.niedermann.owncloud.notes.shared.util.MarkDownUtil.CHECKBOX_CHECKED_STAR;
import static it.niedermann.owncloud.notes.shared.util.MarkDownUtil.CHECKBOX_UNCHECKED_MINUS;
import static it.niedermann.owncloud.notes.shared.util.MarkDownUtil.CHECKBOX_UNCHECKED_STAR;
import static it.niedermann.owncloud.notes.shared.util.MarkDownUtil.getMarkDownConfiguration;
import static it.niedermann.owncloud.notes.shared.util.MarkDownUtil.parseCompat;
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.getFontSizeFromPreferences;
public class FormattingHelpActivity extends BrandedActivity {
private ActivityFormattingHelpBinding binding;
private String content;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
@ -43,61 +28,10 @@ public class FormattingHelpActivity extends BrandedActivity {
setSupportActionBar(binding.toolbar);
content = buildFormattingHelp();
final MarkdownProcessor markdownProcessor = new MarkdownProcessor(this);
markdownProcessor.factory(TextFactory.create());
markdownProcessor.config(getMarkDownConfiguration(binding.content.getContext())
.setOnTodoClickCallback((view, line, lineNumber) -> {
try {
String[] lines = TextUtils.split(content, "\\r?\\n");
/*
* Workaround for RxMarkdown-bug:
* When (un)checking a checkbox in a note which contains code-blocks, the "`"-characters get stripped out in the TextView and therefore the given lineNumber is wrong
* Find number of lines starting with ``` before lineNumber
*/
boolean inCodefence = false;
for (int i = 0; i < lines.length; i++) {
if (lines[i].startsWith("```")) {
inCodefence = !inCodefence;
lineNumber++;
}
if (inCodefence && TextUtils.isEmpty(lines[i])) {
lineNumber++;
}
if (i == lineNumber) {
break;
}
}
/*
* Workaround for multiple RxMarkdown-bugs:
* When (un)checking a checkbox which is in the last line, every time it gets toggled, the last character of the line gets lost.
* When (un)checking a checkbox, every markdown gets stripped in the given line argument
*/
if (lines[lineNumber].startsWith(CHECKBOX_UNCHECKED_MINUS) || lines[lineNumber].startsWith(CHECKBOX_UNCHECKED_STAR)) {
lines[lineNumber] = lines[lineNumber].replace(CHECKBOX_UNCHECKED_MINUS, CHECKBOX_CHECKED_MINUS);
lines[lineNumber] = lines[lineNumber].replace(CHECKBOX_UNCHECKED_STAR, CHECKBOX_CHECKED_STAR);
} else {
lines[lineNumber] = lines[lineNumber].replace(CHECKBOX_CHECKED_MINUS, CHECKBOX_UNCHECKED_MINUS);
lines[lineNumber] = lines[lineNumber].replace(CHECKBOX_CHECKED_STAR, CHECKBOX_UNCHECKED_STAR);
}
content = TextUtils.join("\n", lines);
binding.content.setText(parseCompat(markdownProcessor, content));
} catch (IndexOutOfBoundsException e) {
Toast.makeText(this, R.string.checkbox_could_not_be_toggled, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return line;
}
)
.setOnLinkClickCallback((view, link) -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link))))
.build());
binding.content.setMovementMethod(LinkMovementMethod.getInstance());
binding.content.setText(parseCompat(markdownProcessor, content));
binding.content.setMarkdownString(buildFormattingHelp());
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
binding.content.setTextSize(TypedValue.COMPLEX_UNIT_PX, getFontSizeFromPreferences(this, sp));
if (sp.getBoolean(getString(R.string.pref_key_font), false)) {
binding.content.setTypeface(Typeface.MONOSPACE);
@ -110,6 +44,7 @@ public class FormattingHelpActivity extends BrandedActivity {
final String indention = " ";
final String divider = getString(R.string.formatting_help_divider);
final String codefence = getString(R.string.formatting_help_codefence);
final String outerCodefence = getString(R.string.formatting_help_codefence_outer);
int numberedListItem = 1;
final String lists = getString(R.string.formatting_help_lists_body_1) + lineBreak +
@ -210,14 +145,16 @@ public class FormattingHelpActivity extends BrandedActivity {
lineBreak +
getString(R.string.formatting_help_code_body_1) + lineBreak +
lineBreak +
getString(R.string.formatting_help_codefence_inline_escaped, getString(R.string.formatting_help_code_javascript_inline)) + lineBreak +
getString(R.string.formatting_help_codefence_inline_escaped, getString(R.string.formatting_help_code_javascript_inline)) + " " + lineBreak +
getString(R.string.formatting_help_codefence_inline, getString(R.string.formatting_help_code_javascript_inline)) + lineBreak +
lineBreak +
getString(R.string.formatting_help_code_body_2) + lineBreak +
lineBreak +
getString(R.string.formatting_help_codefence_escaped) + lineBreak +
outerCodefence + lineBreak +
codefence + lineBreak +
javascript +
getString(R.string.formatting_help_codefence_escaped) + lineBreak +
codefence + lineBreak +
outerCodefence + lineBreak +
lineBreak +
codefence + lineBreak +
javascript +
@ -225,9 +162,11 @@ public class FormattingHelpActivity extends BrandedActivity {
lineBreak +
getString(R.string.formatting_help_code_body_3) + lineBreak +
lineBreak +
getString(R.string.formatting_help_codefence_javascript_escaped) + lineBreak +
outerCodefence + lineBreak +
getString(R.string.formatting_help_codefence_javascript) + lineBreak +
javascript +
getString(R.string.formatting_help_codefence_escaped) + lineBreak +
codefence + lineBreak +
outerCodefence + lineBreak +
lineBreak +
getString(R.string.formatting_help_codefence_javascript) + lineBreak +
javascript +

View file

@ -29,7 +29,7 @@
android:orientation="vertical"
tools:context="it.niedermann.owncloud.notes.edit.EditNoteActivity">
<com.yydcdut.markdown.MarkdownTextView
<it.niedermann.android.markdown.markwon.MarkwonMarkdownViewer
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"

View file

@ -231,11 +231,10 @@
<string name="formatting_help_codefence_inline" translateable="false">`%1$s`</string>
<string name="formatting_help_codefence_inline_escaped" translateable="false">\\`%1$s\\`</string>
<string name="formatting_help_codefence" translateable="false">```</string>
<string name="formatting_help_codefence_escaped" translatable="false">\\`\\`\\`</string>
<string name="formatting_help_codefence_outer" translateable="false">````</string>
<string name="formatting_help_javascript_1" translatable="false">int getRandom() {</string>
<string name="formatting_help_javascript_2" translatable="false">return 4;</string>
<string name="formatting_help_javascript_3" translatable="false">}</string>
<string name="formatting_help_codefence_javascript_escaped" translatable="false">\\`\\`\\`javascript</string>
<string name="formatting_help_codefence_javascript" translateable="false">```javascript</string>
<string name="formatting_help_code_javascript_inline" translatable="false">var example = true</string>
<string name="formatting_help_ol" translatable="false">%1$d. %2$s</string>