Search in sources :

Example 66 with DECK

use of com.ichi2.anki.CardBrowser.Column.DECK 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 67 with DECK

use of com.ichi2.anki.CardBrowser.Column.DECK 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 68 with DECK

use of com.ichi2.anki.CardBrowser.Column.DECK in project AnkiChinaAndroid by ankichinateam.

the class AnkiChinaSyncer method getChangedColJson.

private <T extends JSONObject> JSONObject getChangedColJson(int type, List<T> newModels) {
    List<T> changedModel = new ArrayList<>();
    boolean deleted;
    Cursor cur = null;
    Map<Long, Long> oldModel = new HashMap<>();
    JSONObject modelsJson = new JSONObject();
    try {
        cur = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance()).getDb().getDatabase().query("SELECT id,mod FROM synclog WHERE type = " + type, null);
        while (cur.moveToNext()) {
            oldModel.put(cur.getLong(0), cur.getLong(1));
        }
    } finally {
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
    }
    if (oldModel.isEmpty()) {
        changedModel.addAll(newModels);
    } else {
        StringBuilder deletedModelSb = new StringBuilder();
        for (Map.Entry<Long, Long> entry : oldModel.entrySet()) {
            // System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            deleted = true;
            for (T model : newModels) {
                if (!changedModel.contains(model) && (oldModel.get(model.getLong("id")) == null || oldModel.get(model.getLong("id")) < model.getLong("mod"))) {
                    // 新增或修改过的deck
                    changedModel.add(model);
                } else if (model.getLong("id") == entry.getKey()) {
                    // 未被删除的deck
                    deleted = false;
                }
            }
            if (deleted) {
                if (deletedModelSb.length() != 0) {
                    deletedModelSb.append(",");
                }
                deletedModelSb.append(entry.getKey());
            }
        }
        if (deletedModelSb.length() > 0) {
            modelsJson.put("delete", new JSONArray(deletedModelSb.toString().split(",")));
        // modelsJson.put("delete", strArray2jsonArray(deletedModelSb.toString().split(",")));
        }
    }
    JSONArray changedModelJson = new JSONArray();
    for (T item : changedModel) {
        changedModelJson.put(item);
    }
    modelsJson.put("replace", changedModelJson);
    return modelsJson;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) Cursor(android.database.Cursor) JSONObject(com.ichi2.utils.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

Example 69 with DECK

use of com.ichi2.anki.CardBrowser.Column.DECK in project AnkiChinaAndroid by ankichinateam.

the class CustomStudyDialogTest method learnAheadCardsRegressionTest.

@Test
public void learnAheadCardsRegressionTest() {
    // #6289 - Regression Test
    CustomStudyDialog args = CustomStudyDialog.newInstance(CustomStudyDialog.CUSTOM_STUDY_AHEAD, 1);
    FragmentScenario<CustomStudyDialogForTesting> scenario = getDialogScenario(args);
    scenario.moveToState(Lifecycle.State.STARTED);
    scenario.onFragment(f -> {
        MaterialDialog dialog = (MaterialDialog) f.getDialog();
        assertThat(dialog, notNullValue());
        dialog.getActionButton(DialogAction.POSITIVE).callOnClick();
    });
    Deck customStudy = getCol().getDecks().current();
    assertThat("Custom Study should be dynamic", customStudy.getInt("dyn") == 1);
    assertThat("could not find deck: Custom study session", customStudy, notNullValue());
    customStudy.remove("id");
    customStudy.remove("mod");
    customStudy.remove("name");
    String expected = "{\"newToday\":[0,0],\"revToday\":[0,0],\"lrnToday\":[0,0],\"timeToday\":[0,0],\"collapsed\":false,\"dyn\":1,\"desc\":\"\",\"usn\":-1,\"delays\":null,\"separate\":true,\"terms\":[[\"deck:\\\"Default\\\" prop:due<=1\",99999,6]],\"resched\":true,\"return\":true}";
    assertThat(customStudy.toString(), is(expected));
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Deck(com.ichi2.libanki.Deck) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 70 with DECK

use of com.ichi2.anki.CardBrowser.Column.DECK 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)

Aggregations

Deck (com.ichi2.libanki.Deck)100 Collection (com.ichi2.libanki.Collection)97 JSONObject (com.ichi2.utils.JSONObject)88 Test (org.junit.Test)80 JSONArray (com.ichi2.utils.JSONArray)55 Card (com.ichi2.libanki.Card)53 Note (com.ichi2.libanki.Note)50 ArrayList (java.util.ArrayList)47 RobolectricTest (com.ichi2.anki.RobolectricTest)44 DeckConfig (com.ichi2.libanki.DeckConfig)37 JSONException (com.ichi2.utils.JSONException)34 NonNull (androidx.annotation.NonNull)30 HashMap (java.util.HashMap)29 Model (com.ichi2.libanki.Model)23 Map (java.util.Map)22 Intent (android.content.Intent)21 Resources (android.content.res.Resources)18 TextView (android.widget.TextView)18 SharedPreferences (android.content.SharedPreferences)17 Cursor (android.database.Cursor)17