diff --git a/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NoteUtilTest.java b/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NoteUtilTest.java new file mode 100644 index 00000000..c92ec81f --- /dev/null +++ b/app/src/androidTest/java/it/niedermann/owncloud/notes/util/NoteUtilTest.java @@ -0,0 +1,28 @@ +package it.niedermann.owncloud.notes.util; + +import junit.framework.TestCase; + +/** + * Tests the NoteUtil + * Created by stefan on 06.10.15. + */ +public class NoteUtilTest extends TestCase { + public void testParseMarkDown() { + assertTrue(NoteUtil.parseMarkDown("*cursive*").contains("cursive")); + assertTrue(NoteUtil.parseMarkDown("**bold**").contains("bold")); + assertTrue(NoteUtil.parseMarkDown("##header").contains("

header

")); + } + + public void testRemoveMarkDown() { + assertTrue("Aufzählung".equals(NoteUtil.removeMarkDown("* Aufzählung"))); + assertTrue("Header".equals(NoteUtil.removeMarkDown("# Header"))); + } + + public void testIsEmptyLine() { + assertTrue(NoteUtil.isEmptyLine(" ")); + assertTrue(NoteUtil.isEmptyLine("\n")); + assertTrue(NoteUtil.isEmptyLine("\n ")); + assertTrue(NoteUtil.isEmptyLine(" \n")); + assertTrue(NoteUtil.isEmptyLine(" \n ")); + } +} diff --git a/app/src/main/java/it/niedermann/owncloud/notes/model/Note.java b/app/src/main/java/it/niedermann/owncloud/notes/model/Note.java index 2314e33b..57bfb159 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/model/Note.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/model/Note.java @@ -1,30 +1,26 @@ package it.niedermann.owncloud.notes.model; -import android.text.Html; - -import com.commonsware.cwac.anddown.AndDown; - import java.io.Serializable; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; import it.niedermann.owncloud.notes.persistence.NoteSQLiteOpenHelper; +import it.niedermann.owncloud.notes.util.NoteUtil; @SuppressWarnings("serial") public class Note implements Serializable { - - private static final AndDown and_down = new AndDown(); private long id = 0; private String title = ""; private Calendar modified = null; private String content = ""; + private String excerpt = ""; private String htmlContent = null; public Note(long id, Calendar modified, String title, String content) { this.id = id; if(title != null) - this.title = Html.fromHtml(and_down.markdownToHtml(title)).toString().trim(); + setTitle(title); setTitle(title); setContent(content); this.modified = modified; @@ -39,7 +35,7 @@ public class Note implements Serializable { } public void setTitle(String title) { - this.title = Html.fromHtml(and_down.markdownToHtml(title)).toString().trim(); + this.title = NoteUtil.removeMarkDown(title); } @SuppressWarnings("WeakerAccess") @@ -57,13 +53,31 @@ public class Note implements Serializable { } public void setContent(String content) { + setExcerpt(content); this.content = content; this.htmlContent = null; } + public String getExcerpt() { + return excerpt; + } + + private void setExcerpt(String content) { + if (content.contains("\n")) { + String[] lines = content.split("\n"); + int currentLine = 1; + while (NoteUtil.isEmptyLine(lines[currentLine]) && currentLine < lines.length) { + currentLine++; + } + excerpt = NoteUtil.removeMarkDown(lines[currentLine]); + } else { + excerpt = content; + } + } + public String getHtmlContent() { if(htmlContent == null && getContent() != null) { - htmlContent = and_down.markdownToHtml(getContent()); + htmlContent = NoteUtil.parseMarkDown(getContent()); } return htmlContent; } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/model/NoteAdapter.java b/app/src/main/java/it/niedermann/owncloud/notes/model/NoteAdapter.java index a4b92915..dbc06d4b 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/model/NoteAdapter.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/model/NoteAdapter.java @@ -51,18 +51,18 @@ public class NoteAdapter extends ArrayAdapter { // These TextViews are created in the XML files we defined. TextView noteTitle = (TextView) v.findViewById(R.id.noteTitle); - TextView noteModified = (TextView) v - .findViewById(R.id.noteModified); + TextView noteExcerpt = (TextView) v + .findViewById(R.id.noteExcerpt); // check to see if each individual textview is null. // if not, assign some text! if (noteTitle != null) { noteTitle.setText(note.getTitle()); } - if (noteModified != null) { - noteModified.setText(note.getModified("dd.MM.yyyy HH:mm")); - } - } + if (noteExcerpt != null) { + noteExcerpt.setText(note.getExcerpt()); + } + } // the view must be returned to our activity return v; diff --git a/app/src/main/java/it/niedermann/owncloud/notes/util/NoteUtil.java b/app/src/main/java/it/niedermann/owncloud/notes/util/NoteUtil.java new file mode 100644 index 00000000..dcac3e25 --- /dev/null +++ b/app/src/main/java/it/niedermann/owncloud/notes/util/NoteUtil.java @@ -0,0 +1,49 @@ +package it.niedermann.owncloud.notes.util; + +import android.text.Html; + +import com.commonsware.cwac.anddown.AndDown; + +/** + * Provides basic functionality for Note operations. + * Created by stefan on 06.10.15. + */ +public class NoteUtil { + + private static final AndDown and_down = new AndDown(); + + /** + * Parses a MarkDown-String and returns its HTML-Pendant + * + * @param s String MarkDown + * @return String HTML + */ + public static String parseMarkDown(String s) { + return and_down.markdownToHtml(s); + } + + /** + * Strips all MarkDown from the given String + * + * @param s String - MarkDown + * @return Plain Text-String + */ + public static String removeMarkDown(String s) { + return s == null ? "" : Html.fromHtml(and_down.markdownToHtml(s)).toString().trim(); + } + + /** + * Checks if a line is empty. + * " " -> empty + * "\n" -> empty + * "\n " -> empty + * " \n" -> empty + * " \n " -> empty + * + * @param line String - a single Line which ends with \n + * @return boolean isEmpty + */ + public static boolean isEmptyLine(String line) { + return removeMarkDown(line).trim().length() == 0; + } +} diff --git a/app/src/main/res/layout/fragment_notes_list_view.xml b/app/src/main/res/layout/fragment_notes_list_view.xml index c37f00e8..88fbfeb7 100644 --- a/app/src/main/res/layout/fragment_notes_list_view.xml +++ b/app/src/main/res/layout/fragment_notes_list_view.xml @@ -1,17 +1,18 @@ - + android:padding="16dp">