Search in sources :

Example 81 with TaskData

use of com.ichi2.async.TaskData in project AnkiChinaAndroid by ankichinateam.

the class SelfStudyActivity method onCollectionLoaded.

// Finish initializing the activity after the collection has been correctly loaded
@Override
protected void onCollectionLoaded(Collection col) {
    super.onCollectionLoaded(col);
    Timber.d("onCollectionLoaded()");
    registerExternalStorageListener();
    final SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext());
    // Load reference to action bar title
    mActionBarTitle = findViewById(R.id.toolbar_title);
    mTabLayout = findViewById(R.id.tab_layout);
    mTop = findViewById(R.id.invisible_top);
    mTop.setVisibility(View.GONE);
    mRestrictOnTab = "";
    mTabType = getIntent().getIntExtra("type", 0);
    mComplete = findViewById(R.id.tv_complete);
    mComplete.setOnClickListener(v -> toggleMultiSelectMode(false));
    mBack = findViewById(R.id.iv_back);
    mStartStudyButton = findViewById(R.id.confirm);
    mStartStudyButton.setOnClickListener(v -> {
        boolean showDialog = false;
        try {
            if (!preferences.getString(Consts.KEY_SELF_STUDYING_LIST, "").isEmpty()) {
                String[] savedCardListStr = preferences.getString(Consts.KEY_SELF_STUDYING_LIST, "").replace("[", "").replace("]", "").split(", ");
                long[] savedCardList = new long[savedCardListStr.length];
                for (int i = 0; i < savedCardListStr.length; i++) {
                    savedCardList[i] = Long.parseLong(savedCardListStr[i]);
                }
                List<Long> newCardList = getCol().filterToValidCards(savedCardList);
                long[] filterToValidCards = new long[newCardList.size()];
                for (int i = 0; i < newCardList.size(); i++) {
                    filterToValidCards[i] = newCardList.get(i);
                }
                if (preferences.getInt(Consts.KEY_SELF_STUDYING_LIST_INDEX, 0) + 1 < savedCardListStr.length) {
                    // 没跑完
                    Intent previewer = new Intent(SelfStudyActivity.this, Previewer2.class);
                    CustomStyleDialog studyDialog = new CustomStyleDialog.Builder(this).setCustomLayout(R.layout.dialog_common_custom_next).setTitle("是否继续上一次的主动练习?").centerTitle().setMessage("你上次的主动练习还有待学任务,请选择是继续上一次还是开始新的主动练习").setPositiveButton("开始新的", (dialog, which) -> {
                        dialog.dismiss();
                        long[] cardList;
                        if (inMultiSelectMode() && checkedCardCount() > 1) {
                            // Multiple cards have been explicitly selected, so preview only those cards
                            previewer.putExtra("index", 0);
                            cardList = getSelectedCardIds();
                        } else {
                            // Preview all cards, starting from the one that is currently selected
                            // int startIndex = mCheckedCards.isEmpty() ? 0 : mCheckedCards.iterator().next().getPosition();
                            previewer.putExtra("index", 0);
                            cardList = getAllCardIds();
                        }
                        previewer.putExtra("cardList", cardList);
                        preferences.edit().putString(Consts.KEY_SELF_STUDYING_LIST, Arrays.toString(cardList)).putInt(Consts.KEY_SELF_STUDYING_LIST_INDEX, 0).apply();
                        startActivityForResultWithoutAnimation(previewer, PREVIEW_CARDS);
                    }).setNegativeButton("继续上次", (dialog, which) -> {
                        dialog.dismiss();
                        previewer.putExtra("cardList", filterToValidCards);
                        previewer.putExtra("index", preferences.getInt(Consts.KEY_SELF_STUDYING_LIST_INDEX, 0));
                        startActivityForResultWithoutAnimation(previewer, PREVIEW_CARDS);
                    }).create();
                    showDialog = true;
                    studyDialog.show();
                }
            }
        } catch (Exception ignored) {
        }
        if (!showDialog) {
            Intent previewer = new Intent(SelfStudyActivity.this, Previewer2.class);
            long[] cardList;
            if (inMultiSelectMode() && checkedCardCount() > 1) {
                // Multiple cards have been explicitly selected, so preview only those cards
                previewer.putExtra("index", 0);
                cardList = getSelectedCardIds();
            } else {
                // Preview all cards, starting from the one that is currently selected
                // int startIndex = mCheckedCards.isEmpty() ? 0 : mCheckedCards.iterator().next().getPosition();
                previewer.putExtra("index", 0);
                cardList = getAllCardIds();
            }
            previewer.putExtra("cardList", cardList);
            preferences.edit().putString(Consts.KEY_SELF_STUDYING_LIST, Arrays.toString(cardList)).putInt(Consts.KEY_SELF_STUDYING_LIST_INDEX, 0).apply();
            startActivityForResultWithoutAnimation(previewer, PREVIEW_CARDS);
        }
    });
    // Add drop-down menu to select deck to action bar.
    mDropDownDecks = getCol().getDecks().allSorted();
    mDropDownDeckAdapter = new DeckDropDownAdapter(this, mDropDownDecks, R.layout.dropdown_deck_selected_item_self, this);
    mToolbar = findViewById(R.id.toolbar);
    if (mToolbar != null) {
        setSupportActionBar(mToolbar);
    // enable ActionBar app icon to behave as action to toggle nav drawer
    // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    // getSupportActionBar().setHomeButtonEnabled(true);
    // Decide which action to take when the navigation button is tapped.
    // mToolbar.setNavigationIcon(R.mipmap.nav_bar_back_normal);
    // mToolbar.setNavigationOnClickListener(v -> onBackPressed());
    }
    ActionBar mActionBar = getSupportActionBar();
    if (mActionBar != null) {
        mActionBar.setDisplayShowTitleEnabled(false);
    }
    // mOrder = CARD_ORDER_CREATE_TIME;
    String colOrder = getCol().getConf().getString("sortType");
    for (int c = 0; c < fSortTypes.length; ++c) {
        if (fSortTypes[c].equals(colOrder)) {
            mOrder = c;
            break;
        }
    }
    if (mOrder == 1 && preferences.getBoolean("cardBrowserNoSorting", false)) {
        mOrder = 0;
    }
    // This upgrade should already have been done during
    // setConf. However older version of AnkiDroid didn't call
    // upgradeJSONIfNecessary during setConf, which means the
    // conf saved may still have this bug.
    mOrderAsc = Upgrade.upgradeJSONIfNecessary(getCol(), getCol().getConf(), "sortBackwards", false);
    // mOrderAsc = true;
    getCol().getConf().put("sortType", fSortTypes[mOrder]);
    getCol().getConf().put("sortBackwards", mOrderAsc);
    mCards = new ArrayList<>();
    mCardsListView = findViewById(R.id.card_browser_list);
    mMultiModeBottomLayout = findViewById(R.id.rl_multi_mode);
    findViewById(R.id.add_note_action).setOnClickListener(v -> {
        Intent intent = new Intent(SelfStudyActivity.this, NoteEditor.class);
        intent.putExtra(NoteEditor.EXTRA_CALLER, NoteEditor.CALLER_CARDBROWSER_ADD);
        startActivityForResultWithAnimation(intent, ADD_NOTE, ActivityTransitionAnimation.LEFT);
    });
    TextView selectCount = findViewById(R.id.select_count);
    TextView move = findViewById(R.id.move);
    TextView delete = findViewById(R.id.delete);
    TextView cancel = findViewById(R.id.cancel);
    CheckBox stick = findViewById(R.id.stick);
    move.setOnClickListener(v -> {
        if (mCardsAdapter.getSelectedItemIds().isEmpty()) {
            return;
        }
        AlertDialog.Builder builderSingle = new AlertDialog.Builder(SelfStudyActivity.this);
        builderSingle.setTitle(getString(R.string.move_all_to_deck));
        // WARNING: changeDeck depends on this index, so any changes should be reflected there.
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(SelfStudyActivity.this, R.layout.dropdown_deck_item);
        for (Deck deck : getValidDecksForChangeDeck()) {
            try {
                arrayAdapter.add(deck.getString("name"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        builderSingle.setNegativeButton(getString(R.string.cancel), (dialog, which) -> dialog.dismiss());
        builderSingle.setAdapter(arrayAdapter, (dialog, which) -> changeDeck(which));
        builderSingle.show();
    });
    delete.setOnClickListener(v -> {
        if (mCardsAdapter.getSelectedItemIds().isEmpty()) {
            return;
        }
        CollectionTask.launchCollectionTask(DISMISS_MULTI, mDeleteNoteHandler, new TaskData(new Object[] { mCardsAdapter.getSelectedItemIdArray(), Collection.DismissType.DELETE_NOTE_MULTI }));
        toggleMultiSelectMode(false);
        mCardsAdapter.getSelectedItemIds().clear();
        mCardsAdapter.notifyDataSetChanged();
    });
    int[] attrs = new int[] { R.attr.primary_text_third_color999999 };
    TypedArray ta = obtainStyledAttributes(attrs);
    int textGrayColor = ta.getColor(0, ContextCompat.getColor(this, R.color.new_primary_text_third_color));
    ta.recycle();
    stick.setOnCheckedChangeListener((buttonView, isChecked) -> {
        mCardsAdapter.selectItem(isChecked);
        selectCount.setText((isChecked ? "全选" : "已选") + mCardsAdapter.selectItemCount());
        selectCount.setTextColor(isChecked ? ContextCompat.getColor(this, R.color.primary_color) : textGrayColor);
    });
    selectCount.setOnClickListener(v -> stick.performClick());
    cancel.setOnClickListener(v -> {
        toggleMultiSelectMode(false);
    });
    mCardsAdapter = new CardsListAdapter(getLayoutInflater(), this, new CardsListAdapter.CardListAdapterCallback() {

        @Override
        public List<CardCache> getCards() {
            return SelfStudyActivity.this.getCards();
        }

        @Override
        public void onChangeMultiMode(boolean isMultiMode) {
            mMultiModeBottomLayout.setVisibility(isMultiMode ? View.VISIBLE : View.GONE);
            mStartStudyButton.setVisibility(isMultiMode ? View.GONE : mCards.size() > 0 ? View.VISIBLE : View.GONE);
            mSearchView.setVisibility(isMultiMode ? View.INVISIBLE : View.VISIBLE);
            mBack.setVisibility(isMultiMode ? View.GONE : View.VISIBLE);
            mComplete.setVisibility(isMultiMode ? View.VISIBLE : View.GONE);
            selectCount.setText("已选0");
            supportInvalidateOptionsMenu();
        }

        @Override
        public void onItemSelect(int count) {
            selectCount.setText("已选" + count);
            updateMultiselectMenu();
        }
    });
    // link the adapter to the main mCardsListView
    mCardsListView.setAdapter(mCardsAdapter);
    mCardsListView.setLayoutManager(new LinearLayoutManager(this));
    mCardsAdapter.setTvOrderClickListener(v -> showOrderListDialog());
    mCardsAdapter.setIvOrderClickListener(v -> {
        // 修改升序/降序
        mOrderAsc = !mOrderAsc;
        getCol().getConf().put("sortBackwards", mOrderAsc);
        Collections.reverse(mCards);
        updateList();
        mCardsAdapter.updateOrderState(mOrderNames[mOrder], mOrderAsc);
    });
    mCardsAdapter.setDeckClickListener(view -> {
        if (mCardsAdapter.isMultiCheckableMode()) {
            return;
        }
        Intent previewer = new Intent(SelfStudyActivity.this, Previewer.class);
        long[] ids = inMultiSelectMode() && checkedCardCount() > 1 ? getSelectedCardIds() : getAllCardIds();
        long targetId = (long) view.getTag();
        for (int i = 0; i < ids.length; i++) {
            if (ids[i] == targetId) {
                previewer.putExtra("index", i);
                break;
            }
        }
        previewer.putExtra("cardList", ids);
        startActivityForResultWithoutAnimation(previewer, PREVIEW_CARDS);
    // openNoteEditorForCard((long) view.getTag());
    });
    mCardsAdapter.setDeckLongClickListener(view -> {
        if (mCardsAdapter.isMultiCheckableMode()) {
            return false;
        }
        mCardsAdapter.setMultiCheckable(true);
        return true;
    });
    mCardsAdapter.setMarkClickListener(v -> {
        CollectionTask.launchCollectionTask(DISMISS_MULTI, markCardHandler(), new TaskData(new Object[] { new long[] { (long) v.getTag() }, Collection.DismissType.MARK_NOTE_MULTI }));
        mCardsAdapter.notifyDataSetChanged();
    });
    mOrderNames = getResources().getStringArray(R.array.card_browser_order_labels);
    mCardsAdapter.updateOrderState(mOrderNames[mOrder], mOrderAsc);
    mCardsAdapter.setFlagClickListener(v -> {
        if (mListPop == null) {
            mListPop = new ListPopupWindow(this);
            for (int i = 0; i < mFlagRes.length; i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("img", mFlagRes[i]);
                map.put("content", mFlagContent[i]);
                mFlagList.add(map);
            }
            mListPop.setAdapter(new SimpleAdapter(SelfStudyActivity.this, mFlagList, R.layout.item_flags_list, new String[] { "img", "content" }, new int[] { R.id.flag_icon, R.id.flag_text }));
            mListPop.setWidth(v.getRootView().getWidth() / 2);
            mListPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            // 设置是否是模式
            mListPop.setModal(true);
        }
        mListPop.setOnItemClickListener((parent, view, position, id) -> {
            CollectionTask.launchCollectionTask(DISMISS_MULTI, flagCardHandler(), new TaskData(new Object[] { new long[] { (long) v.getTag() }, Collection.DismissType.FLAG, position }));
            mCardsAdapter.notifyDataSetChanged();
            mListPop.dismiss();
        });
        mListPop.setAnchorView(v);
        mListPop.show();
    });
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    // If a valid value for last deck exists then use it, otherwise use libanki selected deck
    if ((getLastDeckId() != null && getLastDeckId() == ALL_DECKS_ID)) {
        selectAllDecks();
    } else if (getLastDeckId() != null && getCol().getDecks().get(getLastDeckId(), false) != null) {
        selectDeckById(getLastDeckId());
    } else {
        selectDeckById(getCol().getDecks().selected());
    }
    initSearchView();
    initTabLayout();
    findViewById(R.id.shadeView).setOnClickListener(v -> {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
        if (mOrderListWindow != null && mOrderListWindow.isShowing()) {
            mOrderListWindow.dismiss();
        }
    });
}
Also used : Arrays(java.util.Arrays) Bundle(android.os.Bundle) TagsDialog(com.ichi2.anki.dialogs.TagsDialog) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) ImageView(android.widget.ImageView) RangeSeekBar(com.jaygoo.widget.RangeSeekBar) ActionBar(androidx.appcompat.app.ActionBar) Decks(com.ichi2.libanki.Decks) Handler(android.os.Handler) Map(java.util.Map) JSONException(com.ichi2.utils.JSONException) OnRangeChangedListener(com.jaygoo.widget.OnRangeChangedListener) ContextCompat(androidx.core.content.ContextCompat) DUE(com.ichi2.anki.CardBrowser.Column.DUE) Upgrade(com.ichi2.upgrade.Upgrade) CREATED(com.ichi2.anki.CardBrowser.Column.CREATED) IntentFilter(android.content.IntentFilter) Set(java.util.Set) JSONObject(com.ichi2.utils.JSONObject) SearchView(androidx.appcompat.widget.SearchView) INTERVAL(com.ichi2.anki.CardBrowser.Column.INTERVAL) Nullable(androidx.annotation.Nullable) CHANGED(com.ichi2.anki.CardBrowser.Column.CHANGED) Consts(com.ichi2.libanki.Consts) CardCache(com.ichi2.anki.CardBrowser.CardCache) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) DECK(com.ichi2.anki.CardBrowser.Column.DECK) CARD(com.ichi2.anki.CardBrowser.Column.CARD) WarpLinearLayout(com.ichi2.ui.WarpLinearLayout) UPDATE_NOTE(com.ichi2.async.CollectionTask.TASK_TYPE.UPDATE_NOTE) NOTE_TYPE(com.ichi2.anki.CardBrowser.Column.NOTE_TYPE) TAGS(com.ichi2.anki.CardBrowser.Column.TAGS) SystemClock(android.os.SystemClock) RescheduleDialog(com.ichi2.anki.dialogs.RescheduleDialog) SdCardReceiver(com.ichi2.anki.receiver.SdCardReceiver) DISMISS_MULTI(com.ichi2.async.CollectionTask.TASK_TYPE.DISMISS_MULTI) Column(com.ichi2.anki.CardBrowser.Column) ArrayList(java.util.ArrayList) LAPSES(com.ichi2.anki.CardBrowser.Column.LAPSES) RENDER_BROWSER_QA(com.ichi2.async.CollectionTask.TASK_TYPE.RENDER_BROWSER_QA) SFLD(com.ichi2.anki.CardBrowser.Column.SFLD) Toast(android.widget.Toast) Menu(android.view.Menu) SimpleMessageDialog(com.ichi2.anki.dialogs.SimpleMessageDialog) REVIEWS(com.ichi2.anki.CardBrowser.Column.REVIEWS) IntegerDialog(com.ichi2.anki.dialogs.IntegerDialog) EASE(com.ichi2.anki.CardBrowser.Column.EASE) CheckResult(androidx.annotation.CheckResult) TextUtils(android.text.TextUtils) Gravity(android.view.Gravity) ArrayAdapter(android.widget.ArrayAdapter) SharedPreferences(android.content.SharedPreferences) TypedValue(android.util.TypedValue) ActivityTransitionAnimation(com.ichi2.anim.ActivityTransitionAnimation) CardBrowserOrderDialog(com.ichi2.anki.dialogs.CardBrowserOrderDialog) CardBrowser.sCardBrowserCard(com.ichi2.anki.CardBrowser.sCardBrowserCard) LinearLayout(android.widget.LinearLayout) WindowManager(android.view.WindowManager) UNDO(com.ichi2.async.CollectionTask.TASK_TYPE.UNDO) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) Permissions(com.ichi2.utils.Permissions) FunctionalInterfaces(com.ichi2.utils.FunctionalInterfaces) CheckBox(android.widget.CheckBox) Locale(java.util.Locale) View(android.view.View) Button(android.widget.Button) TaskData(com.ichi2.async.TaskData) AdapterView(android.widget.AdapterView) RecyclerView(androidx.recyclerview.widget.RecyclerView) CardBrowserMySearchesDialog(com.ichi2.anki.dialogs.CardBrowserMySearchesDialog) TabLayout(com.google.android.material.tabs.TabLayout) SEARCH_CARDS(com.ichi2.async.CollectionTask.TASK_TYPE.SEARCH_CARDS) ANSWER(com.ichi2.anki.CardBrowser.Column.ANSWER) BroadcastReceiver(android.content.BroadcastReceiver) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) AlertDialog(android.app.AlertDialog) Objects(java.util.Objects) List(java.util.List) TextView(android.widget.TextView) OnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) SimpleAdapter(android.widget.SimpleAdapter) OrderListAdapter(com.ichi2.anki.widgets.OrderListAdapter) RelativeLayout(android.widget.RelativeLayout) Toolbar(androidx.appcompat.widget.Toolbar) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Snackbar(com.google.android.material.snackbar.Snackbar) Context(android.content.Context) EDITED(com.ichi2.anki.CardBrowser.Column.EDITED) Intent(android.content.Intent) HashMap(java.util.HashMap) CHECK_CARD_SELECTION(com.ichi2.async.CollectionTask.TASK_TYPE.CHECK_CARD_SELECTION) Collection(com.ichi2.libanki.Collection) TypedArray(android.content.res.TypedArray) MenuItem(android.view.MenuItem) InputMethodManager(android.view.inputmethod.InputMethodManager) HashSet(java.util.HashSet) CardsListAdapter(com.ichi2.anki.widgets.CardsListAdapter) DeckDropDownAdapter(com.ichi2.anki.widgets.DeckDropDownAdapter) WidgetStatus(com.ichi2.widget.WidgetStatus) QUESTION(com.ichi2.anki.CardBrowser.Column.QUESTION) Utils(com.ichi2.libanki.Utils) ListPopupWindow(androidx.appcompat.widget.ListPopupWindow) Iterator(java.util.Iterator) Stats(com.ichi2.libanki.stats.Stats) PopupWindow(android.widget.PopupWindow) CollectionTask(com.ichi2.async.CollectionTask) Spinner(android.widget.Spinner) KeyBoardListenerLayout(com.ichi2.ui.KeyBoardListenerLayout) Card(com.ichi2.libanki.Card) Collections(java.util.Collections) VisibleForTesting(androidx.annotation.VisibleForTesting) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) AlertDialog(android.app.AlertDialog) HashMap(java.util.HashMap) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) DeckDropDownAdapter(com.ichi2.anki.widgets.DeckDropDownAdapter) TaskData(com.ichi2.async.TaskData) ListPopupWindow(androidx.appcompat.widget.ListPopupWindow) TypedArray(android.content.res.TypedArray) TextView(android.widget.TextView) ActionBar(androidx.appcompat.app.ActionBar) SharedPreferences(android.content.SharedPreferences) Deck(com.ichi2.libanki.Deck) JSONException(com.ichi2.utils.JSONException) SimpleAdapter(android.widget.SimpleAdapter) Intent(android.content.Intent) JSONException(com.ichi2.utils.JSONException) CardsListAdapter(com.ichi2.anki.widgets.CardsListAdapter) CardCache(com.ichi2.anki.CardBrowser.CardCache) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) CheckBox(android.widget.CheckBox) JSONObject(com.ichi2.utils.JSONObject) ArrayAdapter(android.widget.ArrayAdapter)

Example 82 with TaskData

use of com.ichi2.async.TaskData in project AnkiChinaAndroid by ankichinateam.

the class SelfStudyActivity method executeChangeCollectionTask.

@VisibleForTesting(otherwise = VisibleForTesting.NONE)
// should only be called from changeDeck()
void executeChangeCollectionTask(long[] ids, long newDid) {
    // line required for unit tests, not necessary, but a noop in regular call.
    mNewDid = newDid;
    CollectionTask.launchCollectionTask(DISMISS_MULTI, new ChangeDeckHandler(this), new TaskData(new Object[] { ids, Collection.DismissType.CHANGE_DECK_MULTI, newDid }));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) TaskData(com.ichi2.async.TaskData) VisibleForTesting(androidx.annotation.VisibleForTesting)

Example 83 with TaskData

use of com.ichi2.async.TaskData in project AnkiChinaAndroid by ankichinateam.

the class SelfStudyActivity method updateMultiselectMenu.

private void updateMultiselectMenu() {
    Timber.d("updateMultiselectMenu()");
    if (mActionBarMenu == null || mActionBarMenu.findItem(R.id.action_suspend_card) == null) {
        return;
    }
    if (getSelectedCardIds().length != 0) {
        CollectionTask.cancelAllTasks(CHECK_CARD_SELECTION);
        CollectionTask.launchCollectionTask(CHECK_CARD_SELECTION, mCheckSelectedCardsHandler, new TaskData(new Object[] { getSelectedCards(), getCards() }));
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) TaskData(com.ichi2.async.TaskData)

Example 84 with TaskData

use of com.ichi2.async.TaskData in project AnkiChinaAndroid by ankichinateam.

the class SelfStudyActivity method searchCards.

private void searchCards() {
    // cancel the previous search & render tasks if still running
    invalidate();
    String searchText;
    if (mSearchTerms == null) {
        mSearchTerms = "";
    }
    if (!"".equals(mSearchTerms) && (mSearchView != null)) {
        mSearchView.setQuery(mSearchTerms, false);
    }
    if (mSearchTerms == null) {
        Timber.i("update mSearchTerms text:%s", mSearchTerms);
        mSearchTerms = "";
    }
    Timber.i("show the final mSearchTerms text:%s", mSearchTerms);
    if (mSearchTerms.contains("deck:")) {
        searchText = mRestrictOnTab + " " + mSearchStudyFilter + " " + mSearchMarkFilter + " " + mSearchAnswerFilter + " " + mRangeSearchReviewTimesFilter + " " + mRangeCreateTimeFilter + " " + mRangeSearchAnswerFilter + " " + mRangeSearchForgetTimesFilter + " " + mTagsFilter + " " + mSearchTerms;
    } else {
        searchText = mRestrictOnDeck + " " + mRestrictOnTab + " " + mSearchStudyFilter + " " + mSearchMarkFilter + " " + mSearchAnswerFilter + " " + mRangeSearchReviewTimesFilter + " " + mRangeCreateTimeFilter + " " + mRangeSearchAnswerFilter + " " + mRangeSearchForgetTimesFilter + " " + mTagsFilter + " " + mSearchTerms;
    }
    // String showingText=searchText;
    // if(!mShowGrammarInSearchView){
    // searchText=mSearchTerms;
    // }
    Timber.i("mRestrictOnDeck: +%s+  mRestrictOnTab +%s+  mSearchStudyFilter +%s+  mSearchMarkFilter +%s+  mSearchAnswerFilter+%s+  mRangeSearchReviewTimesFilter + %s + mRangeCreateTimeFilter +%s+  mRangeSearchAnswerFilter +%s+  mRangeSearchForgetTimesFilter +%s+ mTagsFilter +%s+  mSearchTerms +%s+ ", mRestrictOnDeck, mRestrictOnTab, mSearchStudyFilter, mSearchMarkFilter, mSearchAnswerFilter, mRangeSearchReviewTimesFilter, mRangeCreateTimeFilter, mRangeSearchAnswerFilter, mRangeSearchForgetTimesFilter, mTagsFilter, mSearchTerms);
    Timber.i("show the final search text:%s", searchText);
    if (!mShowGrammarInSearchView) {
        mSearchView.setQuery(mSearchTerms, false);
    } else if (!"".equals(searchText) && (mSearchView != null)) {
        mSearchView.setQuery(searchText, false);
    }
    if (colIsOpen() && mCardsAdapter != null) {
        // clear the existing card list
        mCards = new ArrayList<>();
        mCardsAdapter.notifyDataSetChanged();
        // estimate maximum number of cards that could be visible (assuming worst-case minimum row height of 20dp)
        int numCardsToRender = (int) Math.ceil(mCardsListView.getHeight() / TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics())) + 5;
        Timber.i("I wanna get %d cards", numCardsToRender);
        // Perform database query to get all card ids
        CollectionTask.launchCollectionTask(SEARCH_CARDS, mSearchCardsHandler, new TaskData(new Object[] { searchText, ((mOrder != CARD_ORDER_NONE)), numCardsToRender, mColumn1Index, mColumn2Index }));
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) TaskData(com.ichi2.async.TaskData)

Example 85 with TaskData

use of com.ichi2.async.TaskData in project AnkiChinaAndroid by ankichinateam.

the class StudyOptionsFragment method onMenuItemClick.

@SuppressWarnings("deprecation")
@Override
public boolean onMenuItemClick(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_browser:
            {
                Timber.i("DeckPicker:: Old browser button pressed");
                getAnkiActivity().openOldCardBrowser();
                return true;
            }
        case R.id.action_undo:
            Timber.i("StudyOptionsFragment:: Undo button pressed");
            CollectionTask.launchCollectionTask(UNDO, undoListener);
            return true;
        case R.id.action_deck_options:
            Timber.i("StudyOptionsFragment:: Deck options button pressed");
            if (getCol().getDecks().isDyn(getCol().getDecks().selected())) {
                openFilteredDeckOptions();
            } else {
                Intent i = new Intent(getActivity(), DeckOptions.class);
                getActivity().startActivityForResult(i, DECK_OPTIONS);
                ActivityTransitionAnimation.slide(getActivity(), ActivityTransitionAnimation.FADE);
            }
            return true;
        case R.id.action_setting:
            Timber.i("StudyOptionsFragment:: Deck setting button pressed");
            Intent i = new Intent(getActivity(), StudySettingActivity.class);
            getActivity().startActivityForResult(i, DECK_OPTIONS);
            ActivityTransitionAnimation.slide(getActivity(), ActivityTransitionAnimation.FADE);
            return true;
        case R.id.action_custom_study:
            Timber.i("StudyOptionsFragment:: custom study button pressed");
            showCustomStudyContextMenu();
            return true;
        case R.id.action_unbury:
            Timber.i("StudyOptionsFragment:: unbury button pressed");
            getCol().getSched().unburyCardsForDeck();
            refreshInterfaceAndDecklist(true);
            item.setVisible(false);
            return true;
        case R.id.action_rebuild:
            Timber.i("StudyOptionsFragment:: rebuild cram deck button pressed");
            mProgressDialog = StyledProgressDialog.show(getActivity(), "", getResources().getString(R.string.rebuild_filtered_deck), true);
            CollectionTask.launchCollectionTask(REBUILD_CRAM, getCollectionTaskListener(true));
            return true;
        case R.id.action_empty:
            Timber.i("StudyOptionsFragment:: empty cram deck button pressed");
            mProgressDialog = StyledProgressDialog.show(getActivity(), "", getResources().getString(R.string.empty_filtered_deck), false);
            CollectionTask.launchCollectionTask(EMPTY_CRAM, getCollectionTaskListener(true));
            return true;
        case R.id.action_rename:
            ((AnkiActivity) getActivity()).renameDeckDialog(getCol().getDecks().selected());
            return true;
        case R.id.action_delete:
            getAnkiActivity().mContextMenuDid = getCol().getDecks().selected();
            ((AnkiActivity) getActivity()).confirmDeckDeletion(getCol().getDecks().selected());
            return true;
        case R.id.action_export:
            ((AnkiActivity) getActivity()).exportDeck(getCol().getDecks().selected());
            return true;
        case R.id.action_add_card:
            addNote();
            return true;
        case R.id.create_deck:
            getAnkiActivity().mContextMenuDid = getCol().getDecks().selected();
            ((AnkiActivity) getActivity()).createSubdeckDialog();
            return true;
        case // 停止/恢复
        R.id.action_suspend:
            mDeckIsStopped = deckIsStopped();
            if (mDeckIsStopped) {
                customDialog = new CustomStyleDialog.Builder(getAnkiActivity()).setTitle("恢复学习").setMessage("恢复学习后将每天新卡数和复习数值调整成默认值,请确认是否开始学习").setPositiveButton("确定", (dialog, which) -> {
                    resumeDeckStudy();
                    dialog.dismiss();
                }).setNegativeButton("取消", (dialog, which) -> dialog.dismiss()).create();
            } else {
                customDialog = new CustomStyleDialog.Builder(getAnkiActivity()).setTitle("停止学习").setMessage("暂停学习将会自动把每天新卡数和复习数调整为零,适用于有事暂停一两周不学习,若该记忆库学习的时长不超过2个月,又暂停了30天未学习,建议重设进度,重新开始学习").setPositiveButton("确定", (dialog, which) -> {
                    stopDeckStudy();
                    dialog.dismiss();
                }).setNegativeButton("取消", (dialog, which) -> dialog.dismiss()).create();
            }
            customDialog.show();
            return true;
        case // 重设学习进度
        R.id.action_reset_card_progress:
            customDialog = new CustomStyleDialog.Builder(getAnkiActivity()).setTitle("重设学习进度").setMessage("重设学习进度后,该记忆库的所有卡片学习记录都会被清除,请谨慎操作").setPositiveButton("确定", (dialog, which) -> {
                CollectionTask.launchCollectionTask(RESET_DECK, new ResetCardHandler(StudyOptionsFragment.this), new TaskData(new Object[] { "deck:\"" + getCol().getDecks().current().getString("name") + "\" " }));
                dialog.dismiss();
            }).setNegativeButton("取消", (dialog, which) -> dialog.dismiss()).create();
            customDialog.show();
            return true;
        default:
            return false;
    }
}
Also used : DividerItemDecoration(androidx.recyclerview.widget.DividerItemDecoration) Bundle(android.os.Bundle) TypeToken(com.google.gson.reflect.TypeToken) Deck(com.ichi2.libanki.Deck) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) WindowManager(android.view.WindowManager) ImageView(android.widget.ImageView) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) Maps(java8.util.Maps) KEY_STOPPED(com.ichi2.anki.StudySettingActivity.KEY_STOPPED) DeckInfoListAdapter(com.ichi2.anki.widgets.DeckInfoListAdapter) Decks(com.ichi2.libanki.Decks) Gson(com.google.gson.Gson) Locale(java.util.Locale) Handler(android.os.Handler) DeckConfig(com.ichi2.libanki.DeckConfig) Map(java.util.Map) Fragment(androidx.fragment.app.Fragment) View(android.view.View) Button(android.widget.Button) TaskData(com.ichi2.async.TaskData) RecyclerView(androidx.recyclerview.widget.RecyclerView) REQUEST_BROWSE_CARDS(com.ichi2.anki.DeckPicker.REQUEST_BROWSE_CARDS) TabLayout(com.google.android.material.tabs.TabLayout) TYPE_LIFE(com.ichi2.libanki.stats.Stats.AxisType.TYPE_LIFE) SHOW_STUDYOPTIONS(com.ichi2.anki.DeckPicker.SHOW_STUDYOPTIONS) Set(java.util.Set) FragmentTransaction(androidx.fragment.app.FragmentTransaction) DisplayMetrics(android.util.DisplayMetrics) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) DeckPickerContextMenu(com.ichi2.anki.dialogs.DeckPickerContextMenu) List(java.util.List) Nullable(androidx.annotation.Nullable) TextView(android.widget.TextView) NextCardHandler(com.ichi2.anki.AbstractFlashcardViewer.NextCardHandler) ALL_DECKS_ID(com.ichi2.libanki.stats.Stats.ALL_DECKS_ID) CompatHelper(com.ichi2.compat.CompatHelper) Toolbar(androidx.appcompat.widget.Toolbar) RelativeLayout(android.widget.RelativeLayout) TaskListenerWithContext(com.ichi2.async.TaskListenerWithContext) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Window(android.view.Window) Snackbar(com.google.android.material.snackbar.Snackbar) DialogFragment(androidx.fragment.app.DialogFragment) KEY_SHOW_TTS_ICON(com.ichi2.libanki.Consts.KEY_SHOW_TTS_ICON) Context(android.content.Context) TaskListener(com.ichi2.async.TaskListener) Dialog(android.app.Dialog) Intent(android.content.Intent) StepsPreference.convertToJSON(com.ichi2.preferences.StepsPreference.convertToJSON) HashMap(java.util.HashMap) Collection(com.ichi2.libanki.Collection) StyledProgressDialog(com.ichi2.themes.StyledProgressDialog) PixelFormat(android.graphics.PixelFormat) Editable(android.text.Editable) TypedArray(android.content.res.TypedArray) MenuItem(android.view.MenuItem) TASK_TYPE(com.ichi2.async.CollectionTask.TASK_TYPE) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) Menu(android.view.Menu) Utils(com.ichi2.libanki.Utils) AbstractDeckTreeNode(com.ichi2.libanki.sched.AbstractDeckTreeNode) Stats(com.ichi2.libanki.stats.Stats) LayoutInflater(android.view.LayoutInflater) CollectionTask(com.ichi2.async.CollectionTask) Color(android.graphics.Color) Gravity(android.view.Gravity) SharedPreferences(android.content.SharedPreferences) TypedValue(android.util.TypedValue) TreeMap(java.util.TreeMap) KEY_MIND_MODE(com.ichi2.anki.StudySettingActivity.KEY_MIND_MODE) CustomStudyDialog(com.ichi2.anki.dialogs.CustomStudyDialog) DeckPicker.fadeOut(com.ichi2.anki.DeckPicker.fadeOut) Activity(android.app.Activity) ActivityTransitionAnimation(com.ichi2.anim.ActivityTransitionAnimation) DeckPicker.fadeIn(com.ichi2.anki.DeckPicker.fadeIn) STUDY_SETTING(com.ichi2.anki.StudySettingActivity.STUDY_SETTING) EditText(android.widget.EditText) CustomStyleDialog(com.ichi2.ui.CustomStyleDialog) Resources(android.content.res.Resources) Intent(android.content.Intent) TaskData(com.ichi2.async.TaskData)

Aggregations

Collection (com.ichi2.libanki.Collection)67 TaskData (com.ichi2.async.TaskData)46 JSONObject (com.ichi2.utils.JSONObject)35 Card (com.ichi2.libanki.Card)16 JSONException (com.ichi2.utils.JSONException)15 ArrayList (java.util.ArrayList)14 JSONObject (org.json.JSONObject)13 Resources (android.content.res.Resources)12 HashMap (java.util.HashMap)12 SharedPreferences (android.content.SharedPreferences)11 Deck (com.ichi2.libanki.Deck)11 Intent (android.content.Intent)10 View (android.view.View)9 TextView (android.widget.TextView)9 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)9 TaskListener (com.ichi2.async.TaskListener)9 Map (java.util.Map)9 VisibleForTesting (androidx.annotation.VisibleForTesting)8 CollectionTask (com.ichi2.async.CollectionTask)8 IOException (java.io.IOException)8