0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 03:52:15 +02:00

fix(model): generate req field for old client compatibility

req is unused in current clients but that is new enough we'll keep it for
compatibility for now

Fixes #8945
This commit is contained in:
Mike Hardy 2021-05-25 15:57:43 -05:00
parent 4c6044c13a
commit ca2aab0f8e

View File

@ -201,6 +201,13 @@ public class Models {
m.put("mod", mCol.getTime().intTime());
m.put("usn", mCol.usn());
// TODO: fix empty id problem on _updaterequired (needed for model adding)
if (!isModelNew(m)) {
// this fills in the `req` chunk of the model. Not used on AnkiDroid 2.15+ or AnkiDesktop 2.1.x
// Included only for backwards compatibility (to AnkiDroid <2.14 etc)
// https://forums.ankiweb.net/t/is-req-still-used-or-present/9977
// https://github.com/ankidroid/Anki-Android/issues/8945
_updateRequired(m);
}
if (templates) {
_syncTemplates(m);
}
@ -958,6 +965,25 @@ public class Models {
* ***********************************************************************************************
*/
private void _updateRequired(Model m) {
if (m.isCloze()) {
// nothing to do
return;
}
JSONArray req = new JSONArray();
List<String> flds = m.getFieldsNames();
JSONArray templates = m.getJSONArray("tmpls");
for (JSONObject t: templates.jsonObjectIterable()) {
Object[] ret = _reqForTemplate(m, flds, t);
JSONArray r = new JSONArray();
r.put(t.getInt("ord"));
r.put(ret[0]);
r.put(ret[1]);
req.put(r);
}
m.put("req", req);
}
@SuppressWarnings("PMD.UnusedLocalVariable") // 'String f' is unused upstream as well
private Object[] _reqForTemplate(Model m, List<String> flds, JSONObject t) {
int nbFields = flds.size();