use of com.ichi2.libanki.Model in project Anki-Android by ankidroid.
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);
}
use of com.ichi2.libanki.Model in project Anki-Android by ankidroid.
the class TemporaryModel method fromBundle.
/**
* Load the TemporaryModel from the filename included in a Bundle
*
* @param bundle a Bundle that should contain persisted JSON under INTENT_MODEL_FILENAME key
* @return re-hydrated TemporaryModel or null if there was a problem, null means should reload from database
*/
@Nullable
public static TemporaryModel fromBundle(Bundle bundle) {
String editedModelFileName = bundle.getString(INTENT_MODEL_FILENAME);
// Bundle.getString is @Nullable, so we have to check.
if (editedModelFileName == null) {
Timber.d("fromBundle() - model file name under key %s", INTENT_MODEL_FILENAME);
return null;
}
Timber.d("onCreate() loading saved model file %s", editedModelFileName);
Model tempModelJSON;
try {
tempModelJSON = getTempModel((editedModelFileName));
} catch (IOException e) {
Timber.w(e, "Unable to load saved model file");
return null;
}
TemporaryModel model = new TemporaryModel(tempModelJSON);
model.loadTemplateChanges(bundle);
return model;
}
use of com.ichi2.libanki.Model 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;
}
use of com.ichi2.libanki.Model in project Anki-Android by ankidroid.
the class Collection method render_output_legacy.
@NonNull
@RustCleanup("Hack for Card Template Previewer, needs review")
public TemplateRenderOutput render_output_legacy(@NonNull Card c, boolean reload, boolean browser) {
Note f = c.note(reload);
Model m = c.model();
JSONObject t = c.template();
long did;
if (c.isInDynamicDeck()) {
did = c.getODid();
} else {
did = c.getDid();
}
HashMap<String, String> qa;
if (browser) {
String bqfmt = t.getString("bqfmt");
String bafmt = t.getString("bafmt");
qa = _renderQA(c.getId(), m, did, c.getOrd(), f.stringTags(), f.getFields(), c.internalGetFlags(), browser, bqfmt, bafmt);
} else {
qa = _renderQA(c.getId(), m, did, c.getOrd(), f.stringTags(), f.getFields(), c.internalGetFlags());
}
return new TemplateRenderOutput(qa.get("q"), qa.get("a"), null, null, c.model().getString("css"));
}
use of com.ichi2.libanki.Model in project Anki-Android by ankidroid.
the class Collection method _tmplsFromOrds.
/**
* @param model A note type
* @param avail Ords of cards from this note type.
* @return One template by element i of avail, for the i-th card. For standard template, avail should contains only existing ords.
* for cloze, avail should contains only non-negative numbers, and the i-th card is a copy of the first card, with a different ord.
*/
private ArrayList<JSONObject> _tmplsFromOrds(Model model, ArrayList<Integer> avail) {
JSONArray tmpls;
if (model.isStd()) {
tmpls = model.getJSONArray("tmpls");
ArrayList<JSONObject> ok = new ArrayList<>(avail.size());
for (Integer ord : avail) {
ok.add(tmpls.getJSONObject(ord));
}
return ok;
} else {
// cloze - generate temporary templates from first
JSONObject template0 = model.getJSONArray("tmpls").getJSONObject(0);
ArrayList<JSONObject> ok = new ArrayList<>(avail.size());
for (int ord : avail) {
JSONObject t = template0.deepClone();
t.put("ord", ord);
ok.add(t);
}
return ok;
}
}
Aggregations