Search in sources :

Example 21 with Template

use of com.ichi2.libanki.template.Template in project Anki-Android by ankidroid.

the class TokenizerTest method test_handlebar_token.

private void test_handlebar_token(@NonNull String template, @NonNull Tokenizer.TokenKind token, @NonNull String field_name, @NonNull String remaining) {
    IResult expected = new IResult(new Tokenizer.Token(token, field_name), remaining);
    assertThat(new_handlebar_token(template), is(expected));
    assertThat(handlebar_token(template, true), is(expected));
    assertThat(handlebar_token(template, false), is(expected));
    String legacy_template = new_to_legacy(template);
    IResult legacy_expected = expected.new_to_legacy();
    assertThat(legacy_handlebar_token(legacy_template), is(legacy_expected));
    assertThat(handlebar_token(legacy_template, true), is(legacy_expected));
    assertThat(handlebar_token(legacy_template, false), nullValue());
}
Also used : Token(com.ichi2.libanki.template.Tokenizer.Token) IResult(com.ichi2.libanki.template.Tokenizer.IResult)

Example 22 with Template

use of com.ichi2.libanki.template.Template in project Anki-Android by ankidroid.

the class CardTemplateEditor method onDeckSelected.

/**
 * When a deck is selected via Deck Override
 */
@Override
public void onDeckSelected(@Nullable SelectableDeck deck) {
    if (getTempModel().getModel().isCloze()) {
        Timber.w("Attempted to set deck for cloze model");
        UIUtils.showThemedToast(this, getString(R.string.multimedia_editor_something_wrong), 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.multimedia_editor_something_wrong), true);
        return;
    }
    String message;
    if (deck == null) {
        Timber.i("Removing default template from template '%s'", templateName);
        template.put("did", JSONObject.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 23 with Template

use of com.ichi2.libanki.template.Template in project Anki-Android by ankidroid.

the class CardTemplatePreviewerTest method testPreviewUnsavedTemplate_Basic.

@Test
public void testPreviewUnsavedTemplate_Basic() {
    String modelName = "Basic";
    Model collectionBasicModelOriginal = getCurrentDatabaseModelCopy(modelName);
    List<String> fields = collectionBasicModelOriginal.getFieldsNames();
    JSONObject template = collectionBasicModelOriginal.getJSONArray("tmpls").getJSONObject(0);
    template.put("qfmt", template.getString("qfmt") + "PREVIEWER_TEST");
    String tempModelPath = TemporaryModel.saveTempModel(getTargetContext(), collectionBasicModelOriginal);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.putExtra(TemporaryModel.INTENT_MODEL_FILENAME, tempModelPath);
    intent.putExtra("index", 0);
    ActivityController<TestCardTemplatePreviewer> previewerController = Robolectric.buildActivity(TestCardTemplatePreviewer.class, intent).create().start().resume().visible();
    saveControllerForCleanup((previewerController));
    TestCardTemplatePreviewer testCardTemplatePreviewer = previewerController.get();
    String[] arr = testCardTemplatePreviewer.getDummyCard(collectionBasicModelOriginal, 0).note().getFields();
    assertThat(arr[0], is("(" + fields.get(0) + ")"));
    assertThat(arr[1], is("(" + fields.get(1) + ")"));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) Intent(android.content.Intent) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 24 with Template

use of com.ichi2.libanki.template.Template in project Anki-Android by ankidroid.

the class NoteEditor method updateCards.

/**
 * Update the list of card templates for current note type
 */
private void updateCards(JSONObject model) {
    Timber.d("updateCards()");
    JSONArray tmpls = model.getJSONArray("tmpls");
    StringBuilder cardsList = new StringBuilder();
    // Build comma separated list of card names
    Timber.d("updateCards() template count is %s", tmpls.length());
    for (int i = 0; i < tmpls.length(); i++) {
        String name = tmpls.getJSONObject(i).optString("name");
        // If more than one card, and we have an existing card, underline existing card
        if (!mAddNote && tmpls.length() > 1 && model == mEditorNote.model() && mCurrentEditedCard != null && mCurrentEditedCard.template().optString("name").equals(name)) {
            name = "<u>" + name + "</u>";
        }
        cardsList.append(name);
        if (i < tmpls.length() - 1) {
            cardsList.append(", ");
        }
    }
    // Make cards list red if the number of cards is being reduced
    if (!mAddNote && tmpls.length() < mEditorNote.model().getJSONArray("tmpls").length()) {
        cardsList = new StringBuilder("<font color='red'>" + cardsList + "</font>");
    }
    mCardsButton.setText(HtmlCompat.fromHtml(getResources().getString(R.string.CardEditorCards, cardsList.toString()), HtmlCompat.FROM_HTML_MODE_LEGACY));
}
Also used : JSONArray(com.ichi2.utils.JSONArray) SuppressLint(android.annotation.SuppressLint)

Example 25 with Template

use of com.ichi2.libanki.template.Template in project Anki-Android by ankidroid.

the class Collection method deleteCardsWithInvalidModelOrdinals.

private ArrayList<String> deleteCardsWithInvalidModelOrdinals(Runnable notifyProgress, Model m) throws JSONException {
    Timber.d("deleteCardsWithInvalidModelOrdinals()");
    ArrayList<String> problems = new ArrayList<>(1);
    notifyProgress.run();
    if (m.isStd()) {
        JSONArray tmpls = m.getJSONArray("tmpls");
        ArrayList<Integer> ords = new ArrayList<>(tmpls.length());
        for (JSONObject tmpl : tmpls.jsonObjectIterable()) {
            ords.add(tmpl.getInt("ord"));
        }
        // cards with invalid ordinal
        ArrayList<Long> ids = mDb.queryLongList("SELECT id FROM cards WHERE ord NOT IN " + Utils.ids2str(ords) + " AND nid IN ( " + "SELECT id FROM notes WHERE mid = ?)", m.getLong("id"));
        if (!ids.isEmpty()) {
            problems.add("Deleted " + ids.size() + " card(s) with missing template.");
            remCards(ids);
        }
    }
    return problems;
}
Also used : JSONObject(com.ichi2.utils.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.ichi2.utils.JSONArray)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)53 Test (org.junit.Test)35 JSONArray (com.ichi2.utils.JSONArray)28 Model (com.ichi2.libanki.Model)24 ArrayList (java.util.ArrayList)16 Intent (android.content.Intent)14 RobolectricTest (com.ichi2.anki.RobolectricTest)14 Collection (com.ichi2.libanki.Collection)12 Note (com.ichi2.libanki.Note)12 SuppressLint (android.annotation.SuppressLint)9 HashMap (java.util.HashMap)8 Bundle (android.os.Bundle)7 JSONException (com.ichi2.utils.JSONException)7 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)6 Card (com.ichi2.libanki.Card)6 ShadowActivity (org.robolectric.shadows.ShadowActivity)6 ShadowIntent (org.robolectric.shadows.ShadowIntent)6 View (android.view.View)5 ContentResolver (android.content.ContentResolver)4 ContentValues (android.content.ContentValues)4