Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 67609666 authored by Stefan Niedermann's avatar Stefan Niedermann Committed by Niedermann IT-Dienstleistungen
Browse files

#116 "Help: format" option in the menu

parent 45ce9019
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -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"
+8 −7
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ import it.niedermann.owncloud.notes.branding.BrandedSnackbar;
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;
@@ -83,6 +84,7 @@ import static android.view.View.VISIBLE;
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 {

@@ -555,19 +557,18 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
    }

    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)) {
@@ -583,7 +584,7 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
                onItemClick(item);
            }
        });
        adapterMenu.setItems(itemsMenu);
        adapterMenu.setItems(asList(itemFormattingHelp, itemTrashbin, itemSettings, itemAbout));
        binding.navigationMenu.setAdapter(adapterMenu);
    }

+105 −0
Original line number Diff line number Diff line
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 −0
Original line number Diff line number Diff line
<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 −0
Original line number Diff line number Diff line
<?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>
 No newline at end of file
Loading