Search in sources :

Example 1 with Sched

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

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

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

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

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

Aggregations

Sched (com.ichi2.libanki.Sched)6 Card (com.ichi2.libanki.Card)5 Collection (com.ichi2.libanki.Collection)3 Note (com.ichi2.libanki.Note)3 AnkiDb (com.ichi2.anki.AnkiDb)2 JSONObject (org.json.JSONObject)1