Skip to content

Commit

Permalink
chore(libanki): parents()
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed May 1, 2024
1 parent 8a83534 commit e7c8164
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,31 @@ class Decks(private val col: Collection) {
}
}

/** All parents of did. */
@LibAnkiAlias("parents")
fun parents(did: DeckId, nameMap: Map<String, Deck>? = null): List<Deck> {
// get parent and grandparent names
val parentsNames = mutableListOf<String>()
for (part in immediateParentPath(get(did)!!.name)) {
if (parentsNames.isEmpty()) {
parentsNames.add(part)
} else {
parentsNames.append("${parentsNames.last()}::$part")
}
}
// convert to objects
val parents = mutableListOf<Deck>()
for (parentName in parentsNames) {
val deck = if (nameMap != null) {
nameMap[parentName]
} else {
get(id(parentName))
}!!
parents.add(deck)
}
return parents.toList()
}

/*
* Filtered decks
*************************************************************
Expand Down

0 comments on commit e7c8164

Please sign in to comment.