Skip to content

Commit

Permalink
chore: LibAnkiAlias annotation
Browse files Browse the repository at this point in the history
Helps developers matching a method in `libAnki` to `pylib`
  • Loading branch information
david-allison authored and lukstbit committed Mar 16, 2024
1 parent 23b507a commit 49eb38f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Decks(private val col: Collection) {
return deck.id
}

@LibAnkiAlias("add_deck_legacy")
private fun addDeckLegacy(deck: Deck): OpChangesWithId {
val changes = col.backend.addDeckLegacy(
json = BackendUtils.to_json_bytes(deck)
Expand All @@ -68,6 +69,7 @@ class Decks(private val col: Collection) {
}

/** A sorted sequence of deck names and IDs. */
@LibAnkiAlias("all_names_and_ids")
fun allNamesAndIds(
skipEmptyDefault: Boolean = false,
includeFiltered: Boolean = true
Expand All @@ -77,6 +79,7 @@ class Decks(private val col: Collection) {
}
}

@LibAnkiAlias("id_for_name")
fun idForName(name: String): DeckId? {
return try {
col.backend.getDeckIdByName(name)
Expand All @@ -93,6 +96,7 @@ class Decks(private val col: Collection) {
}
}

@LibAnkiAlias("new_deck_legacy")
private fun newDeckLegacy(filtered: Boolean): Deck {
val deck = BackendUtils.from_json_bytes(col.backend.newDeckLegacy(filtered))
return Deck(
Expand Down Expand Up @@ -123,6 +127,7 @@ class Decks(private val col: Collection) {
}

/** Get deck with NAME, ignoring case. */
@LibAnkiAlias("by_name")
fun byName(name: String): Deck? {
val id = this.idForName(name)
if (id != null) {
Expand Down Expand Up @@ -151,6 +156,7 @@ class Decks(private val col: Collection) {
/* Deck configurations */

/** A list of all deck config. */
@LibAnkiAlias("all_config")
fun allConfig(): List<DeckConfig> {
return BackendUtils.jsonToArray(col.backend.allDeckConfigLegacy())
.jsonObjectIterable()
Expand All @@ -159,6 +165,7 @@ class Decks(private val col: Collection) {
}

/** Falls back on default config if deck or config missing */
@LibAnkiAlias("config_dict_for_deck_id")
fun configDictForDeckId(did: DeckId): DeckConfig {
val conf = get(did)?.conf ?: 1
return DeckConfig(BackendUtils.from_json_bytes(col.backend.getDeckConfigLegacy(conf)))
Expand All @@ -168,6 +175,7 @@ class Decks(private val col: Collection) {
g.id = col.backend.addOrUpdateDeckConfigLegacy(g.toString().toByteStringUtf8())
}

@LibAnkiAlias("add_config")
private fun addConfig(
name: String
): DeckConfig {
Expand All @@ -181,15 +189,18 @@ class Decks(private val col: Collection) {
return DeckConfig(BackendUtils.from_json_bytes(col.backend.newDeckConfigLegacy()))
}

@LibAnkiAlias("set_config_id_for_deck_dict")
fun setConfigIdForDeckDict(grp: Deck, id: DeckConfigId) {
grp.conf = id
this.save(grp)
}

/* Reverts to default if provided id missing */
@LibAnkiAlias("get_config")
fun getConfig(confId: DeckConfigId): DeckConfig =
DeckConfig(BackendUtils.from_json_bytes(col.backend.getDeckConfigLegacy(confId)))

@LibAnkiAlias("add_config_returning_id")
fun addConfigReturningId(name: String): Long {
return addConfig(name).id
}
Expand All @@ -206,6 +217,7 @@ class Decks(private val col: Collection) {
}

/** @return The currently selected deck ID. */
@LibAnkiAlias("get_current_id")
fun getCurrentId(): DeckId = col.backend.getCurrentDeck().id

/** @return The currently selected deck ID. */
Expand All @@ -231,6 +243,7 @@ class Decks(private val col: Collection) {
*/

/** Return a new dynamic deck and set it as the current deck. */
@LibAnkiAlias("new_filtered")
fun newFiltered(name: String): DeckId {
val deck = this.newDeckLegacy(true)
deck.name = name
Expand All @@ -239,6 +252,7 @@ class Decks(private val col: Collection) {
return deck.id
}

@LibAnkiAlias("is_filtered")
fun isFiltered(did: DeckId): Boolean {
return this.get(did)?.isFiltered == true
}
Expand Down
25 changes: 25 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/libanki/utils/LibAnkiAlias.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2024 David Allison <davidallisongithub@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ichi2.libanki.utils

/**
* Specifies the name of the method in anki's pylib
*
* https://github.com/ankitects/anki/tree/main/pylib/anki
*/
@Retention(AnnotationRetention.SOURCE)
annotation class LibAnkiAlias(val alias: String)

0 comments on commit 49eb38f

Please sign in to comment.