Search in sources :

Example 76 with Model

use of com.ichi2.libanki.Model in project Anki-Android by ankidroid.

the class NoteEditor method updateFieldsFromMap.

/**
 * Update all the field EditText views based on the currently selected note type and the mModelChangeFieldMap
 */
private void updateFieldsFromMap(Model newModel) {
    FieldChangeType type = FieldChangeType.refreshWithMap(newModel, mModelChangeFieldMap, shouldReplaceNewlines());
    populateEditFields(type, true);
    updateCards(newModel);
}
Also used : FieldChangeType(com.ichi2.anki.noteeditor.FieldState.FieldChangeType)

Example 77 with Model

use of com.ichi2.libanki.Model in project Anki-Android by ankidroid.

the class TemporaryModel method fromBundle.

/**
 * Load the TemporaryModel from the filename included in a Bundle
 *
 * @param bundle a Bundle that should contain persisted JSON under INTENT_MODEL_FILENAME key
 * @return re-hydrated TemporaryModel or null if there was a problem, null means should reload from database
 */
@Nullable
public static TemporaryModel fromBundle(Bundle bundle) {
    String editedModelFileName = bundle.getString(INTENT_MODEL_FILENAME);
    // Bundle.getString is @Nullable, so we have to check.
    if (editedModelFileName == null) {
        Timber.d("fromBundle() - model file name under key %s", INTENT_MODEL_FILENAME);
        return null;
    }
    Timber.d("onCreate() loading saved model file %s", editedModelFileName);
    Model tempModelJSON;
    try {
        tempModelJSON = getTempModel((editedModelFileName));
    } catch (IOException e) {
        Timber.w(e, "Unable to load saved model file");
        return null;
    }
    TemporaryModel model = new TemporaryModel(tempModelJSON);
    model.loadTemplateChanges(bundle);
    return model;
}
Also used : Model(com.ichi2.libanki.Model) IOException(java.io.IOException) Nullable(androidx.annotation.Nullable)

Example 78 with Model

use of com.ichi2.libanki.Model in project Anki-Android by ankidroid.

the class Collection method deleteCardsWithInvalidModelOrdinals.

private ArrayList<String> deleteCardsWithInvalidModelOrdinals(Runnable notifyProgress, Model m) throws JSONException {
    Timber.d("deleteCardsWithInvalidModelOrdinals()");
    ArrayList<String> problems = new ArrayList<>(1);
    notifyProgress.run();
    if (m.isStd()) {
        JSONArray tmpls = m.getJSONArray("tmpls");
        ArrayList<Integer> ords = new ArrayList<>(tmpls.length());
        for (JSONObject tmpl : tmpls.jsonObjectIterable()) {
            ords.add(tmpl.getInt("ord"));
        }
        // cards with invalid ordinal
        ArrayList<Long> ids = mDb.queryLongList("SELECT id FROM cards WHERE ord NOT IN " + Utils.ids2str(ords) + " AND nid IN ( " + "SELECT id FROM notes WHERE mid = ?)", m.getLong("id"));
        if (!ids.isEmpty()) {
            problems.add("Deleted " + ids.size() + " card(s) with missing template.");
            remCards(ids);
        }
    }
    return problems;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray)

Example 79 with Model

use of com.ichi2.libanki.Model in project Anki-Android by ankidroid.

the class Collection method render_output_legacy.

@NonNull
@RustCleanup("Hack for Card Template Previewer, needs review")
public TemplateRenderOutput render_output_legacy(@NonNull Card c, boolean reload, boolean browser) {
    Note f = c.note(reload);
    Model m = c.model();
    JSONObject t = c.template();
    long did;
    if (c.isInDynamicDeck()) {
        did = c.getODid();
    } else {
        did = c.getDid();
    }
    HashMap<String, String> qa;
    if (browser) {
        String bqfmt = t.getString("bqfmt");
        String bafmt = t.getString("bafmt");
        qa = _renderQA(c.getId(), m, did, c.getOrd(), f.stringTags(), f.getFields(), c.internalGetFlags(), browser, bqfmt, bafmt);
    } else {
        qa = _renderQA(c.getId(), m, did, c.getOrd(), f.stringTags(), f.getFields(), c.internalGetFlags());
    }
    return new TemplateRenderOutput(qa.get("q"), qa.get("a"), null, null, c.model().getString("css"));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) TemplateRenderOutput(com.ichi2.libanki.TemplateManager.TemplateRenderContext.TemplateRenderOutput) RustCleanup(net.ankiweb.rsdroid.RustCleanup) NonNull(androidx.annotation.NonNull)

Example 80 with Model

use of com.ichi2.libanki.Model in project Anki-Android by ankidroid.

the class Collection method _tmplsFromOrds.

/**
 * @param model A note type
 * @param avail Ords of cards from this note type.
 * @return One template by element i of avail, for the i-th card. For standard template, avail should contains only existing ords.
 * for cloze, avail should contains only non-negative numbers, and the i-th card is a copy of the first card, with a different ord.
 */
private ArrayList<JSONObject> _tmplsFromOrds(Model model, ArrayList<Integer> avail) {
    JSONArray tmpls;
    if (model.isStd()) {
        tmpls = model.getJSONArray("tmpls");
        ArrayList<JSONObject> ok = new ArrayList<>(avail.size());
        for (Integer ord : avail) {
            ok.add(tmpls.getJSONObject(ord));
        }
        return ok;
    } else {
        // cloze - generate temporary templates from first
        JSONObject template0 = model.getJSONArray("tmpls").getJSONObject(0);
        ArrayList<JSONObject> ok = new ArrayList<>(avail.size());
        for (int ord : avail) {
            JSONObject t = template0.deepClone();
            t.put("ord", ord);
            ok.add(t);
        }
        return ok;
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)124 Model (com.ichi2.libanki.Model)95 Test (org.junit.Test)82 JSONArray (com.ichi2.utils.JSONArray)79 Collection (com.ichi2.libanki.Collection)53 ArrayList (java.util.ArrayList)48 Note (com.ichi2.libanki.Note)40 RobolectricTest (com.ichi2.anki.RobolectricTest)38 JSONException (com.ichi2.utils.JSONException)32 Intent (android.content.Intent)30 Card (com.ichi2.libanki.Card)27 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)26 HashMap (java.util.HashMap)22 Bundle (android.os.Bundle)20 NonNull (androidx.annotation.NonNull)20 SuppressLint (android.annotation.SuppressLint)16 View (android.view.View)16 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)15 IOException (java.io.IOException)15 Nullable (androidx.annotation.Nullable)14