Search in sources :

Example 91 with JSONObject

use of com.ichi2.utils.JSONObject in project Anki-Android by ankidroid.

the class NoteEditor method updateCards.

/**
 * Update the list of card templates for current note type
 */
private void updateCards(JSONObject model) {
    Timber.d("updateCards()");
    JSONArray tmpls = model.getJSONArray("tmpls");
    StringBuilder cardsList = new StringBuilder();
    // Build comma separated list of card names
    Timber.d("updateCards() template count is %s", tmpls.length());
    for (int i = 0; i < tmpls.length(); i++) {
        String name = tmpls.getJSONObject(i).optString("name");
        // If more than one card, and we have an existing card, underline existing card
        if (!mAddNote && tmpls.length() > 1 && model == mEditorNote.model() && mCurrentEditedCard != null && mCurrentEditedCard.template().optString("name").equals(name)) {
            name = "<u>" + name + "</u>";
        }
        cardsList.append(name);
        if (i < tmpls.length() - 1) {
            cardsList.append(", ");
        }
    }
    // Make cards list red if the number of cards is being reduced
    if (!mAddNote && tmpls.length() < mEditorNote.model().getJSONArray("tmpls").length()) {
        cardsList = new StringBuilder("<font color='red'>" + cardsList + "</font>");
    }
    mCardsButton.setText(HtmlCompat.fromHtml(getResources().getString(R.string.CardEditorCards, cardsList.toString()), HtmlCompat.FROM_HTML_MODE_LEGACY));
}
Also used : JSONArray(com.ichi2.utils.JSONArray) SuppressLint(android.annotation.SuppressLint)

Example 92 with JSONObject

use of com.ichi2.utils.JSONObject in project Anki-Android by ankidroid.

the class NoteEditor method hasUnsavedChanges.

private boolean hasUnsavedChanges() {
    if (!collectionHasLoaded()) {
        return false;
    }
    // changed note type?
    if (!mAddNote && mCurrentEditedCard != null) {
        final JSONObject newModel = getCurrentlySelectedModel();
        final JSONObject oldModel = mCurrentEditedCard.model();
        if (!newModel.equals(oldModel)) {
            return true;
        }
    }
    // changed deck?
    if (!mAddNote && mCurrentEditedCard != null && mCurrentEditedCard.getDid() != mCurrentDid) {
        return true;
    }
    // changed fields?
    if (mFieldEdited) {
        return true;
    }
    // changed tags?
    return mTagsEdited;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 93 with JSONObject

use of com.ichi2.utils.JSONObject 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 94 with JSONObject

use of com.ichi2.utils.JSONObject 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 95 with JSONObject

use of com.ichi2.utils.JSONObject 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)272 JSONArray (com.ichi2.utils.JSONArray)110 ArrayList (java.util.ArrayList)59 Test (org.junit.Test)55 Collection (com.ichi2.libanki.Collection)46 IOException (java.io.IOException)40 RobolectricTest (com.ichi2.anki.RobolectricTest)34 JSONException (com.ichi2.utils.JSONException)34 Note (com.ichi2.libanki.Note)33 Model (com.ichi2.libanki.Model)31 HashMap (java.util.HashMap)31 SuppressLint (android.annotation.SuppressLint)29 JSONObject (org.json.JSONObject)27 Response (okhttp3.Response)25 JSONException (org.json.JSONException)24 NonNull (androidx.annotation.NonNull)23 Card (com.ichi2.libanki.Card)21 File (java.io.File)21 Map (java.util.Map)20 Intent (android.content.Intent)19