From 67609666256181cfc5ffcd044ce12e794c57d70a Mon Sep 17 00:00:00 2001 From: Stefan Niedermann Date: Sun, 7 Jun 2020 17:35:40 +0200 Subject: [PATCH] #116 "Help: format" option in the menu --- app/src/main/AndroidManifest.xml | 6 + .../activity/NotesListViewActivity.java | 15 +- .../FormattingHelpActivity.java | 105 ++++++++++++ .../drawable/ic_baseline_help_outline_24.xml | 5 + .../res/layout/activity_formatting_help.xml | 43 +++++ app/src/main/res/raw/formatting_help.md | 150 ++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 7 files changed, 318 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/it/niedermann/owncloud/notes/formattinghelp/FormattingHelpActivity.java create mode 100644 app/src/main/res/drawable/ic_baseline_help_outline_24.xml create mode 100644 app/src/main/res/layout/activity_formatting_help.xml create mode 100644 app/src/main/res/raw/formatting_help.md diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fc42c3cf9..f3117ead2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -62,6 +62,12 @@ android:name=".android.activity.ExceptionActivity" android:label="@string/app_name" /> + + items) { } private void setupNavigationMenu() { + final NavigationItem itemFormattingHelp = new NavigationItem("formattingHelp", getString(R.string.action_formatting_help), null, R.drawable.ic_baseline_help_outline_24); final NavigationItem itemTrashbin = new NavigationItem("trashbin", getString(R.string.action_trashbin), null, R.drawable.ic_delete_grey600_24dp); final NavigationItem itemSettings = new NavigationItem("settings", getString(R.string.action_settings), null, R.drawable.ic_settings_grey600_24dp); final NavigationItem itemAbout = new NavigationItem("about", getString(R.string.simple_about), null, R.drawable.ic_info_outline_grey600_24dp); - ArrayList itemsMenu = new ArrayList<>(3); - itemsMenu.add(itemTrashbin); - itemsMenu.add(itemSettings); - itemsMenu.add(itemAbout); - NavigationAdapter adapterMenu = new NavigationAdapter(this, new NavigationAdapter.ClickListener() { @Override public void onItemClick(NavigationItem item) { - if (itemSettings.equals(item)) { + if (itemFormattingHelp.equals(item)) { + Intent formattingHelpIntent = new Intent(getApplicationContext(), FormattingHelpActivity.class); + startActivity(formattingHelpIntent); + } else if (itemSettings.equals(item)) { Intent settingsIntent = new Intent(getApplicationContext(), PreferencesActivity.class); startActivityForResult(settingsIntent, server_settings); } else if (itemAbout.equals(item)) { @@ -583,7 +584,7 @@ public void onIconClick(NavigationItem item) { onItemClick(item); } }); - adapterMenu.setItems(itemsMenu); + adapterMenu.setItems(asList(itemFormattingHelp, itemTrashbin, itemSettings, itemAbout)); binding.navigationMenu.setAdapter(adapterMenu); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/formattinghelp/FormattingHelpActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/formattinghelp/FormattingHelpActivity.java new file mode 100644 index 000000000..332cbb7e1 --- /dev/null +++ b/app/src/main/java/it/niedermann/owncloud/notes/formattinghelp/FormattingHelpActivity.java @@ -0,0 +1,105 @@ +package it.niedermann.owncloud.notes.formattinghelp; + +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.text.TextUtils; +import android.widget.Toast; + +import androidx.annotation.Nullable; + +import com.yydcdut.markdown.MarkdownProcessor; +import com.yydcdut.markdown.syntax.text.TextFactory; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +import it.niedermann.owncloud.notes.R; +import it.niedermann.owncloud.notes.android.fragment.ExceptionDialogFragment; +import it.niedermann.owncloud.notes.branding.BrandedActivity; +import it.niedermann.owncloud.notes.databinding.ActivityFormattingHelpBinding; + +import static it.niedermann.owncloud.notes.util.MarkDownUtil.CHECKBOX_CHECKED_MINUS; +import static it.niedermann.owncloud.notes.util.MarkDownUtil.CHECKBOX_CHECKED_STAR; +import static it.niedermann.owncloud.notes.util.MarkDownUtil.CHECKBOX_UNCHECKED_MINUS; +import static it.niedermann.owncloud.notes.util.MarkDownUtil.CHECKBOX_UNCHECKED_STAR; +import static it.niedermann.owncloud.notes.util.MarkDownUtil.getMarkDownConfiguration; +import static it.niedermann.owncloud.notes.util.MarkDownUtil.parseCompat; + +public class FormattingHelpActivity extends BrandedActivity { + + private static final String TAG = FormattingHelpActivity.class.getSimpleName(); + private ActivityFormattingHelpBinding binding; + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + binding = ActivityFormattingHelpBinding.inflate(getLayoutInflater()); + setContentView(binding.getRoot()); + + setSupportActionBar(binding.toolbar); + final StringBuilder stringBuilder = new StringBuilder(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.formatting_help)))) { + String line; + while ((line = reader.readLine()) != null) { + stringBuilder.append(line).append("\n"); + } + } catch (IOException e) { + ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()); + } + + String content = stringBuilder.toString(); + + 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 + */ + for (int i = 0; i < lines.length; i++) { + if (lines[i].startsWith("```")) { + 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); + } + + binding.content.setText(parseCompat(markdownProcessor, TextUtils.join("\n", lines))); + } 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.setText(parseCompat(markdownProcessor, content)); + } + + @Override + public void applyBrand(int mainColor, int textColor) { + + } +} diff --git a/app/src/main/res/drawable/ic_baseline_help_outline_24.xml b/app/src/main/res/drawable/ic_baseline_help_outline_24.xml new file mode 100644 index 000000000..7897850ec --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_help_outline_24.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/activity_formatting_help.xml b/app/src/main/res/layout/activity_formatting_help.xml new file mode 100644 index 000000000..69c6838d5 --- /dev/null +++ b/app/src/main/res/layout/activity_formatting_help.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/raw/formatting_help.md b/app/src/main/res/raw/formatting_help.md new file mode 100644 index 000000000..79cc6ac39 --- /dev/null +++ b/app/src/main/res/raw/formatting_help.md @@ -0,0 +1,150 @@ +# Context based formatting + +A major design goal of the Notes app is to provide a distraction free tool. Though you will be able to format your texts with Markdown. For various of the below mentioned examples, you can use shortcuts so you can format your notes without typing in the codes below. +Just select a range of text or tap on your cursor at any position and you will get a popup menu which contains next to the default entries `Cut`, `Copy`, `Select all` entries like `Link` or `Checkbox`. + +--- + +# Text + +It's very easy to make some words **bold** and other words *italic* with Markdown. You can ~~strike~~ some words through and even [link to Google](http://google.com). + +``` +It's very easy to make some words **bold** and other words *italic* with Markdown. You can ~~strike~~ some words through and even [link to Google](http://google.com). +``` + +--- + +# Lists + +Sometimes you want numbered lists: + +1. One +2. Two +3. Three + +Sometimes you want bullet points: + +* Start a line with a star +* Profit! + +Alternatively, + +- Dashes work just as well +- And if you have sub points, put two spaces before the dash or star: + - Like this + - And this + +``` +Sometimes you want numbered lists: + +1. One +2. Two +3. Three + +Sometimes you want bullet points: + +* Start a line with a star +* Profit! + +Alternatively, + +- Dashes work just as well +- And if you have sub points, put two spaces before the dash or star: + - Like this + - And this +``` + +--- + +# Checkbox + +To create a checkbox, use a list followed by brackets + +- [ ] Item 1 +* [ ] Item 2 + +``` +To create a checkbox, use a list followed by brackets + +- [ ] Item 1 +* [ ] Item 2 +``` + +--- + +# Structured documents + +Sometimes it's useful to have different levels of headings to structure your documents. Start lines with a `#` to create headings. Multiple `##` in a row denote smaller heading sizes. + +### This is a third-tier heading + +You can use one `#` all the way up to `######` six for different heading sizes. + +If you'd like to quote someone, use the > character before the line: + +> Coffee. The finest organic suspension ever devised... I beat the Borg with it. +> - Captain Janeway + +``` +# Structured documents + +Sometimes it's useful to have different levels of headings to structure your documents. Start lines with a `#` to create headings. Multiple `##` in a row denote smaller heading sizes. + +### This is a third-tier heading + +You can use one `#` all the way up to `######` six for different heading sizes. + +If you'd like to quote someone, use the > character before the line: + +> Coffee. The finest organic suspension ever devised... I beat the Borg with it. +> - Captain Janeway +``` + +--- + +# Code + +There are many different ways to style code with Markdown. If you have inline code blocks, wrap them in backticks: + +\`var example = true\` +`var example = true` + +Markdown also supports something called code fencing, which allows for multiple lines without indentation: + +\`\`\` +if (isAwesome){ + return true +} +\`\`\` + +``` +if (isAwesome){ + return true +} +``` + +And if you'd like to use syntax highlighting, include the language: + +\`\`\`javascript +if (isAwesome){ + return true +} +\`\`\` + +```javascript +if (isAwesome){ + return true +} +``` + +--- + +# Unsupported + +While we try to continuously improve the support for Markdown, there are a few features which are not yet supported by Notes: + +- Tables +- Images + +If you are interested in contributing support for one of those features, get in contact with us via GitHub or E-Mail. \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e45acb06f..6e6a6a4c2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -197,6 +197,7 @@ Behavior Share content of %1$d notes Manage accounts + Formatting