Search in sources :

Example 36 with Book

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

the class SyncFragment method forceSaveBook.

/**
 * Saves book to its linked remote book, or to the one-and-only repository .
 */
@SuppressLint("StaticFieldLeak")
public void forceSaveBook(final long bookId) {
    new AsyncTask<Void, Void, Object>() {

        @Override
        protected Object doInBackground(Void... params) {
            Book book = BooksClient.get(getActivity(), bookId);
            if (book != null) {
                try {
                    String repoUrl;
                    String fileName;
                    /* Prefer link. */
                    if (book.hasLink()) {
                        repoUrl = book.getLinkRepo().toString();
                    } else {
                        repoUrl = repoForSavingBook();
                    }
                    fileName = BookName.getFileName(getContext(), book);
                    mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.PROGRESS, resources.getString(R.string.force_saving_to_uri, repoUrl)));
                    return mShelf.saveBookToRepo(repoUrl, fileName, book, BookName.Format.ORG);
                } catch (Exception e) {
                    e.printStackTrace();
                    mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.ERROR, resources.getString(R.string.force_saving_failed, e.getLocalizedMessage())));
                    return e;
                }
            } else {
                return new IOException(resources.getString(R.string.book_does_not_exist_anymore));
            }
        }

        @Override
        protected void onPostExecute(Object result) {
            if (mListener != null) {
                if (result instanceof Book) {
                    Book book = (Book) result;
                    mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.INFO, resources.getString(R.string.force_saved_to_uri, book.getLastSyncedToRook().getUri())));
                    mListener.onBookSaved(book);
                } else {
                    mListener.onBookForceSavingFailed((Exception) result);
                }
            } else {
                Log.w(TAG, "Listener not set, not handling saveBookToRepo result");
            }
        }
    }.execute();
}
Also used : BookAction(com.orgzly.android.BookAction) Book(com.orgzly.android.Book) IOException(java.io.IOException) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint)

Example 37 with Book

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

the class SyncFragment method forceLoadBook.

/**
 * Load book from repository.
 */
@SuppressLint("StaticFieldLeak")
public void forceLoadBook(final long bookId) {
    new AsyncTask<Void, Object, Object>() {

        @Override
        protected Object doInBackground(Void... params) {
            Book book = BooksClient.get(getActivity(), bookId);
            try {
                if (book == null) {
                    throw new IOException(resources.getString(R.string.book_does_not_exist_anymore));
                }
                if (book.getLinkRepo() == null) {
                    throw new IOException(resources.getString(R.string.message_book_has_no_link));
                }
                mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.PROGRESS, resources.getString(R.string.force_loading_from_uri, book.getLinkRepo())));
                return mShelf.loadBookFromRepo(book.getLinkRepo(), BookName.getFileName(getContext(), book));
            } catch (Exception e) {
                e.printStackTrace();
                mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.ERROR, resources.getString(R.string.force_loading_failed, e.getLocalizedMessage())));
                return e;
            }
        }

        @Override
        protected void onPostExecute(Object result) {
            if (mListener != null) {
                if (result instanceof Book) {
                    Book book = (Book) result;
                    // TODO: Do in bg
                    mShelf.setBookStatus(book, null, new BookAction(BookAction.Type.INFO, resources.getString(R.string.force_loaded_from_uri, book.getLastSyncedToRook().getUri())));
                }
            }
        }
    }.execute();
}
Also used : BookAction(com.orgzly.android.BookAction) Book(com.orgzly.android.Book) IOException(java.io.IOException) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint)

Example 38 with Book

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

the class ProviderTest method testLinkingBook.

@Test
public void testLinkingBook() {
    shelfTestUtils.setupRepo("mock://repo-a");
    shelfTestUtils.setupBook("book-1", "Local content for book 1");
    shelfTestUtils.setBookLink(1, "mock://repo-a");
    Book book = shelf.getBook(1);
    assertNotNull(book.getLinkRepo());
    assertNull(book.getLastSyncedToRook());
}
Also used : Book(com.orgzly.android.Book) OrgzlyTest(com.orgzly.android.OrgzlyTest) Test(org.junit.Test)

Example 39 with Book

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

the class ProviderTest method testCreatingNotes.

@Test
public void testCreatingNotes() throws IOException {
    Book book = shelfTestUtils.setupBook("notebook", "* Note");
    NotePosition root, note1, note2;
    note1 = NotesClient.getNote(context, 1).getPosition();
    root = NotesClient.getNote(context, 2).getPosition();
    assertEquals(1, NotesClient.getCount(context, book.getId()));
    assertNotNull(root);
    assertEquals(1, root.getLft());
    assertEquals(6, note1.getLft());
    assertEquals(11, note1.getRgt());
    assertEquals(16, root.getRgt());
    Note note = new Note();
    note.getPosition().setBookId(book.getId());
    NotesClient.create(context, note, null, System.currentTimeMillis());
    note1 = NotesClient.getNote(context, 1).getPosition();
    root = NotesClient.getNote(context, 2).getPosition();
    note2 = NotesClient.getNote(context, 3).getPosition();
    assertEquals(2, NotesClient.getCount(context, book.getId()));
    assertNotNull(note2);
    assertTrue(root.getLft() < note1.getLft());
    assertTrue(note1.getLft() < note1.getRgt());
    assertTrue(note1.getRgt() < note2.getLft());
    assertTrue(note2.getLft() < note2.getRgt());
    assertTrue(note2.getRgt() < root.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 40 with Book

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

the class ProviderTest method testGetFirstNoteIdForBook.

@Test
public void testGetFirstNoteIdForBook() throws IOException {
    shelfTestUtils.setupBook("notebook", "* Note 1\n* Note 2\n* Note 3");
    Book book = shelf.getBook(1);
    assertEquals(1, NotesClient.getFirstNoteId(context, book.getId()));
    /* Delete the first note. */
    NotesClient.delete(context, new long[] { 1L });
    assertEquals(2, NotesClient.getFirstNoteId(context, book.getId()));
    /* Delete all other notes. */
    NotesClient.delete(context, new long[] { 2L, 3L });
    assertEquals(0, NotesClient.getFirstNoteId(context, book.getId()));
}
Also used : Book(com.orgzly.android.Book) OrgzlyTest(com.orgzly.android.OrgzlyTest) Test(org.junit.Test)

Aggregations

Book (com.orgzly.android.Book)60 OrgzlyTest (com.orgzly.android.OrgzlyTest)47 Test (org.junit.Test)47 Note (com.orgzly.android.Note)21 NotePosition (com.orgzly.android.NotePosition)11 IOException (java.io.IOException)8 Shelf (com.orgzly.android.Shelf)6 Uri (android.net.Uri)5 Context (android.content.Context)4 Intent (android.content.Intent)4 TypedArray (android.content.res.TypedArray)4 NavigationView (android.support.design.widget.NavigationView)4 SearchView (android.support.v7.widget.SearchView)4 MenuItem (android.view.MenuItem)4 View (android.view.View)4 TextView (android.widget.TextView)4 Query (com.orgzly.android.query.Query)4 File (java.io.File)4 Activity (android.app.Activity)3 AlertDialog (android.app.AlertDialog)3