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);
}
}
}
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);
}
}
}
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;
}
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);
}
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);
}
}
Aggregations