Skip to content

Commit f56924f

Browse files
criticalAYBrayanDSO
authored andcommitted
fix: API persist deck description on insert
1 parent 311ae33 commit f56924f

2 files changed

Lines changed: 49 additions & 12 deletions

File tree

AnkiDroid/src/androidTest/java/com/ichi2/anki/tests/ContentProviderTest.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1802,6 +1802,51 @@ class ContentProviderTest : InstrumentedTest() {
18021802
}
18031803
}
18041804

1805+
@Test
1806+
fun testInsertDeckWithDescription() {
1807+
val deckName = "test_deck_with_desc"
1808+
val deckDesc = "This is the deck description"
1809+
val values =
1810+
ContentValues().apply {
1811+
put(FlashCardsContract.Deck.DECK_NAME, deckName)
1812+
put(FlashCardsContract.Deck.DECK_DESC, deckDesc)
1813+
}
1814+
val newDeckUri = contentResolver.insert(FlashCardsContract.Deck.CONTENT_ALL_URI, values)
1815+
assertNotNull("Check that URI returned from insert is not null", newDeckUri)
1816+
val newDeckId = newDeckUri!!.lastPathSegment!!.toLong()
1817+
testDeckIds.add(newDeckId)
1818+
1819+
val reopenedCol = reopenCol()
1820+
val savedDeck = reopenedCol.decks.getLegacy(newDeckId)
1821+
assertNotNull("Check that the inserted deck exists", savedDeck)
1822+
assertEquals(
1823+
"Check that the deck description was persisted",
1824+
deckDesc,
1825+
savedDeck!!.description,
1826+
)
1827+
}
1828+
1829+
@Test
1830+
fun testInsertDeckWithoutDescription() {
1831+
val deckName = "test_deck_no_desc"
1832+
val values =
1833+
ContentValues().apply {
1834+
put(FlashCardsContract.Deck.DECK_NAME, deckName)
1835+
}
1836+
val newDeckUri = contentResolver.insert(FlashCardsContract.Deck.CONTENT_ALL_URI, values)
1837+
assertNotNull("Check that URI returned from insert is not null", newDeckUri)
1838+
val newDeckId = newDeckUri!!.lastPathSegment!!.toLong()
1839+
testDeckIds.add(newDeckId)
1840+
1841+
val savedDeck = col.decks.getLegacy(newDeckId)
1842+
assertNotNull("Check that the inserted deck exists", savedDeck)
1843+
assertEquals(
1844+
"Description should default to empty when not provided",
1845+
"",
1846+
savedDeck!!.description,
1847+
)
1848+
}
1849+
18051850
/**
18061851
* Test that query for the next card in the schedule returns a valid result without any deck selector
18071852
*/

AnkiDroid/src/main/java/com/ichi2/anki/provider/CardContentProvider.kt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,18 +1034,10 @@ class CardContentProvider : ContentProvider() {
10341034
throw IllegalArgumentException(filteredSubdeck.message)
10351035
}
10361036
val deck: Deck = col.decks.getLegacy(did)!!
1037-
@KotlinCleanup("remove the null check if deck is found to be not null in DeckManager.get(Long)")
1038-
@Suppress("SENSELESS_COMPARISON")
1039-
if (deck != null) {
1040-
try {
1041-
val deckDesc = values.getAsString(FlashCardsContract.Deck.DECK_DESC)
1042-
if (deckDesc != null) {
1043-
deck.put("desc", deckDesc)
1044-
}
1045-
} catch (e: JSONException) {
1046-
Timber.e(e, "Could not set a field of new deck %s", deckName)
1047-
return null
1048-
}
1037+
val deckDesc = values.getAsString(FlashCardsContract.Deck.DECK_DESC)
1038+
if (deckDesc != null) {
1039+
deck.description = deckDesc
1040+
col.decks.save(deck)
10491041
}
10501042
Uri.withAppendedPath(FlashCardsContract.Deck.CONTENT_ALL_URI, did.toString())
10511043
}

0 commit comments

Comments
 (0)