Search in sources :

Example 16 with JSONArray

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

the class Collection method basicCheck.

/**
 * DB maintenance *********************************************************** ************************************
 */
/*
     * Basic integrity check for syncing. True if ok.
     */
public boolean basicCheck() {
    // cards without notes
    if (mDb.queryScalar("select 1 from cards where nid not in (select id from notes) limit 1") > 0) {
        return false;
    }
    boolean badNotes = mDb.queryScalar("select 1 from notes where id not in (select distinct nid from cards) " + "or mid not in " + Utils.ids2str(getModels().ids()) + " limit 1") > 0;
    // notes without cards or models
    if (badNotes) {
        return false;
    }
    // invalid ords
    for (JSONObject m : getModels().all()) {
        // ignore clozes
        if (m.getInt("type") != Consts.MODEL_STD) {
            continue;
        }
        // Make a list of valid ords for this model
        JSONArray tmpls = m.getJSONArray("tmpls");
        boolean badOrd = mDb.queryScalar("select 1 from cards where (ord < 0 or ord >= ?) and nid in ( " + "select id from notes where mid = ?) limit 1", tmpls.length(), m.getLong("id")) > 0;
        if (badOrd) {
            return false;
        }
    }
    return true;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Example 17 with JSONArray

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

the class Sched method _lapseConf.

@Override
@NonNull
protected JSONObject _lapseConf(@NonNull Card card) {
    DeckConfig conf = _cardConf(card);
    // normal deck
    if (card.getODid() == 0) {
        return conf.getJSONObject("lapse");
    }
    // dynamic deck; override some attributes, use original deck for others
    DeckConfig oconf = mCol.getDecks().confForDid(card.getODid());
    JSONArray delays = conf.optJSONArray("delays");
    if (delays == null) {
        delays = oconf.getJSONObject("lapse").getJSONArray("delays");
    }
    JSONObject dict = new JSONObject();
    // original deck
    dict.put("minInt", oconf.getJSONObject("lapse").getInt("minInt"));
    dict.put("leechFails", oconf.getJSONObject("lapse").getInt("leechFails"));
    dict.put("leechAction", oconf.getJSONObject("lapse").getInt("leechAction"));
    dict.put("mult", oconf.getJSONObject("lapse").getDouble("mult"));
    // overrides
    dict.put("delays", delays);
    dict.put("resched", conf.getBoolean("resched"));
    return dict;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) DeckConfig(com.ichi2.libanki.DeckConfig) NonNull(androidx.annotation.NonNull)

Example 18 with JSONArray

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

the class Sched method _newConf.

/**
 * Tools ******************************************************************** ***************************
 */
@Override
@NonNull
protected JSONObject _newConf(@NonNull Card card) {
    DeckConfig conf = _cardConf(card);
    // normal deck
    if (card.getODid() == 0) {
        return conf.getJSONObject("new");
    }
    // dynamic deck; override some attributes, use original deck for others
    DeckConfig oconf = mCol.getDecks().confForDid(card.getODid());
    JSONArray delays = conf.optJSONArray("delays");
    if (delays == null) {
        delays = oconf.getJSONObject("new").getJSONArray("delays");
    }
    JSONObject dict = new JSONObject();
    // original deck
    dict.put("ints", oconf.getJSONObject("new").getJSONArray("ints"));
    dict.put("initialFactor", oconf.getJSONObject("new").getInt("initialFactor"));
    dict.put("bury", oconf.getJSONObject("new").optBoolean("bury", true));
    // overrides
    dict.put("delays", delays);
    dict.put("separate", conf.getBoolean("separate"));
    dict.put("order", Consts.NEW_CARDS_DUE);
    dict.put("perDay", mReportLimit);
    return dict;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) DeckConfig(com.ichi2.libanki.DeckConfig) NonNull(androidx.annotation.NonNull)

Example 19 with JSONArray

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

the class NoteImporter method initMapping.

public void initMapping() {
    List<String> flds = new ArrayList<>();
    JSONArray array = mModel.getJSONArray("flds");
    for (int i = 0; i < array.length(); i++) {
        flds.add(array.getJSONObject(i).getString("name"));
    }
    // truncate to provided count
    flds = flds.subList(0, Math.min(flds.size(), fields()));
    // if there's room left, add tags
    if (fields() > flds.size()) {
        flds.add("_tags");
    }
    // and if there's still room left, pad
    int iterations = fields() - flds.size();
    for (int i = 0; i < iterations; i++) {
        flds.add(null);
    }
    mMapping = flds;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray)

Example 20 with JSONArray

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

the class Models method scmhash.

/**
 * Schema hash ***********************************************************************************************
 */
/**
 * Return a hash of the schema, to see if models are compatible.
 */
public String scmhash(Model m) {
    String s = "";
    JSONArray flds = m.getJSONArray("flds");
    for (int i = 0; i < flds.length(); ++i) {
        s += flds.getJSONObject(i).getString("name");
    }
    JSONArray tmpls = m.getJSONArray("tmpls");
    for (int i = 0; i < tmpls.length(); ++i) {
        JSONObject t = tmpls.getJSONObject(i);
        s += t.getString("name");
    }
    return Utils.checksum(s);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

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