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

refactor: Kotlin - fix nullability #12047

Merged
merged 1 commit into from
Aug 19, 2022
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
20 changes: 10 additions & 10 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -757,19 +757,19 @@ open class Collection(
*/
@KotlinCleanup("Check CollectionTask<Int?, Int> - should be fine")
@KotlinCleanup("change to ArrayList!")
fun genCards(nids: kotlin.collections.Collection<Long?>?, model: Model): ArrayList<Long>? {
fun genCards(nids: kotlin.collections.Collection<Long>, model: Model): ArrayList<Long>? {
return genCards<CollectionTask<Int?, Int>>(Utils.collection2Array(nids), model)
}

fun <T> genCards(
nids: kotlin.collections.Collection<Long?>?,
nids: kotlin.collections.Collection<Long>,
model: Model,
task: T?
): ArrayList<Long>? where T : ProgressSender<Int?>?, T : CancelListener? {
return genCards(Utils.collection2Array(nids), model, task)
}

fun genCards(nids: kotlin.collections.Collection<Long?>?, mid: NoteTypeId): ArrayList<Long>? {
fun genCards(nids: kotlin.collections.Collection<Long>, mid: NoteTypeId): ArrayList<Long>? {
return genCards(nids, models.get(mid)!!)
}

Expand All @@ -789,7 +789,7 @@ open class Collection(
*/
@JvmOverloads
fun <T> genCards(
nids: LongArray?,
nids: LongArray,
model: Model,
task: T? = null
): ArrayList<Long>? where T : ProgressSender<Int?>?, T : CancelListener? {
Expand Down Expand Up @@ -1036,11 +1036,11 @@ open class Collection(
}

// NOT IN LIBANKI //
fun cardCount(vararg dids: Long?): Int {
fun cardCount(vararg dids: Long): Int {
return db.queryScalar("SELECT count() FROM cards WHERE did IN " + Utils.ids2str(dids))
}

fun isEmptyDeck(vararg dids: Long?): Boolean {
fun isEmptyDeck(vararg dids: Long): Boolean {
return cardCount(*dids) == 0
}

Expand Down Expand Up @@ -1080,7 +1080,7 @@ open class Collection(
return rem
}

fun emptyCardReport(cids: List<Long>?): String {
fun emptyCardReport(cids: List<Long>): String {
val rep = StringBuilder()
db.query(
"select group_concat(ord+1), count(), flds from cards c, notes n " +
Expand Down Expand Up @@ -1122,15 +1122,15 @@ open class Collection(
/** Update field checksums and sort cache, after find&replace, etc.
* @param nids
*/
fun updateFieldCache(nids: kotlin.collections.Collection<Long>?) {
fun updateFieldCache(nids: kotlin.collections.Collection<Long>) {
val snids = Utils.ids2str(nids)
updateFieldCache(snids)
}

/** Update field checksums and sort cache, after find&replace, etc.
* @param nids
*/
fun updateFieldCache(nids: LongArray?) {
fun updateFieldCache(nids: LongArray) {
val snids = Utils.ids2str(nids)
updateFieldCache(snids)
}
Expand Down Expand Up @@ -2304,7 +2304,7 @@ open class Collection(
/**
* Card Flags *****************************************************************************************************
*/
fun setUserFlag(flag: Int, cids: List<Long>?) {
fun setUserFlag(flag: Int, cids: List<Long>) {
assert(0 <= flag && flag <= 7)
db.execute(
"update cards set flags = (flags & ~?) | ?, usn=?, mod=? where id in " + Utils.ids2str(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ open class Anki2Importer(col: Collection?, file: String) : Importer(col!!, file)
return _mediaData(fname, dst.media.dir())
}

private fun _writeDstMedia(fname: String?, data: BufferedInputStream) {
private fun _writeDstMedia(fname: String, data: BufferedInputStream) {
try {
val path = File(dst.media.dir(), Utils.nfcNormalized(fname)).absolutePath
Utils.writeToFile(data, path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import com.ichi2.libanki.*
import com.ichi2.libanki.template.ParsedNode
import com.ichi2.libanki.utils.StringUtils
import com.ichi2.libanki.utils.TimeManager
import com.ichi2.utils.Assert
import com.ichi2.utils.HashUtil
import com.ichi2.utils.HtmlUtils
import com.ichi2.utils.JSONObject
import com.ichi2.utils.*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this was done by Android Studio, do you want to revert it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is apparently a really contentious area.
I read through a couple threads, and this one (with a focus around here pinterest/ktlint#48 (comment)) was informative for me.
It appears that star imports are actually a more Kotlin-y thing (IntelliJ / Android Studio add them by default, the language implementation uses them, and the package is the "unit" of import in Kotlin more than a single file like Java)

So I'm leaning towards not caring about star imports, and ktlint in it's default configuration also does not care, so I'll let this go.

Note that I'm injecting opinion here, it's a matter of taste. Feel free to disagree. If most people disagree, it's possible to configure ktlint to disallow star imports from what I understand, and that's the thing to do so these rules are enforced by tooling not convention


// Ported from https://github.com/ankitects/anki/blob/50fdf9b03dec33c99a501f332306f378db5eb4ea/pylib/anki/importing/noteimp.py
// Aside from 9f676dbe0b2ad9b87a3bf89d7735b4253abd440e, which allows empty notes.
Expand All @@ -37,6 +34,7 @@ open class NoteImporter(col: com.ichi2.libanki.Collection, file: String) : Impor

/** _nextID in python */
private var mNextId: Long = 0
@KotlinCleanup("maybe lateinit/nonnull")
private var mIds: ArrayList<Long>? = null
private var mEmptyNotes = false
private var mUpdateCount = 0
Expand Down Expand Up @@ -210,9 +208,9 @@ open class NoteImporter(col: com.ichi2.libanki.Collection, file: String) : Impor
addNew(_new)
addUpdates(updates)
// make sure to update sflds, etc
mCol.updateFieldCache(mIds)
mCol.updateFieldCache(mIds!!)
// generate cards
if (mCol.genCards(mIds, mModel)!!.isNotEmpty()) {
if (mCol.genCards(mIds!!, mModel)!!.isNotEmpty()) {
this.log.add(0, getString(R.string.note_importer_empty_cards_found))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ abstract class BaseSched(val col: Collection) {
"end)"
}

fun remFromDyn(cids: Iterable<Long>?) {
fun remFromDyn(cids: Iterable<Long>) {
emptyDyn("id IN " + Utils.ids2str(cids) + " AND odid")
}

Expand Down