Search in sources :

Example 11 with TaskListener

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

the class CustomStudyDialog method createCustomStudySession.

/**
 * Create a custom study session
 *
 * @param delays  delay options for scheduling algorithm
 * @param terms   search terms
 * @param resched whether to reschedule the cards based on the answers given (or ignore them if false)
 */
private void createCustomStudySession(JSONArray delays, Object[] terms, Boolean resched) {
    Deck dyn;
    final AnkiActivity activity = getAnkiActivity();
    Collection col = CollectionHelper.getInstance().getCol(activity);
    long did = getArguments().getLong("did");
    String deckToStudyName = col.getDecks().get(did).getString("name");
    String customStudyDeck = getResources().getString(R.string.custom_study_deck_name);
    Deck cur = col.getDecks().byName(customStudyDeck);
    if (cur != null) {
        Timber.i("Found deck: '%s'", customStudyDeck);
        if (cur.getInt("dyn") != 1) {
            Timber.w("Deck: '%s' was non-dynamic", customStudyDeck);
            new MaterialDialog.Builder(getActivity()).content(R.string.custom_study_deck_exists).negativeText(R.string.dialog_cancel).build().show();
            return;
        } else {
            Timber.i("Emptying dynamic deck '%s' for custom study", customStudyDeck);
            // safe to empty
            col.getSched().emptyDyn(cur.getLong("id"));
            // reuse; don't delete as it may have children
            dyn = cur;
            col.getDecks().select(cur.getLong("id"));
        }
    } else {
        Timber.i("Creating Dynamic Deck '%s' for custom study", customStudyDeck);
        long customStudyDid = col.getDecks().newDyn(customStudyDeck);
        dyn = col.getDecks().get(customStudyDid);
    }
    if (!dyn.has("terms")) {
        // #5959 - temp code to diagnose why terms doesn't exist.
        // normally we wouldn't want to log this much, but we need to know how deep the corruption is to fix the
        // issue
        Timber.w("Invalid Dynamic Deck: %s", dyn);
        AnkiDroidApp.sendExceptionReport("Custom Study Deck had no terms", "CustomStudyDialog - createCustomStudySession");
        UIUtils.showThemedToast(this.getContext(), getString(R.string.custom_study_rebuild_deck_corrupt), false);
        return;
    }
    // and then set various options
    if (delays.length() > 0) {
        dyn.put("delays", delays);
    } else {
        dyn.put("delays", JSONObject.NULL);
    }
    JSONArray ar = dyn.getJSONArray("terms");
    ar.getJSONArray(0).put(0, "deck:\"" + deckToStudyName + "\" " + terms[0]);
    ar.getJSONArray(0).put(1, terms[1]);
    @Consts.DYN_PRIORITY int priority = (Integer) terms[2];
    ar.getJSONArray(0).put(2, priority);
    dyn.put("resched", resched);
    // Rebuild the filtered deck
    Timber.i("Rebuilding Custom Study Deck");
    TaskListener listener = createCustomStudySessionListener();
    CollectionTask.launchCollectionTask(REBUILD_CRAM, listener);
    // Hide the dialogs
    activity.dismissAllDialogFragments();
}
Also used : AnkiActivity(com.ichi2.anki.AnkiActivity) JSONArray(com.ichi2.utils.JSONArray) TaskListener(com.ichi2.async.TaskListener) Deck(com.ichi2.libanki.Deck) Collection(com.ichi2.libanki.Collection)

Example 12 with TaskListener

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

the class RobolectricTest method waitForTask.

protected synchronized void waitForTask(CollectionTask.TASK_TYPE taskType, @Nullable TaskData data, int timeoutMs) throws InterruptedException {
    boolean[] completed = new boolean[] { false };
    TaskListener listener = new TaskListener() {

        @Override
        public void onPreExecute() {
        }

        @Override
        public void onPostExecute(TaskData result) {
            if (result == null || !result.getBoolean()) {
                throw new IllegalArgumentException("Task failed");
            }
            completed[0] = true;
            synchronized (RobolectricTest.this) {
                RobolectricTest.this.notify();
            }
        }
    };
    CollectionTask.launchCollectionTask(taskType, listener, data);
    wait(timeoutMs);
    if (!completed[0]) {
        throw new IllegalStateException(String.format("Task %s didn't finish in %d ms", taskType, timeoutMs));
    }
}
Also used : TaskListener(com.ichi2.async.TaskListener) TaskData(com.ichi2.async.TaskData)

Aggregations

TaskListener (com.ichi2.async.TaskListener)10 TaskData (com.ichi2.async.TaskData)6 CollectionTask (com.ichi2.async.CollectionTask)2 Deck (com.ichi2.libanki.Deck)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 AnkiActivity (com.ichi2.anki.AnkiActivity)1 DeckRenameException (com.ichi2.anki.exception.DeckRenameException)1 TASK_TYPE (com.ichi2.async.CollectionTask.TASK_TYPE)1 DeckTask (com.ichi2.async.DeckTask)1 TaskData (com.ichi2.async.DeckTask.TaskData)1 TaskListener (com.ichi2.async.DeckTask.TaskListener)1 Collection (com.ichi2.libanki.Collection)1 JSONArray (com.ichi2.utils.JSONArray)1 JSONException (com.ichi2.utils.JSONException)1 IOException (java.io.IOException)1