Search in sources :

Example 56 with Template

use of com.ichi2.libanki.template.Template in project AnkiChinaAndroid by ankichinateam.

the class ImportTest method testAnki2DiffmodelTemplates.

@Test
public void testAnki2DiffmodelTemplates() throws IOException, JSONException, ImportExportException {
    // different from the above as this one tests only the template text being
    // changed, not the number of cards/fields
    // import the first version of the model
    String tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "diffmodeltemplates-1.apkg");
    AnkiPackageImporter imp = new AnkiPackageImporter(testCol, tmp);
    imp.setDupeOnSchemaChange(true);
    imp.run();
    // then the version with updated template
    tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "diffmodeltemplates-2.apkg");
    imp = new AnkiPackageImporter(testCol, tmp);
    imp.setDupeOnSchemaChange(true);
    imp.run();
    // collection should contain the note we imported
    assertEquals(1, testCol.noteCount());
    // the front template should contain the text added in the 2nd package
    Long tcid = testCol.findCards("").get(0);
    Note tnote = testCol.getCard(tcid).note();
    assertTrue(testCol.findTemplates(tnote).get(0).getString("qfmt").contains("Changed Front Template"));
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) Note(com.ichi2.libanki.Note) Test(org.junit.Test)

Example 57 with Template

use of com.ichi2.libanki.template.Template in project AnkiChinaAndroid by ankichinateam.

the class Collection method deleteCardsWithInvalidModelOrdinals.

private ArrayList<String> deleteCardsWithInvalidModelOrdinals(Runnable notifyProgress, JSONObject m) throws JSONException {
    Timber.d("deleteCardsWithInvalidModelOrdinals()");
    ArrayList<String> problems = new ArrayList<>();
    notifyProgress.run();
    if (m.getInt("type") == Consts.MODEL_STD) {
        ArrayList<Integer> ords = new ArrayList<>();
        JSONArray tmpls = m.getJSONArray("tmpls");
        for (int t = 0; t < tmpls.length(); t++) {
            ords.add(tmpls.getJSONObject(t).getInt("ord"));
        }
        // cards with invalid ordinal
        ArrayList<Long> ids = mDb.queryLongList("SELECT id FROM cards WHERE ord NOT IN " + Utils.ids2str(ords) + " AND nid IN ( " + "SELECT id FROM notes WHERE mid = ?)", m.getLong("id"));
        if (ids.size() > 0) {
            problems.add("Deleted " + ids.size() + " card(s) with missing template.");
            remCards(ids);
        }
    }
    return problems;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray) SuppressLint(android.annotation.SuppressLint)

Example 58 with Template

use of com.ichi2.libanki.template.Template in project AnkiChinaAndroid by ankichinateam.

the class Collection method previewCards.

public List<Card> previewCards(Note note, @Consts.CARD_TYPE int type, int did) {
    ArrayList<JSONObject> cms = null;
    if (type == Consts.CARD_TYPE_NEW) {
        cms = findTemplates(note);
    } else if (type == Consts.CARD_TYPE_LRN) {
        cms = new ArrayList<>();
        for (Card c : note.cards()) {
            cms.add(c.template());
        }
    } else {
        cms = new ArrayList<>();
        JSONArray tmpls = note.model().getJSONArray("tmpls");
        for (int i = 0; i < tmpls.length(); ++i) {
            cms.add(tmpls.getJSONObject(i));
        }
    }
    if (cms.isEmpty()) {
        return new ArrayList<>();
    }
    List<Card> cards = new ArrayList<>();
    for (JSONObject template : cms) {
        cards.add(_newCard(note, template, 1, did, false));
    }
    return cards;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray)

Example 59 with Template

use of com.ichi2.libanki.template.Template in project AnkiChinaAndroid by ankichinateam.

the class Models method remTemplate.

/**
 * Removing a template
 *
 * @return False if removing template would leave orphan notes.
 * @throws ConfirmModSchemaException
 */
public boolean remTemplate(Model m, JSONObject template) throws ConfirmModSchemaException {
    if (m.getJSONArray("tmpls").length() <= 1) {
        return false;
    }
    // find cards using this template
    JSONArray tmpls = m.getJSONArray("tmpls");
    int ord = -1;
    for (int i = 0; i < tmpls.length(); ++i) {
        if (tmpls.getJSONObject(i).equals(template)) {
            ord = i;
            break;
        }
    }
    if (ord == -1) {
        throw new IllegalArgumentException("Invalid template proposed for delete");
    }
    // the code in "isRemTemplateSafe" was in place here in libanki. It is extracted to a method for reuse
    List<Long> cids = getCardIdsForModel(m.getLong("id"), new int[] { ord });
    if (cids == null) {
        Timber.d("remTemplate getCardIdsForModel determined it was unsafe to delete the template");
        return false;
    }
    // ok to proceed; remove cards
    Timber.d("remTemplate proceeding to delete the template and %d cards", cids.size());
    mCol.modSchema();
    mCol.remCards(cids);
    // shift ordinals
    mCol.getDb().execute("update cards set ord = ord - 1, usn = ?, mod = ? where nid in (select id from notes where mid = ?) and ord > ?", mCol.usn(), mCol.getTime().intTime(), m.getLong("id"), ord);
    tmpls = m.getJSONArray("tmpls");
    JSONArray tmpls2 = new JSONArray();
    for (int i = 0; i < tmpls.length(); ++i) {
        if (template.equals(tmpls.getJSONObject(i))) {
            continue;
        }
        tmpls2.put(tmpls.getJSONObject(i));
    }
    m.put("tmpls", tmpls2);
    _updateTemplOrds(m);
    save(m);
    Timber.d("remTemplate done working");
    return true;
}
Also used : JSONArray(com.ichi2.utils.JSONArray)

Example 60 with Template

use of com.ichi2.libanki.template.Template in project AnkiChinaAndroid by ankichinateam.

the class Models method availOrds.

/**
 * Given a joined field string, return available template ordinals
 */
public ArrayList<Integer> availOrds(Model m, String[] sfld) {
    if (m.getInt("type") == Consts.MODEL_CLOZE) {
        return _availClozeOrds(m, sfld);
    }
    int nbField = sfld.length;
    String[] fields = new String[nbField];
    for (int i = 0; i < nbField; i++) {
        fields[i] = sfld[i].trim();
    }
    ArrayList<Integer> avail = new ArrayList<>();
    JSONArray reqArray = m.getJSONArray("req");
    for (int i = 0; i < reqArray.length(); i++) {
        JSONArray sr = reqArray.getJSONArray(i);
        int ord = sr.getInt(0);
        String type = sr.getString(1);
        JSONArray req = sr.getJSONArray(2);
        if ("none".equals(type)) {
            // unsatisfiable template
            continue;
        } else if ("all".equals(type)) {
            // AND requirement?
            boolean ok = true;
            for (int j = 0; j < req.length(); j++) {
                int idx = req.getInt(j);
                if (fields[idx] == null || fields[idx].length() == 0) {
                    // missing and was required
                    ok = false;
                    break;
                }
            }
            if (!ok) {
                continue;
            }
        } else if ("any".equals(type)) {
            // OR requirement?
            boolean ok = false;
            for (int j = 0; j < req.length(); j++) {
                int idx = req.getInt(j);
                if (fields[idx] != null && fields[idx].length() != 0) {
                    // missing and was required
                    ok = true;
                    break;
                }
            }
            if (!ok) {
                continue;
            }
        }
        avail.add(ord);
    }
    return avail;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)53 Test (org.junit.Test)35 JSONArray (com.ichi2.utils.JSONArray)28 Model (com.ichi2.libanki.Model)24 ArrayList (java.util.ArrayList)16 Intent (android.content.Intent)14 RobolectricTest (com.ichi2.anki.RobolectricTest)14 Collection (com.ichi2.libanki.Collection)12 Note (com.ichi2.libanki.Note)12 SuppressLint (android.annotation.SuppressLint)9 HashMap (java.util.HashMap)8 Bundle (android.os.Bundle)7 JSONException (com.ichi2.utils.JSONException)7 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)6 Card (com.ichi2.libanki.Card)6 ShadowActivity (org.robolectric.shadows.ShadowActivity)6 ShadowIntent (org.robolectric.shadows.ShadowIntent)6 View (android.view.View)5 ContentResolver (android.content.ContentResolver)4 ContentValues (android.content.ContentValues)4