Skip to content

Commit

Permalink
Merge branch 'release-1.7.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
nevenz committed Jun 10, 2019
2 parents 119ff42 + 6d37d99 commit 425a930
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 42 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
minSdkVersion 16 // Jelly Bean (4.1)
targetSdkVersion 28 // Android Pie (9)
applicationId "com.orgzly"
versionCode 136
versionName "1.7.4"
versionCode 137
versionName "1.7.5"

testInstrumentationRunner "com.orgzly.android.OrgzlyTestRunner"
// testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
87 changes: 62 additions & 25 deletions app/src/androidTest/java/com/orgzly/android/misc/StructureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1262,45 +1262,30 @@ public void testNewNoteAfterMovingUnder() throws IOException {
@Test
public void testMoveNoteDown() throws IOException {
BookView book = testUtils.setupBook(
"test_book",
"* TODO First\n" +
"SCHEDULED: <2018-04-24 Tue>\n" +
"\n" +
"content\n" +
"\n" +
"Book A",
"* First\n" + // Move down
"** 1.1\n" +
"** 1.2\n" +
"\n" +
"* TODO Second\n" +
"SCHEDULED: <2018-04-23 Mon>\n" +
"\n" +
"* Second\n" +
"** 2.1\n" +
"** 2.2\n" +
"\n" +
"* TODO Third\n"

"* Third\n"
);

Note firstNote = dataRepository.getLastNote("First");

UseCaseRunner.run(new NoteMove(
book.getBook().getId(), Collections.singleton(firstNote.getId()), 1));

String actual = dataRepository.getBookContent("test_book", BookFormat.ORG);
String actual = dataRepository.getBookContent("Book A", BookFormat.ORG);

String expectedBook = "* TODO Second\n" +
"SCHEDULED: <2018-04-23 Mon>\n" +
"\n" +
String expectedBook = "* Second\n" +
"** 2.1\n" +
"** 2.2\n" +
"* TODO First\n" +
"SCHEDULED: <2018-04-24 Tue>\n" +
"\n" +
"content\n" +
"\n" +
"* First\n" +
"** 1.1\n" +
"** 1.2\n" +
"* TODO Third\n";
"* Third\n";

assertEquals(expectedBook, actual);
}
Expand Down Expand Up @@ -1461,9 +1446,9 @@ public void demoteMultiple() throws IOException {
BookView book = testUtils.setupBook(
"Book A",
"* Note A-01\n" +
"* Note A-02\n" +
"* Note A-02\n" + // (1) Demote
"* Note A-03\n" +
"** Note A-04\n" +
"** Note A-04\n" + // (1) Demote
"*** Note A-05\n");

UseCaseRunner.run(new NoteDemote(
Expand Down Expand Up @@ -1563,4 +1548,56 @@ public void keptNotePropertiesAndNoteEvents() throws IOException {
assertEquals(1, dataRepository.getNoteEvents(noteId).size());
assertEquals(2, dataRepository.getNoteProperties(noteId).size());
}


@Test
public void testSubtreesAligned() throws IOException {
BookView book = testUtils.setupBook(
"Book A",
"* Note A-01\n" +
"* Note A-02\n" + // (2) Refile under A-01
"** Note A-03\n" + // (1) Move up and down
"** Note A-04\n");

Note a01 = dataRepository.getLastNote("Note A-01");
Note a02 = dataRepository.getLastNote("Note A-02");
Note a03 = dataRepository.getLastNote("Note A-03");
Note a04 = dataRepository.getLastNote("Note A-04");

UseCaseRunner.run(new NoteMove(
book.getBook().getId(), Collections.singleton(a03.getId()), 1));
UseCaseRunner.run(new NoteMove(
book.getBook().getId(), Collections.singleton(a03.getId()), -1));
UseCaseRunner.run(new NoteMove(
book.getBook().getId(), Collections.singleton(a03.getId()), 1));
UseCaseRunner.run(new NoteMove(
book.getBook().getId(), Collections.singleton(a03.getId()), -1));

UseCaseRunner.run(new NoteRefile(
Collections.singleton(a02.getId()),
new NotePlace(book.getBook().getId(), a01.getId(), Place.UNDER)));

String actual = dataRepository.getBookContent("Book A", BookFormat.ORG);

String expectedBook =
"* Note A-01\n" +
"** Note A-02\n" +
"*** Note A-03\n" +
"*** Note A-04\n";

assertEquals(expectedBook, actual);

a01 = dataRepository.getLastNote("Note A-01");
a02 = dataRepository.getLastNote("Note A-02");
a03 = dataRepository.getLastNote("Note A-03");
a04 = dataRepository.getLastNote("Note A-04");

assertTrue(a01.getPosition().getLft() < a02.getPosition().getLft());
assertTrue(a02.getPosition().getLft() < a03.getPosition().getLft());
assertTrue(a03.getPosition().getLft() < a03.getPosition().getRgt());
assertTrue(a03.getPosition().getRgt() < a04.getPosition().getLft());
assertTrue(a04.getPosition().getLft() < a04.getPosition().getRgt());
assertTrue(a04.getPosition().getRgt() < a02.getPosition().getRgt());
assertTrue(a02.getPosition().getRgt() < a01.getPosition().getRgt());
}
}
111 changes: 96 additions & 15 deletions app/src/main/java/com/orgzly/android/data/DataRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import com.orgzly.BuildConfig
import com.orgzly.R
import com.orgzly.android.*
import com.orgzly.android.data.mappers.OrgMapper
import com.orgzly.android.db.OrgzlyDatabase
import com.orgzly.android.db.NotesClipboard
import com.orgzly.android.db.OrgzlyDatabase
import com.orgzly.android.db.dao.NoteDao
import com.orgzly.android.db.dao.NoteViewDao
import com.orgzly.android.db.dao.ReminderTimeDao
Expand All @@ -40,13 +40,18 @@ import com.orgzly.android.ui.NotePlace
import com.orgzly.android.ui.Place
import com.orgzly.android.ui.note.NoteBuilder
import com.orgzly.android.ui.note.NotePayload
import com.orgzly.android.ui.notes.book.BookFragment
import com.orgzly.android.usecase.RepoCreate
import com.orgzly.android.util.*
import com.orgzly.org.*
import com.orgzly.org.OrgActiveTimestamps
import com.orgzly.org.OrgFile
import com.orgzly.org.OrgFileSettings
import com.orgzly.org.OrgProperties
import com.orgzly.org.datetime.OrgDateTime
import com.orgzly.org.datetime.OrgRange
import com.orgzly.org.parser.*
import com.orgzly.org.parser.OrgNestedSetParserListener
import com.orgzly.org.parser.OrgNodeInSet
import com.orgzly.org.parser.OrgParser
import com.orgzly.org.parser.OrgParserWriter
import com.orgzly.org.utils.StateChangeLogic
import java.io.*
import java.util.*
Expand Down Expand Up @@ -691,29 +696,105 @@ class DataRepository @Inject constructor(
return pastedNoteIds.size
}

data class NoteWithPosition(val note: Note, val level: Int, val lft: Long, val rgt: Long)

fun getSubtreesAligned(ids: Set<Long>): List<Note> {
var offset = 0L
var subtreeRgt = 0L
var levelOffset = 0

return db.note().getNotesForSubtrees(ids).map { note ->
var sequence = 0L
var prevLevel = 0

val stack = Stack<NoteWithPosition>()

val notesPerLft = TreeMap<Long, Note>()

fun output(note: Note, level: Int, lft: Long, rgt: Long) {
notesPerLft[lft] = note.copy(
position = note.position.copy(level = level, lft = lft, rgt = rgt))
}

var notes = 0

db.note().getNotesForSubtrees(ids).forEach { note ->
notes++

// First note or next subtree
if (subtreeRgt == 0L || note.position.rgt > subtreeRgt) {
offset += note.position.lft - subtreeRgt - 1
levelOffset = note.position.level - 1
subtreeRgt = note.position.rgt
levelOffset = note.position.level - 1

while (! stack.empty()) {
val popped = stack.pop()

sequence++

output(popped.note, popped.level, popped.lft, sequence)
}

prevLevel = 0
}

val level = note.position.level - levelOffset
val lft = note.position.lft - offset
val rgt = note.position.rgt - offset

note.copy(
position = note.position.copy(
level = level,
lft = lft,
rgt = rgt))
if (prevLevel < level) {
sequence++

val lft = sequence
val rgt = 0L // Unknown yet

stack.push(NoteWithPosition(note, level, lft, rgt))

} else if (prevLevel == level) {
sequence++

val popped = stack.pop()

output(popped.note, popped.level, popped.lft, sequence)

sequence++

val lft = sequence
val rgt = 0L // Unknown yet

stack.push(NoteWithPosition(note, level, lft, rgt))

} else {
while (!stack.empty()) {
val popped = stack.peek()

if (popped.level >= level) {
stack.pop()

sequence++

output(popped.note, popped.level, popped.lft, sequence)

} else {
break
}
}

sequence++

val lft = sequence
val rgt = 0L // Unknown yet

stack.push(NoteWithPosition(note, level, lft, rgt))
}

prevLevel = level
}

while (! stack.empty()) {
val popped = stack.pop()

sequence++

output(popped.note, popped.level, popped.lft, sequence)
}

return notesPerLft.values.toList()
}

private fun moveSubtrees(selectedIds: Set<Long>, place: Place, targetNoteId: Long): Int {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/layout/dialog_whats_new.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
android:text="@string/whats_new_intro"/>


<TextView
style="@style/WhatsNewVersion"
android:text="v1.7.5"
tools:ignore="HardcodedText" />

<com.orgzly.android.ui.views.WhatsNewChange
style="@style/WhatsNewChange"
app:text="Fix moving notes around causing invalid tree structure in some cases" />


<TextView
style="@style/WhatsNewVersion"
android:text="v1.7.4"
Expand Down

0 comments on commit 425a930

Please sign in to comment.