From 25e263446008f86f02e96094a5f0226a2528f998 Mon Sep 17 00:00:00 2001 From: Stefan Niedermann Date: Mon, 4 Jan 2021 09:33:10 +0100 Subject: [PATCH] Adjust FormattingHelpActivity --- .../notes/FormattingHelpActivity.java | 85 +++---------------- .../res/layout/activity_formatting_help.xml | 2 +- app/src/main/res/values/strings.xml | 3 +- 3 files changed, 14 insertions(+), 76 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/FormattingHelpActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/FormattingHelpActivity.java index 0e537956..393ff43c 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/FormattingHelpActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/FormattingHelpActivity.java @@ -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 + diff --git a/app/src/main/res/layout/activity_formatting_help.xml b/app/src/main/res/layout/activity_formatting_help.xml index 54aa64ec..89b51160 100644 --- a/app/src/main/res/layout/activity_formatting_help.xml +++ b/app/src/main/res/layout/activity_formatting_help.xml @@ -29,7 +29,7 @@ android:orientation="vertical" tools:context="it.niedermann.owncloud.notes.edit.EditNoteActivity"> - `%1$s` \\`%1$s\\` ``` - \\`\\`\\` + ```` int getRandom() { return 4; } - \\`\\`\\`javascript ```javascript var example = true %1$d. %2$s