Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NF: Remove some JSONObject #17710

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ open class CardTemplateEditor :
}

fun modelHasChanged(): Boolean {
val oldModel: JSONObject? = getColUnsafe.notetypes.get(modelId)
val oldModel: NotetypeJson? = getColUnsafe.notetypes.get(modelId)
return tempModel != null && tempModel!!.notetype.toString() != oldModel.toString()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import com.ichi2.libanki.Collection
import com.ichi2.libanki.NoteTypeId
import com.ichi2.libanki.NotetypeJson
import com.ichi2.utils.KotlinCleanup
import org.json.JSONObject
import timber.log.Timber
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -337,7 +336,7 @@ class CardTemplateNotetype(
*/
fun saveTempModel(
context: Context,
tempModel: JSONObject,
tempModel: NotetypeJson,
): String? {
Timber.d("saveTempModel() saving tempModel")
var tempModelFile: File
Expand Down
5 changes: 2 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import org.json.JSONArray
import org.json.JSONObject
import timber.log.Timber
import java.io.File
import java.util.LinkedList
Expand Down Expand Up @@ -1130,8 +1129,8 @@ class NoteEditor :

// changed note type?
if (!addNote && currentEditedCard != null) {
val newModel: JSONObject? = currentlySelectedNotetype
val oldModel: JSONObject = currentEditedCard!!.noteType(getColUnsafe)
val newModel = currentlySelectedNotetype
val oldModel = currentEditedCard!!.noteType(getColUnsafe)
if (newModel != oldModel) {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import com.ichi2.anki.CollectionManager
import com.ichi2.anki.IntentHandler
import com.ichi2.anki.R
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Deck
import com.ichi2.libanki.DeckConfigId
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.sched.DeckNode
import org.json.JSONObject
import timber.log.Timber

class ReminderService : BroadcastReceiver() {
Expand Down Expand Up @@ -147,7 +147,7 @@ class ReminderService : BroadcastReceiver() {
val decks: MutableList<DeckNode> = ArrayList(dues.size)
// This loop over top level deck only. No notification will ever occur for subdecks.
for (node in dues) {
val deck: JSONObject? = col.decks.get(node.did)
val deck: Deck? = col.decks.get(node.did)
// Dynamic deck has no "conf", so are not added here.
if (deck != null && deck.optLong("conf") == dConfId) {
decks.add(node)
Expand Down
15 changes: 7 additions & 8 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ class Config(
fun getObject(
key: String,
default: JSONObject,
): JSONObject =
try {
JSONObject(backend.getConfigJson(key).toStringUtf8())
} catch (ex: BackendNotFoundException) {
default
} catch (ex: JSONException) {
default
}
) = try {
JSONObject(backend.getConfigJson(key).toStringUtf8())
} catch (ex: BackendNotFoundException) {
default
} catch (ex: JSONException) {
default
}
}
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/utils/JSONObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fun <T : JSONObject> JSONObject.deepClonedInto(clone: T): T {
* @return Exactly the same object, with a different type.
*/

fun fromMap(map: Map<String, Any>): JSONObject =
fun fromMap(map: Map<String, Any>) =
JSONObject().apply {
map.forEach { (k, v) -> put(k, v) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class CardTemplateNotetypeTest : RobolectricTest() {
CardTemplateNotetype.clearTempModelFiles()

// Make sure save / retrieve works
val tempModelPath = CardTemplateNotetype.saveTempModel(targetContext, JSONObject("{\"foo\": \"bar\"}"))
val tempModelPath = CardTemplateNotetype.saveTempModel(targetContext, NotetypeJson("{\"foo\": \"bar\"}"))
assertNotNull("Saving temp model unsuccessful", tempModelPath)
val tempModel: JSONObject = CardTemplateNotetype.getTempModel(tempModelPath!!)
val tempModel = CardTemplateNotetype.getTempModel(tempModelPath!!)
assertNotNull("Temp model not read successfully", tempModel)
Assert.assertEquals(JSONObject("{\"foo\": \"bar\"}").toString(), tempModel.toString())

Expand Down
Loading