From e7c8164c42b1951d9a7842e130fecd1df0f25338 Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Wed, 1 May 2024 20:26:05 -0300 Subject: [PATCH] chore(libanki): parents() https://github.com/ankitects/anki/blob/4fc535ec8810c77838d49252ed83921ff99301a6/pylib/anki/decks.py#L482-L501 --- .../src/main/java/com/ichi2/libanki/Decks.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt index 3b9b35651f29..8f27967641f6 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt @@ -490,6 +490,31 @@ class Decks(private val col: Collection) { } } + /** All parents of did. */ + @LibAnkiAlias("parents") + fun parents(did: DeckId, nameMap: Map? = null): List { + // get parent and grandparent names + val parentsNames = mutableListOf() + 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() + for (parentName in parentsNames) { + val deck = if (nameMap != null) { + nameMap[parentName] + } else { + get(id(parentName)) + }!! + parents.add(deck) + } + return parents.toList() + } + /* * Filtered decks *************************************************************