Skip to content

Commit

Permalink
Use backend to generate deck list for V1 scheduler
Browse files Browse the repository at this point in the history
Currently limited to V1 due to changes in 2.1.41
  • Loading branch information
dae committed Jun 11, 2022
1 parent d9835cf commit 427e494
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.ichi2.libanki.backend

import BackendProto.Backend
import BackendProto.Backend.ExtractAVTagsOut
import BackendProto.Backend.RenderCardOut
import android.content.Context
Expand All @@ -26,6 +27,8 @@ import com.ichi2.libanki.Decks
import com.ichi2.libanki.TemplateManager.TemplateRenderContext
import com.ichi2.libanki.backend.exception.BackendNotSupportedException
import com.ichi2.libanki.backend.model.SchedTimingToday
import com.ichi2.libanki.sched.DeckDueTreeNode
import com.ichi2.libanki.sched.TreeNode
import com.ichi2.utils.KotlinCleanup
import net.ankiweb.rsdroid.RustV1Cleanup

Expand Down Expand Up @@ -84,4 +87,9 @@ interface DroidBackend {

@Throws(BackendNotSupportedException::class)
fun renderCardForTemplateManager(templateRenderContext: TemplateRenderContext): RenderCardOut

fun deckDueTree(includeCounts: Boolean): Backend.DeckTreeNode

@KotlinCleanup("move to SchedV2 once it's converted to Kotlin")
fun legacyDeckDueTree(includeCounts: Boolean): List<TreeNode<DeckDueTreeNode>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.ichi2.libanki.backend

import BackendProto.Backend
import BackendProto.Backend.ExtractAVTagsOut
import BackendProto.Backend.RenderCardOut
import android.content.Context
Expand All @@ -25,6 +26,8 @@ import com.ichi2.libanki.TemplateManager.TemplateRenderContext
import com.ichi2.libanki.backend.exception.BackendNotSupportedException
import com.ichi2.libanki.backend.model.SchedTimingToday
import com.ichi2.libanki.backend.model.SchedTimingTodayProto
import com.ichi2.libanki.sched.DeckDueTreeNode
import com.ichi2.libanki.sched.TreeNode
import net.ankiweb.rsdroid.BackendFactory
import net.ankiweb.rsdroid.database.RustV11SQLiteOpenHelperFactory

Expand Down Expand Up @@ -94,6 +97,14 @@ open class RustDroidBackend(
throw BackendNotSupportedException()
}

override fun deckDueTree(includeCounts: Boolean): Backend.DeckTreeNode {
TODO("Not yet implemented")
}

override fun legacyDeckDueTree(includeCounts: Boolean): List<TreeNode<DeckDueTreeNode>> {
TODO("Not yet implemented")
}

companion object {
const val UNUSED_VALUE = 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import com.ichi2.libanki.DB
import com.ichi2.libanki.TemplateManager
import com.ichi2.libanki.backend.BackendUtils.to_json_bytes
import com.ichi2.libanki.backend.model.to_backend_note
import com.ichi2.libanki.sched.DeckDueTreeNode
import com.ichi2.libanki.sched.TreeNode
import com.ichi2.libanki.utils.TimeManager
import com.ichi2.utils.JSONObject
import net.ankiweb.rsdroid.BackendFactory
import net.ankiweb.rsdroid.BackendV1
Expand Down Expand Up @@ -71,4 +74,36 @@ class RustDroidV16Backend(private val backendFactory: BackendFactory) : RustDroi
backend.renderExistingCard(templateRenderContext._card.id, templateRenderContext._browser)
}
}

override fun deckDueTree(includeCounts: Boolean): Backend.DeckTreeNode {
val now = if (includeCounts) {
TimeManager.time.intTime()
} else {
0 // counts are skipped
}
return backend.deckTree(now, 0)
}

override fun legacyDeckDueTree(includeCounts: Boolean): List<TreeNode<DeckDueTreeNode>> {
fun toLegacyNode(node: Backend.DeckTreeNode, parentName: String): TreeNode<DeckDueTreeNode> {
val thisName = if (parentName.isEmpty()) {
node.name
} else {
"$parentName::${node.name}"
}
val treeNode = TreeNode(
DeckDueTreeNode(
thisName,
node.deckId,
node.reviewCount,
node.learnCount,
node.newCount,
)
)
treeNode.children.addAll(node.childrenList.asSequence().map { toLegacyNode(it, thisName) })
return treeNode
}
val top = deckDueTree(includeCounts)
return toLegacyNode(top, "").children
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.ichi2.libanki.sched
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Decks
import com.ichi2.utils.KotlinCleanup
import net.ankiweb.rsdroid.RustCleanup
import java.util.*
import kotlin.math.max
import kotlin.math.min
Expand All @@ -34,6 +35,7 @@ import kotlin.math.min
*/
@KotlinCleanup("maybe possible to remove gettres for revCount/lrnCount")
@KotlinCleanup("rename name -> fullDeckName")
@RustCleanup("after migration, consider dropping this and using backend tree structure directly")
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(
Expand Down
11 changes: 11 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/libanki/sched/SchedV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.text.style.StyleSpan;
import android.util.Pair;

import com.ichi2.anki.AnkiDroidApp;
import com.ichi2.anki.R;
import com.ichi2.async.CancelListener;
import com.ichi2.async.CollectionTask;
Expand Down Expand Up @@ -593,7 +594,17 @@ protected int _walkingCount(@NonNull LimitMethod limFn, @NonNull CountMethod cnt
}

@Nullable
@RustCleanup("enable for v2 once backend is updated to 2.1.41+")
@RustCleanup("once both v1 and v2 are using backend, cancelListener can be removed")
public List<TreeNode<DeckDueTreeNode>> deckDueTree(@Nullable CancelListener cancelListener) {
if (AnkiDroidApp.TESTING_USE_V16_BACKEND && this instanceof Sched) {
// The 2.1.34 backend code can't be used for V2 at the moment, because
// the deck list count handling changed in 2.1.41, and test_review_limits
// expects the newer behaviour (which was already ported to AnkiDroid).
// So we only use the backend to calculate V1 at the moment.
return mCol.getBackend().legacyDeckDueTree(true);
}

List<DeckDueTreeNode> allDecksSorted = deckDueList(cancelListener);
if (allDecksSorted == null) {
return null;
Expand Down

0 comments on commit 427e494

Please sign in to comment.