Search in sources :

Example 16 with NotePosition

use of com.orgzly.android.NotePosition in project orgzly-android by orgzly.

the class DemoteNotesAction method demote.

private int demote(SQLiteDatabase db) {
    Cursor cursor;
    NotePosition note;
    long previousSiblingId;
    ContentValues values;
    /* Get note info. */
    cursor = db.query(DbNote.TABLE, null, DbNote._ID + " IN (" + ids + ")", null, null, null, null);
    try {
        if (cursor.moveToFirst()) {
            note = DbNote.positionFromCursor(cursor);
        } else {
            return 0;
        }
    } finally {
        cursor.close();
    }
    previousSiblingId = DatabaseUtils.getPreviousSiblingId(db, note);
    if (previousSiblingId <= 0) {
        return 0;
    }
    /* Delete affected notes from ancestors table. */
    String w = "(SELECT " + DbNote._ID + " FROM " + DbNote.TABLE + " WHERE " + DatabaseUtils.whereDescendantsAndNotes(bookId, ids) + ")";
    String sql = "DELETE FROM " + DbNoteAncestor.TABLE + " WHERE " + DbNoteAncestor.NOTE_ID + " IN " + w;
    if (BuildConfig.LOG_DEBUG)
        LogUtils.d("SQL", sql);
    db.execSQL(sql);
    /* Cut note and all its descendants. */
    long batchId = System.currentTimeMillis();
    values = new ContentValues();
    values.put(DbNote.IS_CUT, batchId);
    db.update(DbNote.TABLE, values, DatabaseUtils.whereDescendantsAndNote(bookId, note.getLft(), note.getRgt()), null);
    /* Paste under previous sibling. */
    values = new ContentValues();
    values.put(ProviderContract.Paste.Param.BATCH_ID, batchId);
    values.put(ProviderContract.Paste.Param.NOTE_ID, previousSiblingId);
    values.put(ProviderContract.Paste.Param.SPOT, Place.UNDER.toString());
    new PasteNotesAction(values).run(db);
    DatabaseUtils.updateBookMtime(db, bookId);
    return 1;
}
Also used : ContentValues(android.content.ContentValues) NotePosition(com.orgzly.android.NotePosition) Cursor(android.database.Cursor)

Example 17 with NotePosition

use of com.orgzly.android.NotePosition in project orgzly-android by orgzly.

the class MoveNotesAction method run.

@Override
public int run(SQLiteDatabase db) {
    NotePosition selectedNotePosition = DbNote.getPosition(db, id);
    NotePlace notePlace = null;
    if (direction == -1) {
        /* Move up - paste above previous sibling. */
        Cursor cursor = db.query(DbNote.TABLE, new String[] { DbNote._ID, DbNote.LEVEL }, DatabaseUtils.whereUncutBookNotes(bookId) + " AND " + DbNote.RGT + " < " + selectedNotePosition.getLft(), null, null, null, DbNote.RGT + " DESC");
        try {
            if (cursor.moveToFirst()) {
                long prevNoteId = cursor.getLong(0);
                long prevNoteLevel = cursor.getLong(1);
                if (prevNoteLevel == selectedNotePosition.getLevel()) {
                    notePlace = new NotePlace(bookId, prevNoteId, Place.ABOVE);
                }
            }
        } finally {
            cursor.close();
        }
    } else {
        /* Move down - paste below next sibling. */
        Cursor cursor = db.query(DbNote.TABLE, new String[] { DbNote._ID, DbNote.LEVEL }, DatabaseUtils.whereUncutBookNotes(bookId) + " AND " + DbNote.LFT + " > " + selectedNotePosition.getRgt(), null, null, null, DbNote.LFT);
        try {
            if (cursor.moveToFirst()) {
                long nextNoteId = cursor.getLong(0);
                long nextNoteLevel = cursor.getLong(1);
                if (nextNoteLevel == selectedNotePosition.getLevel()) {
                    notePlace = new NotePlace(bookId, nextNoteId, Place.BELOW);
                }
            }
        } finally {
            cursor.close();
        }
    }
    if (notePlace != null) {
        ContentValues values;
        /* Delete affected notes from ancestors table. */
        String w = "(SELECT " + DbNote._ID + " FROM " + DbNote.TABLE + " WHERE " + DatabaseUtils.whereDescendantsAndNote(bookId, selectedNotePosition.getLft(), selectedNotePosition.getRgt()) + ")";
        String sql = "DELETE FROM " + DbNoteAncestor.TABLE + " WHERE " + DbNoteAncestor.NOTE_ID + " IN " + w;
        if (BuildConfig.LOG_DEBUG)
            LogUtils.d("SQL", sql);
        db.execSQL(sql);
        /* Cut note and all its descendants. */
        long batchId = System.currentTimeMillis();
        values = new ContentValues();
        values.put(DbNote.IS_CUT, batchId);
        db.update(DbNote.TABLE, values, DatabaseUtils.whereDescendantsAndNote(bookId, selectedNotePosition.getLft(), selectedNotePosition.getRgt()), null);
        /* Paste. */
        values = new ContentValues();
        values.put(ProviderContract.Paste.Param.BATCH_ID, batchId);
        values.put(ProviderContract.Paste.Param.NOTE_ID, notePlace.getNoteId());
        values.put(ProviderContract.Paste.Param.SPOT, notePlace.getPlace().toString());
        new PasteNotesAction(values).run(db);
        DatabaseUtils.updateBookMtime(db, bookId);
    }
    return 0;
}
Also used : ContentValues(android.content.ContentValues) NotePosition(com.orgzly.android.NotePosition) NotePlace(com.orgzly.android.ui.NotePlace) Cursor(android.database.Cursor)

Example 18 with NotePosition

use of com.orgzly.android.NotePosition in project orgzly-android by orgzly.

the class ShareActivityTest method testNoteInsertedLast.

@Test
public void testNoteInsertedLast() {
    shelfTestUtils.setupBook("book-one", "* Note 1\n** Note 2");
    startActivityWithIntent(Intent.ACTION_SEND, "text/plain", "Note 3", null);
    onView(withId(R.id.done)).perform(click());
    NotePosition n1 = shelf.getNote("Note 1").getPosition();
    NotePosition n2 = shelf.getNote("Note 2").getPosition();
    NotePosition n3 = shelf.getNote("Note 3").getPosition();
    assertTrue(n1.getLft() < n2.getLft());
    assertTrue(n2.getLft() < n2.getRgt());
    assertTrue(n2.getRgt() < n1.getRgt());
    assertTrue(n1.getRgt() < n3.getLft());
    assertTrue(n3.getLft() < n3.getRgt());
}
Also used : NotePosition(com.orgzly.android.NotePosition) OrgzlyTest(com.orgzly.android.OrgzlyTest) Test(org.junit.Test)

Example 19 with NotePosition

use of com.orgzly.android.NotePosition in project orgzly-android by orgzly.

the class StructureTest method testPromote2.

@Test
public void testPromote2() throws IOException {
    Book book = shelfTestUtils.setupBook("notebook", "" + "description\n" + "\n" + "* Note 1\n" + "** Note 1.1\n" + "*** Note 1.1.1\n" + "** Note 1.2\n" + "* Note 2\n");
    Note note = shelf.getNote("Note 1.1.1");
    /* Promote 1.1.1 twice. */
    assertEquals(1, shelf.promote(book.getId(), note.getId()));
    assertEquals(1, shelf.promote(book.getId(), note.getId()));
    assertEquals("description\n" + "\n" + "* Note 1\n" + "** Note 1.1\n" + "** Note 1.2\n" + "* Note 1.1.1\n" + "* Note 2\n", shelf.getBookContent("notebook", BookName.Format.ORG));
    NotePosition n1 = shelf.getNote("Note 1").getPosition();
    NotePosition n11 = shelf.getNote("Note 1.1").getPosition();
    NotePosition n12 = shelf.getNote("Note 1.2").getPosition();
    NotePosition n111 = shelf.getNote("Note 1.1.1").getPosition();
    NotePosition n2 = shelf.getNote("Note 2").getPosition();
    assertEquals(2, n1.getDescendantsCount());
    assertEquals(0, n11.getDescendantsCount());
    assertEquals(0, n12.getDescendantsCount());
    assertEquals(0, n111.getDescendantsCount());
    assertEquals(0, n2.getDescendantsCount());
    assertEquals(1, n1.getLevel());
    assertEquals(2, n11.getLevel());
    assertEquals(2, n12.getLevel());
    assertEquals(1, n111.getLevel());
    assertEquals(1, n2.getLevel());
    assertTrue(n1.getLft() < n11.getLft());
    assertTrue(n11.getLft() < n11.getRgt());
    assertTrue(n11.getRgt() < n12.getLft());
    assertTrue(n12.getLft() < n12.getRgt());
    assertTrue(n12.getRgt() < n1.getRgt());
    assertTrue(n1.getRgt() < n111.getLft());
    assertTrue(n111.getLft() < n111.getRgt());
    assertTrue(n111.getRgt() < n2.getLft());
    assertTrue(n2.getLft() < n2.getRgt());
}
Also used : Book(com.orgzly.android.Book) NotePosition(com.orgzly.android.NotePosition) Note(com.orgzly.android.Note) OrgzlyTest(com.orgzly.android.OrgzlyTest) Test(org.junit.Test)

Example 20 with NotePosition

use of com.orgzly.android.NotePosition in project orgzly-android by orgzly.

the class StructureTest method testNewNoteAbove.

@Test
public void testNewNoteAbove() throws IOException {
    Book book = shelfTestUtils.setupBook("notebook", "description\n" + "* Note 1\n" + // ** Note 1.0
    "** Note 1.1\n" + "*** Note 1.1.1\n" + "** Note 1.2\n");
    NotePosition note1, note11, note111, note12, note10;
    /* Create new note above Note 1.1. */
    Note n = new Note();
    n.getPosition().setBookId(book.getId());
    n.getHead().setTitle("Note 1.0");
    NotePlace target = new NotePlace(book.getId(), shelf.getNote("Note 1.1").getId(), Place.ABOVE);
    shelf.createNote(n, target);
    note1 = shelf.getNote("Note 1").getPosition();
    note10 = shelf.getNote("Note 1.0").getPosition();
    note11 = shelf.getNote("Note 1.1").getPosition();
    note111 = shelf.getNote("Note 1.1.1").getPosition();
    note12 = shelf.getNote("Note 1.2").getPosition();
    assertTrue(note1.getLft() < note10.getLft());
    assertTrue(note10.getLft() < note10.getRgt());
    assertTrue(note10.getRgt() < note11.getLft());
    assertTrue(note11.getLft() < note111.getLft());
    assertTrue(note111.getLft() < note111.getRgt());
    assertTrue(note111.getRgt() < note11.getRgt());
    assertTrue(note11.getRgt() < note12.getLft());
    assertTrue(note12.getLft() < note12.getRgt());
    assertTrue(note12.getRgt() < note1.getRgt());
}
Also used : Book(com.orgzly.android.Book) NotePosition(com.orgzly.android.NotePosition) Note(com.orgzly.android.Note) NotePlace(com.orgzly.android.ui.NotePlace) OrgzlyTest(com.orgzly.android.OrgzlyTest) Test(org.junit.Test)

Aggregations

NotePosition (com.orgzly.android.NotePosition)22 OrgzlyTest (com.orgzly.android.OrgzlyTest)13 Test (org.junit.Test)13 Book (com.orgzly.android.Book)11 Note (com.orgzly.android.Note)11 Cursor (android.database.Cursor)6 ContentValues (android.content.ContentValues)5 NotePlace (com.orgzly.android.ui.NotePlace)4 SuppressLint (android.annotation.SuppressLint)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 Uri (android.net.Uri)1 DbNote (com.orgzly.android.provider.models.DbNote)1 SqliteQueryBuilder (com.orgzly.android.query.sql.SqliteQueryBuilder)1 OrgFile (com.orgzly.org.OrgFile)1 OrgHead (com.orgzly.org.OrgHead)1 OrgProperties (com.orgzly.org.OrgProperties)1 OrgNestedSetParserListener (com.orgzly.org.parser.OrgNestedSetParserListener)1 OrgNodeInSet (com.orgzly.org.parser.OrgNodeInSet)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1