Search in sources :

Example 11 with Model

use of com.ichi2.libanki.Model in project AnkiChinaAndroid by ankichinateam.

the class CardTemplateEditor method onDeckSelected.

/**
 * When a deck is selected via Deck Override
 */
@Override
public void onDeckSelected(@Nullable SelectableDeck deck) {
    if (Models.isCloze(getTempModel().getModel())) {
        Timber.w("Attempted to set deck for cloze model");
        UIUtils.showThemedToast(this, getString(R.string.model_manager_deck_override_cloze_error), true);
        return;
    }
    int ordinal = mViewPager.getCurrentItem();
    JSONObject template = getTempModel().getTemplate(ordinal);
    String templateName = template.getString("name");
    if (deck != null && Decks.isDynamic(getCol(), deck.getDeckId())) {
        Timber.w("Attempted to set default deck of %s to dynamic deck %s", templateName, deck.getName());
        UIUtils.showThemedToast(this, getString(R.string.model_manager_deck_override_dynamic_deck_error), true);
        return;
    }
    String message;
    if (deck == null) {
        Timber.i("Removing default template from template '%s'", templateName);
        template.put("did", null);
        message = getString(R.string.model_manager_deck_override_removed_message, templateName);
    } else {
        Timber.i("Setting template '%s' to '%s'", templateName, deck.getName());
        template.put("did", deck.getDeckId());
        message = getString(R.string.model_manager_deck_override_added_message, templateName, deck.getName());
    }
    UIUtils.showThemedToast(this, message, true);
    // Deck Override can change from "on" <-> "off"
    supportInvalidateOptionsMenu();
}
Also used : JSONObject(com.ichi2.utils.JSONObject)

Example 12 with Model

use of com.ichi2.libanki.Model in project AnkiChinaAndroid by ankichinateam.

the class CardTemplatePreviewer method getDummyCard.

/**
 * Get a dummy card
 */
@Nullable
protected Card getDummyCard(Model model, int ordinal) {
    Timber.d("getDummyCard() Creating dummy note for ordinal %s", ordinal);
    if (model == null) {
        return null;
    }
    Note n = getCol().newNote(model);
    ArrayList<String> fieldNames = Models.fieldNames(model);
    for (int i = 0; i < fieldNames.size() && i < n.getFields().length; i++) {
        n.setField(i, fieldNames.get(i));
    }
    try {
        JSONObject template = model.getJSONArray("tmpls").getJSONObject(ordinal);
        PreviewerCard card = (PreviewerCard) getCol().getNewLinkedCard(new PreviewerCard(getCol()), n, template, 1, 0, false);
        card.setNote(n);
        return card;
    } catch (Exception e) {
        Timber.e("getDummyCard() unable to create card");
    }
    return null;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Note(com.ichi2.libanki.Note) IOException(java.io.IOException) Nullable(androidx.annotation.Nullable)

Example 13 with Model

use of com.ichi2.libanki.Model in project AnkiChinaAndroid by ankichinateam.

the class NoteEditor method updateFieldsFromMap.

/**
 * Update all the field EditText views based on the currently selected note type and the mModelChangeFieldMap
 */
private void updateFieldsFromMap(Model newModel) {
    FieldChangeType type = FieldChangeType.refreshWithMap(newModel, mModelChangeFieldMap, shouldReplaceNewlines());
    populateEditFields(type, true);
    updateCards(newModel);
}
Also used : FieldChangeType(com.ichi2.anki.noteeditor.FieldState.FieldChangeType)

Example 14 with Model

use of com.ichi2.libanki.Model in project AnkiChinaAndroid by ankichinateam.

the class SelfStudyActivity method removeNotesView.

/**
 * Removes cards from view. Doesn't delete them in model (database).
 *
 * @param reorderCards Whether to rearrange the positions of checked items (DEFECT: Currently deselects all)
 */
private void removeNotesView(java.util.Collection<Long> cardsIds, boolean reorderCards) {
    long reviewerCardId = getReviewerCardId();
    List<CardCache> oldMCards = getCards();
    Map<Long, Integer> idToPos = getPositionMap(oldMCards);
    Set<Long> idToRemove = new HashSet<Long>();
    for (Long cardId : cardsIds) {
        if (cardId == reviewerCardId) {
            mReloadRequired = true;
        }
        if (idToPos.containsKey(cardId)) {
            idToRemove.add(cardId);
        }
    }
    List<CardCache> newMCards = new ArrayList<>();
    int pos = 0;
    for (CardCache card : oldMCards) {
        if (!idToRemove.contains(card.getId())) {
            newMCards.add(new CardCache(card, pos++));
        }
    }
    mCards = newMCards;
    if (reorderCards) {
        // Suboptimal from a UX perspective, we should reorder
        // but this is only hit on a rare sad path and we'd need to rejig the data structures to allow an efficient
        // search
        Timber.w("Removing current selection due to unexpected removal of cards");
    }
    updateList();
}
Also used : CardCache(com.ichi2.anki.CardBrowser.CardCache) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 15 with Model

use of com.ichi2.libanki.Model in project AnkiChinaAndroid by ankichinateam.

the class CardContentProvider method addModelToCursor.

private void addModelToCursor(Long modelId, Models models, MatrixCursor rv, String[] columns) {
    Model jsonObject = models.get(modelId);
    MatrixCursor.RowBuilder rb = rv.newRow();
    try {
        for (String column : columns) {
            if (column.equals(FlashCardsContract.Model._ID)) {
                rb.add(modelId);
            } else if (column.equals(FlashCardsContract.Model.NAME)) {
                rb.add(jsonObject.getString("name"));
            } else if (column.equals(FlashCardsContract.Model.FIELD_NAMES)) {
                JSONArray flds = jsonObject.getJSONArray("flds");
                String[] allFlds = new String[flds.length()];
                for (int idx = 0; idx < flds.length(); idx++) {
                    allFlds[idx] = flds.getJSONObject(idx).optString("name", "");
                }
                rb.add(Utils.joinFields(allFlds));
            } else if (column.equals(FlashCardsContract.Model.NUM_CARDS)) {
                rb.add(jsonObject.getJSONArray("tmpls").length());
            } else if (column.equals(FlashCardsContract.Model.CSS)) {
                rb.add(jsonObject.getString("css"));
            } else if (column.equals(FlashCardsContract.Model.DECK_ID)) {
                // #6378 - Anki Desktop changed schema temporarily to allow null
                rb.add(jsonObject.optLong("did", Consts.DEFAULT_DECK_ID));
            } else if (column.equals(FlashCardsContract.Model.SORT_FIELD_INDEX)) {
                rb.add(jsonObject.getLong("sortf"));
            } else if (column.equals(FlashCardsContract.Model.TYPE)) {
                rb.add(jsonObject.getLong("type"));
            } else if (column.equals(FlashCardsContract.Model.LATEX_POST)) {
                rb.add(jsonObject.getString("latexPost"));
            } else if (column.equals(FlashCardsContract.Model.LATEX_PRE)) {
                rb.add(jsonObject.getString("latexPre"));
            } else if (column.equals(FlashCardsContract.Model.NOTE_COUNT)) {
                rb.add(models.useCount(jsonObject));
            } else {
                throw new UnsupportedOperationException("Column \"" + column + "\" is unknown");
            }
        }
    } catch (JSONException e) {
        Timber.e(e, "Error parsing JSONArray");
        throw new IllegalArgumentException("Model " + modelId + " is malformed", e);
    }
}
Also used : Model(com.ichi2.libanki.Model) JSONArray(com.ichi2.utils.JSONArray) JSONException(com.ichi2.utils.JSONException) MatrixCursor(android.database.MatrixCursor)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)124 Model (com.ichi2.libanki.Model)95 Test (org.junit.Test)82 JSONArray (com.ichi2.utils.JSONArray)79 Collection (com.ichi2.libanki.Collection)53 ArrayList (java.util.ArrayList)48 Note (com.ichi2.libanki.Note)40 RobolectricTest (com.ichi2.anki.RobolectricTest)38 JSONException (com.ichi2.utils.JSONException)32 Intent (android.content.Intent)30 Card (com.ichi2.libanki.Card)27 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)26 HashMap (java.util.HashMap)22 Bundle (android.os.Bundle)20 NonNull (androidx.annotation.NonNull)20 SuppressLint (android.annotation.SuppressLint)16 View (android.view.View)16 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)15 IOException (java.io.IOException)15 Nullable (androidx.annotation.Nullable)14