Skip to content

Commit

Permalink
#2 Render markdown inline
Browse files Browse the repository at this point in the history
Replace Bypass library with RxMarkdown library
  • Loading branch information
stefan-niedermann committed Dec 7, 2016
1 parent a76b8f0 commit 39336f5
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 52 deletions.
18 changes: 10 additions & 8 deletions app/build.gradle
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion '24.0.1'
compileSdkVersion 25
buildToolsVersion '25.0.1'

defaultConfig {
applicationId "it.niedermann.owncloud.notes"
minSdkVersion 11
targetSdkVersion 24
targetSdkVersion 25
versionCode 14
versionName "0.9.0"
}
Expand All @@ -20,10 +20,12 @@ android {
}

dependencies {
compile 'com.commit451:bypasses:1.0.1'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.yydcdut:rxmarkdown:0.0.7'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.5'
compile 'com.android.support:support-v4:25.0.1'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:design:25.0.1'
compile 'com.android.support:recyclerview-v7:25.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Expand Up @@ -6,7 +6,7 @@

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="24" />
android:targetSdkVersion="25" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
Expand Down Expand Up @@ -58,8 +58,8 @@
<activity
android:name="it.niedermann.owncloud.notes.android.activity.EditNoteActivity"
android:label="@string/menu_edit"
android:windowSoftInputMode="stateVisible"
android:parentActivityName="it.niedermann.owncloud.notes.android.activity.NotesListViewActivity"></activity>
android:parentActivityName="it.niedermann.owncloud.notes.android.activity.NotesListViewActivity"
android:windowSoftInputMode="stateVisible"></activity>
<activity
android:name="it.niedermann.owncloud.notes.android.activity.AboutActivity"
android:label="@string/menu_about"
Expand Down
Expand Up @@ -10,6 +10,11 @@
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.yydcdut.rxmarkdown.RxMDEditText;
import com.yydcdut.rxmarkdown.RxMarkdown;
import com.yydcdut.rxmarkdown.factory.EditFactory;

import java.util.Timer;
import java.util.TimerTask;
Expand All @@ -20,6 +25,8 @@
import it.niedermann.owncloud.notes.model.DBNote;
import it.niedermann.owncloud.notes.persistence.NoteSQLiteOpenHelper;
import it.niedermann.owncloud.notes.util.ICallback;
import it.niedermann.owncloud.notes.util.MarkDownUtil;
import rx.Subscriber;

public class EditNoteActivity extends AppCompatActivity {

Expand All @@ -31,7 +38,7 @@ public class EditNoteActivity extends AppCompatActivity {
private static final long DELAY = 2000; // in ms
private static final long DELAY_AFTER_SYNC = 5000; // in ms

private EditText content = null;
private RxMDEditText content = null;
private DBNote note, originalNote;
private int notePosition = 0;
private Timer timer, timerNextSync;
Expand All @@ -53,9 +60,29 @@ protected void onCreate(final Bundle savedInstanceState) {
originalNote = (DBNote) savedInstanceState.getSerializable(PARAM_ORIGINAL_NOTE);
notePosition = savedInstanceState.getInt(PARAM_NOTE_POSITION);
}
content = (EditText) findViewById(R.id.editContent);
content = (RxMDEditText) findViewById(R.id.editContent);
content.setText(note.getContent());
content.setEnabled(true);

RxMarkdown.live(content)
.config(MarkDownUtil.getMarkDownConfiguration(getApplicationContext()))
.factory(EditFactory.create())
.intoObservable()
.subscribe(new Subscriber<CharSequence>() {
@Override
public void onCompleted() {
}

@Override
public void onError(Throwable e) {
}

@Override
public void onNext(CharSequence charSequence) {
content.setText(charSequence, TextView.BufferType.SPANNABLE);
}
});

db = new NoteSQLiteOpenHelper(this);
actionBar = getSupportActionBar();
if (actionBar != null) {
Expand Down
Expand Up @@ -5,19 +5,28 @@
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.yydcdut.rxmarkdown.RxMDTextView;
import com.yydcdut.rxmarkdown.RxMarkdown;
import com.yydcdut.rxmarkdown.factory.TextFactory;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.model.DBNote;
import it.niedermann.owncloud.notes.util.MarkDownUtil;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class NoteActivity extends AppCompatActivity {

public static final String PARAM_NOTE = EditNoteActivity.PARAM_NOTE;

private DBNote note = null;
private TextView noteContent = null;
private RxMDTextView noteContent = null;
private ActionBar actionBar = null;

@Override
Expand All @@ -33,8 +42,30 @@ protected void onCreate(Bundle savedInstanceState) {
actionBar.setTitle(note.getTitle());
actionBar.setSubtitle(DateUtils.getRelativeDateTimeString(getApplicationContext(), note.getModified().getTimeInMillis(), DateUtils.MINUTE_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0));
}
noteContent = (TextView) findViewById(R.id.single_note_content);
noteContent.setText(note.getSpannableContent());
noteContent = (RxMDTextView) findViewById(R.id.single_note_content);

RxMarkdown.with(note.getContent(), this)
.config(MarkDownUtil.getMarkDownConfiguration(getApplicationContext()))
.factory(TextFactory.create())
.intoObservable()
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<CharSequence>() {
@Override
public void onCompleted() {
}

@Override
public void onError(Throwable e) {
Log.v("Note View -------------", e.getStackTrace().toString());
}

@Override
public void onNext(CharSequence charSequence) {
noteContent.setText(charSequence, TextView.BufferType.SPANNABLE);
}
});
noteContent.setText(note.getContent());
findViewById(R.id.fab_edit).setVisibility(View.GONE);
((TextView) findViewById(R.id.single_note_content)).setMovementMethod(LinkMovementMethod.getInstance());
}
Expand Down
Expand Up @@ -22,7 +22,8 @@ public class SingleNoteWidget extends AppWidgetProvider {
public static void updateAppWidget(DBNote note, Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_single_note);
if (note != null) {
updateViews.setTextViewText(R.id.single_note_content, note.getSpannableContent());
//TODO switch to RxMD
//updateViews.setTextViewText(R.id.single_note_content, note.getSpannableContent());
Intent intent = new Intent(context, EditNoteActivity.class);
intent.putExtra(EditNoteActivity.PARAM_NOTE, note);
// http://stackoverflow.com/questions/4011178/multiple-instances-of-widget-only-updating-last-widget
Expand Down
Expand Up @@ -43,11 +43,6 @@ private void setExcerpt(String content) {
excerpt = NoteUtil.generateNoteExcerpt(content);
}

public CharSequence getSpannableContent() {
// TODO Cache the generated CharSequence not possible because CharSequence does not implement Serializable
return NoteUtil.parseMarkDown(getContent());
}

public void setContent(String content) {
super.setContent(content);
setExcerpt(content);
Expand Down
@@ -0,0 +1,27 @@
package it.niedermann.owncloud.notes.util;

import android.content.Context;
import android.support.v4.content.res.ResourcesCompat;

import com.yydcdut.rxmarkdown.RxMDConfiguration;

import it.niedermann.owncloud.notes.R;

/**
* Created by stefan on 07.12.16.
*/

public class MarkDownUtil {

/**
* Ensures every instance of RxMD uses the same configuration
*
* @param context Context
* @return RxMDConfiguration
*/
public static RxMDConfiguration getMarkDownConfiguration(Context context) {
return new RxMDConfiguration.Builder(context)
.setLinkColor(ResourcesCompat.getColor(context.getResources(), R.color.primary, null))
.build();
}
}
27 changes: 0 additions & 27 deletions app/src/main/java/it/niedermann/owncloud/notes/util/NoteUtil.java
Expand Up @@ -2,45 +2,18 @@

import java.util.regex.Pattern;

import in.uncod.android.bypass.Bypass;

/**
* Provides basic functionality for Note operations.
* Created by stefan on 06.10.15.
*/
public class NoteUtil {
private static final Bypass bypass = new Bypass();

private static final Pattern pLists = Pattern.compile("^\\s*[*+-]\\s+", Pattern.MULTILINE);
private static final Pattern pHeadings = Pattern.compile("^#+\\s+(.*?)\\s*#*$", Pattern.MULTILINE);
private static final Pattern pHeadingLine = Pattern.compile("^(?:=*|-*)$", Pattern.MULTILINE);
private static final Pattern pEmphasis = Pattern.compile("(\\*+|_+)(.*?)\\1", Pattern.MULTILINE);
private static final Pattern pSpace1 = Pattern.compile("^\\s+", Pattern.MULTILINE);
private static final Pattern pSpace2 = Pattern.compile("\\s+$", Pattern.MULTILINE);

/**
* Parses a MarkDown-String and returns a Spannable
*
* @param s String - MarkDown
* @return Spannable
*/
public static CharSequence parseMarkDown(String s) {
/*
* Appends two spaces at the end of every line to force a line break.
*
* @see #24
*/
StringBuilder sb = new StringBuilder();
for (String line : s.split("\n")) {
sb.append(line);
// If line is not a list item
if (!line.trim().matches("^([\\-*]|[0-9]+\\.)(.)*")) {
sb.append(" ");
}
sb.append("\n");
}
return bypass.markdownToSpannable(sb.toString());
}

/**
* Strips all MarkDown from the given String
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_edit.xml
Expand Up @@ -12,7 +12,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
<com.yydcdut.rxmarkdown.RxMDEditText
android:id="@+id/editContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_single_note.xml
Expand Up @@ -9,7 +9,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
<com.yydcdut.rxmarkdown.RxMDTextView
android:id="@+id/single_note_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/widget_single_note.xml
Expand Up @@ -5,7 +5,7 @@
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
<com.yydcdut.rxmarkdown.RxMDTextView
android:id="@+id/single_note_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down

0 comments on commit 39336f5

Please sign in to comment.