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();
}
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();
}
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());
}
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());
}
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()));
}
Aggregations