Search in sources :

Example 31 with Sched

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

the class DeckTask method doInBackgroundUpdateValuesFromDeck.

private TaskData doInBackgroundUpdateValuesFromDeck(TaskData... params) {
    // Log.i(AnkiDroidApp.TAG, "doInBackgroundUpdateValuesFromDeck");
    try {
        Sched sched = params[0].getCollection().getSched();
        Object[] obj = params[0].getObjArray();
        boolean reset = (Boolean) obj[0];
        if (reset) {
            sched.reset();
        }
        int[] counts = sched.counts();
        int totalNewCount = sched.totalNewForCurrentDeck();
        int totalCount = sched.cardCount();
        double progressMature = ((double) sched.matureCount()) / ((double) totalCount);
        double progressAll = 1 - (((double) (totalNewCount + counts[1])) / ((double) totalCount));
        double[][] serieslist = null;
        // only calculate stats if necessary
        if ((Boolean) obj[1]) {
            serieslist = Stats.getSmallDueStats(sched.getCol());
        }
        return new TaskData(new Object[] { counts[0], counts[1], counts[2], totalNewCount, totalCount, progressMature, progressAll, sched.eta(counts), serieslist });
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundUpdateValuesFromDeck - an error occurred: " + e);
        return null;
    }
}
Also used : Sched(com.ichi2.libanki.Sched) JSONObject(org.json.JSONObject)

Example 32 with Sched

use of com.ichi2.libanki.sched.Sched 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)

Example 33 with Sched

use of com.ichi2.libanki.sched.Sched 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 34 with Sched

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

the class DeckTask method doInBackgroundUndo.

private TaskData doInBackgroundUndo(TaskData... params) {
    Sched sched = params[0].getSched();
    Collection col = sched.getCol();
    try {
        col.getDb().getDatabase().beginTransaction();
        Card newCard;
        try {
            long cid = col.undo();
            if (cid != 0) {
                // a review was undone,
                newCard = col.getCard(cid);
                col.reset();
                col.getSched().decrementCounts(newCard);
                sHadCardQueue = true;
            } else {
                // TODO: do not fetch new card if a non review operation has
                // been undone
                col.reset();
                newCard = getCard(sched);
            }
            // TODO: handle leech undoing properly
            publishProgress(new TaskData(newCard, 0));
            col.getDb().getDatabase().setTransactionSuccessful();
        } finally {
            col.getDb().getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundUndo - RuntimeException on undoing: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundUndo");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : Sched(com.ichi2.libanki.Sched) Collection(com.ichi2.libanki.Collection) Card(com.ichi2.libanki.Card)

Example 35 with Sched

use of com.ichi2.libanki.sched.Sched 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)

Aggregations

Card (com.ichi2.libanki.Card)44 Collection (com.ichi2.libanki.Collection)38 Test (org.junit.Test)35 RobolectricTest (com.ichi2.anki.RobolectricTest)28 Note (com.ichi2.libanki.Note)19 AbstractSched (com.ichi2.libanki.sched.AbstractSched)18 DeckConfig (com.ichi2.libanki.DeckConfig)11 JSONObject (com.ichi2.utils.JSONObject)9 Cursor (android.database.Cursor)7 Sched (com.ichi2.libanki.Sched)6 SchedV2 (com.ichi2.libanki.sched.SchedV2)6 JSONArray (com.ichi2.utils.JSONArray)6 SuppressLint (android.annotation.SuppressLint)4 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)4 DB (com.ichi2.libanki.DB)4 Model (com.ichi2.libanki.Model)4 Sched (com.ichi2.libanki.sched.Sched)4 ArrayList (java.util.ArrayList)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 ContentValues (android.content.ContentValues)3