Search in sources :

Example 1 with UseCaseResult

use of com.orgzly.android.usecase.UseCaseResult 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());
        }
    });
}
Also used : Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) NoteDelete(com.orgzly.android.usecase.NoteDelete) UseCase(com.orgzly.android.usecase.UseCase) ActivityUtils(com.orgzly.android.ui.util.ActivityUtils) Query(com.orgzly.android.query.Query) Handler(android.os.Handler) Fragment(androidx.fragment.app.Fragment) BookFormat(com.orgzly.android.BookFormat) DrawerNavigationView(com.orgzly.android.ui.drawer.DrawerNavigationView) NoteUpdateState(com.orgzly.android.usecase.NoteUpdateState) NoteFindWithProperty(com.orgzly.android.usecase.NoteFindWithProperty) IntentFilter(android.content.IntentFilter) Set(java.util.Set) SearchView(androidx.appcompat.widget.SearchView) Book(com.orgzly.android.db.entity.Book) BuildConfig(com.orgzly.BuildConfig) CommonActivity(com.orgzly.android.ui.CommonActivity) Note(com.orgzly.android.db.entity.Note) BookImportFromUri(com.orgzly.android.usecase.BookImportFromUri) DottedQueryBuilder(com.orgzly.android.query.user.DottedQueryBuilder) DrawerLayout(androidx.drawerlayout.widget.DrawerLayout) NotePaste(com.orgzly.android.usecase.NotePaste) BookExportToUri(com.orgzly.android.usecase.BookExportToUri) SavedSearchUpdate(com.orgzly.android.usecase.SavedSearchUpdate) NoteDao(com.orgzly.android.db.dao.NoteDao) SavedSearchFragment(com.orgzly.android.ui.savedsearch.SavedSearchFragment) R(com.orgzly.R) SyncService(com.orgzly.android.sync.SyncService) Menu(android.view.Menu) NoteFragment(com.orgzly.android.ui.note.NoteFragment) ActionModeListener(com.orgzly.android.ui.ActionModeListener) BookFragment(com.orgzly.android.ui.notes.book.BookFragment) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NoteDemote(com.orgzly.android.usecase.NoteDemote) IOException(java.io.IOException) SavedSearchMoveUp(com.orgzly.android.usecase.SavedSearchMoveUp) File(java.io.File) ArrayAdapter(android.widget.ArrayAdapter) NotePromote(com.orgzly.android.usecase.NotePromote) Configuration(android.content.res.Configuration) BottomActionBar(com.orgzly.android.ui.BottomActionBar) Notifications(com.orgzly.android.ui.notifications.Notifications) LogUtils(com.orgzly.android.util.LogUtils) SavedSearchCreate(com.orgzly.android.usecase.SavedSearchCreate) BookForceSave(com.orgzly.android.usecase.BookForceSave) NoteUpdateDeadlineTime(com.orgzly.android.usecase.NoteUpdateDeadlineTime) SavedSearchExport(com.orgzly.android.usecase.SavedSearchExport) BookSparseTreeForNote(com.orgzly.android.usecase.BookSparseTreeForNote) ActionBarDrawerToggle(androidx.appcompat.app.ActionBarDrawerToggle) View(android.view.View) NoteMove(com.orgzly.android.usecase.NoteMove) NoteUpdateStateToggle(com.orgzly.android.usecase.NoteUpdateStateToggle) BroadcastReceiver(android.content.BroadcastReceiver) ViewGroup(android.view.ViewGroup) AlertDialog(android.app.AlertDialog) Nullable(org.jetbrains.annotations.Nullable) Repo(com.orgzly.android.db.entity.Repo) List(java.util.List) App(com.orgzly.android.App) BookCreate(com.orgzly.android.usecase.BookCreate) Toolbar(androidx.appcompat.widget.Toolbar) BookPrefaceFragment(com.orgzly.android.ui.notes.book.BookPrefaceFragment) BookImportGettingStarted(com.orgzly.android.usecase.BookImportGettingStarted) SimpleOneLinerDialog(com.orgzly.android.ui.dialogs.SimpleOneLinerDialog) BooksFragment(com.orgzly.android.ui.books.BooksFragment) ReposActivity(com.orgzly.android.ui.repos.ReposActivity) NotNull(org.jetbrains.annotations.NotNull) Snackbar(com.google.android.material.snackbar.Snackbar) OrgDateTime(com.orgzly.org.datetime.OrgDateTime) AutoSync(com.orgzly.android.sync.AutoSync) BookExport(com.orgzly.android.usecase.BookExport) Context(android.content.Context) UseCaseRunner(com.orgzly.android.usecase.UseCaseRunner) SavedSearchMoveDown(com.orgzly.android.usecase.SavedSearchMoveDown) NavigationView(com.google.android.material.navigation.NavigationView) Intent(android.content.Intent) LocalBroadcastManager(androidx.localbroadcastmanager.content.LocalBroadcastManager) DisplayManager(com.orgzly.android.ui.DisplayManager) AppPermissions(com.orgzly.android.util.AppPermissions) MenuItem(android.view.MenuItem) SettingsActivity(com.orgzly.android.ui.settings.SettingsActivity) Lifecycle(androidx.lifecycle.Lifecycle) NoteCut(com.orgzly.android.usecase.NoteCut) AppPreferences(com.orgzly.android.prefs.AppPreferences) LifecycleOwner(androidx.lifecycle.LifecycleOwner) AppIntent(com.orgzly.android.AppIntent) SavedSearchDelete(com.orgzly.android.usecase.SavedSearchDelete) Place(com.orgzly.android.ui.Place) OutputStream(java.io.OutputStream) SavedSearchesFragment(com.orgzly.android.ui.savedsearches.SavedSearchesFragment) NotesClipboard(com.orgzly.android.db.NotesClipboard) BookLinkUpdate(com.orgzly.android.usecase.BookLinkUpdate) GravityCompat(androidx.core.view.GravityCompat) SavedSearch(com.orgzly.android.db.entity.SavedSearch) ActionMode(androidx.appcompat.view.ActionMode) NotePlace(com.orgzly.android.ui.NotePlace) NoteCopy(com.orgzly.android.usecase.NoteCopy) UseCaseResult(com.orgzly.android.usecase.UseCaseResult) BookForceLoad(com.orgzly.android.usecase.BookForceLoad) Condition(com.orgzly.android.query.Condition) BookUpdatePreface(com.orgzly.android.usecase.BookUpdatePreface) NoteUpdateScheduledTime(com.orgzly.android.usecase.NoteUpdateScheduledTime) BookName(com.orgzly.android.BookName) NoteDao(com.orgzly.android.db.dao.NoteDao) DottedQueryBuilder(com.orgzly.android.query.user.DottedQueryBuilder) UseCase(com.orgzly.android.usecase.UseCase) UseCaseResult(com.orgzly.android.usecase.UseCaseResult) Intent(android.content.Intent) AppIntent(com.orgzly.android.AppIntent) NoteFindWithProperty(com.orgzly.android.usecase.NoteFindWithProperty) Repo(com.orgzly.android.db.entity.Repo) Book(com.orgzly.android.db.entity.Book) BookLinkUpdate(com.orgzly.android.usecase.BookLinkUpdate) DrawerLayout(androidx.drawerlayout.widget.DrawerLayout) File(java.io.File) ArrayAdapter(android.widget.ArrayAdapter)

Example 2 with UseCaseResult

use of com.orgzly.android.usecase.UseCaseResult in project orgzly-android by orgzly.

the class SyncFragment method run.

public void run(UseCase action) {
    App.EXECUTORS.diskIO().execute(() -> {
        try {
            UseCaseResult result = UseCaseRunner.run(action);
            App.EXECUTORS.mainThread().execute(() -> mListener.onSuccess(action, result));
        } catch (Throwable e) {
            e.printStackTrace();
            App.EXECUTORS.mainThread().execute(() -> mListener.onError(action, e));
        }
    });
}
Also used : UseCaseResult(com.orgzly.android.usecase.UseCaseResult)

Aggregations

AlertDialog (android.app.AlertDialog)1 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 Configuration (android.content.res.Configuration)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 ArrayAdapter (android.widget.ArrayAdapter)1 NonNull (androidx.annotation.NonNull)1 ActionBarDrawerToggle (androidx.appcompat.app.ActionBarDrawerToggle)1 ActionMode (androidx.appcompat.view.ActionMode)1 SearchView (androidx.appcompat.widget.SearchView)1 Toolbar (androidx.appcompat.widget.Toolbar)1 GravityCompat (androidx.core.view.GravityCompat)1