Skip to content

Commit

Permalink
#116 "Help: format" option in the menu
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-niedermann committed Jun 7, 2020
1 parent 45ce901 commit 6760966
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 7 deletions.
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
android:name=".android.activity.ExceptionActivity"
android:label="@string/app_name" />

<activity
android:name=".formattinghelp.FormattingHelpActivity"
android:label="@string/action_formatting_help"
android:parentActivityName=".android.activity.NotesListViewActivity"
android:windowSoftInputMode="stateHidden" />

<activity
android:name=".manageaccounts.ManageAccountsActivity"
android:label="@string/manage_accounts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.databinding.ActivityNotesListViewBinding;
import it.niedermann.owncloud.notes.databinding.DrawerLayoutBinding;
import it.niedermann.owncloud.notes.formattinghelp.FormattingHelpActivity;
import it.niedermann.owncloud.notes.model.Capabilities;
import it.niedermann.owncloud.notes.model.Category;
import it.niedermann.owncloud.notes.model.DBNote;
Expand All @@ -83,6 +84,7 @@
import static it.niedermann.owncloud.notes.branding.BrandingUtil.getSecondaryForegroundColorDependingOnTheme;
import static it.niedermann.owncloud.notes.util.ColorUtil.contrastRatioIsSufficient;
import static it.niedermann.owncloud.notes.util.SSOUtil.askForNewAccount;
import static java.util.Arrays.asList;

public class NotesListViewActivity extends LockedActivity implements NoteClickListener, ViewProvider, MoveAccountListener, AccountSwitcherListener {

Expand Down Expand Up @@ -555,19 +557,18 @@ protected void onPostExecute(List<NavigationItem> 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<NavigationItem> 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)) {
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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) {

}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_help_outline_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#757575"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#757575" android:pathData="M11,18h2v-2h-2v2zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8zM12,6c-2.21,0 -4,1.79 -4,4h2c0,-1.1 0.9,-2 2,-2s2,0.9 2,2c0,2 -3,1.75 -3,5h2c0,-2.25 3,-2.5 3,-5 0,-2.21 -1.79,-4 -4,-4z"/>
</vector>
43 changes: 43 additions & 0 deletions app/src/main/res/layout/activity_formatting_help.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary"
android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetStartWithNavigation="0dp"
app:navigationIcon="@drawable/ic_arrow_back_grey600_24dp"
app:title="@string/action_formatting_help"
app:titleMarginStart="0dp" />
</com.google.android.material.appbar.AppBarLayout>

<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="it.niedermann.owncloud.notes.android.activity.EditNoteActivity">

<com.yydcdut.markdown.MarkdownTextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/spacer_2x"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/fg_default"
android:textIsSelectable="true"
android:theme="@style/textViewStyle"
tools:text="@tools:sample/lorem/random" />
</ScrollView>
</LinearLayout>
150 changes: 150 additions & 0 deletions app/src/main/res/raw/formatting_help.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
<string name="simple_behavior">Behavior</string>
<string name="share_multiple">Share content of %1$d notes</string>
<string name="manage_accounts">Manage accounts</string>
<string name="action_formatting_help">Formatting</string>

<!-- Array: note modes -->
<string-array name="noteMode_entries">
Expand Down

0 comments on commit 6760966

Please sign in to comment.