Search in sources :

Example 86 with JSONArray

use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.

the class Syncer method getTags.

/**
 * Tags ********************************************************************
 */
private JSONArray getTags() {
    JSONArray result = new JSONArray();
    if (mCol.getServer()) {
        for (TagUsnTuple t : mCol.getTags().allItems()) {
            if (t.getUsn() >= mMinUsn) {
                result.put(t.getTag());
            }
        }
    } else {
        for (TagUsnTuple t : mCol.getTags().allItems()) {
            if (t.getUsn() == -1) {
                String tag = t.getTag();
                mCol.getTags().add(t.getTag(), mMaxUsn);
                result.put(tag);
            }
        }
        mCol.getTags().save();
    }
    return result;
}
Also used : TagUsnTuple(com.ichi2.libanki.backend.model.TagUsnTuple) JSONArray(com.ichi2.utils.JSONArray)

Example 87 with JSONArray

use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.

the class Syncer method getDecks.

/**
 * Decks ********************************************************************
 */
private JSONArray getDecks() {
    JSONArray result = new JSONArray();
    if (mCol.getServer()) {
        JSONArray decks = new JSONArray();
        for (Deck g : mCol.getDecks().all()) {
            if (g.getInt("usn") >= mMinUsn) {
                decks.put(g);
            }
        }
        JSONArray dconfs = new JSONArray();
        for (DeckConfig g : mCol.getDecks().allConf()) {
            if (g.getInt("usn") >= mMinUsn) {
                dconfs.put(g);
            }
        }
        result.put(decks);
        result.put(dconfs);
    } else {
        JSONArray decks = new JSONArray();
        for (Deck g : mCol.getDecks().all()) {
            if (g.getInt("usn") == -1) {
                g.put("usn", mMaxUsn);
                decks.put(g);
            }
        }
        JSONArray dconfs = new JSONArray();
        for (DeckConfig g : mCol.getDecks().allConf()) {
            if (g.getInt("usn") == -1) {
                g.put("usn", mMaxUsn);
                dconfs.put(g);
            }
        }
        mCol.getDecks().save();
        result.put(decks);
        result.put(dconfs);
    }
    return result;
}
Also used : JSONArray(com.ichi2.utils.JSONArray) Deck(com.ichi2.libanki.Deck) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 88 with JSONArray

use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.

the class Syncer method sanityCheck.

public JSONObject sanityCheck() {
    JSONObject result = new JSONObject();
    try {
        if (mCol.getDb().queryScalar("SELECT count() FROM cards WHERE nid NOT IN (SELECT id FROM notes)") != 0) {
            Timber.e("Sync - SanityCheck: there are cards without mother notes");
            result.put("client", "missing notes");
            return result;
        }
        if (mCol.getDb().queryScalar("SELECT count() FROM notes WHERE id NOT IN (SELECT DISTINCT nid FROM cards)") != 0) {
            Timber.e("Sync - SanityCheck: there are notes without cards");
            result.put("client", "missing cards");
            return result;
        }
        if (mCol.getDb().queryScalar("SELECT count() FROM cards WHERE usn = -1") != 0) {
            Timber.e("Sync - SanityCheck: there are unsynced cards");
            result.put("client", "cards had usn = -1");
            return result;
        }
        if (mCol.getDb().queryScalar("SELECT count() FROM notes WHERE usn = -1") != 0) {
            Timber.e("Sync - SanityCheck: there are unsynced notes");
            result.put("client", "notes had usn = -1");
            return result;
        }
        if (mCol.getDb().queryScalar("SELECT count() FROM revlog WHERE usn = -1") != 0) {
            Timber.e("Sync - SanityCheck: there are unsynced revlogs");
            result.put("client", "revlog had usn = -1");
            return result;
        }
        if (mCol.getDb().queryScalar("SELECT count() FROM graves WHERE usn = -1") != 0) {
            Timber.e("Sync - SanityCheck: there are unsynced graves");
            result.put("client", "graves had usn = -1");
            return result;
        }
        for (Deck g : mCol.getDecks().all()) {
            if (g.getInt("usn") == -1) {
                Timber.e("Sync - SanityCheck: unsynced deck: %s", g.getString("name"));
                result.put("client", "deck had usn = -1");
                return result;
            }
        }
        if (mCol.getTags().minusOneValue()) {
            Timber.e("Sync - SanityCheck: there are unsynced tags");
            result.put("client", "tag had usn = -1");
            return result;
        }
        boolean found = false;
        for (JSONObject m : mCol.getModels().all()) {
            if (mCol.getServer()) {
                // the web upgrade was mistakenly setting usn
                if (m.getInt("usn") < 0) {
                    m.put("usn", 0);
                    found = true;
                }
            } else {
                if (m.getInt("usn") == -1) {
                    Timber.e("Sync - SanityCheck: unsynced model: %s", m.getString("name"));
                    result.put("client", "model had usn = -1");
                    return result;
                }
            }
        }
        if (found) {
            mCol.getModels().save();
        }
        // check for missing parent decks
        mCol.getSched().deckDueList();
        // return summary of deck
        JSONArray check = new JSONArray();
        JSONArray counts = new JSONArray();
        // #5666 - not in libAnki
        // We modified mReportLimit inside the scheduler, and this causes issues syncing dynamic decks.
        AbstractSched syncScheduler = mCol.createScheduler(SYNC_SCHEDULER_REPORT_LIMIT);
        syncScheduler.resetCounts();
        Counts counts_ = syncScheduler.counts();
        counts.put(counts_.getNew());
        counts.put(counts_.getLrn());
        counts.put(counts_.getRev());
        check.put(counts);
        check.put(mCol.getDb().queryScalar("SELECT count() FROM cards"));
        check.put(mCol.getDb().queryScalar("SELECT count() FROM notes"));
        check.put(mCol.getDb().queryScalar("SELECT count() FROM revlog"));
        check.put(mCol.getDb().queryScalar("SELECT count() FROM graves"));
        check.put(mCol.getModels().all().size());
        check.put(mCol.getDecks().all().size());
        check.put(mCol.getDecks().allConf().size());
        result.put("client", check);
        return result;
    } catch (JSONException e) {
        Timber.e(e, "Syncer.sanityCheck()");
        throw new RuntimeException(e);
    }
}
Also used : Counts(com.ichi2.libanki.sched.Counts) JSONObject(com.ichi2.utils.JSONObject) AbstractSched(com.ichi2.libanki.sched.AbstractSched) JSONArray(com.ichi2.utils.JSONArray) Deck(com.ichi2.libanki.Deck) JSONException(com.ichi2.utils.JSONException)

Example 89 with JSONArray

use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.

the class SchedV2 method _graduatingIvl.

private int _graduatingIvl(@NonNull Card card, @NonNull JSONObject conf, boolean early, boolean fuzz) {
    if (card.getType() == Consts.CARD_TYPE_REV || card.getType() == Consts.CARD_TYPE_RELEARNING) {
        int bonus = early ? 1 : 0;
        return card.getIvl() + bonus;
    }
    int ideal;
    JSONArray ints = conf.getJSONArray("ints");
    if (!early) {
        // graduate
        ideal = ints.getInt(0);
    } else {
        // early remove
        ideal = ints.getInt(1);
    }
    if (fuzz) {
        ideal = _fuzzedIvl(ideal);
    }
    return ideal;
}
Also used : JSONArray(com.ichi2.utils.JSONArray)

Example 90 with JSONArray

use of com.ichi2.utils.JSONArray in project Anki-Android by ankidroid.

the class SchedV2 method _delayForGrade.

protected int _delayForGrade(JSONObject conf, int left) {
    left = left % 1000;
    try {
        double delay;
        JSONArray delays = conf.getJSONArray("delays");
        int len = delays.length();
        try {
            delay = delays.getDouble(len - left);
        } catch (JSONException e) {
            Timber.w(e);
            if (conf.getJSONArray("delays").length() > 0) {
                delay = conf.getJSONArray("delays").getDouble(0);
            } else {
                // user deleted final step; use dummy value
                delay = 1.0;
            }
        }
        return (int) (delay * 60.0);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONArray(com.ichi2.utils.JSONArray) JSONException(com.ichi2.utils.JSONException)

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