Search in sources :

Example 6 with Models

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

the class Collection method load.

/**
 * DB-related *************************************************************** ********************************
 */
public void load() {
    Cursor cursor = null;
    String deckConf = "";
    try {
        // Read in deck table columns
        cursor = mDb.query("SELECT crt, mod, scm, dty, usn, ls, " + "conf, dconf, tags,ver FROM col");
        if (!cursor.moveToFirst()) {
            return;
        }
        mCrt = cursor.getLong(0);
        mMod = cursor.getLong(1);
        mScm = cursor.getLong(2);
        // No longer used
        mDty = cursor.getInt(3) == 1;
        mUsn = cursor.getInt(4);
        mLs = cursor.getLong(5);
        mConf = new JSONObject(cursor.getString(6));
        deckConf = cursor.getString(7);
        mTagsJson = cursor.getString(8);
        mTags.load(mTagsJson);
        mVer = cursor.getInt(9);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    // getModels().load(loadColumn("models")); This code has been
    // moved to `CollectionHelper::loadLazyCollection` for
    // efficiency Models are loaded lazily on demand. The
    // application layer can asynchronously pre-fetch those parts;
    // otherwise they get loaded when required.
    Timber.i("load new collection");
    mDecks.load(loadColumn("decks"), deckConf);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Cursor(android.database.Cursor)

Example 7 with Models

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

the class Models method scmhash.

/**
 * Schema hash ***********************************************************************************************
 */
/**
 * Return a hash of the schema, to see if models are compatible.
 */
public String scmhash(Model m) {
    String s = "";
    JSONArray flds = m.getJSONArray("flds");
    for (int i = 0; i < flds.length(); ++i) {
        s += flds.getJSONObject(i).getString("name");
    }
    JSONArray tmpls = m.getJSONArray("tmpls");
    for (int i = 0; i < tmpls.length(); ++i) {
        JSONObject t = tmpls.getJSONObject(i);
        s += t.getString("name");
    }
    return Utils.checksum(s);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Example 8 with Models

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

the class Syncer method getModels.

/**
 * Models ********************************************************************
 */
private JSONArray getModels() {
    JSONArray result = new JSONArray();
    if (mCol.getServer()) {
        for (JSONObject m : mCol.getModels().all()) {
            if (m.getInt("usn") >= mMinUsn) {
                result.put(m);
            }
        }
    } else {
        for (JSONObject m : mCol.getModels().all()) {
            if (m.getInt("usn") == -1) {
                m.put("usn", mMaxUsn);
                result.put(m);
            }
        }
        mCol.getModels().save();
    }
    return result;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray)

Example 9 with Models

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

the class AnkiChinaSyncer method compareSyncTable.

private JSONObject compareSyncTable() {
    mCol = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
    CollectionHelper.getInstance().lockCollection();
    DB db = mCol.getDb();
    db.execute("create index if not exists ix_synclog_id on synclog (id);)");
    int num = db.queryScalar("SELECT id FROM synclog");
    if (num == 0) {
        // 表是空的,则直接同步所有内容到该表
        Timber.w("no record in synclog table!");
    }
    JSONObject root = new JSONObject();
    JSONObject colJson = new JSONObject();
    JSONObject colJsonReplace = new JSONObject();
    // JSONObject decksJson = new JSONObject();
    // JSONObject modelsJson = new JSONObject();
    // JSONObject dconfJson = new JSONObject();
    colJsonReplace.put("crt", mCol.getCrt());
    colJsonReplace.put("mod", mCol.getMod());
    colJsonReplace.put("scm", mCol.getScm());
    colJsonReplace.put("ver", mCol.getVer());
    colJsonReplace.put("dty", mCol.getDirty() ? 1 : 0);
    colJsonReplace.put("usn", mCol.getUsnForSync());
    colJsonReplace.put("ls", mCol.getLs());
    colJsonReplace.put("conf", mCol.getConf());
    colJsonReplace.put("tags", mCol.getTagsJson());
    colJson.put("replace", colJsonReplace);
    root.put("col", colJson);
    root.put("decks", getChangedColJson(SYNC_LOG_TYPE_DECKS, mCol.getDecks().all()));
    root.put("dconf", getChangedColJson(SYNC_LOG_TYPE_DCONF, mCol.getDecks().allConf()));
    root.put("models", getChangedColJson(SYNC_LOG_TYPE_MODELS, mCol.getModels().all()));
    updateDialogProgress(SYNCING_DATA, "整理数据中", 6);
    root.put("cards", getChangedCardsOrNote(SYNC_LOG_TYPE_CARD));
    root.put("notes", getChangedCardsOrNote(SYNC_LOG_TYPE_NOTE));
    root.put("revlog", getAddedRevLog());
    CollectionHelper.getInstance().unlockCollection();
    // root.put("revlog", getChangedCardsOrNote(SYNC_LOG_TYPE_REVLOG));
    return root;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) DB(com.ichi2.libanki.DB) SuppressLint(android.annotation.SuppressLint)

Example 10 with Models

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

the class ReviewerTest method baseDeckName.

@Test
public void baseDeckName() {
    Collection col = getCol();
    Models models = col.getModels();
    Decks decks = col.getDecks();
    Long didAb = decks.id("A::B");
    Model basic = models.byName(AnkiDroidApp.getAppResources().getString(R.string.basic_model_name));
    basic.put("did", didAb);
    addNoteUsingBasicModel("foo", "bar");
    Long didA = decks.id("A");
    decks.select(didA);
    Reviewer reviewer = startReviewer();
    waitForAsyncTasksToComplete();
    assertThat(reviewer.getSupportActionBar().getTitle(), is("B"));
}
Also used : Decks(com.ichi2.libanki.Decks) Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) Models(com.ichi2.libanki.Models) Test(org.junit.Test)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)48 Model (com.ichi2.libanki.Model)28 JSONArray (com.ichi2.utils.JSONArray)26 Test (org.junit.Test)26 Collection (com.ichi2.libanki.Collection)25 RobolectricTest (com.ichi2.anki.RobolectricTest)16 Note (com.ichi2.libanki.Note)16 JSONException (com.ichi2.utils.JSONException)13 ArrayList (java.util.ArrayList)13 Card (com.ichi2.libanki.Card)11 Models (com.ichi2.libanki.Models)11 SuppressLint (android.annotation.SuppressLint)10 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)7 ModelManager (com.ichi2.libanki.ModelManager)7 File (java.io.File)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Map (java.util.Map)6 ContentValues (android.content.ContentValues)5 NonNull (androidx.annotation.NonNull)5