Skip to content

Commit ecfe4b1

Browse files
1 parent e1bd644 commit ecfe4b1

File tree

1 file changed

+33
-17
lines changed
  • libanki/src/main/java/com/ichi2/anki/libanki

1 file changed

+33
-17
lines changed

libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -289,19 +289,29 @@ class Decks(
289289
return col.db.queryScalar("select count() from cards where did in $strIds or odid in $strIds")
290290
}
291291

292-
@RustCleanup("implement and make public")
293292
@LibAnkiAlias("get")
294-
@Suppress("unused", "unused_parameter")
293+
@Suppress("unused", "unused_parameter", "LiftReturnOrAssignment")
295294
@CheckResult
296-
private fun get(
297-
did: DeckId,
295+
fun get(
296+
did: DeckId?,
298297
default: Boolean = true,
299-
): Deck? =
300-
try {
301-
Deck(BackendUtils.fromJsonBytes(col.backend.getDeckLegacy(did)))
302-
} catch (ex: BackendNotFoundException) {
303-
null
298+
): Deck? {
299+
if (did == null) {
300+
if (default) {
301+
return getLegacy(Consts.DEFAULT_DECK_ID)
302+
} else {
303+
return null
304+
}
305+
}
306+
val deck = getLegacy(did)
307+
if (deck != null) {
308+
return deck
309+
} else if (default) {
310+
return getLegacy(Consts.DEFAULT_DECK_ID)
311+
} else {
312+
return null
304313
}
314+
}
305315

306316
/** Get deck with NAME, ignoring case. */
307317
@LibAnkiAlias("by_name")
@@ -333,15 +343,18 @@ class Decks(
333343
fun updateDict(deck: Deck): OpChanges = col.backend.updateDeckLegacy(toJsonBytes(deck))
334344

335345
/** Rename deck prefix to NAME if not exists. Updates children. */
336-
@RustCleanup("return OpChanges")
337346
@LibAnkiAlias("rename")
338347
fun rename(
339348
deck: Deck,
340349
newName: String,
341-
) {
342-
deck.name = newName
343-
this.save(deck)
344-
}
350+
) = col.backend.renameDeck(deckId = deck.id, newName = newName)
351+
352+
/** Rename deck prefix to NAME if not exists. Updates children. */
353+
@LibAnkiAlias("rename")
354+
fun rename(
355+
deckId: DeckId,
356+
newName: String,
357+
) = col.backend.renameDeck(deckId = deckId, newName = newName)
345358

346359
/*
347360
* Drag/drop
@@ -491,16 +504,19 @@ class Decks(
491504
*************************************************************
492505
*/
493506

494-
/** Returns the [deck name][Deck.name], or 'no deck' if not found */
507+
/** Returns the [deck name][Deck.name], or 'no deck' if not found and default is set to false */
495508
@LibAnkiAlias("name")
496509
@CheckResult
497-
fun name(did: DeckId): String = getLegacy(did)?.name ?: col.backend.tr.decksNoDeck()
510+
fun name(
511+
did: DeckId,
512+
default: Boolean = false,
513+
): String = get(did, default = default)?.name ?: col.backend.tr.decksNoDeck()
498514

499515
/** Returns the [deck name][Deck.name], or `null` if not found */
500516
@LibAnkiAlias("name_if_exists")
501517
@Suppress("unused")
502518
@CheckResult
503-
fun nameIfExists(did: DeckId): String? = getLegacy(did)?.name
519+
fun nameIfExists(did: DeckId): String? = get(did, default = false)?.name
504520

505521
@RustCleanup("implement and make public")
506522
@Suppress("unused", "unused_parameter")

0 commit comments

Comments
 (0)