use of com.orgzly.android.Book in project orgzly-android by orgzly.
the class BooksClient method fromCursor.
public static Book fromCursor(Cursor cursor) {
Book book = new Book(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.NAME)), cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.PREFACE)), cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.MTIME)), cursor.getInt(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.IS_DUMMY)) == 1);
book.getOrgFileSettings().setTitle(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.TITLE)));
book.getOrgFileSettings().setIndented(cursor.getInt(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.IS_INDENTED)) == 1);
book.setId(cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param._ID)));
book.setSyncStatus(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNC_STATUS)));
book.setDetectedEncoding(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.DETECTED_ENCODING)));
book.setSelectedEncoding(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SELECTED_ENCODING)));
book.setUsedEncoding(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.USED_ENCODING)));
/* Set link. */
int linkRepoUriColumn = cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LINK_REPO_URL);
if (!cursor.isNull(linkRepoUriColumn)) {
Uri uri = Uri.parse(cursor.getString(linkRepoUriColumn));
book.setLinkRepo(uri);
}
/* Set versioned rook. */
if (!cursor.isNull(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_URL))) {
Uri syncRookUri = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_URL)));
Uri syncRepoUri = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_REPO_URL)));
String rev = cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_REVISION));
long mtime = cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.SYNCED_ROOK_MTIME));
VersionedRook vrook = new VersionedRook(syncRepoUri, syncRookUri, rev, mtime);
book.setLastSyncedToRook(vrook);
}
/* Set last action. */
String lastActionMessage = cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LAST_ACTION));
if (!TextUtils.isEmpty(lastActionMessage)) {
BookAction.Type lastActionType = BookAction.Type.valueOf(cursor.getString(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LAST_ACTION_TYPE)));
long lastActionTimestamp = cursor.getLong(cursor.getColumnIndexOrThrow(ProviderContract.Books.Param.LAST_ACTION_TIMESTAMP));
BookAction lastAction = new BookAction(lastActionType, lastActionMessage, lastActionTimestamp);
book.setLastAction(lastAction);
}
return book;
}
use of com.orgzly.android.Book in project orgzly-android by orgzly.
the class BookNamesake method getAll.
/**
* Create links between each local book and each remote book with the same name.
*/
public static Map<String, BookNamesake> getAll(Context context, List<Book> books, List<VersionedRook> versionedRooks) {
Map<String, BookNamesake> namesakes = new HashMap<>();
/* Create links from all local books first. */
for (Book book : books) {
BookNamesake pair = new BookNamesake(book.getName());
namesakes.put(book.getName(), pair);
pair.setBook(book);
}
/* Set repo books. */
for (VersionedRook book : versionedRooks) {
String fileName = BookName.getFileName(context, book.getUri());
String name = BookName.fromFileName(fileName).getName();
BookNamesake pair = namesakes.get(name);
if (pair == null) {
/* Local file doesn't exists, create new pair. */
pair = new BookNamesake(name);
namesakes.put(name, pair);
}
/* Add remote book. */
pair.addRook(book);
}
return namesakes;
}
use of com.orgzly.android.Book in project orgzly-android by orgzly.
the class MainActivity method onBookRenameRequest.
@Override
public void onBookRenameRequest(final long bookId) {
final Book book = BooksClient.get(this, bookId);
if (book == null) {
return;
}
final View dialogView = View.inflate(this, R.layout.dialog_book_rename, null);
final TextInputLayout nameInputLayout = dialogView.findViewById(R.id.name_input_layout);
final EditText name = dialogView.findViewById(R.id.name);
DialogInterface.OnClickListener dialogClickListener = (dialog, which) -> {
switch(which) {
case DialogInterface.BUTTON_POSITIVE:
doRenameBook(book, name.getText().toString(), nameInputLayout);
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(getString(R.string.rename_book, MiscUtils.quotedString(book.getName()))).setPositiveButton(R.string.rename, dialogClickListener).setNegativeButton(R.string.cancel, dialogClickListener).setView(dialogView);
name.setText(book.getName());
final AlertDialog dialog = builder.create();
/* Finish on keyboard action press. */
name.setOnEditorActionListener((v, actionId, event) -> {
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return true;
});
final Activity activity = this;
dialog.setOnShowListener(d -> ActivityUtils.INSTANCE.openSoftKeyboard(activity, name));
dialog.setOnDismissListener(d -> ActivityUtils.INSTANCE.closeSoftKeyboard(activity));
name.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable str) {
/* Disable the button is nothing is entered. */
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!TextUtils.isEmpty(str));
}
});
dialog.show();
}
use of com.orgzly.android.Book in project orgzly-android by orgzly.
the class ShareActivity method getBooksList.
/**
* Collects list of books from database.
* If there are no books available, create one.
*/
private List<Book> getBooksList() {
Shelf shelf = new Shelf(this);
List<Book> books = shelf.getBooks();
if (books.size() == 0) {
try {
Book book = shelf.createBook(AppPreferences.shareNotebook(getApplicationContext()));
books.add(book);
} catch (IOException e) {
// TODO: Test and handle better.
e.printStackTrace();
finish();
}
}
return books;
}
use of com.orgzly.android.Book in project orgzly-android by orgzly.
the class ProviderTest method testCreatingFirstEmptyBook.
@Test
public void testCreatingFirstEmptyBook() throws IOException {
shelfTestUtils.setupBook("notebook", "");
Book book = shelf.getBook(1);
assertEquals("There should be one book", 1, shelf.getBooks().size());
assertEquals("Book should have id 1", 1, book.getId());
assertEquals("Book should not contain any notes", 0, NotesClient.getCount(context, book.getId()));
}
Aggregations