use of com.ichi2.async.CollectionTask in project AnkiChinaAndroid by ankichinateam.
the class CollectionTask method doInBackgroundSaveModel.
/**
* Handles everything for a model change at once - template add / deletes as well as content updates
*/
private TaskData doInBackgroundSaveModel(TaskData param) {
Timber.d("doInBackgroundSaveModel");
Collection col = getCol();
Object[] args = param.getObjArray();
Model model = (Model) args[0];
ArrayList<Object[]> templateChanges = (ArrayList<Object[]>) args[1];
Model oldModel = col.getModels().get(model.getLong("id"));
// TODO need to save all the cards that will go away, for undo
// (do I need to remove them from graves during undo also?)
// - undo (except for cards) could just be Models.update(model) / Models.flush() / Collection.reset() (that was prior "undo")
JSONArray newTemplates = model.getJSONArray("tmpls");
col.getDb().getDatabase().beginTransaction();
try {
for (Object[] change : templateChanges) {
JSONArray oldTemplates = oldModel.getJSONArray("tmpls");
switch((TemporaryModel.ChangeType) change[1]) {
case ADD:
Timber.d("doInBackgroundSaveModel() adding template %s", change[0]);
try {
col.getModels().addTemplate(oldModel, newTemplates.getJSONObject((int) change[0]));
} catch (Exception e) {
Timber.e(e, "Unable to add template %s to model %s", change[0], model.getLong("id"));
return new TaskData(e.getLocalizedMessage(), false);
}
break;
case DELETE:
Timber.d("doInBackgroundSaveModel() deleting template currently at ordinal %s", change[0]);
try {
col.getModels().remTemplate(oldModel, oldTemplates.getJSONObject((int) change[0]));
} catch (Exception e) {
Timber.e(e, "Unable to delete template %s from model %s", change[0], model.getLong("id"));
return new TaskData(e.getLocalizedMessage(), false);
}
break;
default:
Timber.w("Unknown change type? %s", change[1]);
break;
}
}
col.getModels().save(model, true);
col.getModels().update(model);
col.reset();
col.save();
if (col.getDb().getDatabase().inTransaction()) {
col.getDb().getDatabase().setTransactionSuccessful();
} else {
Timber.i("CollectionTask::SaveModel was not in a transaction? Cannot mark transaction successful.");
}
} finally {
if (col.getDb().getDatabase().inTransaction()) {
col.getDb().getDatabase().endTransaction();
} else {
Timber.i("CollectionTask::SaveModel was not in a transaction? Cannot end transaction.");
}
}
return new TaskData(true);
}
use of com.ichi2.async.CollectionTask in project AnkiChinaAndroid by ankichinateam.
the class Sched method deckDueList.
/**
* Returns [deckname, did, rev, lrn, new]
*/
@Override
@Nullable
public List<DeckDueTreeNode> deckDueList(@Nullable CollectionTask collectionTask, long did) {
_checkDay();
mCol.getDecks().checkIntegrity();
ArrayList<Deck> decks = deckLimitWithParent(did, mCol);
// ArrayList<Deck> decks = mCol.getDecks().allSorted();
Timber.i("show decks" + did + " size:" + decks.size());
HashMap<String, Integer[]> lims = new HashMap<>();
ArrayList<DeckDueTreeNode> data = new ArrayList<>();
long time = SystemClock.elapsedRealtime();
for (Deck deck : decks) {
if (collectionTask != null && collectionTask.isCancelled()) {
return null;
}
String deckName = deck.getString("name");
String p = Decks.parent(deckName);
// new
int nlim = _deckNewLimitSingle(deck);
int rlim = _deckRevLimitSingle(deck);
if (!TextUtils.isEmpty(p)) {
Integer[] parentLims = lims.get(Decks.normalizeName(p));
// 'temporary for diagnosis of bug #6383'
Assert.that(parentLims != null, "Deck %s is supposed to have parent %s. It has not be found.", deckName, p);
nlim = Math.min(nlim, parentLims[0]);
// review
rlim = Math.min(rlim, parentLims[1]);
}
// long time = SystemClock.elapsedRealtime();
int _new = _newForDeck(deck.getLong("id"), nlim);
// Timber.i("cost time for new:"+(SystemClock.elapsedRealtime()-time));
// learning
int lrn = _lrnForDeck(deck.getLong("id"));
// Timber.i("cost time for lrn:"+(SystemClock.elapsedRealtime()-time));
// reviews
int rev = _revForDeck(deck.getLong("id"), rlim);
// Timber.i("cost time for rev:"+(SystemClock.elapsedRealtime()-time));
// save to list
double[] datas = did == ALL_DECKS_ID ? _getCardDataCount(deck.getLong("id")) : new double[] { 0, 0, 0 };
// Timber.i("cost time for datas:"+(SystemClock.elapsedRealtime()-time));
// Timber.i("add deck in tree:%s", deckName);
data.add(new DeckDueTreeNode(mCol, deck.getString("name"), deck.getLong("id"), rev, lrn, _new, datas));
// add deck as a parent
lims.put(Decks.normalizeName(deck.getString("name")), new Integer[] { nlim, rlim });
}
Timber.i("cost time for datas:" + (SystemClock.elapsedRealtime() - time));
return data;
}
use of com.ichi2.async.CollectionTask in project AnkiChinaAndroid by ankichinateam.
the class SchedV2 method deckDueList.
// Overridden
@Nullable
public List<DeckDueTreeNode> deckDueList(@Nullable CollectionTask collectionTask, long did) {
_checkDay();
mCol.getDecks().checkIntegrity();
// ArrayList<Deck> decks = mCol.getDecks().allSorted();
ArrayList<Deck> decks = deckLimitWithParent(did, mCol);
Timber.i("show decks" + did + " size:" + decks.size());
HashMap<String, Integer[]> lims = new HashMap<>();
ArrayList<DeckDueTreeNode> data = new ArrayList<>();
HashMap<Long, HashMap> childMap = mCol.getDecks().childMap();
for (Deck deck : decks) {
if (collectionTask != null && collectionTask.isCancelled()) {
return null;
}
String deckName = deck.getString("name");
String p = Decks.parent(deckName);
// new
int nlim = _deckNewLimitSingle(deck);
Integer plim = null;
if (!TextUtils.isEmpty(p)) {
Integer[] parentLims = lims.get(Decks.normalizeName(p));
// 'temporary for diagnosis of bug #6383'
Assert.that(parentLims != null, "Deck %s is supposed to have parent %s. It has not be found.", deckName, p);
nlim = Math.min(nlim, parentLims[0]);
// reviews
plim = parentLims[1];
}
int _new = _newForDeck(deck.getLong("id"), nlim);
// learning
int lrn = _lrnForDeck(deck.getLong("id"));
// reviews
int rlim = _deckRevLimitSingle(deck, plim);
int rev = _revForDeck(deck.getLong("id"), rlim, childMap);
double[] datas = did == ALL_DECKS_ID ? _getCardDataCount(deck.getLong("id")) : new double[] { 0, 0, 0 };
// save to list
// Timber.i("add deck in tree:%s", deckName);
data.add(new DeckDueTreeNode(mCol, deck.getString("name"), deck.getLong("id"), rev, lrn, _new, datas));
// add deck as a parent
lims.put(Decks.normalizeName(deck.getString("name")), new Integer[] { nlim, rlim });
}
return data;
}
use of com.ichi2.async.CollectionTask in project Anki-Android by ankidroid.
the class SchedV2 method deckDueList.
// Overridden
@Nullable
public List<DeckDueTreeNode> deckDueList(@Nullable CancelListener collectionTask) {
_checkDay();
mCol.getDecks().checkIntegrity();
List<Deck> decks = mCol.getDecks().allSorted();
HashMap<String, Integer[]> lims = HashUtil.HashMapInit(decks.size());
ArrayList<DeckDueTreeNode> deckNodes = new ArrayList<>(decks.size());
Decks.Node childMap = mCol.getDecks().childMap();
for (Deck deck : decks) {
if (isCancelled(collectionTask)) {
return null;
}
String deckName = deck.getString("name");
String p = Decks.parent(deckName);
// new
int nlim = _deckNewLimitSingle(deck, false);
Integer plim = null;
if (!TextUtils.isEmpty(p)) {
Integer[] parentLims = lims.get(Decks.normalizeName(p));
// 'temporary for diagnosis of bug #6383'
Assert.that(parentLims != null, "Deck %s is supposed to have parent %s. It has not be found.", deckName, p);
nlim = Math.min(nlim, parentLims[0]);
// reviews
plim = parentLims[1];
}
int _new = _newForDeck(deck.getLong("id"), nlim);
// learning
int lrn = _lrnForDeck(deck.getLong("id"));
// reviews
int rlim = _deckRevLimitSingle(deck, plim, false);
int rev = _revForDeck(deck.getLong("id"), rlim, childMap);
// save to list
deckNodes.add(new DeckDueTreeNode(mCol, deck.getString("name"), deck.getLong("id"), rev, lrn, _new));
// add deck as a parent
lims.put(Decks.normalizeName(deck.getString("name")), new Integer[] { nlim, rlim });
}
return deckNodes;
}
Aggregations