diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/CreateDeckDialog.kt b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/CreateDeckDialog.kt index fe60d485cf42..8ffe81447dbe 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/CreateDeckDialog.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/CreateDeckDialog.kt @@ -194,7 +194,7 @@ class CreateDeckDialog( Timber.i("CreateDeckDialog::createNewDeck") val newDeckId = getColUnsafe.decks.id(deckName) Timber.d("Created deck '%s'; id: %d", deckName, newDeckId) - onNewDeckCreated(newDeckId) + onNewDeckCreated(newDeckId!!) } catch (filteredAncestor: BackendDeckIsFilteredException) { Timber.w(filteredAncestor) return false @@ -234,7 +234,7 @@ class CreateDeckDialog( try { val decks = getColUnsafe.decks val deckId = decks.id(previousDeckName!!) - decks.rename(decks.get(deckId)!!, newName) + decks.rename(decks.get(deckId!!)!!, newName) onNewDeckCreated(deckId) // 11668: Display feedback if a deck is renamed displayFeedback(context.getString(R.string.deck_renamed)) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt b/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt index a68514c2597d..8f35a3d13314 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt @@ -929,7 +929,7 @@ class CardContentProvider : ContentProvider() { throw IllegalArgumentException("Invalid deck name '$deckName'") } try { - did = col.decks.id(deckName) + did = col.decks.id(deckName)!! } catch (filteredSubdeck: BackendDeckIsFilteredException) { throw IllegalArgumentException(filteredSubdeck.message) } diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt index 57a520fa0b8e..41dac211e5f1 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt @@ -34,7 +34,6 @@ import anki.deck_config.UpdateDeckConfigsRequest import anki.decks.DeckTreeNode import anki.decks.FilteredDeckForUpdate import anki.decks.SetDeckCollapsedRequest -import anki.decks.deck import com.google.protobuf.kotlin.toByteStringUtf8 import com.ichi2.annotations.NeedsTest import com.ichi2.libanki.backend.BackendUtils @@ -52,6 +51,8 @@ import java.util.LinkedList typealias UpdateDeckConfigs = UpdateDeckConfigsRequest data class DeckNameId(val name: String, val id: DeckId) +const val DEFAULT_DECK_CONF_ID: DeckConfigId = 1L + // TODO: col was a weakref class Decks(private val col: Collection) { @@ -98,16 +99,18 @@ class Decks(private val col: Collection) { } /** "Add a deck with NAME. Reuse deck if already exists. Return id as int." */ - @RustCleanup("add 'create' parameter + change return type") - fun id(name: String): DeckId { - val id = this.idForName(name) + fun id(name: String, create: Boolean = true, type: DeckConfigId = 0L): DeckId? { + val id = idForName(name) if (id != null) { return id + } else if (!create) { + return null } - val deck = this.newDeckLegacy(false) + + val deck = this.newDeckLegacy(type != 0L) deck.name = name - addDeckLegacy(deck) - return deck.id + val out = addDeckLegacy(deck) + return out.id } fun remove(deckIds: Iterable): OpChangesWithCount { @@ -157,12 +160,11 @@ class Decks(private val col: Collection) { TODO() } - @RustCleanup("implement and make public") + /** "Return a new normal deck. It must be added with [addDeck] after a name assigned. */ @LibAnkiAlias("new_deck") @Suppress("unused") - /** "Return a new normal deck. It must be added with [addDeck] after a name assigned. */ - private fun newDeck(): Deck { - TODO() + fun newDeck(): anki.decks.Deck { + return col.backend.newDeck() } @RustCleanup("implement and make public") @@ -527,7 +529,7 @@ class Decks(private val col: Collection) { val deck = if (nameMap != null) { nameMap[parentName] } else { - get(id(parentName)) + get(id(parentName)!!) }!! parents.add(deck) } @@ -561,14 +563,12 @@ class Decks(private val col: Collection) { ************************************************************* */ - /** Return a new dynamic deck and set it as the current deck. */ + /** For new code, prefer [getOrCreateFilteredDeck] */ @LibAnkiAlias("new_filtered") fun newFiltered(name: String): DeckId { - val deck = this.newDeckLegacy(true) - deck.name = name - addDeckLegacy(deck) - this.select(deck.id) - return deck.id + val did = id(name, type = DEFAULT_DECK_CONF_ID) + select(did!!) + return did } @LibAnkiAlias("is_filtered")