Search in sources :

Example 56 with Queue

use of com.ichi2.libanki.sched.Counts.Queue 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)

Example 57 with Queue

use of com.ichi2.libanki.sched.Counts.Queue in project Anki-Android by ankidroid.

the class CardContentProvider method addModelToCursor.

private void addModelToCursor(Long modelId, ModelManager models, MatrixCursor rv, String[] columns) {
    Model jsonObject = models.get(modelId);
    MatrixCursor.RowBuilder rb = rv.newRow();
    try {
        for (String column : columns) {
            switch(column) {
                case FlashCardsContract.Model._ID:
                    rb.add(modelId);
                    break;
                case FlashCardsContract.Model.NAME:
                    rb.add(jsonObject.getString("name"));
                    break;
                case FlashCardsContract.Model.FIELD_NAMES:
                    JSONArray flds = jsonObject.getJSONArray("flds");
                    String[] allFlds = new String[flds.length()];
                    for (int idx = 0; idx < flds.length(); idx++) {
                        allFlds[idx] = flds.getJSONObject(idx).optString("name", "");
                    }
                    rb.add(Utils.joinFields(allFlds));
                    break;
                case FlashCardsContract.Model.NUM_CARDS:
                    rb.add(jsonObject.getJSONArray("tmpls").length());
                    break;
                case FlashCardsContract.Model.CSS:
                    rb.add(jsonObject.getString("css"));
                    break;
                case FlashCardsContract.Model.DECK_ID:
                    // #6378 - Anki Desktop changed schema temporarily to allow null
                    rb.add(jsonObject.optLong("did", Consts.DEFAULT_DECK_ID));
                    break;
                case FlashCardsContract.Model.SORT_FIELD_INDEX:
                    rb.add(jsonObject.getLong("sortf"));
                    break;
                case FlashCardsContract.Model.TYPE:
                    rb.add(jsonObject.getLong("type"));
                    break;
                case FlashCardsContract.Model.LATEX_POST:
                    rb.add(jsonObject.getString("latexPost"));
                    break;
                case FlashCardsContract.Model.LATEX_PRE:
                    rb.add(jsonObject.getString("latexPre"));
                    break;
                case FlashCardsContract.Model.NOTE_COUNT:
                    rb.add(models.useCount(jsonObject));
                    break;
                default:
                    throw new UnsupportedOperationException("Queue \"" + column + "\" is unknown");
            }
        }
    } catch (JSONException e) {
        Timber.e(e, "Error parsing JSONArray");
        throw new IllegalArgumentException("Model " + modelId + " is malformed", e);
    }
}
Also used : Model(com.ichi2.libanki.Model) JSONArray(com.ichi2.utils.JSONArray) JSONException(com.ichi2.utils.JSONException) MatrixCursor(android.database.MatrixCursor)

Example 58 with Queue

use of com.ichi2.libanki.sched.Counts.Queue in project Anki-Android by ankidroid.

the class ReviewerTest method testLrnQueueAfterUndo.

@Test
public void testLrnQueueAfterUndo() {
    Collection col = getCol();
    JSONObject nw = col.getDecks().confForDid(1).getJSONObject("new");
    MockTime time = (MockTime) col.getTime();
    nw.put("delays", new JSONArray(new int[] { 1, 10, 60, 120 }));
    Card[] cards = new Card[4];
    cards[0] = addRevNoteUsingBasicModelDueToday("1", "bar").firstCard();
    cards[1] = addNoteUsingBasicModel("2", "bar").firstCard();
    cards[2] = addNoteUsingBasicModel("3", "bar").firstCard();
    waitForAsyncTasksToComplete();
    Reviewer reviewer = startReviewer();
    waitForAsyncTasksToComplete();
    equalFirstField(cards[0], reviewer.mCurrentCard);
    reviewer.answerCard(Consts.BUTTON_ONE);
    waitForAsyncTasksToComplete();
    equalFirstField(cards[1], reviewer.mCurrentCard);
    reviewer.answerCard(Consts.BUTTON_ONE);
    waitForAsyncTasksToComplete();
    undo(reviewer);
    waitForAsyncTasksToComplete();
    equalFirstField(cards[1], reviewer.mCurrentCard);
    reviewer.answerCard(getCol().getSched().getGoodNewButton());
    waitForAsyncTasksToComplete();
    equalFirstField(cards[2], reviewer.mCurrentCard);
    time.addM(2);
    reviewer.answerCard(getCol().getSched().getGoodNewButton());
    advanceRobolectricLooperWithSleep();
    // This failed in #6898 because this card was not in the queue
    equalFirstField(cards[0], reviewer.mCurrentCard);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) MockTime(com.ichi2.testutils.MockTime) Card(com.ichi2.libanki.Card) Test(org.junit.Test)

Aggregations

Card (com.ichi2.libanki.Card)31 Test (org.junit.Test)30 RobolectricTest (com.ichi2.anki.RobolectricTest)29 Collection (com.ichi2.libanki.Collection)27 Note (com.ichi2.libanki.Note)24 JSONObject (com.ichi2.utils.JSONObject)20 JSONArray (com.ichi2.utils.JSONArray)17 DeckConfig (com.ichi2.libanki.DeckConfig)15 Cursor (android.database.Cursor)10 ArrayList (java.util.ArrayList)10 Deck (com.ichi2.libanki.Deck)6 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)4 JSONException (com.ichi2.utils.JSONException)3 Nullable (androidx.annotation.Nullable)2 SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)2 Model (com.ichi2.libanki.Model)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 SuppressLint (android.annotation.SuppressLint)1