Search in sources :

Example 61 with ConfirmModSchemaException

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

the class Storage method _upgrade.

private static void _upgrade(Collection col, int ver) {
    try {
        if (ver < 3) {
            // new deck properties
            for (Deck d : col.getDecks().all()) {
                d.put("dyn", DECK_STD);
                d.put("collapsed", false);
                col.getDecks().save(d);
            }
        }
        if (ver < 4) {
            col.modSchemaNoCheck();
            List<Model> models = col.getModels().all();
            ArrayList<Model> clozes = new ArrayList<>(models.size());
            for (Model m : models) {
                if (!m.getJSONArray("tmpls").getJSONObject(0).getString("qfmt").contains("{{cloze:")) {
                    m.put("type", Consts.MODEL_STD);
                } else {
                    clozes.add(m);
                }
            }
            for (Model m : clozes) {
                try {
                    _upgradeClozeModel(col, m);
                } catch (ConfirmModSchemaException e) {
                    // Will never be reached as we already set modSchemaNoCheck()
                    throw new RuntimeException(e);
                }
            }
            col.getDb().execute("UPDATE col SET ver = 4");
        }
        if (ver < 5) {
            col.getDb().execute("UPDATE cards SET odue = 0 WHERE queue = 2");
            col.getDb().execute("UPDATE col SET ver = 5");
        }
        if (ver < 6) {
            col.modSchemaNoCheck();
            for (Model m : col.getModels().all()) {
                m.put("css", new JSONObject(Models.DEFAULT_MODEL).getString("css"));
                JSONArray ar = m.getJSONArray("tmpls");
                for (JSONObject t : ar.jsonObjectIterable()) {
                    if (!t.has("css")) {
                        continue;
                    }
                    m.put("css", m.getString("css") + "\n" + t.getString("css").replace(".card ", ".card" + t.getInt("ord") + 1));
                    t.remove("css");
                }
                col.getModels().save(m);
            }
            col.getDb().execute("UPDATE col SET ver = 6");
        }
        if (ver < 7) {
            col.modSchemaNoCheck();
            col.getDb().execute("UPDATE cards SET odue = 0 WHERE (type = " + Consts.CARD_TYPE_LRN + " OR queue = 2) AND NOT odid");
            col.getDb().execute("UPDATE col SET ver = 7");
        }
        if (ver < 8) {
            col.modSchemaNoCheck();
            col.getDb().execute("UPDATE cards SET due = due / 1000 WHERE due > 4294967296");
            col.getDb().execute("UPDATE col SET ver = 8");
        }
        if (ver < 9) {
            col.getDb().execute("UPDATE col SET ver = 9");
        }
        if (ver < 10) {
            col.getDb().execute("UPDATE cards SET left = left + left * 1000 WHERE queue = " + Consts.QUEUE_TYPE_LRN);
            col.getDb().execute("UPDATE col SET ver = 10");
        }
        if (ver < 11) {
            col.modSchemaNoCheck();
            for (Deck d : col.getDecks().all()) {
                if (d.isDyn()) {
                    int order = d.getInt("order");
                    // failed order was removed
                    if (order >= 5) {
                        order -= 1;
                    }
                    JSONArray terms = new JSONArray(Arrays.asList(d.getString("search"), d.getInt("limit"), order));
                    d.put("terms", new JSONArray());
                    d.getJSONArray("terms").put(0, terms);
                    d.remove("search");
                    d.remove("limit");
                    d.remove("order");
                    d.put("resched", true);
                    d.put("return", true);
                } else {
                    if (!d.has("extendNew")) {
                        d.put("extendNew", 10);
                        d.put("extendRev", 50);
                    }
                }
                col.getDecks().save(d);
            }
            for (DeckConfig c : col.getDecks().allConf()) {
                JSONObject r = c.getJSONObject("rev");
                r.put("ivlFct", r.optDouble("ivlFct", 1));
                if (r.has("ivlfct")) {
                    r.remove("ivlfct");
                }
                r.put("maxIvl", 36500);
                col.getDecks().save(c);
            }
            for (Model m : col.getModels().all()) {
                JSONArray tmpls = m.getJSONArray("tmpls");
                for (JSONObject t : tmpls.jsonObjectIterable()) {
                    t.put("bqfmt", "");
                    t.put("bafmt", "");
                }
                col.getModels().save(m);
            }
            col.getDb().execute("update col set ver = 11");
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) JSONException(com.ichi2.utils.JSONException)

Example 62 with ConfirmModSchemaException

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

the class ModelBrowser method deleteModelDialog.

/*
     * Displays a confirmation box asking if you want to delete the note type and then deletes it if confirmed
     */
private void deleteModelDialog() {
    if (mModelIds.size() > 1) {
        Runnable confirm = () -> {
            mCol.modSchemaNoCheck();
            deleteModel();
            dismissContextMenu();
        };
        Runnable cancel = this::dismissContextMenu;
        try {
            mCol.modSchema();
            ConfirmationDialog d = new ConfirmationDialog();
            d.setArgs(getResources().getString(R.string.model_delete_warning));
            d.setConfirm(confirm);
            d.setCancel(cancel);
            ModelBrowser.this.showDialogFragment(d);
        } catch (ConfirmModSchemaException e) {
            e.log();
            ConfirmationDialog c = new ConfirmationDialog();
            c.setArgs(getResources().getString(R.string.full_sync_confirmation));
            c.setConfirm(confirm);
            c.setCancel(cancel);
            showDialogFragment(c);
        }
    } else // Prevent users from deleting last model
    {
        showToast(getString(R.string.toast_last_model));
    }
}
Also used : ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog)

Example 63 with ConfirmModSchemaException

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

the class ModelFieldEditor method repositionFieldDialog.

/*
     * Allows the user to select a number less than the number of fields in the current model to
     * reposition the current field to
     * Processing time is scales with number of items
     */
private void repositionFieldDialog() {
    mFieldNameInput = new FixedEditText(this);
    mFieldNameInput.setRawInputType(InputType.TYPE_CLASS_NUMBER);
    new MaterialEditTextDialog.Builder(this, mFieldNameInput).title(String.format(getResources().getString(R.string.model_field_editor_reposition), 1, mFieldLabels.size())).positiveText(R.string.dialog_ok).onPositive((dialog, which) -> {
        String newPosition = mFieldNameInput.getText().toString();
        int pos;
        try {
            pos = Integer.parseInt(newPosition);
        } catch (NumberFormatException n) {
            Timber.w(n);
            UIUtils.showThemedToast(this, getResources().getString(R.string.toast_out_of_range), true);
            return;
        }
        if (pos < 1 || pos > mFieldLabels.size()) {
            UIUtils.showThemedToast(this, getResources().getString(R.string.toast_out_of_range), true);
        } else {
            changeHandler listener = changeFieldHandler();
            // Input is valid, now attempt to modify
            try {
                mCol.modSchema();
                TaskManager.launchCollectionTask(new CollectionTask.RepositionField(mMod, mNoteFields.getJSONObject(mCurrentPos), pos - 1), listener);
            } catch (ConfirmModSchemaException e) {
                e.log();
                // Handle mod schema confirmation
                ConfirmationDialog c = new ConfirmationDialog();
                c.setArgs(getResources().getString(R.string.full_sync_confirmation));
                Runnable confirm = () -> {
                    try {
                        mCol.modSchemaNoCheck();
                        TaskManager.launchCollectionTask(new CollectionTask.RepositionField(mMod, mNoteFields.getJSONObject(mCurrentPos), pos - 1), listener);
                        dismissContextMenu();
                    } catch (JSONException e1) {
                        throw new RuntimeException(e1);
                    }
                };
                c.setConfirm(confirm);
                c.setCancel(mConfirmDialogCancel);
                ModelFieldEditor.this.showDialogFragment(c);
            }
        }
    }).negativeText(R.string.dialog_cancel).show();
}
Also used : JSONException(com.ichi2.utils.JSONException) CollectionTask(com.ichi2.async.CollectionTask) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) FixedEditText(com.ichi2.ui.FixedEditText)

Example 64 with ConfirmModSchemaException

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

the class ModelFieldEditor method sortByField.

/*
     * Changes the sort field (that displays in card browser) to the current field
     */
private void sortByField() {
    changeHandler listener = changeFieldHandler();
    try {
        mCol.modSchema();
        TaskManager.launchCollectionTask(new CollectionTask.ChangeSortField(mMod, mCurrentPos), listener);
    } catch (ConfirmModSchemaException e) {
        e.log();
        // Handler mMod schema confirmation
        ConfirmationDialog c = new ConfirmationDialog();
        c.setArgs(getResources().getString(R.string.full_sync_confirmation));
        Runnable confirm = () -> {
            mCol.modSchemaNoCheck();
            TaskManager.launchCollectionTask(new CollectionTask.ChangeSortField(mMod, mCurrentPos), listener);
            dismissContextMenu();
        };
        c.setConfirm(confirm);
        c.setCancel(mConfirmDialogCancel);
        ModelFieldEditor.this.showDialogFragment(c);
    }
}
Also used : ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) CollectionTask(com.ichi2.async.CollectionTask) ConfirmationDialog(com.ichi2.anki.dialogs.ConfirmationDialog)

Example 65 with ConfirmModSchemaException

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

the class ModelFieldEditor method renameField.

/*
     * Renames the current field
     */
private void renameField() throws ConfirmModSchemaException {
    String fieldLabel = mFieldNameInput.getText().toString().replaceAll("[\\n\\r]", "");
    JSONObject field = mNoteFields.getJSONObject(mCurrentPos);
    mCol.getModels().renameField(mMod, field, fieldLabel);
    mCol.getModels().save();
    fullRefreshList();
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)39 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)26 JSONArray (com.ichi2.utils.JSONArray)23 Test (org.junit.Test)22 RobolectricTest (com.ichi2.anki.RobolectricTest)14 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)12 Collection (com.ichi2.libanki.Collection)12 Model (com.ichi2.libanki.Model)12 Note (com.ichi2.libanki.Note)8 ArrayList (java.util.ArrayList)8 JSONException (com.ichi2.utils.JSONException)6 HashMap (java.util.HashMap)5 SdkSuppress (androidx.test.filters.SdkSuppress)4 TaskData (com.ichi2.async.TaskData)4 ModelManager (com.ichi2.libanki.ModelManager)4 TextImporter (com.ichi2.libanki.importer.TextImporter)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Cursor (android.database.Cursor)3 Uri (android.net.Uri)3 Bundle (android.os.Bundle)3