Skip to content

Commit

Permalink
Don't store collection reference in DeckTreeNode
Browse files Browse the repository at this point in the history
Pass it in to processChildren() instead, which is cleaner, and will
work better with the move to a backend method.
  • Loading branch information
dae committed Jun 25, 2022
1 parent 9f47447 commit b8ae330
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import java.util.*
* [processChildren] should be called if the children of this node are modified.
*/
abstract class AbstractDeckTreeNode(
val col: Collection,
/**
* @return The full deck name, e.g. "A::B::C"
*/
Expand Down Expand Up @@ -68,7 +67,7 @@ abstract class AbstractDeckTreeNode(
)
}

abstract fun processChildren(children: List<AbstractDeckTreeNode>, addRev: Boolean)
abstract fun processChildren(col: Collection, children: List<AbstractDeckTreeNode>, addRev: Boolean)

override fun toString(): String {
val buf = StringBuffer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import kotlin.math.min
*/
@KotlinCleanup("maybe possible to remove gettres for revCount/lrnCount")
@KotlinCleanup("rename name -> fullDeckName")
class DeckDueTreeNode(col: Collection, name: String, did: Long, override var revCount: Int, override var lrnCount: Int, override var newCount: Int) : AbstractDeckTreeNode(col, name, did) {
class DeckDueTreeNode(name: String, did: Long, override var revCount: Int, override var lrnCount: Int, override var newCount: Int) : AbstractDeckTreeNode(name, did) {
override fun toString(): String {
return String.format(
Locale.US, "%s, %d, %d, %d, %d",
Expand All @@ -50,7 +50,7 @@ class DeckDueTreeNode(col: Collection, name: String, did: Long, override var rev
newCount = max(0, min(newCount, limit))
}

override fun processChildren(children: List<AbstractDeckTreeNode>, addRev: Boolean) {
override fun processChildren(col: Collection, children: List<AbstractDeckTreeNode>, addRev: Boolean) {
// tally up children counts
for (ch in children) {
lrnCount += ch.lrnCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package com.ichi2.libanki.sched

import com.ichi2.libanki.Collection
import com.ichi2.utils.KotlinCleanup
import net.ankiweb.rsdroid.RustCleanup

@KotlinCleanup("confusing nullability for col, verify real nullability after code related to scheduling is fully migrated to kotlin")
class DeckTreeNode(col: Collection, name: String, did: Long) : AbstractDeckTreeNode(col, name, did) {
override fun processChildren(children: List<AbstractDeckTreeNode>, addRev: Boolean) {
@RustCleanup("processChildren() can be removed after migrating to backend implementation")
class DeckTreeNode(name: String, did: Long) : AbstractDeckTreeNode(name, did) {
override fun processChildren(col: Collection, children: List<AbstractDeckTreeNode>, addRev: Boolean) {
// intentionally blank
}
}
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/libanki/sched/Sched.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private void unburyCardsForDeck(@NonNull List<Long> allDecks) {
// reviews
int rev = _revForDeck(deck.getLong("id"), rlim);
// save to list
deckNodes.add(new DeckDueTreeNode(getCol(), deck.getString("name"), deck.getLong("id"), rev, lrn, _new));
deckNodes.add(new DeckDueTreeNode(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});
}
Expand Down
6 changes: 3 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/libanki/sched/SchedV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ protected int _walkingCount(@NonNull LimitMethod limFn, @NonNull CountMethod cnt
int rlim = _deckRevLimitSingle(deck, plim, false);
int rev = _revForDeck(deck.getLong("id"), rlim, childMap);
// save to list
deckNodes.add(new DeckDueTreeNode(getCol(), deck.getString("name"), deck.getLong("id"), rev, lrn, _new));
deckNodes.add(new DeckDueTreeNode(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});
}
Expand All @@ -580,7 +580,7 @@ protected int _walkingCount(@NonNull LimitMethod limFn, @NonNull CountMethod cnt
// Similar to deckDueList
ArrayList<DeckTreeNode> allDecksSorted = new ArrayList<>();
for (JSONObject deck : getCol().getDecks().allSorted()) {
DeckTreeNode g = new DeckTreeNode(getCol(), deck.getString("name"), deck.getLong("id"));
DeckTreeNode g = new DeckTreeNode(deck.getString("name"), deck.getLong("id"));
allDecksSorted.add(g);
}
// End of the similar part.
Expand Down Expand Up @@ -666,7 +666,7 @@ public List<TreeNode<DeckDueTreeNode>> deckDueTree(@Nullable CancelListener canc
TreeNode<T> toAdd = new TreeNode<>(child);
toAdd.getChildren().addAll(childrenNode);
List<T> childValues = childrenNode.stream().map(TreeNode::getValue).collect(Collectors.toList());
child.processChildren(childValues, "std".equals(getName()));
child.processChildren(mCol, childValues, "std".equals(getName()));

sortedChildren.add(toAdd);
}
Expand Down
28 changes: 14 additions & 14 deletions AnkiDroid/src/test/java/com/ichi2/libanki/sched/SchedV2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,16 @@ protected static List<TreeNode<DeckDueTreeNode>> expectedTree(Collection col, bo
// These matched the previous Java data
// These may want to be changed back
List<TreeNode<DeckDueTreeNode>> expected = new ArrayList<>();
DeckDueTreeNode caz = new DeckDueTreeNode(col, "cmxieunwoogyxsctnjmv::abcdefgh::ZYXW", 1596783600480L, 0, 0, 0);
DeckDueTreeNode ca = new DeckDueTreeNode(col, "cmxieunwoogyxsctnjmv::abcdefgh", 1596783600460L, 0, 0, 0);
DeckDueTreeNode ci = new DeckDueTreeNode(col, "cmxieunwoogyxsctnjmv::INSBGDS", 1596783600500L, 0, 0, 0);
DeckDueTreeNode c = new DeckDueTreeNode(col, "cmxieunwoogyxsctnjmv", 1596783600440L, 0, 0, 0);
DeckDueTreeNode defaul = new DeckDueTreeNode(col, "Default", 1, 0, 0, 0);
DeckDueTreeNode s = new DeckDueTreeNode(col, "scxipjiyozczaaczoawo", 1596783600420L, 0, 0, 0);
DeckDueTreeNode f = new DeckDueTreeNode(col, "blank::foobar", 1596783600540L, 0, 0, 0);
DeckDueTreeNode b = new DeckDueTreeNode(col, "blank", 1596783600520L, 0, 0, 0);
DeckDueTreeNode aBlank = new DeckDueTreeNode(col, "A::blank", 1596783600580L, 0, 0, 0);
DeckDueTreeNode a = new DeckDueTreeNode(col, "A", 1596783600560L, 0, 0, 0);
DeckDueTreeNode caz = new DeckDueTreeNode("cmxieunwoogyxsctnjmv::abcdefgh::ZYXW", 1596783600480L, 0, 0, 0);
DeckDueTreeNode ca = new DeckDueTreeNode("cmxieunwoogyxsctnjmv::abcdefgh", 1596783600460L, 0, 0, 0);
DeckDueTreeNode ci = new DeckDueTreeNode("cmxieunwoogyxsctnjmv::INSBGDS", 1596783600500L, 0, 0, 0);
DeckDueTreeNode c = new DeckDueTreeNode("cmxieunwoogyxsctnjmv", 1596783600440L, 0, 0, 0);
DeckDueTreeNode defaul = new DeckDueTreeNode("Default", 1, 0, 0, 0);
DeckDueTreeNode s = new DeckDueTreeNode("scxipjiyozczaaczoawo", 1596783600420L, 0, 0, 0);
DeckDueTreeNode f = new DeckDueTreeNode("blank::foobar", 1596783600540L, 0, 0, 0);
DeckDueTreeNode b = new DeckDueTreeNode("blank", 1596783600520L, 0, 0, 0);
DeckDueTreeNode aBlank = new DeckDueTreeNode("A::blank", 1596783600580L, 0, 0, 0);
DeckDueTreeNode a = new DeckDueTreeNode("A", 1596783600560L, 0, 0, 0);


TreeNode<DeckDueTreeNode> cazNode = new TreeNode<>(caz);
Expand All @@ -135,23 +135,23 @@ protected static List<TreeNode<DeckDueTreeNode>> expectedTree(Collection col, bo

// add "caz" to "ca"
caNode.getChildren().add(cazNode);
caNode.getValue().processChildren(Collections.singletonList(cazNode.getValue()), addRev);
caNode.getValue().processChildren(col, Collections.singletonList(cazNode.getValue()), addRev);

// add "ca" and "ci" to "c"
cNode.getChildren().add(caNode);
cNode.getChildren().add(ciNode);
ArrayList<DeckDueTreeNode> cChildren = new ArrayList<>();
cChildren.add(caNode.getValue());
cChildren.add(ciNode.getValue());
cNode.getValue().processChildren(cChildren, addRev);
cNode.getValue().processChildren(col, cChildren, addRev);

// add "f" to "b"
bNode.getChildren().add(fNode);
bNode.getValue().processChildren(Collections.singletonList(fNode.getValue()), addRev);
bNode.getValue().processChildren(col, Collections.singletonList(fNode.getValue()), addRev);

// add "A::" to "A"
aNode.getChildren().add(aBlankNode);
aNode.getValue().processChildren(Collections.singletonList(aBlankNode.getValue()), addRev);
aNode.getValue().processChildren(col, Collections.singletonList(aBlankNode.getValue()), addRev);

expected.add(aNode);
expected.add(bNode);
Expand Down

0 comments on commit b8ae330

Please sign in to comment.