Search in sources :

Example 46 with JSONObject

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);
}
Also used : ContentValues(android.content.ContentValues) JSONObject(com.ichi2.utils.JSONObject)

Example 47 with JSONObject

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;
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 48 with JSONObject

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;
    }
}
Also used : ContentValues(android.content.ContentValues) JSONObject(com.ichi2.utils.JSONObject) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 49 with JSONObject

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;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) SuppressLint(android.annotation.SuppressLint) Pair(android.util.Pair) Template(com.ichi2.libanki.template.Template)

Example 50 with JSONObject

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;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

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