Search in sources :

Example 1 with SFLD

use of com.ichi2.anki.CardBrowser.Column.SFLD in project Anki-Android by ankidroid.

the class Models method _availStandardOrds.

/**
 * Given a joined field string and a standard note type, return available template ordinals
 */
public static ArrayList<Integer> _availStandardOrds(Model m, String[] sfld, List<ParsedNode> nodes, boolean allowEmpty) {
    Set<String> nonEmptyFields = m.nonEmptyFields(sfld);
    ArrayList<Integer> avail = new ArrayList<>(nodes.size());
    for (int i = 0; i < nodes.size(); i++) {
        ParsedNode node = nodes.get(i);
        if (node != null && !node.template_is_empty(nonEmptyFields)) {
            avail.add(i);
        }
    }
    if (allowEmpty && avail.isEmpty()) {
        /* According to anki documentation:
            When adding/importing, if a normal note doesn’t generate any cards, Anki will now add a blank card 1 instead of refusing to add the note. */
        avail.add(0);
    }
    return avail;
}
Also used : ParsedNode(com.ichi2.libanki.template.ParsedNode) ArrayList(java.util.ArrayList)

Example 2 with SFLD

use of com.ichi2.anki.CardBrowser.Column.SFLD in project Anki-Android by Ramblurr.

the class CardBrowser method onPrepareDialog.

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    Resources res = getResources();
    StyledDialog ad = (StyledDialog) dialog;
    switch(id) {
        case DIALOG_ORDER:
            for (int i = 0; i < mOrderByFields.length; ++i) {
                if (i != CARD_ORDER_NONE && i == mOrder) {
                    if (mOrderAsc) {
                        ad.changeListItem(i, mOrderByFields[i] + " (\u25b2)");
                    } else {
                        ad.changeListItem(i, mOrderByFields[i] + " (\u25bc)");
                    }
                } else {
                    ad.changeListItem(i, mOrderByFields[i]);
                }
            }
            break;
        case DIALOG_CONTEXT_MENU:
            HashMap<String, String> card = mCards.get(mPositionInCardsList);
            int flags = Integer.parseInt(card.get("flags"));
            if (flags == 2 || flags == 3) {
                ad.changeListItem(CONTEXT_MENU_MARK, res.getString(R.string.card_browser_unmark_card));
            // Log.d(AnkiDroidApp.TAG, "Selected Card is currently marked");
            } else {
                ad.changeListItem(CONTEXT_MENU_MARK, res.getString(R.string.card_browser_mark_card));
            }
            if (flags == 1 || flags == 3) {
                ad.changeListItem(CONTEXT_MENU_SUSPEND, res.getString(R.string.card_browser_unsuspend_card));
            // Log.d(AnkiDroidApp.TAG, "Selected Card is currently suspended");
            } else {
                ad.changeListItem(CONTEXT_MENU_SUSPEND, res.getString(R.string.card_browser_suspend_card));
            }
            ad.setTitle(card.get("sfld"));
            break;
        case DIALOG_TAGS:
            mSelectedTags.clear();
            ad.setMultiChoiceItems(allTags, new boolean[allTags.length], new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String tag = allTags[which];
                    if (mSelectedTags.contains(tag)) {
                        // Log.d(AnkiDroidApp.TAG, "unchecked tag: " + tag);
                        mSelectedTags.remove(tag);
                    } else {
                        // Log.d(AnkiDroidApp.TAG, "checked tag: " + tag);
                        mSelectedTags.add(tag);
                    }
                }
            });
            break;
    }
}
Also used : OnClickListener(android.content.DialogInterface.OnClickListener) DialogInterface(android.content.DialogInterface) StyledDialog(com.ichi2.themes.StyledDialog) Resources(android.content.res.Resources)

Example 3 with SFLD

use of com.ichi2.anki.CardBrowser.Column.SFLD in project Anki-Android by Ramblurr.

the class CardBrowser method updateCardInList.

private void updateCardInList(Card card, String updatedCardTags) {
    Note note = card.note();
    int pos;
    for (Card c : note.cards()) {
        pos = getPosition(mCards, c.getId());
        if (pos < 0 || pos >= mCards.size()) {
            continue;
        }
        if (updatedCardTags != null) {
            mCards.get(pos).put("tags", updatedCardTags);
        }
        String sfld = note.getSFld();
        mCards.get(pos).put("sfld", sfld);
        if (mWholeCollection) {
            String deckName;
            try {
                deckName = mCol.getDecks().get(card.getDid()).getString("name");
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
            mCards.get(pos).put("deck", deckName);
        }
        String flags = Integer.toString((c.getQueue() == -1 ? 1 : 0) + (note.hasTag("marked") ? 2 : 0));
        mCards.get(pos).put("flags", flags);
    }
    updateList();
}
Also used : Note(com.ichi2.libanki.Note) JSONException(org.json.JSONException) Card(com.ichi2.libanki.Card)

Example 4 with SFLD

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

the class AnkiChinaSyncer method handleServerData.

// 35%+25%
private void handleServerData(JSONObject item) {
    mCol = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
    CollectionHelper.getInstance().lockCollection();
    updateDialogProgress(SYNCING_DATA, "更新全局配置中", mCurrentProgress + 1);
    DB db = mCol.getDb();
    try {
        JSONObject remoteCol = item.getJSONObject("col").getJSONObject("replace");
        // db.execute("update col set id ="+remoteCol.getInt("id")+","+"set crt =\"+remoteCol.getLong(\"crt\")");
        // db.execute("update col set crt ="+remoteCol.getLong("crt"));
        // db.execute("update col set mod ="+remoteCol.getLong("mod"));
        // db.execute("update col set scm ="+remoteCol.getLong("scm"));
        // db.execute("update col set ver ="+remoteCol.getInt("ver"));
        // db.execute("update col set dty ="+remoteCol.getInt("dty"));
        // db.execute("update col set usn ="+remoteCol.getInt("usn"));
        // db.execute("update col set ls ="+remoteCol.getLong("ls"));
        // db.execute("update col set conf ="+remoteCol.getString("conf"));
        // db.execute("update col set tags ="+remoteCol.getString("tags"));
        // db.execute("update col set tags ="+remoteCol.getString("tags"));
        // db.execute("update col set id = %d,");
        Timber.i("remote col config:%s", remoteCol.toString());
        @SuppressLint("DefaultLocale") String sql = String.format("update col set id = %d,crt = %d,mod=%d,scm=%d,ver=%d,dty=%d,usn=%d,ls=%d,conf='%s',tags='%s'", remoteCol.getInt("id"), remoteCol.getLong("crt"), remoteCol.getLong("mod"), remoteCol.getLong("scm"), remoteCol.getInt("ver"), remoteCol.getInt("dty"), remoteCol.getInt("usn"), remoteCol.getLong("ls"), remoteCol.getString("conf"), remoteCol.getString("tags") == null || !remoteCol.getString("tags").startsWith("{") ? "{}" : remoteCol.getString("tags"));
        db.execute(sql);
        mCol.load();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Decks currentDecks = mCol.getDecks();
    // 删除多余的内容
    try {
        JSONArray deletedDecks = item.getJSONObject("decks").getJSONArray("delete");
        if (deletedDecks.length() > 0) {
            double percent = 2.0 / deletedDecks.length();
            for (int i = 0; i < deletedDecks.length(); i++) {
                String deckID = deletedDecks.getString(i);
                currentDecks.rem(Long.parseLong(deckID));
                updateDialogProgress(SYNCING_DATA, "删除多余牌组中", mCurrentProgress + percent);
            }
            mCol.save();
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
    try {
        JSONArray deletedDConf = item.getJSONObject("dconf").getJSONArray("delete");
        if (deletedDConf.length() > 0) {
            double percent = 2.0 / deletedDConf.length();
            for (int i = 0; i < deletedDConf.length(); i++) {
                String id = deletedDConf.getString(i);
                mCol.getDecks().remConf(Long.parseLong(id));
                updateDialogProgress(SYNCING_DATA, "删除多余牌组配置中", mCurrentProgress + percent);
            }
            mCol.save();
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
    try {
        JSONArray deletedModel = item.getJSONObject("models").getJSONArray("delete");
        if (deletedModel.length() > 0) {
            double percent = 2.0 / deletedModel.length();
            for (int i = 0; i < deletedModel.length(); i++) {
                String id = deletedModel.getString(i);
                mCol.getModels().rem(mCol.getModels().get(Long.parseLong(id)));
                updateDialogProgress(SYNCING_DATA, "删除多余模板中", mCurrentProgress + percent);
            }
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    updateDialogProgress(SYNCING_DATA, "删除多余卡牌中", mCurrentProgress + 2);
    try {
        JSONArray deletedCards = item.getJSONObject("cards").getJSONArray("delete");
        List<Long> sids = ids2longList(deletedCards);
        Timber.e("need delete cards num:%s", sids.size());
        // db.execute("DELETE FROM cards WHERE id IN " + sids);
        mCol.remCards(sids);
        mCol.save();
    } catch (Exception e) {
        e.printStackTrace();
    }
    updateDialogProgress(SYNCING_DATA, "删除多余笔记中", mCurrentProgress + 2);
    try {
        JSONArray deletedNotes = item.getJSONObject("notes").getJSONArray("delete");
        long[] sids = ids2longArray(deletedNotes);
        // db.execute("DELETE FROM notes WHERE id IN " + sids);
        mCol.remNotes(sids);
        mCol.save();
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        JSONObject replaceDecks = item.getJSONObject("decks").getJSONObject("replace");
        if (replaceDecks.length() > 0) {
            double percent = 2.0 / replaceDecks.length();
            Iterator<String> it = replaceDecks.keys();
            while (it.hasNext()) {
                String next = it.next();
                try {
                    // mCol.getDecks().getDecks().put(Long.parseLong(next), new Deck(replaceDecks.getJSONObject(next)));
                    mCol.getDecks().update(new Deck(replaceDecks.getJSONObject(next)));
                    updateDialogProgress(SYNCING_DATA, "同步牌组数据中", mCurrentProgress + percent);
                } catch (Exception e) {
                // 只遍历model id
                }
            }
            mCol.save();
        }
    } catch (Exception e) {
    // e.printStackTrace();
    }
    db.getDatabase().beginTransaction();
    try {
        JSONArray replace = item.getJSONObject("revlog").getJSONArray("replace");
        if (replace.length() > 0) {
            for (int i = 0; i < replace.length(); i++) {
                log(replace.getJSONArray(i).get(0), replace.getJSONArray(i).get(1), replace.getJSONArray(i).get(2), replace.getJSONArray(i).get(3), replace.getJSONArray(i).get(4), replace.getJSONArray(i).get(5), replace.getJSONArray(i).get(6), replace.getJSONArray(i).get(7), replace.getJSONArray(i).get(8));
            }
            db.getDatabase().setTransactionSuccessful();
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.getDatabase().endTransaction();
    }
    // Timber.e("看看是null还是null:%s,%s", (item.getJSONObject("dconf").get("replace")==JSONObject.NULL), (item.getJSONObject("dconf").getString("replace").equals("null")));
    try {
        JSONObject replaceDConf = item.getJSONObject("dconf").getJSONObject("replace");
        if (replaceDConf.length() > 0) {
            double percent = 2.0 / replaceDConf.length();
            Iterator<String> it = replaceDConf.keys();
            while (it.hasNext()) {
                String next = it.next();
                try {
                    Long.parseLong(next);
                    mCol.getDecks().updateConf(new DeckConfig(replaceDConf.getJSONObject(next)));
                    updateDialogProgress(SYNCING_DATA, "同步牌组配置数据中", mCurrentProgress + percent);
                } catch (Exception e) {
                // 只遍历model id
                }
            }
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        JSONObject replaceModels = item.getJSONObject("models").getJSONObject("replace");
        if (replaceModels.length() > 0) {
            double percent = 5.0 / replaceModels.length();
            Iterator<String> it = replaceModels.keys();
            while (it.hasNext()) {
                String next = it.next();
                try {
                    Long.parseLong(next);
                    mCol.getModels().update(new Model(replaceModels.getJSONObject(next)));
                    updateDialogProgress(SYNCING_DATA, "同步模板数据中", mCurrentProgress + percent);
                } catch (Exception e) {
                // 只遍历model id
                }
            }
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    db.getDatabase().beginTransaction();
    try {
        JSONArray replace = item.getJSONObject("notes").getJSONArray("replace");
        if (replace.length() > 0) {
            double percent = mPullNotesPerPercent / replace.length();
            for (int i = 0; i < replace.length(); i++) {
                // String values = replace.getJSONArray(i).toString().replace("[", "").replace("]", "").replaceAll("\"","'").replaceAll("\u001f","\u001f");
                // String sql = "replace into notes(id,guid,mid,mod,usn,tags,flds,sfld,csum,flags,data) values ( " + values + ")";
                // 
                db.execute("insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?)", replace.getJSONArray(i).get(0), replace.getJSONArray(i).get(1), replace.getJSONArray(i).get(2), replace.getJSONArray(i).get(3), replace.getJSONArray(i).get(4), replace.getJSONArray(i).get(5), replace.getJSONArray(i).get(6), replace.getJSONArray(i).get(7), replace.getJSONArray(i).get(8), replace.getJSONArray(i).get(9), replace.getJSONArray(i).get(10));
                updateDialogProgress(SYNCING_DATA, "同步笔记数据中", mCurrentProgress + percent);
            }
            db.getDatabase().setTransactionSuccessful();
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.getDatabase().endTransaction();
    }
    db.getDatabase().beginTransaction();
    try {
        JSONArray replace = item.getJSONObject("cards").getJSONArray("replace");
        if (replace.length() > 0) {
            double percent = 5.0 / replace.length();
            for (int i = 0; i < replace.length(); i++) {
                // String values = replace.getJSONArray(i).toString().replace("[", "").replace("]", "").replaceAll("\"","'").replaceAll("\u001f","\u001f");
                // String sql = "replace into cards(id,nid,did,ord,mod,usn,type,queue,due,ivl,factor,reps,lapses,left,odue,odid,flags,data) values ( " + values + ")";
                // Timber.i("update dialog progress:%d", percent);
                db.execute("insert or replace into cards values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", replace.getJSONArray(i).get(0), replace.getJSONArray(i).get(1), replace.getJSONArray(i).get(2), replace.getJSONArray(i).get(3), replace.getJSONArray(i).get(4), replace.getJSONArray(i).get(5), replace.getJSONArray(i).get(6), replace.getJSONArray(i).get(7), replace.getJSONArray(i).get(8), replace.getJSONArray(i).get(9), replace.getJSONArray(i).get(10), replace.getJSONArray(i).get(11), replace.getJSONArray(i).get(12), replace.getJSONArray(i).get(13), replace.getJSONArray(i).get(14), replace.getJSONArray(i).get(15), replace.getJSONArray(i).get(16), replace.getJSONArray(i).get(17));
                updateDialogProgress(SYNCING_DATA, "同步卡牌数据中", mCurrentProgress + percent);
            }
            db.getDatabase().setTransactionSuccessful();
            mCol.save();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        db.getDatabase().endTransaction();
    }
    CollectionHelper.getInstance().unlockCollection();
}
Also used : Decks(com.ichi2.libanki.Decks) JSONArray(com.ichi2.utils.JSONArray) Deck(com.ichi2.libanki.Deck) FileNotFoundException(java.io.FileNotFoundException) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) IOException(java.io.IOException) SuppressLint(android.annotation.SuppressLint) JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) SuppressLint(android.annotation.SuppressLint) DB(com.ichi2.libanki.DB) DeckConfig(com.ichi2.libanki.DeckConfig)

Example 5 with SFLD

use of com.ichi2.anki.CardBrowser.Column.SFLD 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

JSONArray (com.ichi2.utils.JSONArray)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 DialogInterface (android.content.DialogInterface)1 OnClickListener (android.content.DialogInterface.OnClickListener)1 Resources (android.content.res.Resources)1 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 Card (com.ichi2.libanki.Card)1 DB (com.ichi2.libanki.DB)1 Deck (com.ichi2.libanki.Deck)1 DeckConfig (com.ichi2.libanki.DeckConfig)1 Decks (com.ichi2.libanki.Decks)1 Model (com.ichi2.libanki.Model)1 Note (com.ichi2.libanki.Note)1 ParsedNode (com.ichi2.libanki.template.ParsedNode)1 StyledDialog (com.ichi2.themes.StyledDialog)1 JSONObject (com.ichi2.utils.JSONObject)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 JSONException (org.json.JSONException)1