Search in sources :

Example 6 with NotePlace

use of com.orgzly.android.ui.NotePlace 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 7 with NotePlace

use of com.orgzly.android.ui.NotePlace 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

NotePlace (com.orgzly.android.ui.NotePlace)7 Book (com.orgzly.android.Book)4 Note (com.orgzly.android.Note)4 NotePosition (com.orgzly.android.NotePosition)4 OrgzlyTest (com.orgzly.android.OrgzlyTest)4 Test (org.junit.Test)4 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 DbNoteView (com.orgzly.android.provider.views.DbNoteView)1 HeadsListViewAdapter (com.orgzly.android.ui.HeadsListViewAdapter)1 Selection (com.orgzly.android.ui.Selection)1 GesturedListView (com.orgzly.android.ui.views.GesturedListView)1