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);
}
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);
}
}
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"));
}
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;
}
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;
}
Aggregations