use of com.orgzly.android.usecase.UseCase 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());
}
});
}
Aggregations