Search in sources :

Example 1 with Function

use of com.ichi2.utils.FunctionalInterfaces.Function in project AnkiChinaAndroid by ankichinateam.

the class BasicTextFieldController method createCloneButton.

/**
 * @param layoutTools This creates a button, which will call a dialog, allowing to pick from another note's fields
 *            one, and use it's value in the current one.
 * @param p layout params
 */
private void createCloneButton(LinearLayout layoutTools, LayoutParams p) {
    // Makes sense only for two and more fields
    if (mNote.getNumberOfFields() > 1) {
        // Should be more than one text not empty fields for clone to make
        // sense
        mPossibleClones = new ArrayList<>();
        int numTextFields = 0;
        for (int i = 0; i < mNote.getNumberOfFields(); ++i) {
            // Sort out non text and empty fields
            IField curField = mNote.getField(i);
            if (curField == null) {
                continue;
            }
            if (curField.getType() != EFieldType.TEXT) {
                continue;
            }
            if (curField.getText() == null) {
                continue;
            }
            if (curField.getText().length() == 0) {
                continue;
            }
            // as well as the same field
            if (curField.getText().contentEquals(mField.getText())) {
                continue;
            }
            // collect clone sources
            mPossibleClones.add(curField.getText());
            ++numTextFields;
        }
        // Nothing to clone from
        if (numTextFields < 1) {
            return;
        }
        Button btnOtherField = new Button(mActivity);
        btnOtherField.setText(gtxt(R.string.multimedia_editor_text_field_editing_clone));
        layoutTools.addView(btnOtherField, p);
        final BasicTextFieldController controller = this;
        btnOtherField.setOnClickListener(v -> {
            PickStringDialogFragment fragment = new PickStringDialogFragment();
            fragment.setChoices(mPossibleClones);
            fragment.setOnclickListener(controller);
            fragment.setTitle(gtxt(R.string.multimedia_editor_text_field_editing_clone_source));
            fragment.show(mActivity.getSupportFragmentManager(), "pick.clone");
        // flow continues in the onClick function
        });
    }
}
Also used : Button(android.widget.Button) PickStringDialogFragment(com.ichi2.anki.multimediacard.activity.PickStringDialogFragment)

Example 2 with Function

use of com.ichi2.utils.FunctionalInterfaces.Function in project AnkiChinaAndroid by ankichinateam.

the class CollectionTask method doInBackgroundRenderBrowserQA.

private TaskData doInBackgroundRenderBrowserQA(TaskData param) {
    // TODO: Convert this to accept the following to make thread-safe:
    // (Range<Position>, Function<Position, BrowserCard>)
    Timber.d("doInBackgroundRenderBrowserQA");
    Collection col = getCol();
    List<CardBrowser.CardCache> cards = (List<CardBrowser.CardCache>) param.getObjArray()[0];
    Integer startPos = (Integer) param.getObjArray()[1];
    Integer n = (Integer) param.getObjArray()[2];
    int column1Index = (Integer) param.getObjArray()[3];
    int column2Index = (Integer) param.getObjArray()[4];
    List<Long> invalidCardIds = new ArrayList<>();
    // for each specified card in the browser list
    for (int i = startPos; i < startPos + n; i++) {
        // Stop if cancelled
        if (isCancelled()) {
            Timber.d("doInBackgroundRenderBrowserQA was aborted");
            return null;
        }
        if (i < 0 || i >= cards.size()) {
            continue;
        }
        CardBrowser.CardCache card;
        try {
            card = cards.get(i);
        } catch (IndexOutOfBoundsException e) {
            // we won't reach any more cards.
            continue;
        }
        if (card.isLoaded()) {
            // We've already rendered the answer, we don't need to do it again.
            continue;
        }
        // Extract card item
        try {
            // Ensure that card still exists.
            card.getCard();
        } catch (WrongId e) {
            // #5891 - card can be inconsistent between the deck browser screen and the collection.
            // Realistically, we can skip any exception as it's a rendering task which should not kill the
            // process
            long cardId = card.getId();
            Timber.e(e, "Could not process card '%d' - skipping and removing from sight", cardId);
            invalidCardIds.add(cardId);
            continue;
        }
        // Update item
        card.load(false, column1Index, column2Index);
        float progress = (float) i / n * 100;
        publishProgress(new TaskData((int) progress));
    }
    return new TaskData(new Object[] { cards, invalidCardIds });
}
Also used : ArrayList(java.util.ArrayList) CardBrowser(com.ichi2.anki.CardBrowser) Collection(com.ichi2.libanki.Collection) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) WrongId(com.ichi2.libanki.WrongId)

Example 3 with Function

use of com.ichi2.utils.FunctionalInterfaces.Function in project AnkiChinaAndroid by ankichinateam.

the class Sched method _updateCutoff.

/**
 * Daily cutoff ************************************************************* **********************************
 * This function uses GregorianCalendar so as to be sensitive to leap years, daylight savings, etc.
 */
@Override
public void _updateCutoff() {
    Integer oldToday = mToday;
    // days since col created
    mToday = (int) ((getTime().intTime() - mCol.getCrt()) / SECONDS_PER_DAY);
    // end of day cutoff
    mDayCutoff = mCol.getCrt() + ((mToday + 1) * SECONDS_PER_DAY);
    if (oldToday != mToday) {
        mCol.log(mToday, mDayCutoff);
    }
    // instead
    for (Deck deck : mCol.getDecks().all()) {
        update(deck);
    }
    // unbury if the day has rolled over
    int unburied = mCol.getConf().optInt("lastUnburied", 0);
    if (unburied < mToday) {
        SyncStatus.ignoreDatabaseModification(this::unburyCards);
    }
}
Also used : Deck(com.ichi2.libanki.Deck)

Example 4 with Function

use of com.ichi2.utils.FunctionalInterfaces.Function in project AnkiChinaAndroid by ankichinateam.

the class SchedV2 method _updateCutoff.

/**
 * Daily cutoff ************************************************************* **********************************
 * This function uses GregorianCalendar so as to be sensitive to leap years, daylight savings, etc.
 */
/* Overriden: other way to count time*/
public void _updateCutoff() {
    Integer oldToday = mToday == null ? 0 : mToday;
    // days since col created
    mToday = _daysSinceCreation();
    // end of day cutoff
    mDayCutoff = _dayCutoff();
    if (oldToday != mToday) {
        mCol.log(mToday, mDayCutoff);
    }
    // instead
    for (Deck deck : mCol.getDecks().all()) {
        update(deck);
    }
    // unbury if the day has rolled over
    int unburied = mCol.getConf().optInt("lastUnburied", 0);
    if (unburied < mToday) {
        SyncStatus.ignoreDatabaseModification(this::unburyCards);
        mCol.getConf().put("lastUnburied", mToday);
    }
}
Also used : Deck(com.ichi2.libanki.Deck)

Example 5 with Function

use of com.ichi2.utils.FunctionalInterfaces.Function in project Anki-Android by ankidroid.

the class Sched method _updateCutoff.

/**
 * Daily cutoff ************************************************************* **********************************
 * This function uses GregorianCalendar so as to be sensitive to leap years, daylight savings, etc.
 */
@Override
public void _updateCutoff() {
    Integer oldToday = mToday;
    // days since col created
    mToday = (int) ((getTime().intTime() - mCol.getCrt()) / SECONDS_PER_DAY);
    // end of day cutoff
    mDayCutoff = mCol.getCrt() + ((mToday + 1) * SECONDS_PER_DAY);
    if (!mToday.equals(oldToday)) {
        mCol.log(mToday, mDayCutoff);
    }
    // instead
    for (Deck deck : mCol.getDecks().all()) {
        update(deck);
    }
    // unbury if the day has rolled over
    int unburied = mCol.get_config("lastUnburied", 0);
    if (unburied < mToday) {
        SyncStatus.ignoreDatabaseModification(this::unburyCards);
    }
}
Also used : Deck(com.ichi2.libanki.Deck)

Aggregations

Deck (com.ichi2.libanki.Deck)4 IOException (java.io.IOException)4 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)3 JSONException (com.ichi2.utils.JSONException)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 SuppressLint (android.annotation.SuppressLint)2 SQLiteDatabaseLockedException (android.database.sqlite.SQLiteDatabaseLockedException)2 Button (android.widget.Button)2 PickStringDialogFragment (com.ichi2.anki.multimediacard.activity.PickStringDialogFragment)2 NoSuchDeckException (com.ichi2.libanki.exception.NoSuchDeckException)2 FunctionalInterfaces (com.ichi2.utils.FunctionalInterfaces)2 JSONObject (com.ichi2.utils.JSONObject)2 RustCleanup (net.ankiweb.rsdroid.RustCleanup)2 ContentResolver (android.content.ContentResolver)1 ContentValues (android.content.ContentValues)1 Context (android.content.Context)1 Resources (android.content.res.Resources)1 Cursor (android.database.Cursor)1