use of com.orgzly.android.db.entity.Book in project orgzly-android by orgzly.
the class MainActivity method setupViewModel.
private void setupViewModel() {
sharedMainActivityViewModel.getDrawerLockState().observe(this, isLocked -> {
DrawerLayout layout = mDrawerLayout;
if (layout != null) {
if (isLocked == null || !isLocked) {
layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
} else {
layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
});
sharedMainActivityViewModel.getFragmentState().observe(this, state -> {
if (BuildConfig.LOG_DEBUG)
LogUtils.d(TAG, "Observed fragment state: " + state);
if (state != null) {
getSupportActionBar().setTitle(state.getTitle());
// Clean up whitespace for multi-line query
CharSequence subTitle = state.getSubTitle();
if (subTitle != null) {
subTitle = subTitle.toString().replaceAll("\\s{2,}", " ");
}
getSupportActionBar().setSubtitle(subTitle);
drawerNavigationView.updateActiveFragment(state.getTag());
/* Update floating action button. */
MainFab.updateFab(this, state.getTag(), state.getSelectionCount());
}
});
viewModel.getOpenNoteWithPropertyRequestEvent().observeSingle(this, pair -> {
if (pair != null) {
UseCase action = pair.getFirst();
UseCaseResult result = pair.getSecond();
if (action instanceof NoteFindWithProperty) {
NoteFindWithProperty thisAction = (NoteFindWithProperty) action;
if (result.getUserData() != null) {
NoteDao.NoteIdBookId note = (NoteDao.NoteIdBookId) result.getUserData();
DisplayManager.displayExistingNote(getSupportFragmentManager(), note.getBookId(), note.getNoteId());
} else {
showSnackbar(getString(R.string.no_such_link_target, thisAction.getName(), thisAction.getValue()));
}
}
}
});
viewModel.getOpenFileLinkRequestEvent().observeSingle(this, result -> {
if (result != null && result.getUserData() != null) {
Object userData = result.getUserData();
if (userData instanceof Book) {
Book book = (Book) userData;
Intent intent = new Intent(AppIntent.ACTION_OPEN_BOOK);
intent.putExtra(AppIntent.EXTRA_BOOK_ID, book.getId());
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
if (BuildConfig.LOG_DEBUG)
LogUtils.d(TAG, "sending intent", intent);
} else if (userData instanceof File) {
File file = (File) userData;
openFileIfExists(file);
}
}
});
viewModel.getOpenNoteRequestEvent().observeSingle(this, note -> MainActivity.openSpecificNote(note.getPosition().getBookId(), note.getId()));
viewModel.getSetBookLinkRequestEvent().observeSingle(this, result -> {
Book book = result.getBook();
List<Repo> links = result.getLinks();
CharSequence[] urls = result.getUrls();
int checked = result.getSelected();
if (links.isEmpty()) {
showSnackbarWithReposLink(getString(R.string.no_repos));
} else {
ArrayAdapter<Repo> adapter = new ArrayAdapter<>(this, R.layout.item_repo, R.id.item_repo_url);
adapter.addAll(links);
dialog = new AlertDialog.Builder(this).setTitle(R.string.book_link).setSingleChoiceItems(urls, checked, (d, which) -> {
mSyncFragment.run(new BookLinkUpdate(book.getId(), links.get(which)));
dialog.dismiss();
dialog = null;
}).setNeutralButton(R.string.remove_notebook_link, (dialog, which) -> {
mSyncFragment.run(new BookLinkUpdate(book.getId()));
}).setNegativeButton(R.string.cancel, null).show();
}
});
viewModel.getSavedSearchedExportEvent().observeSingle(this, count -> {
showSnackbar(getResources().getQuantityString(R.plurals.exported_searches, count, count));
});
viewModel.getSavedSearchedImportEvent().observeSingle(this, count -> {
showSnackbar(getResources().getQuantityString(R.plurals.imported_searches, count, count));
});
viewModel.getErrorEvent().observeSingle(this, error -> {
if (error != null) {
showSnackbar(error.getLocalizedMessage());
}
});
}
use of com.orgzly.android.db.entity.Book in project orgzly-android by orgzly.
the class SyncTest method testDummyShouldNotBeSavedWhenHavingOneRepo.
@Test
public void testDummyShouldNotBeSavedWhenHavingOneRepo() {
Repo repoA = testUtils.setupRepo(RepoType.MOCK, "mock://repo-a");
Repo repoB = testUtils.setupRepo(RepoType.MOCK, "mock://repo-b");
testUtils.setupRook(repoA, "mock://repo-a/booky.org", "", "1abcdef", 1400067155);
testUtils.setupRook(repoB, "mock://repo-b/booky.org", "", "2abcdef", 1400067156);
Book book;
Map<String, BookNamesake> namesakes;
namesakes = testUtils.sync();
book = dataRepository.getBook("booky");
assertEquals(BookSyncStatus.DUMMY_WITHOUT_LINK_AND_MULTIPLE_ROOKS, namesakes.get("booky").getStatus());
assertTrue(book.isDummy());
testUtils.deleteRepo("mock://repo-a");
testUtils.deleteRepo("mock://repo-b");
testUtils.setupRepo(RepoType.MOCK, "mock://repo-c");
namesakes = testUtils.sync();
book = dataRepository.getBook("booky");
assertEquals(BookSyncStatus.ONLY_DUMMY, namesakes.get("booky").getStatus());
// TODO: We should delete it, no point of having a dummy and no remote book
assertTrue(book.isDummy());
}
use of com.orgzly.android.db.entity.Book in project orgzly-android by orgzly.
the class ShareActivity method getDataFromIntent.
public Data getDataFromIntent(Intent intent) {
Data data = new Data();
mError = null;
String action = intent.getAction();
String type = intent.getType();
if (action == null) {
// mError = getString(R.string.share_action_not_set);
} else if (type == null) {
// mError = getString(R.string.share_type_not_set);
} else if (action.equals(Intent.ACTION_SEND)) {
if (type.startsWith("text/")) {
if (intent.hasExtra(Intent.EXTRA_TEXT)) {
data.title = intent.getStringExtra(Intent.EXTRA_TEXT);
} else if (intent.hasExtra(Intent.EXTRA_STREAM)) {
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
data.title = uri.getLastPathSegment();
/*
* Store file's content as note content.
*/
try {
File file = new File(uri.getPath());
/* Don't read large files. */
if (file.length() > MAX_TEXT_FILE_LENGTH_FOR_CONTENT) {
mError = "File has " + file.length() + " bytes (refusing to read files larger then " + MAX_TEXT_FILE_LENGTH_FOR_CONTENT + " bytes)";
} else {
data.content = MiscUtils.readStringFromFile(file);
}
} catch (IOException e) {
e.printStackTrace();
mError = "Failed reading the content of " + uri.toString() + ": " + e.toString();
}
}
if (data.title != null && data.content == null && intent.hasExtra(Intent.EXTRA_SUBJECT)) {
data.content = data.title;
data.title = intent.getStringExtra(Intent.EXTRA_SUBJECT);
}
if (intent.hasExtra(AppIntent.EXTRA_QUERY_STRING)) {
Query query = new DottedQueryParser().parse(intent.getStringExtra(AppIntent.EXTRA_QUERY_STRING));
String bookName = QueryUtils.extractFirstBookNameFromQuery(query.getCondition());
if (bookName != null) {
Book book = dataRepository.getBook(bookName);
if (book != null) {
data.bookId = book.getId();
}
}
}
if (intent.hasExtra(AppIntent.EXTRA_BOOK_ID)) {
data.bookId = intent.getLongExtra(AppIntent.EXTRA_BOOK_ID, 0L);
}
} else if (type.startsWith("image/")) {
// Handle single image being sent
handleSendImage(intent, data);
} else {
mError = getString(R.string.share_type_not_supported, type);
}
} else if (action.equals("com.google.android.gm.action.AUTO_SEND")) {
if (type.startsWith("text/") && intent.hasExtra(Intent.EXTRA_TEXT)) {
data.title = intent.getStringExtra(Intent.EXTRA_TEXT);
}
} else {
mError = getString(R.string.share_action_not_supported, action);
}
/* Make sure that title is never empty. */
if (data.title == null) {
data.title = "";
}
return data;
}
use of com.orgzly.android.db.entity.Book in project orgzly-android by orgzly.
the class TemplateChooserActivity method onBookClicked.
@Override
public void onBookClicked(long bookId) {
if (action == null) {
return;
}
if (!action.equals(Intent.ACTION_CREATE_SHORTCUT)) {
return;
}
Book book = dataRepository.getBook(bookId);
if (book == null) {
Toast.makeText(this, R.string.book_does_not_exist_anymore, Toast.LENGTH_SHORT).show();
setResult(RESULT_CANCELED);
finish();
return;
}
String id = "template-" + bookId;
String name = book.getName();
String title = BookUtils.getFragmentTitleForBook(book);
Intent launchIntent = ShareActivity.createNewNoteInNotebookIntent(this, bookId);
IconCompat icon = createIcon();
ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(this, id).setShortLabel(name).setLongLabel(title).setIcon(icon).setIntent(launchIntent).build();
setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(this, shortcut));
finish();
}
use of com.orgzly.android.db.entity.Book in project orgzly-android by orgzly.
the class MainActivity method setupSearchView.
/**
* SearchView setup and query text listeners.
* TODO: http://developer.android.com/training/search/setup.html
*/
private void setupSearchView(Menu menu) {
final MenuItem searchItem = menu.findItem(R.id.activity_action_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint(getString(R.string.search_hint));
/* When user starts the search, fill the search box with text depending on current fragment. */
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* Make search as wide as possible. */
ViewGroup.LayoutParams layoutParams = searchView.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
/* For Query fragment, fill the box with full query. */
String q = DisplayManager.getDisplayedQuery(getSupportFragmentManager());
if (q != null) {
searchView.setQuery(q + " ", false);
} else {
/* If searching from book, add book name to query. */
Book book = getActiveFragmentBook();
if (book != null) {
DottedQueryBuilder builder = new DottedQueryBuilder();
String query = builder.build(new Query(new Condition.InBook(book.getName())));
searchView.setQuery(query + " ", false);
}
}
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String str) {
return false;
}
@Override
public boolean onQueryTextSubmit(String str) {
if (BuildConfig.LOG_DEBUG)
LogUtils.d(TAG, str);
/* Close search. */
searchItem.collapseActionView();
DisplayManager.displayQuery(getSupportFragmentManager(), str.trim());
return true;
}
});
}
Aggregations