Search in sources :

Example 21 with JSONArray

use of com.ichi2.utils.JSONArray 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 22 with JSONArray

use of com.ichi2.utils.JSONArray 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)

Example 23 with JSONArray

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

the class Models method remField.

public void remField(Model m, JSONObject field) throws ConfirmModSchemaException {
    mCol.modSchema();
    JSONArray flds = m.getJSONArray("flds");
    JSONArray flds2 = new JSONArray();
    int idx = -1;
    for (int i = 0; i < flds.length(); ++i) {
        if (field.equals(flds.getJSONObject(i))) {
            idx = i;
            continue;
        }
        flds2.put(flds.getJSONObject(i));
    }
    m.put("flds", flds2);
    int sortf = m.getInt("sortf");
    if (sortf >= m.getJSONArray("flds").length()) {
        m.put("sortf", sortf - 1);
    }
    _updateFieldOrds(m);
    _transformFields(m, new TransformFieldDelete(idx));
    if (idx == sortIdx(m)) {
        // need to rebuild
        mCol.updateFieldCache(Utils.toPrimitive(nids(m)));
    }
    renameField(m, field, null);
}
Also used : JSONArray(com.ichi2.utils.JSONArray)

Example 24 with JSONArray

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

the class Models method _updateFieldOrds.

public void _updateFieldOrds(JSONObject m) {
    JSONArray flds = m.getJSONArray("flds");
    for (int i = 0; i < flds.length(); i++) {
        JSONObject f = flds.getJSONObject(i);
        f.put("ord", i);
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Example 25 with JSONArray

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

the class Models method fieldNames.

public static ArrayList<String> fieldNames(Model m) {
    JSONArray flds = m.getJSONArray("flds");
    ArrayList<String> names = new ArrayList<>();
    for (int i = 0; i < flds.length(); i++) {
        names.add(flds.getJSONObject(i).getString("name"));
    }
    return names;
}
Also used : JSONArray(com.ichi2.utils.JSONArray) ArrayList(java.util.ArrayList)

Aggregations

JSONArray (com.ichi2.utils.JSONArray)188 JSONObject (com.ichi2.utils.JSONObject)97 Collection (com.ichi2.libanki.Collection)54 Test (org.junit.Test)44 ArrayList (java.util.ArrayList)42 Note (com.ichi2.libanki.Note)40 Card (com.ichi2.libanki.Card)39 DeckConfig (com.ichi2.libanki.DeckConfig)37 RobolectricTest (com.ichi2.anki.RobolectricTest)36 Deck (com.ichi2.libanki.Deck)19 Model (com.ichi2.libanki.Model)19 JSONException (com.ichi2.utils.JSONException)19 Cursor (android.database.Cursor)17 HashMap (java.util.HashMap)16 SuppressLint (android.annotation.SuppressLint)15 IOException (java.io.IOException)14 NonNull (androidx.annotation.NonNull)13 File (java.io.File)10 Response (okhttp3.Response)10 JSONArray (org.json.JSONArray)8