use of com.ichi2.utils.JSONObject 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.utils.JSONObject in project Anki-Android by ankidroid.
the class NoteEditor method hasUnsavedChanges.
private boolean hasUnsavedChanges() {
if (!collectionHasLoaded()) {
return false;
}
// changed note type?
if (!mAddNote && mCurrentEditedCard != null) {
final JSONObject newModel = getCurrentlySelectedModel();
final JSONObject oldModel = mCurrentEditedCard.model();
if (!newModel.equals(oldModel)) {
return true;
}
}
// changed deck?
if (!mAddNote && mCurrentEditedCard != null && mCurrentEditedCard.getDid() != mCurrentDid) {
return true;
}
// changed fields?
if (mFieldEdited) {
return true;
}
// changed tags?
return mTagsEdited;
}
use of com.ichi2.utils.JSONObject 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.utils.JSONObject 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.utils.JSONObject 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