Search in sources :

Example 6 with Note

use of com.ichi2.libanki.Note in project Anki-Android by Ramblurr.

the class MockNoteFactory method makeNote.

public static IMultimediaEditableNote makeNote() {
    MultimediaEditableNote note = new MultimediaEditableNote();
    note.setNumFields(4);
    TextField tf = new TextField();
    tf.setText("world");
    note.setField(0, tf);
    TextField tf2 = new TextField();
    tf2.setText("Welt");
    note.setField(1, tf2);
    TextField tf3 = new TextField();
    tf3.setText("Übung");
    note.setField(2, tf3);
    ImageField imageField = new ImageField();
    imageField.setImagePath("/mnt/sdcard/img/1.jpg");
    note.setField(3, imageField);
    return note;
}
Also used : ImageField(com.ichi2.anki.multimediacard.fields.ImageField) MultimediaEditableNote(com.ichi2.anki.multimediacard.impl.MultimediaEditableNote) IMultimediaEditableNote(com.ichi2.anki.multimediacard.IMultimediaEditableNote) TextField(com.ichi2.anki.multimediacard.fields.TextField)

Example 7 with Note

use of com.ichi2.libanki.Note in project Anki-Android by Ramblurr.

the class Collection method _renderQA.

public HashMap<String, String> _renderQA(Object[] data, List<String> args) {
    // data is [cid, nid, mid, did, ord, tags, flds]
    // unpack fields and create dict
    String[] flist = Utils.splitFields((String) data[6]);
    Map<String, String> fields = new HashMap<String, String>();
    long modelId = (Long) data[2];
    JSONObject model = mModels.get(modelId);
    Map<String, Pair<Integer, JSONObject>> fmap = mModels.fieldMap(model);
    for (String fname : fmap.keySet()) {
        fields.put(fname, flist[fmap.get(fname).first]);
    }
    fields.put("Tags", ((String) data[5]).trim());
    try {
        fields.put("Type", (String) model.get("name"));
        fields.put("Deck", mDecks.name((Long) data[3]));
        JSONObject template;
        if (model.getInt("type") == Sched.MODEL_STD) {
            template = model.getJSONArray("tmpls").getJSONObject((Integer) data[4]);
        } else {
            template = model.getJSONArray("tmpls").getJSONObject(0);
        }
        fields.put("Card", template.getString("name"));
        fields.put("c" + (((Integer) data[4]) + 1), "1");
        // render q & a
        HashMap<String, String> d = new HashMap<String, String>();
        try {
            d.put("id", Long.toString((Long) data[0]));
            String qfmt = template.getString("qfmt");
            String afmt = template.getString("afmt");
            String html;
            String format;
            // runFilter mungeFields for type "q"
            Models.fieldParser fparser = new Models.fieldParser(fields);
            Matcher m = fClozePattern.matcher(qfmt);
            format = m.replaceFirst(String.format(Locale.US, "{{cq:%d:", ((Integer) data[4]) + 1));
            m = fAltClozePattern.matcher(format);
            format = m.replaceFirst(String.format(Locale.US, "<%%cq:%d:", ((Integer) data[4]) + 1));
            html = mModels.getCmpldTemplate(format).execute(fparser);
            html = (String) AnkiDroidApp.getHooks().runFilter("mungeQA", html, "q", fields, model, data, this);
            d.put("q", html);
            // empty cloze?
            if (model.getInt("type") == Sched.MODEL_CLOZE) {
                if (getModels()._availClozeOrds(model, (String) data[6], false).size() == 0) {
                    d.put("q", "Please edit this note and add some cloze deletions.");
                }
            }
            fields.put("FrontSide", d.get("q"));
            // runFilter mungeFields for type "a"
            fparser = new Models.fieldParser(fields);
            m = fClozePattern.matcher(afmt);
            format = m.replaceFirst(String.format(Locale.US, "{{ca:%d:", ((Integer) data[4]) + 1));
            m = fAltClozePattern.matcher(format);
            format = m.replaceFirst(String.format(Locale.US, "<%%ca:%d:", ((Integer) data[4]) + 1));
            html = mModels.getCmpldTemplate(format).execute(fparser);
            html = (String) AnkiDroidApp.getHooks().runFilter("mungeQA", html, "a", fields, model, data, this);
            d.put("a", html);
            // empty cloze?
            if (model.getInt("type") == Sched.MODEL_CLOZE) {
                if (getModels()._availClozeOrds(model, (String) data[6], false).size() == 0) {
                    d.put("q", AnkiDroidApp.getAppResources().getString(com.ichi2.anki.R.string.empty_cloze_warning, "<a href=" + HELP_SITE + "#cloze>" + AnkiDroidApp.getAppResources().getString(com.ichi2.anki.R.string.help_cloze) + "</a>"));
                }
            }
        } catch (MustacheException e) {
            Resources res = AnkiDroidApp.getAppResources();
            String templateError = String.format(TEMPLATE_ERROR, res.getString(R.string.template_error), res.getString(R.string.template_error_detail), e.getMessage(), res.getString(R.string.note_type), model.getString("name"), res.getString(R.string.card_type), template.getString("name"), res.getString(R.string.template_error_fix));
            d.put("q", templateError);
            d.put("a", templateError);
        }
        return d;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) MustacheException(com.samskivert.mustache.MustacheException) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Resources(android.content.res.Resources) Pair(com.ichi2.anki.Pair)

Example 8 with Note

use of com.ichi2.libanki.Note in project Anki-Android by Ramblurr.

the class Models method _availClozeOrds.

public ArrayList<Integer> _availClozeOrds(JSONObject m, String flds, boolean allowEmpty) {
    String[] sflds = Utils.splitFields(flds);
    Map<String, Pair<Integer, JSONObject>> map = fieldMap(m);
    Set<Integer> ords = new HashSet<Integer>();
    Matcher matcher1 = null;
    try {
        matcher1 = fClozePattern1.matcher(m.getJSONArray("tmpls").getJSONObject(0).getString("qfmt"));
    // Libanki makes two finds for each case of the cloze tags, but we embed both in the pattern.
    // Please note, that this approach is not 100% correct, as we allow cases like {{cloze:...%>
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
    while (matcher1.find()) {
        String fname = matcher1.group(1);
        if (!map.containsKey(fname)) {
            continue;
        }
        int ord = map.get(fname).first;
        Matcher matcher2 = fClozePattern2.matcher(sflds[ord]);
        while (matcher2.find()) {
            ords.add(Integer.parseInt(matcher2.group(1)) - 1);
        }
    }
    if (ords.contains(-1)) {
        ords.remove(-1);
    }
    if (ords.isEmpty() && allowEmpty) {
        // empty clozes use first ord
        return new ArrayList<Integer>(Arrays.asList(new Integer[] { 0 }));
    }
    return new ArrayList<Integer>(ords);
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Pair(com.ichi2.anki.Pair) HashSet(java.util.HashSet)

Example 9 with Note

use of com.ichi2.libanki.Note in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundDismissNote.

private TaskData doInBackgroundDismissNote(TaskData... params) {
    Sched sched = params[0].getSched();
    Collection col = sched.getCol();
    Card card = params[0].getCard();
    Note note = card.note();
    int type = params[0].getInt();
    try {
        col.getDb().getDatabase().beginTransaction();
        try {
            switch(type) {
                case 4:
                    // collect undo information
                    col.markUndo(Collection.UNDO_BURY_CARD, new Object[] { col.getDirty(), note.cards(), card.getId() });
                    // then bury
                    sched.buryCards(new long[] { card.getId() });
                    sHadCardQueue = true;
                    break;
                case 0:
                    // collect undo information
                    col.markUndo(Collection.UNDO_BURY_NOTE, new Object[] { col.getDirty(), note.cards(), card.getId() });
                    // then bury
                    sched.buryNote(note.getId());
                    sHadCardQueue = true;
                    break;
                case 1:
                    // collect undo information
                    col.markUndo(Collection.UNDO_SUSPEND_CARD, new Object[] { card });
                    // suspend card
                    if (card.getQueue() == -1) {
                        sched.unsuspendCards(new long[] { card.getId() });
                    } else {
                        sched.suspendCards(new long[] { card.getId() });
                    }
                    sHadCardQueue = true;
                    break;
                case 2:
                    // collect undo information
                    ArrayList<Card> cards = note.cards();
                    long[] cids = new long[cards.size()];
                    for (int i = 0; i < cards.size(); i++) {
                        cids[i] = cards.get(i).getId();
                    }
                    col.markUndo(Collection.UNDO_SUSPEND_NOTE, new Object[] { cards, card.getId() });
                    // suspend note
                    sched.suspendCards(cids);
                    sHadCardQueue = true;
                    break;
                case 3:
                    // collect undo information
                    ArrayList<Card> allCs = note.cards();
                    long[] cardIds = new long[allCs.size()];
                    for (int i = 0; i < allCs.size(); i++) {
                        cardIds[i] = allCs.get(i).getId();
                    }
                    col.markUndo(Collection.UNDO_DELETE_NOTE, new Object[] { note, allCs, card.getId() });
                    // delete note
                    col.remNotes(new long[] { note.getId() });
                    sHadCardQueue = true;
                    break;
            }
            publishProgress(new TaskData(getCard(col.getSched()), 0));
            col.getDb().getDatabase().setTransactionSuccessful();
        } finally {
            col.getDb().getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundSuspendCard - RuntimeException on suspending card: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSuspendCard");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : Sched(com.ichi2.libanki.Sched) Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) Card(com.ichi2.libanki.Card)

Example 10 with Note

use of com.ichi2.libanki.Note in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundUpdateNote.

private TaskData doInBackgroundUpdateNote(TaskData[] params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundUpdateNote");
    // Save the note
    Sched sched = params[0].getSched();
    Collection col = sched.getCol();
    Card editCard = params[0].getCard();
    Note editNote = editCard.note();
    boolean fromReviewer = params[0].getBoolean();
    // mark undo
    col.markUndo(Collection.UNDO_EDIT_NOTE, new Object[] { col.getNote(editNote.getId()), editCard.getId(), fromReviewer });
    try {
        col.getDb().getDatabase().beginTransaction();
        try {
            // TODO: undo integration
            editNote.flush();
            // flush card too, in case, did has been changed
            editCard.flush();
            if (fromReviewer) {
                Card newCard;
                if (col.getDecks().active().contains(editCard.getDid())) {
                    newCard = editCard;
                    newCard.load();
                    // reload qa-cache
                    newCard.getQuestion(true);
                } else {
                    newCard = getCard(sched);
                }
                publishProgress(new TaskData(newCard));
            } else {
                publishProgress(new TaskData(editCard, editNote.stringTags()));
            }
            col.getDb().getDatabase().setTransactionSuccessful();
        } finally {
            col.getDb().getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundUpdateNote - RuntimeException on updating fact: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundUpdateNote");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : Sched(com.ichi2.libanki.Sched) Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) Card(com.ichi2.libanki.Card)

Aggregations

Note (com.ichi2.libanki.Note)11 JSONException (org.json.JSONException)11 JSONObject (org.json.JSONObject)8 IMultimediaEditableNote (com.ichi2.anki.multimediacard.IMultimediaEditableNote)7 MultimediaEditableNote (com.ichi2.anki.multimediacard.impl.MultimediaEditableNote)6 Collection (com.ichi2.libanki.Collection)6 TextField (com.ichi2.anki.multimediacard.fields.TextField)4 Card (com.ichi2.libanki.Card)4 AnkiDb (com.ichi2.anki.AnkiDb)3 IField (com.ichi2.anki.multimediacard.fields.IField)3 ImageField (com.ichi2.anki.multimediacard.fields.ImageField)3 Sched (com.ichi2.libanki.Sched)3 IOException (java.io.IOException)3 JSONArray (org.json.JSONArray)3 SuppressLint (android.annotation.SuppressLint)2 Intent (android.content.Intent)2 Resources (android.content.res.Resources)2 SpannableString (android.text.SpannableString)2 SpannedString (android.text.SpannedString)2 TextView (android.widget.TextView)2