Skip to content

Commit

Permalink
calculate start and end offset of each note part
Browse files Browse the repository at this point in the history
  • Loading branch information
artbristol committed Apr 1, 2021
1 parent 8081ff4 commit 50b066e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.orgzly.android.ui.notes

import com.orgzly.android.ui.notes.NoteContent.TableNoteContent
import com.orgzly.android.ui.notes.NoteContent.TextNoteContent
import org.hamcrest.Matchers.emptyCollectionOf
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThat
Expand All @@ -19,7 +17,7 @@ class NoteContentTest {

@Test
fun emptyLinesShouldStayInSingleSection() {
checkExpected("\n\n", listOf(TextNoteContent("\n\n")))
checkExpected("\n\n", listOf(NoteContent("\n\n", 0, 1, NoteContent.TextType.TEXT)))
}

@Test
Expand All @@ -28,36 +26,36 @@ class NoteContentTest {
|
foo|bar""", listOf(
TextNoteContent("foo\n"),
TableNoteContent("|\n"),
TextNoteContent("\nfoo|bar")
NoteContent("foo\n", 0, 3, NoteContent.TextType.TEXT),
NoteContent("|\n", 4, 5, NoteContent.TextType.TABLE),
NoteContent("\nfoo|bar", 6, 13, NoteContent.TextType.TEXT)
))
}

@Test
fun singleTable() {
checkExpected("""|a|b|
|c|d|
""", listOf(TableNoteContent("""|a|b|
""", listOf(NoteContent("""|a|b|
|c|d|
""")))
""", 0, 11, NoteContent.TextType.TABLE)))
}

@Test
fun singleTableNoFinalNewline() {
checkExpected("""|a|b|
|c|d|""", listOf(TableNoteContent("""|a|b|
|c|d|""")))
|c|d|""", listOf(NoteContent("""|a|b|
|c|d|""", 0, 10, NoteContent.TextType.TABLE)))
}

@Test
fun singleLineTextTableText() {
checkExpected("""foo
|
bar""", listOf(
TextNoteContent("foo\n"),
TableNoteContent("|\n"),
TextNoteContent("bar")
NoteContent("foo\n", 0, 3, NoteContent.TextType.TEXT),
NoteContent("|\n", 4, 5, NoteContent.TextType.TABLE),
NoteContent("bar", 6, 8, NoteContent.TextType.TEXT)
))
}

Expand All @@ -68,9 +66,9 @@ bar""", listOf(
|
bar
""", listOf(
TextNoteContent("\n"),
TableNoteContent("|\n"),
TextNoteContent("bar\n")
NoteContent("\n", 0, 0, NoteContent.TextType.TEXT),
NoteContent("|\n", 1, 2, NoteContent.TextType.TABLE),
NoteContent("bar\n", 3, 6, NoteContent.TextType.TEXT)
))
}

Expand All @@ -79,9 +77,9 @@ bar
checkExpected("""|zoo|
|zog|""", listOf(
TableNoteContent("|zoo|\n"),
TextNoteContent("\n"),
TableNoteContent("|zog|")
NoteContent("|zoo|\n", 0, 5, NoteContent.TextType.TABLE),
NoteContent("\n", 6, 6, NoteContent.TextType.TEXT),
NoteContent("|zog|", 7, 11, NoteContent.TextType.TABLE)
))
}

Expand All @@ -91,9 +89,9 @@ bar
|
chops""", listOf(
TextNoteContent("foo\n"),
TableNoteContent("|\n"),
TextNoteContent("\nchops")
NoteContent("foo\n", 0, 3, NoteContent.TextType.TEXT),
NoteContent("|\n", 4, 5, NoteContent.TextType.TABLE),
NoteContent("\nchops", 6, 11, NoteContent.TextType.TEXT)
))
}

Expand All @@ -109,11 +107,11 @@ text3c
|table4|
text5
""", listOf(
TextNoteContent("text1\n"),
TableNoteContent("|table2a|\n|table2b|\n"),
TextNoteContent("text3a\ntext3b\ntext3c\n"),
TableNoteContent("|table4|\n"),
TextNoteContent("text5\n")
NoteContent("text1\n", 0, 5, NoteContent.TextType.TEXT),
NoteContent("|table2a|\n|table2b|\n", 6, 25, NoteContent.TextType.TABLE),
NoteContent("text3a\ntext3b\ntext3c\n", 26, 46, NoteContent.TextType.TEXT),
NoteContent("|table4|\n", 47, 55, NoteContent.TextType.TABLE),
NoteContent("text5\n", 56, 61, NoteContent.TextType.TEXT)
))
}

Expand Down Expand Up @@ -150,5 +148,8 @@ text5

assertEquals(input, roundTripped)

actual.forEach {
assertEquals(it.text, input.substring(it.startOffset, it.endOffset + 1))
}
}
}
33 changes: 16 additions & 17 deletions app/src/main/java/com/orgzly/android/ui/notes/NoteContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,10 @@ package com.orgzly.android.ui.notes
/**
* Represents a subsection of note content: either text, or a table
*/
sealed class NoteContent {
data class NoteContent(val text: String, val startOffset: Int, val endOffset: Int, val textType: TextType) {

abstract val text: String

data class TextNoteContent(override val text: String) : NoteContent() {
}

data class TableNoteContent(override val text: String) : NoteContent() {

fun reformat() {
// placeholder - but would fix all the spacing, missing cells, etc. Complicated
}
enum class TextType {
TEXT, TABLE
}


Expand Down Expand Up @@ -55,12 +47,14 @@ sealed class NoteContent {
}
currentIsTable && !previousIsTable -> {
currentTable = it + "\n"
list.add(TextNoteContent(currentText))
val startOffset = getLastOffset(list)
list.add(NoteContent(currentText, startOffset, startOffset + currentText.length - 1, TextType.TEXT))
currentText = ""
}
!currentIsTable && previousIsTable -> {
currentText = it + "\n"
list.add(TableNoteContent(currentTable))
val startOffset = getLastOffset(list)
list.add(NoteContent(currentTable, startOffset, startOffset + currentTable.length - 1, TextType.TABLE))
currentTable = ""
}
!currentIsTable && !previousIsTable -> {
Expand All @@ -71,18 +65,23 @@ sealed class NoteContent {
}

if (linesForParsing.isNotEmpty()) {

val endOffsetAdjustment = if (missingLastNewline) 2 else 1

if (previousIsTable) {
list.add(TableNoteContent(if (missingLastNewline) {
list.add(NoteContent(if (missingLastNewline) {
currentTable.dropLast(1)
} else currentTable))
} else currentTable, getLastOffset(list), getLastOffset(list) + currentTable.length - endOffsetAdjustment, TextType.TABLE))
} else {
list.add(TextNoteContent(if (missingLastNewline) {
list.add(NoteContent(if (missingLastNewline) {
currentText.dropLast(1)
} else currentText))
} else currentText, getLastOffset(list), getLastOffset(list) + currentText.length - endOffsetAdjustment, TextType.TEXT))
}
}

return list
}

private fun getLastOffset(list: MutableList<NoteContent>) = if (list.isEmpty()) 0 else list.last().endOffset + 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ class NoteItemViewBinder(private val context: Context, private val inBook: Boole
val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

alternatingTableAndTextContent.forEach { noteContent ->
when (noteContent) {
is NoteContent.TableNoteContent -> {
when (noteContent.textType) {
NoteContent.TextType.TABLE -> {

val noteContentSectionTableTextView = layoutInflater.inflate(R.layout.item_note_content_section_table, linearLayout, false)

noteContentSectionTableTextView.findViewById<TextView>(R.id.note_content_section_table_text).text = noteContent.text

linearLayout.addView(noteContentSectionTableTextView)
}
else -> {
NoteContent.TextType.TEXT -> {

val layout = layoutInflater.inflate(R.layout.item_note_content_section_text, linearLayout, false)

Expand Down

0 comments on commit 50b066e

Please sign in to comment.