From 207d43a74de4bbc2bd3a3c04fb688e988003b8e9 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Fri, 3 Jan 2025 22:36:42 +0100 Subject: [PATCH] NF: Remove some `JSONObject` Some of them could be replaced by more specific type, or just the type could be inferred from context --- .../java/com/ichi2/anki/CardTemplateEditor.kt | 2 +- .../java/com/ichi2/anki/CardTemplateNotetype.kt | 3 +-- .../src/main/java/com/ichi2/anki/NoteEditor.kt | 5 ++--- .../com/ichi2/anki/services/ReminderService.kt | 4 ++-- .../src/main/java/com/ichi2/libanki/Config.kt | 15 +++++++-------- .../src/main/java/com/ichi2/utils/JSONObject.kt | 2 +- .../com/ichi2/anki/CardTemplateNotetypeTest.kt | 4 ++-- 7 files changed, 16 insertions(+), 19 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt index 10a43046ed65..0ea93268d586 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt @@ -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() } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateNotetype.kt b/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateNotetype.kt index 2e6579087fe4..d03c1def563f 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateNotetype.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateNotetype.kt @@ -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 @@ -337,7 +336,7 @@ class CardTemplateNotetype( */ fun saveTempModel( context: Context, - tempModel: JSONObject, + tempModel: NotetypeJson, ): String? { Timber.d("saveTempModel() saving tempModel") var tempModelFile: File diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt index d745acf65cd5..dadf8ceea798 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt @@ -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 @@ -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 } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/services/ReminderService.kt b/AnkiDroid/src/main/java/com/ichi2/anki/services/ReminderService.kt index 2ad7bb66a2d0..424afa4c3fc9 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/services/ReminderService.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/services/ReminderService.kt @@ -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() { @@ -147,7 +147,7 @@ class ReminderService : BroadcastReceiver() { val decks: MutableList = 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) diff --git a/AnkiDroid/src/main/java/com/ichi2/libanki/Config.kt b/AnkiDroid/src/main/java/com/ichi2/libanki/Config.kt index 4f315ab95029..5760c6c4705d 100644 --- a/AnkiDroid/src/main/java/com/ichi2/libanki/Config.kt +++ b/AnkiDroid/src/main/java/com/ichi2/libanki/Config.kt @@ -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 + } } diff --git a/AnkiDroid/src/main/java/com/ichi2/utils/JSONObject.kt b/AnkiDroid/src/main/java/com/ichi2/utils/JSONObject.kt index a2d79e62d417..96bfb8d44a67 100644 --- a/AnkiDroid/src/main/java/com/ichi2/utils/JSONObject.kt +++ b/AnkiDroid/src/main/java/com/ichi2/utils/JSONObject.kt @@ -91,7 +91,7 @@ fun JSONObject.deepClonedInto(clone: T): T { * @return Exactly the same object, with a different type. */ -fun fromMap(map: Map): JSONObject = +fun fromMap(map: Map) = JSONObject().apply { map.forEach { (k, v) -> put(k, v) } } diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/CardTemplateNotetypeTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/CardTemplateNotetypeTest.kt index 2c79f612f096..ed361ccf4ea8 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/CardTemplateNotetypeTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/CardTemplateNotetypeTest.kt @@ -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())