Search in sources :

Example 61 with Template

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

the class Models method _addTemplate.

/**
 * Note: should col.genCards() afterwards.
 */
private void _addTemplate(Model m, JSONObject template) {
    // do the actual work of addTemplate. Do not consider whether
    // model is new or not.
    JSONArray tmpls = m.getJSONArray("tmpls");
    tmpls.put(template);
    m.put("tmpls", tmpls);
    _updateTemplOrds(m);
    save(m);
}
Also used : JSONArray(com.ichi2.utils.JSONArray)

Example 62 with Template

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

the class Collection method addNote.

/**
 * Add a note to the collection. Return number of new cards.
 */
public int addNote(Note note) {
    // check we have card models available, then save
    ArrayList<JSONObject> cms = findTemplates(note);
    if (cms.size() == 0) {
        return 0;
    }
    note.flush();
    // deck conf governs which of these are used
    int due = nextID("pos");
    // add cards
    int ncards = 0;
    for (JSONObject template : cms) {
        _newCard(note, template, due);
        ncards += 1;
    }
    return ncards;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) SuppressLint(android.annotation.SuppressLint)

Example 63 with Template

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

the class Storage method _upgradeClozeModel.

private static void _upgradeClozeModel(Collection col, Model m) throws ConfirmModSchemaException {
    m.put("type", Consts.MODEL_CLOZE);
    // convert first template
    JSONObject t = m.getJSONArray("tmpls").getJSONObject(0);
    for (String type : new String[] { "qfmt", "afmt" }) {
        t.put(type, t.getString(type).replaceAll("\\{\\{cloze:1:(.+?)\\}\\}", "{{cloze:$1}}"));
    }
    t.put("name", "Cloze");
    // delete non-cloze cards for the model
    JSONArray tmpls = m.getJSONArray("tmpls");
    ArrayList<JSONObject> rem = new ArrayList<>();
    for (int i = 1; i < tmpls.length(); i++) {
        JSONObject ta = tmpls.getJSONObject(i);
        if (!ta.getString("afmt").contains("{{cloze:")) {
            rem.add(ta);
        }
    }
    for (JSONObject r : rem) {
        col.getModels().remTemplate(m, r);
    }
    JSONArray newTmpls = new JSONArray();
    newTmpls.put(tmpls.getJSONObject(0));
    m.put("tmpls", newTmpls);
    col.getModels()._updateTemplOrds(m);
    col.getModels().save(m);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) ArrayList(java.util.ArrayList)

Example 64 with Template

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

the class ModelTest method test_cloze_ordinals.

@Test
public void test_cloze_ordinals() throws ConfirmModSchemaException {
    Collection col = getCol();
    col.getModels().setCurrent(col.getModels().byName("Cloze"));
    Model m = col.getModels().current();
    Models mm = col.getModels();
    // We replace the default Cloze template
    JSONObject t = Models.newTemplate("ChainedCloze");
    t.put("qfmt", "{{text:cloze:Text}}");
    t.put("afmt", "{{text:cloze:Text}}");
    mm.addTemplateModChanged(m, t);
    mm.save(m);
    col.getModels().remTemplate(m, m.getJSONArray("tmpls").getJSONObject(0));
    Note note = col.newNote();
    note.setItem("Text", "{{c1::firstQ::firstA}}{{c2::secondQ::secondA}}");
    col.addNote(note);
    assertEquals(2, col.cardCount());
    List<Card> cards = note.cards();
    assertEquals(2, cards.size());
    Card c = cards.get(0);
    Card c2 = cards.get(1);
    // first card should have first ord
    assertEquals(0, c.getOrd());
    assertEquals(1, c2.getOrd());
}
Also used : JSONObject(com.ichi2.utils.JSONObject) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 65 with Template

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

the class ModelTest method test_modelChange.

@Test
public void test_modelChange() throws ConfirmModSchemaException {
    Collection col = getCol();
    Model cloze = col.getModels().byName("Cloze");
    // enable second template and add a note
    Model m = col.getModels().current();
    Models mm = col.getModels();
    JSONObject t = Models.newTemplate("Reverse");
    t.put("qfmt", "{{Back}}");
    t.put("afmt", "{{Front}}");
    mm.addTemplateModChanged(m, t);
    mm.save(m);
    Model basic = m;
    Note note = col.newNote();
    note.setItem("Front", "note");
    note.setItem("Back", "b123");
    col.addNote(note);
    // switch fields
    Map<Integer, Integer> map = new HashMap<>();
    map.put(0, 1);
    map.put(1, 0);
    col.getModels().change(basic, new long[] { note.getId() }, basic, map, null);
    note.load();
    assertEquals("b123", note.getItem("Front"));
    assertEquals("note", note.getItem("Back"));
    // switch cards
    Card c0 = note.cards().get(0);
    Card c1 = note.cards().get(1);
    assertThat(c0.q(), containsString("b123"));
    assertThat(c1.q(), containsString("note"));
    assertEquals(0, c0.getOrd());
    assertEquals(1, c1.getOrd());
    col.getModels().change(basic, new long[] { note.getId() }, basic, null, map);
    note.load();
    c0.load();
    c1.load();
    assertThat(c0.q(), containsString("note"));
    assertThat(c1.q(), containsString("b123"));
    assertEquals(1, c0.getOrd());
    assertEquals(0, c1.getOrd());
    // .cards() returns cards in order
    assertEquals(c1.getId(), note.cards().get(0).getId());
    // delete first card
    map = new HashMap<>();
    map.put(0, null);
    map.put(1, 1);
    // if (isWin) {
    // // The low precision timer on Windows reveals a race condition
    // time.sleep(0.05);
    // }
    col.getModels().change(basic, new long[] { note.getId() }, basic, null, map);
    note.load();
    c0.load();
    // the card was deleted
    // but we have two cards, as a new one was generated
    assertEquals(2, note.numberOfCards());
    // an unmapped field becomes blank
    assertEquals("b123", note.getItem("Front"));
    assertEquals("note", note.getItem("Back"));
    col.getModels().change(basic, new long[] { note.getId() }, basic, map, null);
    note.load();
    assertEquals("", note.getItem("Front"));
    assertEquals("note", note.getItem("Back"));
    // another note to try model conversion
    note = col.newNote();
    note.setItem("Front", "f2");
    note.setItem("Back", "b2");
    col.addNote(note);
    // counts = col.getModels().all_use_counts();
    // Using older version of the test
    assertEquals(2, col.getModels().useCount(basic));
    assertEquals(0, col.getModels().useCount(cloze));
    // Identity map
    map = new HashMap<>();
    map.put(0, 0);
    map.put(1, 1);
    col.getModels().change(basic, new long[] { note.getId() }, cloze, map, map);
    note.load();
    assertEquals("f2", note.getItem("Text"));
    assertEquals(2, note.numberOfCards());
    // back the other way, with deletion of second ord
    col.getModels().remTemplate(basic, basic.getJSONArray("tmpls").getJSONObject(1));
    assertEquals(2, col.getDb().queryScalar("select count() from cards where nid = ?", note.getId()));
    map = new HashMap<>();
    map.put(0, 0);
    col.getModels().change(cloze, new long[] { note.getId() }, basic, map, map);
    assertEquals(1, col.getDb().queryScalar("select count() from cards where nid = ?", note.getId()));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

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