Search in sources :

Example 16 with Card

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

the class DeckTask method doInBackgroundAnswerCard.

private TaskData doInBackgroundAnswerCard(TaskData... params) {
    Sched sched = params[0].getSched();
    Card oldCard = params[0].getCard();
    int ease = params[0].getInt();
    Card newCard = null;
    // TODO: proper leech handling
    int oldCardLeech = 0;
    // 0: normal; 1: leech; 2: leech & suspended
    try {
        AnkiDb ankiDB = sched.getCol().getDb();
        ankiDB.getDatabase().beginTransaction();
        try {
            if (oldCard != null) {
                sched.answerCard(oldCard, ease);
            }
            if (newCard == null) {
                newCard = getCard(sched);
            }
            if (newCard != null) {
                // render cards before locking database
                newCard._getQA(true);
            }
            publishProgress(new TaskData(newCard, oldCardLeech));
            ankiDB.getDatabase().setTransactionSuccessful();
        } finally {
            ankiDB.getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundAnswerCard - RuntimeException on answering card: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundAnswerCard");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : AnkiDb(com.ichi2.anki.AnkiDb) Sched(com.ichi2.libanki.Sched) Card(com.ichi2.libanki.Card)

Example 17 with Card

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

the class DeckTask method doInBackgroundMarkCard.

private TaskData doInBackgroundMarkCard(TaskData... params) {
    Card card = params[0].getCard();
    Sched sched = params[0].getSched();
    try {
        AnkiDb ankiDB = sched.getCol().getDb();
        ankiDB.getDatabase().beginTransaction();
        try {
            if (card != null) {
                Note note = card.note();
                sched.getCol().markUndo(Collection.UNDO_MARK_NOTE, new Object[] { note.getId(), note.stringTags(), card.getId() });
                if (note.hasTag("marked")) {
                    note.delTag("marked");
                } else {
                    note.addTag("marked");
                }
                note.flush();
            }
            publishProgress(new TaskData(card));
            ankiDB.getDatabase().setTransactionSuccessful();
        } finally {
            ankiDB.getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundMarkCard - RuntimeException on marking card: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundMarkCard");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : AnkiDb(com.ichi2.anki.AnkiDb) Sched(com.ichi2.libanki.Sched) Note(com.ichi2.libanki.Note) Card(com.ichi2.libanki.Card)

Example 18 with Card

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

the class Sched method progressToday.

/** returns today's progress
     *
     * @param counts (if empty, cached version will be used if any)
     * @param card
     * @return [progressCurrentDeck, progressAllDecks, leftCards, eta]
     */
public float[] progressToday(TreeSet<Object[]> counts, Card card, boolean eta) {
    try {
        int doneCurrent = 0;
        int[] leftCurrent = new int[] { 0, 0, 0 };
        String[] cs = new String[] { "new", "lrn", "rev" };
        long currentDid = 0;
        // current selected deck
        if (counts == null) {
            JSONObject deck = mCol.getDecks().current();
            currentDid = deck.getLong("id");
            for (String s : cs) {
                doneCurrent += deck.getJSONArray(s + "Today").getInt(1);
            }
            if (card != null) {
                int idx = countIdx(card);
                leftCurrent[idx] += idx == 1 ? card.getLeft() / 1000 : 1;
            } else {
                reset();
            }
            leftCurrent[0] += mNewCount;
            leftCurrent[1] += mLrnCount;
            leftCurrent[2] += mRevCount;
        }
        // refresh deck progresses with fresh counts if necessary
        if (counts != null || mCachedDeckCounts == null) {
            if (mCachedDeckCounts == null) {
                mCachedDeckCounts = new HashMap<Long, Pair<String[], long[]>>();
            }
            mCachedDeckCounts.clear();
            if (counts == null) {
                // reload counts
                counts = (TreeSet<Object[]>) deckCounts()[0];
            }
            for (Object[] d : counts) {
                int done = 0;
                JSONObject deck = mCol.getDecks().get((Long) d[1]);
                for (String s : cs) {
                    done += deck.getJSONArray(s + "Today").getInt(1);
                }
                mCachedDeckCounts.put((Long) d[1], new Pair<String[], long[]>((String[]) d[0], new long[] { done, (Integer) d[2], (Integer) d[3], (Integer) d[4] }));
            }
        }
        int doneAll = 0;
        int[] leftAll = new int[] { 0, 0, 0 };
        for (Map.Entry<Long, Pair<String[], long[]>> d : mCachedDeckCounts.entrySet()) {
            // || mCol.getDecks().isDyn(d.getKey());
            boolean exclude = d.getKey() == currentDid;
            if (d.getValue().first.length == 1) {
                if (exclude) {
                    // don't count cached version of current deck
                    continue;
                }
                long[] c = d.getValue().second;
                doneAll += c[0];
                leftAll[0] += c[1];
                leftAll[1] += c[2];
                leftAll[2] += c[3];
            } else if (exclude) {
                // exclude cached values for current deck in order to avoid double count
                long[] c = d.getValue().second;
                doneAll -= c[0];
                leftAll[0] -= c[1];
                leftAll[1] -= c[2];
                leftAll[2] -= c[3];
            }
        }
        doneAll += doneCurrent;
        leftAll[0] += leftCurrent[0];
        leftAll[1] += leftCurrent[1];
        leftAll[2] += leftCurrent[2];
        int totalAll = doneAll + leftAll[0] + leftAll[1] + leftAll[2];
        int totalCurrent = doneCurrent + leftCurrent[0] + leftCurrent[1] + leftCurrent[2];
        float progressCurrent = -1;
        if (totalCurrent != 0) {
            progressCurrent = (float) doneCurrent / (float) totalCurrent;
        }
        float progressTotal = -1;
        if (totalAll != 0) {
            progressTotal = (float) doneAll / (float) totalAll;
        }
        return new float[] { progressCurrent, progressTotal, totalAll - doneAll, eta ? eta(leftAll, false) : -1 };
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }
}
Also used : JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) Pair(com.ichi2.anki.Pair)

Aggregations

Card (com.ichi2.libanki.Card)7 Sched (com.ichi2.libanki.Sched)5 JSONException (org.json.JSONException)5 Collection (com.ichi2.libanki.Collection)4 Note (com.ichi2.libanki.Note)4 Resources (android.content.res.Resources)3 SpannableString (android.text.SpannableString)3 SpannedString (android.text.SpannedString)3 AnkiDb (com.ichi2.anki.AnkiDb)3 DeckTask (com.ichi2.async.DeckTask)3 Matcher (java.util.regex.Matcher)3 JSONObject (org.json.JSONObject)3 DialogInterface (android.content.DialogInterface)2 SharedPreferences (android.content.SharedPreferences)2 SQLException (android.database.SQLException)2 StyleSpan (android.text.style.StyleSpan)2 InputMethodManager (android.view.inputmethod.InputMethodManager)2 Pair (com.ichi2.anki.Pair)2 TaskData (com.ichi2.async.DeckTask.TaskData)2 StyledDialog (com.ichi2.themes.StyledDialog)2