Skip to content

Commit

Permalink
NF: improve the readability of some strings
Browse files Browse the repository at this point in the history
Specifically, the ones that represents json, sql or js.
I found they used to be hard to read, and I expect to lower this issue.
  • Loading branch information
Arthur-Milchior authored and lukstbit committed Dec 14, 2024
1 parent abcfcce commit 47f6c8b
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class NoteEditorTabOrderTest : NoteEditorTest() {
@Ignore(
"""flaky on API 21 as well: com.ichi2.anki.NoteEditorTabOrderTest > testTabOrder[test(AVD) - 5.1.1] FAILED
java.lang.AssertionError:
java.lang.AssertionError:
Expected: is "a"""",
Expected: is "a"""",
)
@Throws(Throwable::class)
fun testTabOrder() {
Expand Down
17 changes: 11 additions & 6 deletions AnkiDroid/src/main/java/com/ichi2/anki/Info.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,17 @@ class Info :
*/
@Suppress("ktlint:standard:max-line-length")
webView.loadUrl(
"javascript:document.body.style.setProperty(\"color\", \"" + textColor + "\");" +
"x=document.getElementsByTagName(\"a\"); for(i=0;i<x.length;i++){x[i].style.color=\"" + anchorTextColor +
"\";}" +
"document.getElementsByTagName(\"h1\")[0].style.color=\"" + textColor + "\";" +
"x=document.getElementsByTagName(\"h2\"); for(i=0;i<x.length;i++){x[i].style.color=\"#E37068\";}" +
"document.body.style.setProperty(\"background\", \"" + background + "\");",
"""javascript:document.body.style.setProperty("color", "$textColor");
x=document.getElementsByTagName("a");
for(i=0; i<x.length; i++){
x[i].style.color="$anchorTextColor";
}
document.getElementsByTagName("h1")[0].style.color="$textColor";
x=document.getElementsByTagName("h2");
for(i=0; i<x.length; i++){
x[i].style.color="#E37068";
}
document.body.style.setProperty("background", "$background");""",
)
}

Expand Down
55 changes: 38 additions & 17 deletions AnkiDroid/src/main/java/com/ichi2/anki/MetaDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,27 @@ object MetaDB {

// Create tables if not exist
metaDb.execSQL(
"CREATE TABLE IF NOT EXISTS languages (" + " _id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"did INTEGER NOT NULL, ord INTEGER, " + "qa INTEGER, " + "language TEXT)",
"""CREATE TABLE IF NOT EXISTS languages (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
did INTEGER NOT NULL,
ord INTEGER,
qa INTEGER,
language TEXT
)""",
)
metaDb.execSQL(
"CREATE TABLE IF NOT EXISTS smallWidgetStatus (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"due INTEGER NOT NULL, eta INTEGER NOT NULL)",
"""CREATE TABLE IF NOT EXISTS smallWidgetStatus (
id INTEGER PRIMARY KEY AUTOINCREMENT,
due INTEGER NOT NULL,
eta INTEGER NOT NULL
)""",
)
metaDb.execSQL(
"CREATE TABLE IF NOT EXISTS micToolbarState (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"did INTEGER NOT NULL, state INTEGER NOT NULL)",
"""CREATE TABLE IF NOT EXISTS micToolbarState (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
did INTEGER NOT NULL,
state INTEGER NOT NULL
)""",
)

updateWidgetStatus(metaDb)
Expand All @@ -94,8 +105,14 @@ object MetaDB {
val columnCount = DatabaseUtil.getTableColumnCount(metaDb, "whiteboardState")
if (columnCount <= 0) {
metaDb.execSQL(
"CREATE TABLE IF NOT EXISTS whiteboardState (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"did INTEGER NOT NULL, state INTEGER, visible INTEGER, lightpencolor INTEGER, darkpencolor INTEGER, stylus INTEGER)",
"""CREATE TABLE IF NOT EXISTS whiteboardState (
_id INTEGER PRIMARY KEY AUTOINCREMENT,
did INTEGER NOT NULL, state INTEGER,
visible INTEGER,
lightpencolor INTEGER,
darkpencolor INTEGER,
stylus INTEGER
)""",
)
return
}
Expand All @@ -120,14 +137,20 @@ object MetaDB {
val columnCount = DatabaseUtil.getTableColumnCount(metaDb, "widgetStatus")
if (columnCount > 0) {
if (columnCount < 7) {
metaDb.execSQL("ALTER TABLE widgetStatus " + "ADD COLUMN eta INTEGER NOT NULL DEFAULT '0'")
metaDb.execSQL("ALTER TABLE widgetStatus " + "ADD COLUMN time INTEGER NOT NULL DEFAULT '0'")
metaDb.execSQL("ALTER TABLE widgetStatus ADD COLUMN eta INTEGER NOT NULL DEFAULT '0'")
metaDb.execSQL("ALTER TABLE widgetStatus ADD COLUMN time INTEGER NOT NULL DEFAULT '0'")
}
} else {
metaDb.execSQL(
"CREATE TABLE IF NOT EXISTS widgetStatus (" + "deckId INTEGER NOT NULL PRIMARY KEY, " +
"deckName TEXT NOT NULL, " + "newCards INTEGER NOT NULL, " + "lrnCards INTEGER NOT NULL, " +
"dueCards INTEGER NOT NULL, " + "progress INTEGER NOT NULL, " + "eta INTEGER NOT NULL)",
"""CREATE TABLE IF NOT EXISTS widgetStatus (
deckId INTEGER NOT NULL PRIMARY KEY,
deckName TEXT NOT NULL,
newCards INTEGER NOT NULL,
lrnCards INTEGER NOT NULL,
dueCards INTEGER NOT NULL,
progress INTEGER NOT NULL,
eta INTEGER NOT NULL
)""",
)
}
}
Expand Down Expand Up @@ -225,7 +248,7 @@ object MetaDB {
try {
if ("" == getLanguage(context, did, ord, qa)) {
mMetaDb!!.execSQL(
"INSERT INTO languages (did, ord, qa, language) " + " VALUES (?, ?, ?, ?);",
"INSERT INTO languages (did, ord, qa, language) VALUES (?, ?, ?, ?);",
arrayOf<Any>(
did,
ord,
Expand Down Expand Up @@ -520,9 +543,7 @@ object MetaDB {
).use { cur ->
if (cur.moveToNext()) {
metaDb.execSQL(
"UPDATE whiteboardState SET did = ?, " +
columnName + "= ? " +
" WHERE _id=?;",
"UPDATE whiteboardState SET did = ?, $columnName= ? WHERE _id=?;",
arrayOf<Any?>(did, value, cur.getString(0)),
)
} else {
Expand Down
7 changes: 4 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ import com.ichi2.annotations.NeedsTest
import com.ichi2.libanki.Card
import com.ichi2.libanki.CardId
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
import com.ichi2.libanki.Consts.QUEUE_TYPE_NEW
import com.ichi2.libanki.Consts.QUEUE_TYPE_SUSPENDED
import com.ichi2.libanki.sched.Counts
import com.ichi2.libanki.sched.CurrentQueueState
import com.ichi2.libanki.undoableOp
Expand Down Expand Up @@ -1617,7 +1618,7 @@ open class Reviewer :
false
} else {
getColUnsafe.db.queryScalar(
"select 1 from cards where nid = ? and id != ? and queue != " + Consts.QUEUE_TYPE_SUSPENDED + " limit 1",
"select 1 from cards where nid = ? and id != ? and queue != $QUEUE_TYPE_SUSPENDED limit 1",
currentCard!!.nid,
currentCard!!.id,
) == 1
Expand All @@ -1631,7 +1632,7 @@ open class Reviewer :
false
} else {
getColUnsafe.db.queryScalar(
"select 1 from cards where nid = ? and id != ? and queue >= " + Consts.QUEUE_TYPE_NEW + " limit 1",
"select 1 from cards where nid = ? and id != ? and queue >= $QUEUE_TYPE_NEW limit 1",
currentCard!!.nid,
currentCard!!.id,
) == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ class CustomStudyDialog(
createCustomStudySession(
JSONArray(),
arrayOf(
"is:new added:" +
n,
"is:new added:$n",
Consts.DYN_MAX_SIZE,
Consts.DYN_OLDEST,
),
Expand Down Expand Up @@ -489,7 +488,7 @@ class CustomStudyDialog(
dyn.put("delays", JSONObject.NULL)
}
val ar = dyn.getJSONArray("terms")
ar.getJSONArray(0).put(0, "deck:\"" + deckToStudyName + "\" " + terms[0])
ar.getJSONArray(0).put(0, """deck:"$deckToStudyName" terms[0]""")
ar.getJSONArray(0).put(1, terms[1])
@DynPriority val priority = terms[2] as Int
ar.getJSONArray(0).put(2, priority)
Expand Down
19 changes: 11 additions & 8 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,11 @@ class Collection(
}
val badNotes =
db.queryScalar(
"select 1 from notes where id not in (select distinct nid from cards) " +
"or mid not in " + ids2str(notetypes.ids()) + " limit 1",
"""select 1 from notes where
id not in (select distinct nid from cards)
or
mid not in ${ids2str(notetypes.ids())}
limit 1""",
) > 0
// notes without cards or models
if (badNotes) {
Expand All @@ -630,8 +633,11 @@ class Collection(
val tmpls = m.getJSONArray("tmpls")
val badOrd =
db.queryScalar(
"select 1 from cards where (ord < 0 or ord >= ?) and nid in ( " +
"select id from notes where mid = ?) limit 1",
"""select 1 from cards where
(ord < 0 or ord >= ?)
and
nid in (select id from notes where mid = ?)
limit 1""",
tmpls.length(),
m.getLong("id"),
) > 0
Expand All @@ -650,10 +656,7 @@ class Collection(
cids: List<Long>,
) {
db.execute(
"update cards set flags = (flags & ~?) | ?, usn=?, mod=? where id in " +
ids2str(
cids,
),
"update cards set flags = (flags & ~?) | ?, usn=?, mod=? where id in ${ids2str(cids)}",
7,
flag.code,
usn(),
Expand Down
12 changes: 5 additions & 7 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Notetypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import com.ichi2.utils.HashUtil
import com.ichi2.utils.jsonObjectIterable
import net.ankiweb.rsdroid.RustCleanup
import net.ankiweb.rsdroid.exceptions.BackendNotFoundException
import org.intellij.lang.annotations.Language
import org.json.JSONArray
import org.json.JSONObject
import timber.log.Timber
Expand Down Expand Up @@ -676,7 +677,7 @@ class Notetypes(
ords: IntArray,
): List<Long>? {
val cardIdsToDeleteSql =
"select c2.id from cards c2, notes n2 where c2.nid=n2.id and n2.mid = ? and c2.ord in " + Utils.ids2str(ords)
"select c2.id from cards c2, notes n2 where c2.nid=n2.id and n2.mid = ? and c2.ord in ${Utils.ids2str(ords)}"
val cids: List<Long> = col.db.queryLongList(cardIdsToDeleteSql, modelId)
// Timber.d("cardIdsToDeleteSql was ' %s' and got %s", cardIdsToDeleteSql, Utils.ids2str(cids));
Timber.d("getCardIdsForModel found %s cards to delete for model %s and ords %s", cids.size, modelId, Utils.ids2str(ords))
Expand All @@ -687,7 +688,7 @@ class Notetypes(
Timber.d("noteCountPreDeleteSql was '%s'", noteCountPreDeleteSql)
Timber.d("preDeleteNoteCount is %s", preDeleteNoteCount)
val noteCountPostDeleteSql =
"select count(distinct(nid)) from cards where nid in (select id from notes where mid = ?) and ord not in " + Utils.ids2str(ords)
"select count(distinct(nid)) from cards where nid in (select id from notes where mid = ?) and ord not in ${Utils.ids2str(ords)}"
Timber.d("noteCountPostDeleteSql was '%s'", noteCountPostDeleteSql)
val postDeleteNoteCount: Int = col.db.queryScalar(noteCountPostDeleteSql, modelId)
Timber.d("postDeleteNoteCount would be %s", postDeleteNoteCount)
Expand All @@ -708,12 +709,9 @@ class Notetypes(
it.put("name", name)
}

@Language("JSON")
private const val DEFAULT_TEMPLATE =
(
"{\"name\": \"\", " + "\"ord\": null, " + "\"qfmt\": \"\", " +
"\"afmt\": \"\", " + "\"did\": null, " + "\"bqfmt\": \"\"," + "\"bafmt\": \"\"," + "\"bfont\": \"\"," +
"\"bsize\": 0 }"
)
"""{"name": "", "ord": null, "qfmt": "", "afmt": "", "did": null, "bqfmt": "","bafmt": "","bfont": "", "bsize": 0 }"""

/** "Mapping of field name -> (ord, field). */
fun fieldMap(notetype: NotetypeJson): Map<String, Pair<Int, JSONObject>> {
Expand Down
47 changes: 19 additions & 28 deletions AnkiDroid/src/main/java/com/ichi2/libanki/sched/Scheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ import com.ichi2.anki.utils.SECONDS_PER_DAY
import com.ichi2.libanki.Card
import com.ichi2.libanki.CardId
import com.ichi2.libanki.Collection
import com.ichi2.libanki.Consts
import com.ichi2.libanki.Consts.CARD_TYPE_LRN
import com.ichi2.libanki.Consts.CARD_TYPE_NEW
import com.ichi2.libanki.Consts.CARD_TYPE_RELEARNING
import com.ichi2.libanki.Consts.CARD_TYPE_REV
import com.ichi2.libanki.Consts.QUEUE_TYPE_NEW
import com.ichi2.libanki.Consts.QUEUE_TYPE_REV
import com.ichi2.libanki.DeckConfig
import com.ichi2.libanki.DeckId
import com.ichi2.libanki.EpochSeconds
Expand Down Expand Up @@ -490,9 +495,7 @@ open class Scheduler(
@Suppress("ktlint:standard:max-line-length")
fun totalNewForCurrentDeck(): Int =
col.db.queryScalar(
"SELECT count() FROM cards WHERE id IN (SELECT id FROM cards WHERE did IN " + deckLimit() + " AND queue = " +
Consts.QUEUE_TYPE_NEW +
" LIMIT ?)",
"SELECT count() FROM cards WHERE id IN (SELECT id FROM cards WHERE did IN ${deckLimit()} AND queue = $QUEUE_TYPE_NEW LIMIT ?)",
REPORT_LIMIT,
)

Expand All @@ -501,9 +504,7 @@ open class Scheduler(
@Suppress("ktlint:standard:max-line-length")
fun totalRevForCurrentDeck(): Int =
col.db.queryScalar(
"SELECT count() FROM cards WHERE id IN (SELECT id FROM cards WHERE did IN " + deckLimit() + " AND queue = " +
Consts.QUEUE_TYPE_REV +
" AND due <= ? LIMIT ?)",
"SELECT count() FROM cards WHERE id IN (SELECT id FROM cards WHERE did IN ${deckLimit()} AND queue = $QUEUE_TYPE_REV AND due <= ? LIMIT ?)",
today,
REPORT_LIMIT,
)
Expand All @@ -526,18 +527,17 @@ open class Scheduler(

/** true if there are any rev cards due. */
@Suppress("ktlint:standard:max-line-length")
open fun revDue(): Boolean =
open fun revDue() =
col.db
.queryScalar(
"SELECT 1 FROM cards WHERE did IN " + deckLimit() + " AND queue = " + Consts.QUEUE_TYPE_REV + " AND due <= ?" +
" LIMIT 1",
"""SELECT 1 FROM cards WHERE did IN ${deckLimit()} AND queue = $QUEUE_TYPE_REV AND due <= ? LIMIT 1""",
today,
) != 0

/** true if there are any new cards due. */
@Suppress("ktlint:standard:max-line-length")
open fun newDue(): Boolean =
col.db.queryScalar("SELECT 1 FROM cards WHERE did IN " + deckLimit() + " AND queue = " + Consts.QUEUE_TYPE_NEW + " LIMIT 1") !=
col.db.queryScalar("SELECT 1 FROM cards WHERE did IN ${deckLimit()} AND queue = $QUEUE_TYPE_NEW LIMIT 1") !=
0

private val etaCache: DoubleArray = doubleArrayOf(-1.0, -1.0, -1.0, -1.0, -1.0, -1.0)
Expand Down Expand Up @@ -574,23 +574,14 @@ open class Scheduler(
col
.db
.query(
"select " +
"avg(case when type = " + Consts.CARD_TYPE_NEW +
" then case when ease > 1 then 1.0 else 0.0 end else null end) as newRate, avg(case when type = " +
Consts.CARD_TYPE_NEW +
" then time else null end) as newTime, " +
"avg(case when type in (" + Consts.CARD_TYPE_LRN + ", " + Consts.CARD_TYPE_RELEARNING +
") then case when ease > 1 then 1.0 else 0.0 end else null end) as revRate, avg(case when type in (" +
Consts.CARD_TYPE_LRN +
", " +
Consts.CARD_TYPE_RELEARNING +
") then time else null end) as revTime, " +
"avg(case when type = " + Consts.CARD_TYPE_REV +
" then case when ease > 1 then 1.0 else 0.0 end else null end) as relrnRate, avg(case when type = " +
Consts.CARD_TYPE_REV +
" then time else null end) as relrnTime " +
"from revlog where id > " +
"?",
"""select
avg(case when type = $CARD_TYPE_NEW then case when ease > 1 then 1.0 else 0.0 end else null end) as newRate,
avg(case when type = $CARD_TYPE_NEW then time else null end) as newTime,
avg(case when type in ($CARD_TYPE_LRN, $CARD_TYPE_RELEARNING) then case when ease > 1 then 1.0 else 0.0 end else null end) as revRate,
avg(case when type in ($CARD_TYPE_LRN, $CARD_TYPE_RELEARNING) then time else null end) as revTime,
avg(case when type = $CARD_TYPE_REV then case when ease > 1 then 1.0 else 0.0 end else null end) as relrnRate,
avg(case when type = $CARD_TYPE_REV then time else null end) as relrnTime
from revlog where id > ?""",
(col.sched.dayCutoff - (10 * SECONDS_PER_DAY)) * 1000,
).use { cur ->
if (!cur.moveToFirst()) {
Expand Down
Loading

0 comments on commit 47f6c8b

Please sign in to comment.