Search in sources :

Example 41 with JSONObject

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

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

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

the class Models method moveTemplate.

public void moveTemplate(Model m, JSONObject template, int idx) {
    JSONArray tmpls = m.getJSONArray("tmpls");
    int oldidx = -1;
    ArrayList<JSONObject> l = new ArrayList<>();
    HashMap<Integer, Integer> oldidxs = new HashMap<>();
    for (int i = 0; i < tmpls.length(); ++i) {
        if (tmpls.getJSONObject(i).equals(template)) {
            oldidx = i;
            if (idx == oldidx) {
                return;
            }
        }
        JSONObject t = tmpls.getJSONObject(i);
        oldidxs.put(t.hashCode(), t.getInt("ord"));
        l.add(t);
    }
    l.remove(oldidx);
    l.add(idx, template);
    m.put("tmpls", new JSONArray(l));
    _updateTemplOrds(m);
    // generate change map - We use StringBuilder
    StringBuilder sb = new StringBuilder();
    tmpls = m.getJSONArray("tmpls");
    for (int i = 0; i < tmpls.length(); ++i) {
        JSONObject t = tmpls.getJSONObject(i);
        sb.append("when ord = ").append(oldidxs.get(t.hashCode())).append(" then ").append(t.getInt("ord"));
        if (i != tmpls.length() - 1) {
            sb.append(" ");
        }
    }
    // apply
    save(m);
    mCol.getDb().execute("update cards set ord = (case " + sb.toString() + " end),usn=?,mod=? where nid in (select id from notes where mid = ?)", mCol.usn(), mCol.getTime().intTime(), m.getLong("id"));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) JSONArray(com.ichi2.utils.JSONArray) ArrayList(java.util.ArrayList)

Example 44 with JSONObject

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

the class Models method getTemplateNames.

public HashMap<Long, HashMap<Integer, String>> getTemplateNames() {
    HashMap<Long, HashMap<Integer, String>> result = new HashMap<>();
    for (Model m : mModels.values()) {
        JSONArray templates;
        templates = m.getJSONArray("tmpls");
        HashMap<Integer, String> names = new HashMap<>();
        for (int i = 0; i < templates.length(); i++) {
            JSONObject t = templates.getJSONObject(i);
            names.put(t.getInt("ord"), t.getString("name"));
        }
        result.put(m.getLong("id"), names);
    }
    return result;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) JSONArray(com.ichi2.utils.JSONArray)

Example 45 with JSONObject

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

the class Models method newTemplate.

/**
 * Templates ***********************************************************************************************
 */
public static JSONObject newTemplate(String name) {
    JSONObject t;
    t = new JSONObject(defaultTemplate);
    t.put("name", name);
    return t;
}
Also used : 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