Search in sources :

Example 36 with CARD

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

the class DeckInfoListAdapter method updateCardsInList.

private void updateCardsInList(List<Card> cards, Map<Long, String> updatedCardTags) {
    List<CardBrowser.CardCache> cardList = mCards;
    Map<Long, Integer> idToPos = getPositionMap(cardList);
    for (Card c : cards) {
        // get position in the mCards search results HashMap
        Integer pos = idToPos.get(c.getId());
        if (pos == null || pos >= mCards.size()) {
            continue;
        }
        // update Q & A etc
        cardList.get(pos).load(true, 0, 1);
    }
    notifyDataSetChanged();
}
Also used : Card(com.ichi2.libanki.Card)

Example 37 with CARD

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

the class ReviewerCustomFonts method getOverrideFontStyle.

/**
 * Returns the CSS used to set the override font.
 *
 * @return the override font style, or the empty string if no override font is set
 */
private String getOverrideFontStyle(Context context, Map<String, AnkiFont> customFontsMap) {
    if (mOverrideFontStyle == null) {
        SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(context);
        AnkiFont defaultFont = customFontsMap.get(preferences.getString("defaultFont", null));
        boolean overrideFont = "1".equals(preferences.getString("overrideFontBehavior", "0"));
        if (defaultFont != null && overrideFont) {
            mOverrideFontStyle = "BODY, .card, * { " + defaultFont.getCSS(true) + " }\n";
        } else {
            mOverrideFontStyle = "";
        }
    }
    return mOverrideFontStyle;
}
Also used : AnkiFont(com.ichi2.anki.AnkiFont) SharedPreferences(android.content.SharedPreferences)

Example 38 with CARD

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

the class NoteService method saveMedia.

/**
 * Saves the multimedia associated with this card to proper path inside anki folder. For each field associated with
 * the note it checks for the following condition a. The field content should have changed b. The field content does
 * not already point to a media inside anki media path If both condition satisfies then it copies the file inside
 * the media path and deletes the file referenced by the note
 *
 * @param noteNew
 */
public static void saveMedia(Collection col, final MultimediaEditableNote noteNew) {
    // if (noteNew.getModelId() == noteOld.getModelId())
    // {
    // int fieldCount = noteNew.getNumberOfFields();
    // for (int i = 0; i < fieldCount; i++)
    // {
    // IField newField = noteNew.getField(i);
    // IField oldField = noteOld.getField(i);
    // if
    // (newField.getFormattedValue().equals(oldField.getFormattedValue()))
    // {
    // continue;
    // }
    // importMediaToDirectory(newField);
    // }
    // }
    // else
    // {
    int fieldCount = noteNew.getNumberOfFields();
    for (int i = 0; i < fieldCount; i++) {
        IField newField = noteNew.getField(i);
        importMediaToDirectory(col, newField);
    }
// }
}
Also used : IField(com.ichi2.anki.multimediacard.fields.IField)

Example 39 with CARD

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

the class Finder method _findTemplate.

private String _findTemplate(String val) {
    // were we given an ordinal number?
    Integer num = null;
    try {
        num = Integer.parseInt(val) - 1;
    } catch (NumberFormatException e) {
        num = null;
    }
    if (num != null) {
        return "c.ord = " + num;
    }
    // search for template names
    List<String> lims = new ArrayList<>();
    for (JSONObject m : mCol.getModels().all()) {
        JSONArray tmpls = m.getJSONArray("tmpls");
        for (int ti = 0; ti < tmpls.length(); ++ti) {
            JSONObject t = tmpls.getJSONObject(ti);
            String templateName = t.getString("name");
            Normalizer.normalize(templateName, Normalizer.Form.NFC);
            if (templateName.equalsIgnoreCase(val)) {
                if (m.getInt("type") == Consts.MODEL_CLOZE) {
                    // if the user has asked for a cloze card, we want
                    // to give all ordinals, so we just limit to the
                    // model instead
                    lims.add("(n.mid = " + m.getLong("id") + ")");
                } else {
                    lims.add("(n.mid = " + m.getLong("id") + " and c.ord = " + t.getInt("ord") + ")");
                }
            }
        }
    }
    return TextUtils.join(" or ", lims.toArray(new String[lims.size()]));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray)

Example 40 with CARD

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

the class Collection method genCards.

public ArrayList<Long> genCards(long[] nids) {
    // build map of (nid,ord) so we don't create dupes
    String snids = Utils.ids2str(nids);
    HashMap<Long, HashMap<Integer, Long>> have = new HashMap<>();
    HashMap<Long, Long> dids = new HashMap<>();
    HashMap<Long, Long> dues = new HashMap<>();
    Cursor cur = null;
    try {
        cur = mDb.getDatabase().query("select id, nid, ord, did, due, odue, odid, type from cards where nid in " + snids, null);
        while (cur.moveToNext()) {
            long id = cur.getLong(0);
            long nid = cur.getLong(1);
            int ord = cur.getInt(2);
            long did = cur.getLong(3);
            long due = cur.getLong(4);
            long odue = cur.getLong(5);
            long odid = cur.getLong(6);
            @Consts.CARD_TYPE int type = cur.getInt(7);
            // existing cards
            if (!have.containsKey(nid)) {
                have.put(nid, new HashMap<Integer, Long>());
            }
            have.get(nid).put(ord, id);
            // if in a filtered deck, add new cards to original deck
            if (odid != 0) {
                did = odid;
            }
            // and their dids
            if (dids.containsKey(nid)) {
                if (dids.get(nid) != 0 && dids.get(nid) != did) {
                    // cards are in two or more different decks; revert to model default
                    dids.put(nid, 0L);
                }
            } else {
                // first card or multiple cards in same deck
                dids.put(nid, did);
            }
            // save due
            if (odid != 0) {
                due = odue;
            }
            if (!dues.containsKey(nid) && type == Consts.CARD_TYPE_NEW) {
                dues.put(nid, due);
            }
        }
    } finally {
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
    }
    // build cards for each note
    ArrayList<Object[]> data = new ArrayList<>();
    long ts = getTime().maxID(mDb);
    long now = getTime().intTime();
    ArrayList<Long> rem = new ArrayList<>();
    int usn = usn();
    cur = null;
    try {
        cur = mDb.getDatabase().query("SELECT id, mid, flds FROM notes WHERE id IN " + snids, null);
        while (cur.moveToNext()) {
            long nid = cur.getLong(0);
            long mid = cur.getLong(1);
            String flds = cur.getString(2);
            Model model = getModels().get(mid);
            ArrayList<Integer> avail = getModels().availOrds(model, Utils.splitFields(flds));
            Long did = dids.get(nid);
            // use sibling due if there is one, else use a new id
            long due;
            if (dues.containsKey(nid)) {
                due = dues.get(nid);
            } else {
                due = nextID("pos");
            }
            if (did == null || did == 0L) {
                did = model.getLong("did");
            }
            // add any missing cards
            for (JSONObject t : _tmplsFromOrds(model, avail)) {
                int tord = t.getInt("ord");
                boolean doHave = have.containsKey(nid) && have.get(nid).containsKey(tord);
                if (!doHave) {
                    // check deck is not a cram deck
                    long ndid;
                    try {
                        ndid = t.getLong("did");
                        if (ndid != 0) {
                            did = ndid;
                        }
                    } catch (JSONException e) {
                    // do nothing
                    }
                    if (getDecks().isDyn(did)) {
                        did = 1L;
                    }
                    // if the deck doesn't exist, use default instead
                    did = mDecks.get(did).getLong("id");
                    // give it a new id instead
                    data.add(new Object[] { ts, nid, did, tord, now, usn, due });
                    ts += 1;
                }
            }
            // note any cards that need removing
            if (have.containsKey(nid)) {
                for (Map.Entry<Integer, Long> n : have.get(nid).entrySet()) {
                    if (!avail.contains(n.getKey())) {
                        rem.add(n.getValue());
                    }
                }
            }
        }
    } finally {
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
    }
    // bulk update
    mDb.executeMany("INSERT INTO cards VALUES (?,?,?,?,?,?,0,0,?,0,0,0,0,0,0,0,0,\"\")", data);
    return rem;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONException(com.ichi2.utils.JSONException) Cursor(android.database.Cursor) SuppressLint(android.annotation.SuppressLint) JSONObject(com.ichi2.utils.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Card (com.ichi2.libanki.Card)222 Test (org.junit.Test)212 Collection (com.ichi2.libanki.Collection)179 Note (com.ichi2.libanki.Note)173 RobolectricTest (com.ichi2.anki.RobolectricTest)168 JSONObject (com.ichi2.utils.JSONObject)114 JSONArray (com.ichi2.utils.JSONArray)80 DeckConfig (com.ichi2.libanki.DeckConfig)72 ArrayList (java.util.ArrayList)48 NonNull (androidx.annotation.NonNull)33 Deck (com.ichi2.libanki.Deck)32 Intent (android.content.Intent)29 JSONException (com.ichi2.utils.JSONException)27 Model (com.ichi2.libanki.Model)26 Context (android.content.Context)23 SuppressLint (android.annotation.SuppressLint)22 Cursor (android.database.Cursor)22 HashMap (java.util.HashMap)21 Matchers.containsString (org.hamcrest.Matchers.containsString)20 List (java.util.List)17