Search in sources :

Example 26 with R

use of com.ichi2.anki.R in project Anki-Android by ankidroid.

the class Syncer method mergeDecks.

private void mergeDecks(JSONArray rchg) {
    JSONArray decks = rchg.getJSONArray(0);
    for (JSONObject deck : decks.jsonObjectIterable()) {
        Deck r = new Deck(deck);
        Deck l = mCol.getDecks().get(r.getLong("id"), false);
        // if missing locally or server is newer, update
        if (l == null || r.getLong("mod") > l.getLong("mod")) {
            mCol.getDecks().update(r);
        }
    }
    JSONArray confs = rchg.getJSONArray(1);
    for (JSONObject deckConfig : confs.jsonObjectIterable()) {
        DeckConfig r = new DeckConfig(deckConfig, DeckConfig.Source.DECK_CONFIG);
        DeckConfig l = mCol.getDecks().getConf(r.getLong("id"));
        // if missing locally or server is newer, update
        if (l == null || r.getLong("mod") > l.getLong("mod")) {
            mCol.getDecks().updateConf(r);
        }
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) Deck(com.ichi2.libanki.Deck) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 27 with R

use of com.ichi2.anki.R in project Anki-Android by ankidroid.

the class Syncer method mergeModels.

private void mergeModels(JSONArray rchg) throws UnexpectedSchemaChange {
    for (JSONObject model : rchg.jsonObjectIterable()) {
        Model r = new Model(model);
        Model l = mCol.getModels().get(r.getLong("id"));
        // if missing locally or server is newer, update
        if (l == null || r.getLong("mod") > l.getLong("mod")) {
            // syncing algorithm should handle this in a better way.
            if (l != null) {
                if (l.getJSONArray("flds").length() != r.getJSONArray("flds").length()) {
                    throw new UnexpectedSchemaChange();
                }
                if (l.getJSONArray("tmpls").length() != r.getJSONArray("tmpls").length()) {
                    throw new UnexpectedSchemaChange();
                }
            }
            mCol.getModels().update(r);
        }
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model)

Example 28 with R

use of com.ichi2.anki.R in project Anki-Android by ankidroid.

the class Syncer method newerRows.

private ArrayList<Object[]> newerRows(JSONArray data, String table, int modIdx) {
    long[] ids = new long[data.length()];
    for (int i = 0; i < data.length(); i++) {
        ids[i] = data.getJSONArray(i).getLong(0);
    }
    Pair<String, Object[]> limAndArg = usnLim();
    Map<Long, Long> lmods = HashUtil.HashMapInit(mCol.getDb().queryScalar("SELECT count() FROM " + table + " WHERE id IN " + Utils.ids2str(ids) + " AND " + limAndArg.first, limAndArg.second));
    try (Cursor cur = mCol.getDb().query("SELECT id, mod FROM " + table + " WHERE id IN " + Utils.ids2str(ids) + " AND " + limAndArg.first, limAndArg.second)) {
        while (cur.moveToNext()) {
            lmods.put(cur.getLong(0), cur.getLong(1));
        }
    }
    ArrayList<Object[]> update = new ArrayList<>(data.length());
    for (JSONArray r : data.jsonArrayIterable()) {
        if (!lmods.containsKey(r.getLong(0)) || lmods.get(r.getLong(0)) < r.getLong(modIdx)) {
            update.add(Utils.jsonArray2Objects(r));
        }
    }
    mCol.log(table, data);
    return update;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) Cursor(android.database.Cursor)

Example 29 with R

use of com.ichi2.anki.R in project Anki-Android by ankidroid.

the class Models method _updateRequired.

/**
 * Required field/text cache
 * ***********************************************************************************************
 */
private void _updateRequired(Model m) {
    if (m.isCloze()) {
        // nothing to do
        return;
    }
    JSONArray req = new JSONArray();
    List<String> flds = m.getFieldsNames();
    JSONArray templates = m.getJSONArray("tmpls");
    for (JSONObject t : templates.jsonObjectIterable()) {
        Object[] ret = _reqForTemplate(m, flds, t);
        JSONArray r = new JSONArray();
        r.put(t.getInt("ord"));
        r.put(ret[0]);
        r.put(ret[1]);
        req.put(r);
    }
    m.put("req", req);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) JSONObject(com.ichi2.utils.JSONObject)

Example 30 with R

use of com.ichi2.anki.R in project Anki-Android by ankidroid.

the class Storage method _upgrade.

private static void _upgrade(Collection col, int ver) {
    try {
        if (ver < 3) {
            // new deck properties
            for (Deck d : col.getDecks().all()) {
                d.put("dyn", DECK_STD);
                d.put("collapsed", false);
                col.getDecks().save(d);
            }
        }
        if (ver < 4) {
            col.modSchemaNoCheck();
            List<Model> models = col.getModels().all();
            ArrayList<Model> clozes = new ArrayList<>(models.size());
            for (Model m : models) {
                if (!m.getJSONArray("tmpls").getJSONObject(0).getString("qfmt").contains("{{cloze:")) {
                    m.put("type", Consts.MODEL_STD);
                } else {
                    clozes.add(m);
                }
            }
            for (Model m : clozes) {
                try {
                    _upgradeClozeModel(col, m);
                } catch (ConfirmModSchemaException e) {
                    // Will never be reached as we already set modSchemaNoCheck()
                    throw new RuntimeException(e);
                }
            }
            col.getDb().execute("UPDATE col SET ver = 4");
        }
        if (ver < 5) {
            col.getDb().execute("UPDATE cards SET odue = 0 WHERE queue = 2");
            col.getDb().execute("UPDATE col SET ver = 5");
        }
        if (ver < 6) {
            col.modSchemaNoCheck();
            for (Model m : col.getModels().all()) {
                m.put("css", new JSONObject(Models.DEFAULT_MODEL).getString("css"));
                JSONArray ar = m.getJSONArray("tmpls");
                for (JSONObject t : ar.jsonObjectIterable()) {
                    if (!t.has("css")) {
                        continue;
                    }
                    m.put("css", m.getString("css") + "\n" + t.getString("css").replace(".card ", ".card" + t.getInt("ord") + 1));
                    t.remove("css");
                }
                col.getModels().save(m);
            }
            col.getDb().execute("UPDATE col SET ver = 6");
        }
        if (ver < 7) {
            col.modSchemaNoCheck();
            col.getDb().execute("UPDATE cards SET odue = 0 WHERE (type = " + Consts.CARD_TYPE_LRN + " OR queue = 2) AND NOT odid");
            col.getDb().execute("UPDATE col SET ver = 7");
        }
        if (ver < 8) {
            col.modSchemaNoCheck();
            col.getDb().execute("UPDATE cards SET due = due / 1000 WHERE due > 4294967296");
            col.getDb().execute("UPDATE col SET ver = 8");
        }
        if (ver < 9) {
            col.getDb().execute("UPDATE col SET ver = 9");
        }
        if (ver < 10) {
            col.getDb().execute("UPDATE cards SET left = left + left * 1000 WHERE queue = " + Consts.QUEUE_TYPE_LRN);
            col.getDb().execute("UPDATE col SET ver = 10");
        }
        if (ver < 11) {
            col.modSchemaNoCheck();
            for (Deck d : col.getDecks().all()) {
                if (d.isDyn()) {
                    int order = d.getInt("order");
                    // failed order was removed
                    if (order >= 5) {
                        order -= 1;
                    }
                    JSONArray terms = new JSONArray(Arrays.asList(d.getString("search"), d.getInt("limit"), order));
                    d.put("terms", new JSONArray());
                    d.getJSONArray("terms").put(0, terms);
                    d.remove("search");
                    d.remove("limit");
                    d.remove("order");
                    d.put("resched", true);
                    d.put("return", true);
                } else {
                    if (!d.has("extendNew")) {
                        d.put("extendNew", 10);
                        d.put("extendRev", 50);
                    }
                }
                col.getDecks().save(d);
            }
            for (DeckConfig c : col.getDecks().allConf()) {
                JSONObject r = c.getJSONObject("rev");
                r.put("ivlFct", r.optDouble("ivlFct", 1));
                if (r.has("ivlfct")) {
                    r.remove("ivlfct");
                }
                r.put("maxIvl", 36500);
                col.getDecks().save(c);
            }
            for (Model m : col.getModels().all()) {
                JSONArray tmpls = m.getJSONArray("tmpls");
                for (JSONObject t : tmpls.jsonObjectIterable()) {
                    t.put("bqfmt", "");
                    t.put("bafmt", "");
                }
                col.getModels().save(m);
            }
            col.getDb().execute("update col set ver = 11");
        }
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) JSONException(com.ichi2.utils.JSONException)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)17 JSONArray (com.ichi2.utils.JSONArray)14 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)7 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)6 Model (com.ichi2.libanki.Model)6 EditText (android.widget.EditText)5 File (java.io.File)5 Random (java.util.Random)5 Nullable (androidx.annotation.Nullable)4 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)4 Connection (com.ichi2.async.Connection)4 IOException (java.io.IOException)4 Intent (android.content.Intent)3 SharedPreferences (android.content.SharedPreferences)3 Cursor (android.database.Cursor)3 Bundle (android.os.Bundle)3 Timber (timber.log.Timber)3 Context (android.content.Context)2 LayoutInflater (android.view.LayoutInflater)2