use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class Storage method _setColVars.
private static void _setColVars(DB db, @NonNull Time time) {
JSONObject g = new JSONObject(Decks.defaultDeck);
g.put("id", 1);
g.put("name", "Default");
g.put("conf", 1);
g.put("mod", time.intTime());
JSONObject gc = new JSONObject(Decks.defaultConf);
gc.put("id", 1);
JSONObject ag = new JSONObject();
ag.put("1", g);
JSONObject agc = new JSONObject();
agc.put("1", gc);
ContentValues values = new ContentValues();
values.put("conf", Collection.defaultConf);
values.put("decks", Utils.jsonToString(ag));
values.put("dconf", Utils.jsonToString(agc));
db.update("col", values);
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class Tags method load.
public void load(String json) {
JSONObject tags = new JSONObject(json);
Iterator<?> i = tags.keys();
while (i.hasNext()) {
String t = (String) i.next();
mTags.put(t, tags.getInt(t));
}
mChanged = false;
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class Tags method flush.
public void flush() {
if (mChanged) {
JSONObject tags = new JSONObject();
for (Map.Entry<String, Integer> t : mTags.entrySet()) {
tags.put(t.getKey(), t.getValue());
}
ContentValues val = new ContentValues();
val.put("tags", Utils.jsonToString(tags));
// TODO: the database update call here sets mod = true. Verify if this is intended.
mCol.getDb().update("col", val);
mChanged = false;
}
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class Collection method _renderQA.
public HashMap<String, String> _renderQA(long cid, Model model, long did, int ord, String tags, String[] flist, int flags, boolean browser, String qfmt, String afmt) {
// data is [cid, nid, mid, did, ord, tags, flds, cardFlags]
// unpack fields and create dict
Map<String, String> fields = new HashMap<>();
Map<String, Pair<Integer, JSONObject>> fmap = Models.fieldMap(model);
for (String name : fmap.keySet()) {
fields.put(name, flist[fmap.get(name).first]);
}
int cardNum = ord + 1;
fields.put("Tags", tags.trim());
fields.put("Type", model.getString("name"));
fields.put("Deck", mDecks.name(did));
String baseName = Decks.basename(fields.get("Deck"));
fields.put("Subdeck", baseName);
fields.put("CardFlag", _flagNameFromCardFlags(flags));
JSONObject template;
if (model.getInt("type") == Consts.MODEL_STD) {
template = model.getJSONArray("tmpls").getJSONObject(ord);
} else {
template = model.getJSONArray("tmpls").getJSONObject(0);
}
fields.put("Card", template.getString("name"));
fields.put(String.format(Locale.US, "c%d", cardNum), "1");
// render q & a
HashMap<String, String> d = new HashMap<>();
d.put("id", Long.toString(cid));
qfmt = TextUtils.isEmpty(qfmt) ? template.getString("qfmt") : qfmt;
afmt = TextUtils.isEmpty(afmt) ? template.getString("afmt") : afmt;
for (Pair<String, String> p : new Pair[] { new Pair<>("q", qfmt), new Pair<>("a", afmt) }) {
String type = p.first;
String format = p.second;
if ("q".equals(type)) {
format = fClozePatternQ.matcher(format).replaceAll(String.format(Locale.US, "{{$1cq-%d:", cardNum));
format = fClozeTagStart.matcher(format).replaceAll(String.format(Locale.US, "<%%cq:%d:", cardNum));
} else {
format = fClozePatternA.matcher(format).replaceAll(String.format(Locale.US, "{{$1ca-%d:", cardNum));
format = fClozeTagStart.matcher(format).replaceAll(String.format(Locale.US, "<%%ca:%d:", cardNum));
// the following line differs from libanki // TODO: why?
// fields.put("FrontSide", mMedia.stripAudio(d.get("q")));
fields.put("FrontSide", d.get("q"));
}
String html = new Template(format, fields).render();
html = ChessFilter.fenToChessboard(html, getContext());
if (!browser) {
// browser don't show image. So compiling LaTeX actually remove information.
html = LaTeX.mungeQA(html, this, model);
}
d.put(type, html);
// empty cloze?
if ("q".equals(type) && model.getInt("type") == Consts.MODEL_CLOZE) {
if (getModels()._availClozeOrds(model, flist, false).size() == 0) {
String link = String.format("<a href=%s#cloze>%s</a>", Consts.HELP_SITE, "help");
d.put("q", mContext.getString(R.string.empty_cloze_warning, link));
}
}
}
return d;
}
use of com.ichi2.utils.JSONObject in project AnkiChinaAndroid by ankichinateam.
the class Decks method load.
public void load(String decks, String dconf) {
mDecks = new HashMap<>();
mDconf = new HashMap<>();
JSONObject decksarray = new JSONObject(decks);
JSONArray ids = decksarray.names();
for (int i = 0; i < ids.length(); i++) {
String id = ids.getString(i);
try {
long longId = Long.parseLong(id);
Deck o = new Deck(decksarray.getJSONObject(id));
mDecks.put(longId, o);
} catch (NumberFormatException e) {
Timber.w("id is not valid");
}
}
mNameMap = NameMap.constructor(mDecks.values());
JSONObject confarray = new JSONObject(dconf);
ids = confarray.names();
for (int i = 0; ids != null && i < ids.length(); i++) {
String id = ids.getString(i);
try {
mDconf.put(Long.parseLong(id), new DeckConfig(confarray.getJSONObject(id)));
} catch (NumberFormatException e) {
Timber.w("id is not valid");
}
}
mChanged = false;
}
Aggregations