use of com.ichi2.anki.exception.ConfirmModSchemaException in project Anki-Android by ankidroid.
the class ModelTest method test_modelChange.
@Test
public void test_modelChange() throws ConfirmModSchemaException {
Collection col = getCol();
Model cloze = col.getModels().byName("Cloze");
// enable second template and add a note
Model basic = col.getModels().current();
ModelManager mm = col.getModels();
JSONObject t = Models.newTemplate("Reverse");
t.put("qfmt", "{{Back}}");
t.put("afmt", "{{Front}}");
mm.addTemplateModChanged(basic, t);
mm.save(basic);
Note note = col.newNote();
note.setItem("Front", "note");
note.setItem("Back", "b123");
col.addNote(note);
// switch fields
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
map.put(1, 0);
col.getModels().change(basic, note.getId(), basic, map, null);
note.load();
assertEquals("b123", note.getItem("Front"));
assertEquals("note", note.getItem("Back"));
// switch cards
Card c0 = note.cards().get(0);
Card c1 = note.cards().get(1);
assertThat(c0.q(), containsString("b123"));
assertThat(c1.q(), containsString("note"));
assertEquals(0, c0.getOrd());
assertEquals(1, c1.getOrd());
col.getModels().change(basic, note.getId(), basic, null, map);
note.load();
c0.load();
c1.load();
assertThat(c0.q(), containsString("note"));
assertThat(c1.q(), containsString("b123"));
assertEquals(1, c0.getOrd());
assertEquals(0, c1.getOrd());
// .cards() returns cards in order
assertEquals(c1.getId(), note.cards().get(0).getId());
// delete first card
map = new HashMap<>();
map.put(0, null);
map.put(1, 1);
// if (isWin) {
// // The low precision timer on Windows reveals a race condition
// time.sleep(0.05);
// }
col.getModels().change(basic, note.getId(), basic, null, map);
note.load();
c0.load();
// the card was deleted
// but we have two cards, as a new one was generated
assertEquals(2, note.numberOfCards());
// an unmapped field becomes blank
assertEquals("b123", note.getItem("Front"));
assertEquals("note", note.getItem("Back"));
col.getModels().change(basic, note.getId(), basic, map, null);
note.load();
assertEquals("", note.getItem("Front"));
assertEquals("note", note.getItem("Back"));
// another note to try model conversion
note = col.newNote();
note.setItem("Front", "f2");
note.setItem("Back", "b2");
col.addNote(note);
// counts = col.getModels().all_use_counts();
// Using older version of the test
assertEquals(2, col.getModels().useCount(basic));
assertEquals(0, col.getModels().useCount(cloze));
// Identity map
map = new HashMap<>();
map.put(0, 0);
map.put(1, 1);
col.getModels().change(basic, note.getId(), cloze, map, map);
note.load();
assertEquals("f2", note.getItem("Text"));
assertEquals(2, note.numberOfCards());
// back the other way, with deletion of second ord
col.getModels().remTemplate(basic, basic.getJSONArray("tmpls").getJSONObject(1));
assertEquals(2, col.getDb().queryScalar("select count() from cards where nid = ?", note.getId()));
map = new HashMap<>();
map.put(0, 0);
col.getModels().change(cloze, note.getId(), basic, map, map);
assertEquals(1, col.getDb().queryScalar("select count() from cards where nid = ?", note.getId()));
}
use of com.ichi2.anki.exception.ConfirmModSchemaException in project Anki-Android by ankidroid.
the class ModelTest method test_chained_mods.
@Test
public void test_chained_mods() throws ConfirmModSchemaException {
Collection col = getCol();
col.getModels().setCurrent(col.getModels().byName("Cloze"));
Model m = col.getModels().current();
ModelManager mm = col.getModels();
// We replace the default Cloze template
JSONObject t = Models.newTemplate("ChainedCloze");
t.put("qfmt", "{{cloze:text:Text}}");
t.put("afmt", "{{cloze:text:Text}}");
mm.addTemplateModChanged(m, t);
mm.save(m);
col.getModels().remTemplate(m, m.getJSONArray("tmpls").getJSONObject(0));
Note note = col.newNote();
String q1 = "<span style=\"color:red\">phrase</span>";
String a1 = "<b>sentence</b>";
String q2 = "<span style=\"color:red\">en chaine</span>";
String a2 = "<i>chained</i>";
note.setItem("Text", "This {{c1::" + q1 + "::" + a1 + "}} demonstrates {{c1::" + q2 + "::" + a2 + "}} clozes.");
assertEquals(1, col.addNote(note));
String question = note.cards().get(0).q();
/* TODO: chained modifier
assertThat("Question «"+question+"» does not contain the expected string", question, containsString("This <span class=cloze>[sentence]</span> demonstrates <span class=cloze>[chained]</span> clozes.")
);
assertThat(note.cards().get(0).a(), containsString("This <span class=cloze>phrase</span> demonstrates <span class=cloze>en chaine</span> clozes."
));
*/
}
use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.
the class ModelBrowser method deleteModel.
/*
* Deletes the currently selected model
*/
private void deleteModel() throws ConfirmModSchemaException {
CollectionTask.launchCollectionTask(DELETE_MODEL, deleteModelHandler(), new TaskData(mCurrentID));
mModels.remove(mModelListPosition);
mModelIds.remove(mModelListPosition);
mModelDisplayList.remove(mModelListPosition);
mCardCounts.remove(mModelListPosition);
refreshList();
}
use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.
the class ModelFieldEditor method sortByField.
/*
* Changes the sort field (that displays in card browser) to the current field
*/
private void sortByField() {
changeHandler listener = changeFieldHandler();
try {
mCol.modSchema();
CollectionTask.launchCollectionTask(CHANGE_SORT_FIELD, listener, new TaskData(new Object[] { mMod, mCurrentPos }));
} catch (ConfirmModSchemaException e) {
// Handler mMod schema confirmation
ConfirmationDialog c = new ConfirmationDialog();
c.setArgs(getResources().getString(R.string.full_sync_confirmation));
Runnable confirm = () -> {
mCol.modSchemaNoCheck();
CollectionTask.launchCollectionTask(CHANGE_SORT_FIELD, listener, new TaskData(new Object[] { mMod, mCurrentPos }));
dismissContextMenu();
};
c.setConfirm(confirm);
c.setCancel(mConfirmDialogCancel);
ModelFieldEditor.this.showDialogFragment(c);
}
}
use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.
the class ModelFieldEditor method renameFieldDialog.
/*
* Creates a dialog to rename the currently selected field
* Processing time is constant
*/
private void renameFieldDialog() {
mFieldNameInput = new EditText(this);
mFieldNameInput.setSingleLine(true);
mFieldNameInput.setText(mFieldLabels.get(mCurrentPos));
mFieldNameInput.setSelection(mFieldNameInput.getText().length());
new MaterialDialog.Builder(this).title(R.string.rename_model).positiveText(R.string.rename).customView(mFieldNameInput, true).onPositive((dialog, which) -> {
String fieldLabel = mFieldNameInput.getText().toString().replaceAll("[\\n\\r]", "");
if (fieldLabel.length() == 0) {
UIUtils.showThemedToast(this, getResources().getString(R.string.toast_empty_name), true);
} else if (containsField(fieldLabel)) {
UIUtils.showThemedToast(this, getResources().getString(R.string.toast_duplicate_field), true);
} else {
// Field is valid, now rename
try {
renameField();
} catch (ConfirmModSchemaException e) {
// Handler mod schema confirmation
ConfirmationDialog c = new ConfirmationDialog();
c.setArgs(getResources().getString(R.string.full_sync_confirmation));
Runnable confirm = () -> {
mCol.modSchemaNoCheck();
try {
renameField();
} catch (ConfirmModSchemaException e1) {
// This should never be thrown
}
dismissContextMenu();
};
c.setConfirm(confirm);
c.setCancel(mConfirmDialogCancel);
ModelFieldEditor.this.showDialogFragment(c);
}
}
}).negativeText(R.string.dialog_cancel).show();
}
Aggregations