Skip to content

Commit

Permalink
update libanki.Decks methods
Browse files Browse the repository at this point in the history
issue 15611
  • Loading branch information
RobozinhoD committed Oct 11, 2024
1 parent cfe2b2c commit 175da0a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
36 changes: 18 additions & 18 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Decks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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<Long>): OpChangesWithCount {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 175da0a

Please sign in to comment.