Search in sources :

Example 26 with CardBrowser

use of com.ichi2.anki.CardBrowser in project Anki-Android by ankidroid.

the class CardBrowser method onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    Timber.d("onCreateOptionsMenu()");
    mActionBarMenu = menu;
    if (!mInMultiSelectMode) {
        // restore drawer click listener and icon
        restoreDrawerIcon();
        getMenuInflater().inflate(R.menu.card_browser, menu);
        mSaveSearchItem = menu.findItem(R.id.action_save_search);
        // the searchview's query always starts empty.
        mSaveSearchItem.setVisible(false);
        mMySearchesItem = menu.findItem(R.id.action_list_my_searches);
        JSONObject savedFiltersObj = getCol().get_config("savedFilters", (JSONObject) null);
        mMySearchesItem.setVisible(savedFiltersObj != null && savedFiltersObj.length() > 0);
        mSearchItem = menu.findItem(R.id.action_search);
        mSearchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // SearchView doesn't support empty queries so we always reset the search when collapsing
                mSearchTerms = "";
                mSearchView.setQuery(mSearchTerms, false);
                searchCards();
                // invalidate options menu so that disappeared icons would appear again
                supportInvalidateOptionsMenu();
                mTempSearchQuery = null;
                return true;
            }
        });
        mSearchView = (CardBrowserSearchView) mSearchItem.getActionView();
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextChange(String newText) {
                if (mSearchView.shouldIgnoreValueChange()) {
                    return true;
                }
                mSaveSearchItem.setVisible(!TextUtils.isEmpty(newText));
                mTempSearchQuery = newText;
                return true;
            }

            @Override
            public boolean onQueryTextSubmit(String query) {
                onSearch();
                mSearchView.clearFocus();
                return true;
            }
        });
        // Fixes #9010 - consistent search after drawer change calls supportInvalidateOptionsMenu (mTempSearchQuery)
        if (!TextUtils.isEmpty(mTempSearchQuery) || !TextUtils.isEmpty(mSearchTerms)) {
            // This calls mSearchView.setOnSearchClickListener
            mSearchItem.expandActionView();
            String toUse = !TextUtils.isEmpty(mTempSearchQuery) ? mTempSearchQuery : mSearchTerms;
            mSearchView.setQuery(toUse, false);
        }
        mSearchView.setOnSearchClickListener(v -> {
            // Provide SearchView with the previous search terms
            mSearchView.setQuery(mSearchTerms, false);
        });
    } else {
        // multi-select mode
        getMenuInflater().inflate(R.menu.card_browser_multiselect, menu);
        showBackIcon();
    }
    if (mActionBarMenu != null && mActionBarMenu.findItem(R.id.action_undo) != null) {
        MenuItem undo = mActionBarMenu.findItem(R.id.action_undo);
        undo.setVisible(getCol().undoAvailable());
        undo.setTitle(getResources().getString(R.string.studyoptions_congrats_undo, getCol().undoName(getResources())));
    }
    // Maybe we were called from ACTION_PROCESS_TEXT.
    // In that case we already fill in the search.
    Intent intent = getIntent();
    Compat compat = CompatHelper.getCompat();
    if (compat.ACTION_PROCESS_TEXT.equals(intent.getAction())) {
        CharSequence search = intent.getCharSequenceExtra(compat.EXTRA_PROCESS_TEXT);
        if (search != null && search.length() != 0) {
            Timber.i("CardBrowser :: Called with search intent: %s", search.toString());
            mSearchView.setQuery(search, true);
            intent.setAction(Intent.ACTION_DEFAULT);
        }
    }
    mPreviewItem = menu.findItem(R.id.action_preview);
    onSelectionChanged();
    updatePreviewMenuItem();
    return super.onCreateOptionsMenu(menu);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) SearchView(androidx.appcompat.widget.SearchView) CardBrowserSearchView(com.ichi2.ui.CardBrowserSearchView) LanguageUtil.getLocaleCompat(com.ichi2.utils.LanguageUtil.getLocaleCompat) Compat(com.ichi2.compat.Compat) MenuItem(android.view.MenuItem) Intent(android.content.Intent)

Example 27 with CardBrowser

use of com.ichi2.anki.CardBrowser in project Anki-Android by ankidroid.

the class CardBrowser method onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (getDrawerToggle().onOptionsItemSelected(item)) {
        return true;
    }
    // (when another operation will be performed on the model, it will undo the latest operation)
    if (mUndoSnackbar != null && mUndoSnackbar.isShown())
        mUndoSnackbar.dismiss();
    int itemId = item.getItemId();
    if (itemId == android.R.id.home) {
        endMultiSelectMode();
        return true;
    } else if (itemId == R.id.action_add_note_from_card_browser) {
        addNoteFromCardBrowser();
        return true;
    } else if (itemId == R.id.action_save_search) {
        String searchTerms = mSearchView.getQuery().toString();
        showDialogFragment(CardBrowserMySearchesDialog.newInstance(null, mMySearchesDialogListener, searchTerms, CardBrowserMySearchesDialog.CARD_BROWSER_MY_SEARCHES_TYPE_SAVE));
        return true;
    } else if (itemId == R.id.action_list_my_searches) {
        JSONObject savedFiltersObj = getCol().get_config("savedFilters", (JSONObject) null);
        HashMap<String, String> savedFilters;
        if (savedFiltersObj != null) {
            savedFilters = HashUtil.HashMapInit(savedFiltersObj.length());
            for (String searchName : savedFiltersObj) {
                savedFilters.put(searchName, savedFiltersObj.optString(searchName));
            }
        } else {
            savedFilters = HashUtil.HashMapInit(0);
        }
        showDialogFragment(CardBrowserMySearchesDialog.newInstance(savedFilters, mMySearchesDialogListener, "", CardBrowserMySearchesDialog.CARD_BROWSER_MY_SEARCHES_TYPE_LIST));
        return true;
    } else if (itemId == R.id.action_sort_by_size) {
        showDialogFragment(CardBrowserOrderDialog.newInstance(mOrder, mOrderAsc, mOrderDialogListener));
        return true;
    } else if (itemId == R.id.action_show_marked) {
        mSearchTerms = "tag:marked";
        mSearchView.setQuery("", false);
        mSearchView.setQueryHint(getResources().getString(R.string.card_browser_show_marked));
        searchCards();
        return true;
    } else if (itemId == R.id.action_show_suspended) {
        mSearchTerms = "is:suspended";
        mSearchView.setQuery("", false);
        mSearchView.setQueryHint(getResources().getString(R.string.card_browser_show_suspended));
        searchCards();
        return true;
    } else if (itemId == R.id.action_search_by_tag) {
        showFilterByTagsDialog();
        return true;
    } else if (itemId == R.id.action_flag_zero) {
        flagTask(0);
        return true;
    } else if (itemId == R.id.action_flag_one) {
        flagTask(1);
        return true;
    } else if (itemId == R.id.action_flag_two) {
        flagTask(2);
        return true;
    } else if (itemId == R.id.action_flag_three) {
        flagTask(3);
        return true;
    } else if (itemId == R.id.action_flag_four) {
        flagTask(4);
        return true;
    } else if (itemId == R.id.action_flag_five) {
        flagTask(5);
        return true;
    } else if (itemId == R.id.action_flag_six) {
        flagTask(6);
        return true;
    } else if (itemId == R.id.action_flag_seven) {
        flagTask(7);
        return true;
    } else if (itemId == R.id.action_select_flag_zero) {
        selectionWithFlagTask(0);
        return true;
    } else if (itemId == R.id.action_select_flag_one) {
        selectionWithFlagTask(1);
        return true;
    } else if (itemId == R.id.action_select_flag_two) {
        selectionWithFlagTask(2);
        return true;
    } else if (itemId == R.id.action_select_flag_three) {
        selectionWithFlagTask(3);
        return true;
    } else if (itemId == R.id.action_select_flag_four) {
        selectionWithFlagTask(4);
        return true;
    } else if (itemId == R.id.action_select_flag_five) {
        selectionWithFlagTask(5);
        return true;
    } else if (itemId == R.id.action_select_flag_six) {
        selectionWithFlagTask(6);
        return true;
    } else if (itemId == R.id.action_select_flag_seven) {
        selectionWithFlagTask(7);
        return true;
    } else if (itemId == R.id.action_delete_card) {
        deleteSelectedNote();
        return true;
    } else if (itemId == R.id.action_mark_card) {
        toggleMark();
        return true;
    } else if (itemId == R.id.action_suspend_card) {
        TaskManager.launchCollectionTask(new CollectionTask.SuspendCardMulti(getSelectedCardIds()), suspendCardHandler());
        return true;
    } else if (itemId == R.id.action_change_deck) {
        showChangeDeckDialog();
        return true;
    } else if (itemId == R.id.action_undo) {
        Timber.w("CardBrowser:: Undo pressed");
        onUndo();
        return true;
    } else if (itemId == R.id.action_select_none) {
        onSelectNone();
        return true;
    } else if (itemId == R.id.action_select_all) {
        onSelectAll();
        return true;
    } else if (itemId == R.id.action_preview) {
        onPreview();
        return true;
    } else if (itemId == R.id.action_reset_cards_progress) {
        Timber.i("NoteEditor:: Reset progress button pressed");
        onResetProgress();
        return true;
    } else if (itemId == R.id.action_reschedule_cards) {
        Timber.i("CardBrowser:: Reschedule button pressed");
        rescheduleSelectedCards();
        return true;
    } else if (itemId == R.id.action_reposition_cards) {
        Timber.i("CardBrowser:: Reposition button pressed");
        // Only new cards may be repositioned
        List<Long> cardIds = getSelectedCardIds();
        for (long cardId : cardIds) {
            if (getCol().getCard(cardId).getQueue() != Consts.QUEUE_TYPE_NEW) {
                SimpleMessageDialog dialog = SimpleMessageDialog.newInstance(getString(R.string.vague_error), getString(R.string.reposition_card_not_new_error), false);
                showDialogFragment(dialog);
                return false;
            }
        }
        IntegerDialog repositionDialog = new IntegerDialog();
        repositionDialog.setArgs(getString(R.string.reposition_card_dialog_title), getString(R.string.reposition_card_dialog_message), 5);
        repositionDialog.setCallbackRunnable(position -> repositionCardsNoValidation(cardIds, position));
        showDialogFragment(repositionDialog);
        return true;
    } else if (itemId == R.id.action_edit_note) {
        openNoteEditorForCurrentlySelectedNote();
        return super.onOptionsItemSelected(item);
    } else if (itemId == R.id.action_view_card_info) {
        List<Long> selectedCardIds = getSelectedCardIds();
        if (!selectedCardIds.isEmpty()) {
            Intent intent = new Intent(this, CardInfo.class);
            intent.putExtra("cardId", selectedCardIds.get(0));
            startActivityWithAnimation(intent, FADE);
        }
        return true;
    } else if (itemId == R.id.action_edit_tags) {
        showEditTagsDialog();
    }
    return super.onOptionsItemSelected(item);
}
Also used : SimpleMessageDialog(com.ichi2.anki.dialogs.SimpleMessageDialog) JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) IntegerDialog(com.ichi2.anki.dialogs.IntegerDialog) Intent(android.content.Intent) CollectionTask(com.ichi2.async.CollectionTask)

Example 28 with CardBrowser

use of com.ichi2.anki.CardBrowser in project Anki-Android by ankidroid.

the class FinderTest method test_deckNameContainingWildcardCanBeSearched.

@Test
public void test_deckNameContainingWildcardCanBeSearched() {
    String val = "*Yr1::Med2::CAS4::F4: Renal::BRS (zanki)::HY";
    Collection col = getCol();
    long currentDid = addDeck(val);
    col.getDecks().select(currentDid);
    Note note = col.newNote();
    note.setItem("Front", "foo");
    note.setItem("Back", "bar");
    note.model().put("did", currentDid);
    col.addNote(note);
    long did = note.firstCard().getDid();
    assertEquals(currentDid, did);
    CardBrowser cb = super.startActivityNormallyOpenCollectionWithIntent(CardBrowser.class, new Intent());
    cb.mDeckSpinnerSelection.updateDeckPosition(currentDid);
    advanceRobolectricLooperWithSleep();
    assertEquals(1L, cb.getCardCount());
}
Also used : Intent(android.content.Intent) CardBrowser(com.ichi2.anki.CardBrowser) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 Intent (android.content.Intent)9 Deck (com.ichi2.libanki.Deck)9 Note (com.ichi2.libanki.Note)7 Card (com.ichi2.libanki.Card)6 MenuItem (android.view.MenuItem)5 CollectionTask (com.ichi2.async.CollectionTask)5 JSONObject (com.ichi2.utils.JSONObject)5 HashSet (java.util.HashSet)5 IntentFilter (android.content.IntentFilter)4 SharedPreferences (android.content.SharedPreferences)4 View (android.view.View)4 AdapterView (android.widget.AdapterView)4 SearchView (androidx.appcompat.widget.SearchView)4 Bundle (android.os.Bundle)3 ListView (android.widget.ListView)3 TextView (android.widget.TextView)3 IntegerDialog (com.ichi2.anki.dialogs.IntegerDialog)3 SimpleMessageDialog (com.ichi2.anki.dialogs.SimpleMessageDialog)3 Consts (com.ichi2.libanki.Consts)3