Search in sources :

Example 11 with AnkiActivity

use of com.ichi2.anki.AnkiActivity 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 AnkiActivity

use of com.ichi2.anki.AnkiActivity in project AnkiChinaAndroid by ankichinateam.

the class DeckPickerConfirmDeleteDeckDialog method onCreateDialog.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Resources res = getResources();
    return new MaterialDialog.Builder(getActivity()).title(res.getString(R.string.delete_deck_title)).content(getArguments().getString("dialogMessage")).iconAttr(R.attr.dialogErrorIcon).positiveText(res.getString(R.string.dialog_positive_delete)).negativeText(res.getString(R.string.dialog_cancel)).cancelable(true).onPositive((dialog, which) -> {
        ((AnkiActivity) getActivity()).deleteContextMenuDeck();
        ((AnkiActivity) getActivity()).dismissAllDialogFragments();
    }).onNegative((dialog, which) -> ((AnkiActivity) getActivity()).dismissAllDialogFragments()).build();
}
Also used : DeckPicker(com.ichi2.anki.DeckPicker) AnkiActivity(com.ichi2.anki.AnkiActivity) Bundle(android.os.Bundle) AnalyticsDialogFragment(com.ichi2.anki.analytics.AnalyticsDialogFragment) R(com.ichi2.anki.R) Dialog(android.app.Dialog) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Resources(android.content.res.Resources) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) AnkiActivity(com.ichi2.anki.AnkiActivity) Resources(android.content.res.Resources)

Aggregations

MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)5 AnkiActivity (com.ichi2.anki.AnkiActivity)5 Intent (android.content.Intent)4 Resources (android.content.res.Resources)4 ArrayList (java.util.ArrayList)4 Bundle (android.os.Bundle)3 View (android.view.View)3 Timber (timber.log.Timber)3 Dialog (android.app.Dialog)2 Context (android.content.Context)2 Uri (android.net.Uri)2 Handler (android.os.Handler)2 TypedValue (android.util.TypedValue)2 WindowManager (android.view.WindowManager)2 TextView (android.widget.TextView)2 NonNull (androidx.annotation.NonNull)2 Nullable (androidx.annotation.Nullable)2 Snackbar (com.google.android.material.snackbar.Snackbar)2 CustomStudyDialog (com.ichi2.anki.dialogs.CustomStudyDialog)2 TaskListener (com.ichi2.async.TaskListener)2