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());
}
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();
}
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) + ")"));
}
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));
}
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;
}
Aggregations