use of androidx.fragment.app.Fragment 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 androidx.fragment.app.Fragment in project orgzly-android by orgzly.
the class DisplayManager method displayQuery.
public static void displayQuery(FragmentManager fragmentManager, @NonNull String queryString) {
// If the same query is already displayed, don't do anything.
String displayedQuery = getDisplayedQuery(fragmentManager);
if (displayedQuery != null && displayedQuery.equals(queryString)) {
return;
}
// Parse query
QueryParser queryParser = new InternalQueryParser();
Query query = queryParser.parse(queryString);
Fragment fragment;
String tag;
// Display agenda or query fragment
if (query.getOptions().getAgendaDays() > 0) {
fragment = AgendaFragment.getInstance(queryString);
tag = AgendaFragment.FRAGMENT_TAG;
} else {
fragment = SearchFragment.getInstance(queryString);
tag = SearchFragment.FRAGMENT_TAG;
}
// Add fragment.
fragmentManager.beginTransaction().setCustomAnimations(R.anim.fragment_enter, R.anim.fragment_exit, R.anim.fragment_enter, R.anim.fragment_exit).addToBackStack(null).replace(R.id.single_pane_container, fragment, tag).commit();
}
use of androidx.fragment.app.Fragment in project orgzly-android by orgzly.
the class DisplayManager method onSavedSearchEditRequest.
/**
* Displays fragment for existing saved search.
*/
public static void onSavedSearchEditRequest(FragmentManager fragmentManager, long id) {
/* Create fragment. */
Fragment fragment = SavedSearchFragment.getInstance(id);
/* Add fragment. */
fragmentManager.beginTransaction().setCustomAnimations(R.anim.fragment_enter, R.anim.fragment_exit, R.anim.fragment_enter, R.anim.fragment_exit).addToBackStack(null).replace(R.id.single_pane_container, fragment, SavedSearchFragment.FRAGMENT_TAG).commit();
}
use of androidx.fragment.app.Fragment in project orgzly-android by orgzly.
the class DisplayManager method getDisplayedQuery.
public static String getDisplayedQuery(FragmentManager fragmentManager) {
Fragment qf = fragmentManager.findFragmentByTag(SearchFragment.FRAGMENT_TAG);
Fragment af = fragmentManager.findFragmentByTag(AgendaFragment.FRAGMENT_TAG);
if (qf != null && qf.isVisible()) {
return ((SearchFragment) qf).getCurrentQuery();
} else if (af != null && af.isVisible()) {
return ((AgendaFragment) af).getCurrentQuery();
}
return null;
}
use of androidx.fragment.app.Fragment in project orgzly-android by orgzly.
the class DisplayManager method displaySavedSearches.
public static void displaySavedSearches(FragmentManager fragmentManager) {
if (isFragmentDisplayed(fragmentManager, SavedSearchesFragment.getFRAGMENT_TAG()) != null) {
return;
}
Fragment fragment = SavedSearchesFragment.getInstance();
FragmentTransaction t = fragmentManager.beginTransaction().setCustomAnimations(R.anim.fragment_enter, R.anim.fragment_exit, R.anim.fragment_enter, R.anim.fragment_exit).addToBackStack(null).replace(R.id.single_pane_container, fragment, SavedSearchesFragment.getFRAGMENT_TAG());
t.commit();
}
Aggregations