Search in sources :

Example 36 with JSONObject

use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.

the class SchedV2 method quickDeckDueTree.

/**
 * Similar to deck due tree, but ignore the number of cards.
 *
 *     It may takes a lot of time to compute the number of card, it
 *     requires multiple database access by deck.  Ignoring this number
 *     lead to the creation of a tree more quickly.
 */
@Override
@NonNull
public List<DeckTreeNode> quickDeckDueTree() {
    // Similar to deckDueTree, ignoring the numbers
    // Similar to deckDueList
    ArrayList<DeckTreeNode> data = new ArrayList<>();
    for (JSONObject deck : mCol.getDecks().allSorted()) {
        DeckTreeNode g = new DeckTreeNode(mCol, deck.getString("name"), deck.getLong("id"));
        data.add(g);
    }
    return _groupChildren(data, false);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) ArrayList(java.util.ArrayList) NonNull(androidx.annotation.NonNull)

Example 37 with JSONObject

use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.

the class Utils method markAsUploaded.

/**
 *       Set usn to 0 in every object.
 *
 *       This method is called during full sync, before uploading, so
 *       during an instant, the value will be zero while the object is
 *       not actually online. This is not a problem because if the sync
 *       fails, a full sync will occur again next time.
 *
 *       @return whether there was a non-zero usn; in this case the list
 *       should be saved before the upload.
 */
public static boolean markAsUploaded(ArrayList<? extends JSONObject> ar) {
    boolean changed = false;
    for (JSONObject obj : ar) {
        if (obj.optInt("usn", 1) != 0) {
            obj.put("usn", 0);
            changed = true;
        }
    }
    return changed;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 38 with JSONObject

use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.

the class Models method scmhash.

/**
 * Schema hash ***********************************************************************************************
 */
/**
 * Return a hash of the schema, to see if models are compatible.
 */
public String scmhash(Model m) {
    String s = "";
    JSONArray flds = m.getJSONArray("flds");
    for (int i = 0; i < flds.length(); ++i) {
        s += flds.getJSONObject(i).getString("name");
    }
    JSONArray tmpls = m.getJSONArray("tmpls");
    for (int i = 0; i < tmpls.length(); ++i) {
        JSONObject t = tmpls.getJSONObject(i);
        s += t.getString("name");
    }
    return Utils.checksum(s);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Example 39 with JSONObject

use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.

the class Models method renameField.

public void renameField(Model m, JSONObject field, String newName) throws ConfirmModSchemaException {
    mCol.modSchema();
    String pat = String.format("\\{\\{([^{}]*)([:#^/]|[^:#/^}][^:}]*?:|)%s\\}\\}", Pattern.quote(field.getString("name")));
    if (newName == null) {
        newName = "";
    }
    String repl = "{{$1$2" + newName + "}}";
    JSONArray tmpls = m.getJSONArray("tmpls");
    for (int i = 0; i < tmpls.length(); ++i) {
        JSONObject t = tmpls.getJSONObject(i);
        for (String fmt : new String[] { "qfmt", "afmt" }) {
            if (!"".equals(newName)) {
                t.put(fmt, t.getString(fmt).replaceAll(pat, repl));
            } else {
                t.put(fmt, t.getString(fmt).replaceAll(pat, ""));
            }
        }
    }
    field.put("name", newName);
    save(m);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Example 40 with JSONObject

use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.

the class Models method _reqForTemplate.

// 'String f' is unused upstream as well
@SuppressWarnings("PMD.UnusedLocalVariable")
private Object[] _reqForTemplate(Model m, ArrayList<String> flds, JSONObject t) {
    int nbFields = flds.size();
    String[] a = new String[nbFields];
    String[] b = new String[nbFields];
    Arrays.fill(a, "ankiflag");
    Arrays.fill(b, "");
    int ord = t.getInt("ord");
    String full = mCol._renderQA(1L, m, 1L, ord, "", a, 0).get("q");
    String empty = mCol._renderQA(1L, m, 1L, ord, "", b, 0).get("q");
    // if full and empty are the same, the template is invalid and there is no way to satisfy it
    if (full.equals(empty)) {
        return new Object[] { "none", new JSONArray(), new JSONArray() };
    }
    String type = "all";
    JSONArray req = new JSONArray();
    for (int i = 0; i < flds.size(); i++) {
        a[i] = "";
        // if no field content appeared, field is required
        if (!mCol._renderQA(1L, m, 1L, ord, "", a, 0).get("q").contains("ankiflag")) {
            req.put(i);
        }
        a[i] = "ankiflag";
    }
    if (req.length() > 0) {
        return new Object[] { type, req };
    }
    // if there are no required fields, switch to any mode
    type = "any";
    req = new JSONArray();
    for (int i = 0; i < flds.size(); i++) {
        b[i] = "1";
        // if not the same as empty, this field can make the card non-blank
        if (!mCol._renderQA(1L, m, 1L, ord, "", b, 0).get("q").equals(empty)) {
            req.put(i);
        }
        b[i] = "";
    }
    return new Object[] { type, req };
}
Also used : JSONArray(com.ichi2.utils.JSONArray) JSONObject(com.ichi2.utils.JSONObject)

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