Search in sources :

Example 91 with JSONArray

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

the class Sched method _fillDyn.

private List<Long> _fillDyn(@NonNull Deck deck) {
    JSONArray terms = deck.getJSONArray("terms").getJSONArray(0);
    String search = terms.getString(0);
    int limit = terms.getInt(1);
    int order = terms.getInt(2);
    SortOrder orderlimit = new SortOrder.AfterSqlOrderBy(_dynOrder(order, limit));
    if (!TextUtils.isEmpty(search.trim())) {
        search = String.format(Locale.US, "(%s)", search);
    }
    search = String.format(Locale.US, "%s -is:suspended -is:buried -deck:filtered -is:learn", search);
    List<Long> ids = mCol.findCards(search, orderlimit);
    if (ids.isEmpty()) {
        return ids;
    }
    // move the cards over
    mCol.log(deck.getLong("id"), ids);
    _moveToDyn(deck.getLong("id"), ids);
    return ids;
}
Also used : JSONArray(com.ichi2.utils.JSONArray) SortOrder(com.ichi2.libanki.SortOrder)

Example 92 with JSONArray

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

the class Sched method _newConf.

/**
 * Tools ******************************************************************** ***************************
 */
@Override
@NonNull
protected JSONObject _newConf(@NonNull Card card) {
    DeckConfig conf = _cardConf(card);
    if (!card.isInDynamicDeck()) {
        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 93 with JSONArray

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

the class ReviewerTest method testMultipleCards.

@Test
public synchronized void testMultipleCards() throws ConfirmModSchemaException {
    addNoteWithThreeCards();
    Collection col = getCol();
    JSONObject nw = col.getDecks().confForDid(1).getJSONObject("new");
    MockTime time = getCollectionTime();
    nw.put("delays", new JSONArray(new int[] { 1, 10, 60, 120 }));
    waitForAsyncTasksToComplete();
    Reviewer reviewer = startReviewer();
    waitForAsyncTasksToComplete();
    assertCounts(reviewer, 3, 0, 0);
    // card 1 is shown
    answerCardOrdinalAsGood(reviewer, 1);
    // card get scheduler in [10, 12.5] minutes
    time.addM(3);
    // We wait 3 minutes to ensure card 2 is scheduled after card 1
    // card 2 is shown
    answerCardOrdinalAsGood(reviewer, 2);
    // Same as above
    time.addM(3);
    // card 3 is shown
    answerCardOrdinalAsGood(reviewer, 3);
    undo(reviewer);
    assertCurrentOrdIs(reviewer, 3);
    // card 3 is shown
    answerCardOrdinalAsGood(reviewer, 3);
    // Anki Desktop shows "1"
    assertCurrentOrdIsNot(reviewer, 3);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) MockTime(com.ichi2.testutils.MockTime) Test(org.junit.Test)

Example 94 with JSONArray

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

the class ReviewerTest method cloneTemplate.

private void cloneTemplate(ModelManager models, Model m) throws ConfirmModSchemaException {
    JSONArray tmpls = m.getJSONArray("tmpls");
    JSONObject defaultTemplate = tmpls.getJSONObject(0);
    JSONObject newTemplate = defaultTemplate.deepClone();
    newTemplate.put("ord", tmpls.length());
    String card_name = getTargetContext().getString(R.string.card_n_name, tmpls.length() + 1);
    newTemplate.put("name", card_name);
    models.addTemplate(m, newTemplate);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) Matchers.emptyString(org.hamcrest.Matchers.emptyString)

Example 95 with JSONArray

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

the class ModelTest method test_req.

@Test
public void test_req() {
    Collection col = getCol();
    ModelManager mm = col.getModels();
    Model basic = mm.byName("Basic");
    assertTrue(basic.has("req"));
    reqSize(basic);
    JSONArray r = basic.getJSONArray("req").getJSONArray(0);
    assertEquals(0, r.getInt(0));
    assertTrue(Arrays.asList(new String[] { REQ_ANY, REQ_ALL }).contains(r.getString(1)));
    assertEquals(1, r.getJSONArray(2).length());
    assertEquals(0, r.getJSONArray(2).getInt(0));
    Model opt = mm.byName("Basic (optional reversed card)");
    reqSize(opt);
    r = opt.getJSONArray("req").getJSONArray(0);
    assertTrue(Arrays.asList(new String[] { REQ_ANY, REQ_ALL }).contains(r.getString(1)));
    assertEquals(1, r.getJSONArray(2).length());
    assertEquals(0, r.getJSONArray(2).getInt(0));
    assertEquals(new JSONArray("[1,\"all\",[1,2]]"), opt.getJSONArray("req").getJSONArray(1));
    // testing any
    opt.getJSONArray("tmpls").getJSONObject(1).put("qfmt", "{{Back}}{{Add Reverse}}");
    mm.save(opt, true);
    assertEquals(new JSONArray("[1, \"any\", [1, 2]]"), opt.getJSONArray("req").getJSONArray(1));
    // testing null
    opt.getJSONArray("tmpls").getJSONObject(1).put("qfmt", "{{^Add Reverse}}{{/Add Reverse}}");
    mm.save(opt, true);
    assertEquals(new JSONArray("[1, \"none\", []]"), opt.getJSONArray("req").getJSONArray(1));
    opt = mm.byName("Basic (type in the answer)");
    reqSize(opt);
    r = opt.getJSONArray("req").getJSONArray(0);
    assertTrue(Arrays.asList(new String[] { REQ_ANY, REQ_ALL }).contains(r.getString(1)));
    if (col.getModels() instanceof ModelsV16) {
        assertEquals(new JSONArray("[0, 1]"), r.getJSONArray(2));
    } else {
        // TODO: Port anki@4e33775ed4346ef136ece6ef5efec5ba46057c6b
        assertEquals(new JSONArray("[0]"), r.getJSONArray(2));
    }
}
Also used : JSONArray(com.ichi2.utils.JSONArray) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

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