Search in sources :

Example 16 with DeckConfig

use of com.ichi2.libanki.DeckConfig in project AnkiChinaAndroid by ankichinateam.

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 17 with DeckConfig

use of com.ichi2.libanki.DeckConfig in project AnkiChinaAndroid by ankichinateam.

the class AnkiChinaSyncer method saveLatestData.

/**
 * 同步结束后将数据写入synclog
 */
@SuppressLint("DefaultLocale")
private void saveLatestData() {
    mCol = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
    CollectionHelper.getInstance().lockCollection();
    mCol.getDb().getDatabase().beginTransaction();
    try {
        mCol.getDb().execute("delete from synclog");
    } catch (Exception e) {
        // mCol=CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
        // mCol.getDb().execute("drop table synclog");
        // mCol.getDb().execute("create table if not exists synclog (" + "    id             integer not null,"
        // + "    type             integer not null," + "    mod             integer not null" + ")");
        e.printStackTrace();
    }
    List<Deck> newDecks = mCol.getDecks().all();
    StringBuilder values = new StringBuilder();
    for (Deck item : newDecks) {
        if (values.length() != 0) {
            values.append(",");
        }
        values.append("(").append(item.getLong("id")).append(",").append(SYNC_LOG_TYPE_DECKS).append(",").append(item.getLong("mod")).append(")");
    }
    // db.execute(String.format("insert into synclog values %s",sbDeck.toString()));
    List<Model> newModels = mCol.getModels().all();
    // StringBuilder sbModel=new StringBuilder();
    for (Model item : newModels) {
        if (values.length() != 0) {
            values.append(",");
        }
        values.append("(").append(item.getLong("id")).append(",").append(SYNC_LOG_TYPE_MODELS).append(",").append(item.getLong("mod")).append(")");
    }
    // db.execute(String.format("insert into synclog values %s",sbModel.toString()));
    List<DeckConfig> newDConf = mCol.getDecks().allConf();
    // StringBuilder sbDConf=new StringBuilder();
    for (DeckConfig item : newDConf) {
        if (values.length() != 0) {
            values.append(",");
        }
        values.append("(").append(item.getLong("id")).append(",").append(SYNC_LOG_TYPE_DCONF).append(",").append(item.getLong("mod")).append(")");
    }
    String sql = String.format("insert into synclog values %s", values.toString());
    Timber.i("sync to local synclog:%s", sql);
    mCol.getDb().execute(sql);
    // List<Card> newDConf = mCol.getDecks().allConf();
    // for(DeckConfig item:newDConf){
    // if(values.length()!=0)values.append(",");
    // values.append("(").append(item.getLong("id")).append(",").append(SYNC_LOG_TYPE_DCONF).append(",").append(item.getLong("mod")).append(")");
    // }
    mCol.getDb().execute(String.format("insert into synclog select cards.id,%d,cards.mod from cards", SYNC_LOG_TYPE_CARD));
    mCol.getDb().execute(String.format("insert into synclog select notes.id,%d,notes.mod from notes", SYNC_LOG_TYPE_NOTE));
    mCol.getDb().execute(String.format("insert into synclog select revlog.id,%d,revlog.id from revlog", SYNC_LOG_TYPE_REVLOG));
    mCol.getDb().getDatabase().setTransactionSuccessful();
    mCol.getDb().getDatabase().endTransaction();
    // mCol.fixIntegrity(new CollectionTask.ProgressCallback(null, AnkiDroidApp.getAppResources()));
    CollectionHelper.getInstance().unlockCollection();
// db.execute(String.format("insert into synclog select revlog.id,%d,revlog.mod from revlog", SYNC_LOG_TYPE_REVLOG));
}
Also used : Model(com.ichi2.libanki.Model) Deck(com.ichi2.libanki.Deck) FileNotFoundException(java.io.FileNotFoundException) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) IOException(java.io.IOException) DeckConfig(com.ichi2.libanki.DeckConfig) SuppressLint(android.annotation.SuppressLint)

Example 18 with DeckConfig

use of com.ichi2.libanki.DeckConfig in project AnkiChinaAndroid by ankichinateam.

the class AbstractSchedTest method regression_7066.

@Test
public void regression_7066() {
    Collection col = getCol();
    DeckConfig dconf = col.getDecks().getConf(1);
    dconf.getJSONObject("new").put("bury", true);
    AbstractSched sched = col.getSched();
    addNoteUsingBasicAndReversedModel("foo", "bar");
    addNoteUsingBasicModel("plop", "foo");
    col.reset();
    Card card = sched.getCard();
    sched.setCurrentCard(card);
    sched.preloadNextCard();
    sched.answerCard(card, Consts.BUTTON_THREE);
    card = sched.getCard();
    sched.setCurrentCard(card);
    AnkiAssert.assertDoesNotThrow(() -> sched.preloadNextCard());
}
Also used : Collection(com.ichi2.libanki.Collection) DeckConfig(com.ichi2.libanki.DeckConfig) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 19 with DeckConfig

use of com.ichi2.libanki.DeckConfig in project AnkiChinaAndroid by ankichinateam.

the class AbstractSchedTest method ensureUndoCorrectCounts.

@Test
public void ensureUndoCorrectCounts() {
    Collection col = getCol();
    AbstractSched sched = col.getSched();
    Deck deck = col.getDecks().get(1);
    DeckConfig dconf = col.getDecks().getConf(1);
    dconf.getJSONObject("new").put("perDay", 10);
    JSONArray newCount = deck.getJSONArray("newToday");
    for (int i = 0; i < 20; i++) {
        Note note = col.newNote();
        note.setField(0, "a");
        col.addNote(note);
    }
    col.reset();
    assertThat(col.cardCount(), is(20));
    assertThat(sched.newCount(), is(10));
    Card card = sched.getCard();
    assertThat(sched.newCount(), is(9));
    assertThat(sched.counts(card)[0], is(10));
    sched.answerCard(card, sched.getGoodNewButton());
    sched.getCard();
    nonTaskUndo(col);
    card.load();
    assertThat(sched.newCount(), is(9));
    assertThat(sched.counts(card)[0], is(10));
}
Also used : Note(com.ichi2.libanki.Note) JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) Deck(com.ichi2.libanki.Deck) DeckConfig(com.ichi2.libanki.DeckConfig) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 20 with DeckConfig

use of com.ichi2.libanki.DeckConfig in project AnkiChinaAndroid by ankichinateam.

the class SchedTest method test_reviewsV1.

@Test
public void test_reviewsV1() throws Exception {
    Collection col = getColV1();
    // add a note
    Note note = col.newNote();
    note.setItem("Front", "one");
    note.setItem("Back", "two");
    col.addNote(note);
    // set the card up as a review card, due 8 days ago
    Card c = note.cards().get(0);
    c.setType(CARD_TYPE_REV);
    c.setQueue(QUEUE_TYPE_REV);
    c.setDue(col.getSched().getToday() - 8);
    c.setFactor(STARTING_FACTOR);
    c.setReps(3);
    c.setLapses(1);
    c.setIvl(100);
    c.startTimer();
    c.flush();
    // save it for later use as well
    Card cardcopy = c.clone();
    // failing it should put it in the learn queue with the default options
    // //////////////////////////////////////////////////////////////////////////////////////////////////
    // different delay to new
    col.reset();
    DeckConfig conf = col.getSched()._cardConf(c);
    conf.getJSONObject("lapse").put("delays", new JSONArray(new double[] { 2, 20 }));
    col.getDecks().save(conf);
    col.getSched().answerCard(c, 1);
    assertEquals(QUEUE_TYPE_LRN, c.getQueue());
    // it should be due tomorrow, with an interval of 1
    assertEquals(col.getSched().getToday() + 1, c.getODue());
    assertEquals(1, c.getIvl());
    // but because it's in the learn queue, its current due time should be in
    // the future
    assertThat(c.getDue(), is(greaterThanOrEqualTo(col.getTime().intTime())));
    assertThat(c.getDue() - col.getTime().intTime(), is(greaterThan(118L)));
    // factor should have been decremented
    assertEquals(2300, c.getFactor());
    // check counters
    assertEquals(2, c.getLapses());
    assertEquals(4, c.getReps());
    // check ests.
    assertEquals(120, col.getSched().nextIvl(c, 1));
    assertEquals(20 * 60, col.getSched().nextIvl(c, 2));
    // try again with an ease of 2 instead
    // //////////////////////////////////////////////////////////////////////////////////////////////////
    c = cardcopy.clone();
    c.flush();
    col.getSched().answerCard(c, 2);
    assertEquals(QUEUE_TYPE_REV, c.getQueue());
    // the new interval should be (100 + 8/4) * 1.2 = 122
    assertTrue(checkRevIvl(col, c, 122));
    assertEquals(col.getSched().getToday() + c.getIvl(), c.getDue());
    // factor should have been decremented
    assertEquals(2350, c.getFactor());
    // check counters
    assertEquals(1, c.getLapses());
    assertEquals(4, c.getReps());
    // ease 3
    // //////////////////////////////////////////////////////////////////////////////////////////////////
    c = cardcopy.clone();
    c.flush();
    col.getSched().answerCard(c, 3);
    // the new interval should be (100 + 8/2) * 2.5 = 260
    assertTrue(checkRevIvl(col, c, 260));
    assertEquals(col.getSched().getToday() + c.getIvl(), c.getDue());
    // factor should have been left alone
    assertEquals(STARTING_FACTOR, c.getFactor());
    // ease 4
    // //////////////////////////////////////////////////////////////////////////////////////////////////
    c = cardcopy.clone();
    c.flush();
    col.getSched().answerCard(c, 4);
    // the new interval should be (100 + 8) * 2.5 * 1.3 = 351
    assertTrue(checkRevIvl(col, c, 351));
    assertEquals(col.getSched().getToday() + c.getIvl(), c.getDue());
    // factor should have been increased
    assertEquals(2650, c.getFactor());
}
Also used : Note(com.ichi2.libanki.Note) JSONArray(com.ichi2.utils.JSONArray) Collection(com.ichi2.libanki.Collection) DeckConfig(com.ichi2.libanki.DeckConfig) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

DeckConfig (com.ichi2.libanki.DeckConfig)85 Collection (com.ichi2.libanki.Collection)57 Card (com.ichi2.libanki.Card)50 Test (org.junit.Test)49 RobolectricTest (com.ichi2.anki.RobolectricTest)48 Note (com.ichi2.libanki.Note)47 JSONArray (com.ichi2.utils.JSONArray)42 JSONObject (com.ichi2.utils.JSONObject)27 Deck (com.ichi2.libanki.Deck)14 NonNull (androidx.annotation.NonNull)12 Cursor (android.database.Cursor)5 JSONException (com.ichi2.utils.JSONException)5 ArrayList (java.util.ArrayList)5 Model (com.ichi2.libanki.Model)4 HashMap (java.util.HashMap)4 SuppressLint (android.annotation.SuppressLint)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 ContentValues (android.content.ContentValues)2 Intent (android.content.Intent)2