Skip to content

Commit 8ce99c0

Browse files
david-allisonmikehardy
authored andcommitted
refactor: Kotlin - fix nullability
This is before converting 'Utils.java' **Collection:** * setUserFlag * genCards * cardCount * emptyCardReport * updateFieldCache **BaseSched:** * remFromDyn **Anki2Importer:** * _writeDstMedia
1 parent 14cdd72 commit 8ce99c0

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

AnkiDroid/src/main/java/com/ichi2/libanki/Collection.kt

+10-10
Original file line numberDiff line numberDiff line change
@@ -757,19 +757,19 @@ open class Collection(
757757
*/
758758
@KotlinCleanup("Check CollectionTask<Int?, Int> - should be fine")
759759
@KotlinCleanup("change to ArrayList!")
760-
fun genCards(nids: kotlin.collections.Collection<Long?>?, model: Model): ArrayList<Long>? {
760+
fun genCards(nids: kotlin.collections.Collection<Long>, model: Model): ArrayList<Long>? {
761761
return genCards<CollectionTask<Int?, Int>>(Utils.collection2Array(nids), model)
762762
}
763763

764764
fun <T> genCards(
765-
nids: kotlin.collections.Collection<Long?>?,
765+
nids: kotlin.collections.Collection<Long>,
766766
model: Model,
767767
task: T?
768768
): ArrayList<Long>? where T : ProgressSender<Int?>?, T : CancelListener? {
769769
return genCards(Utils.collection2Array(nids), model, task)
770770
}
771771

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

@@ -789,7 +789,7 @@ open class Collection(
789789
*/
790790
@JvmOverloads
791791
fun <T> genCards(
792-
nids: LongArray?,
792+
nids: LongArray,
793793
model: Model,
794794
task: T? = null
795795
): ArrayList<Long>? where T : ProgressSender<Int?>?, T : CancelListener? {
@@ -1036,11 +1036,11 @@ open class Collection(
10361036
}
10371037

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

1043-
fun isEmptyDeck(vararg dids: Long?): Boolean {
1043+
fun isEmptyDeck(vararg dids: Long): Boolean {
10441044
return cardCount(*dids) == 0
10451045
}
10461046

@@ -1080,7 +1080,7 @@ open class Collection(
10801080
return rem
10811081
}
10821082

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

11301130
/** Update field checksums and sort cache, after find&replace, etc.
11311131
* @param nids
11321132
*/
1133-
fun updateFieldCache(nids: LongArray?) {
1133+
fun updateFieldCache(nids: LongArray) {
11341134
val snids = Utils.ids2str(nids)
11351135
updateFieldCache(snids)
11361136
}
@@ -2307,7 +2307,7 @@ open class Collection(
23072307
/**
23082308
* Card Flags *****************************************************************************************************
23092309
*/
2310-
fun setUserFlag(flag: Int, cids: List<Long>?) {
2310+
fun setUserFlag(flag: Int, cids: List<Long>) {
23112311
assert(0 <= flag && flag <= 7)
23122312
db.execute(
23132313
"update cards set flags = (flags & ~?) | ?, usn=?, mod=? where id in " + Utils.ids2str(

AnkiDroid/src/main/java/com/ichi2/libanki/importer/Anki2Importer.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ open class Anki2Importer(col: Collection?, file: String) : Importer(col!!, file)
719719
return _mediaData(fname, dst.media.dir())
720720
}
721721

722-
private fun _writeDstMedia(fname: String?, data: BufferedInputStream) {
722+
private fun _writeDstMedia(fname: String, data: BufferedInputStream) {
723723
try {
724724
val path = File(dst.media.dir(), Utils.nfcNormalized(fname)).absolutePath
725725
Utils.writeToFile(data, path)

AnkiDroid/src/main/java/com/ichi2/libanki/importer/NoteImporter.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ import com.ichi2.libanki.*
1111
import com.ichi2.libanki.template.ParsedNode
1212
import com.ichi2.libanki.utils.StringUtils
1313
import com.ichi2.libanki.utils.TimeManager
14-
import com.ichi2.utils.Assert
15-
import com.ichi2.utils.HashUtil
16-
import com.ichi2.utils.HtmlUtils
17-
import com.ichi2.utils.JSONObject
14+
import com.ichi2.utils.*
1815

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

3835
/** _nextID in python */
3936
private var mNextId: Long = 0
37+
@KotlinCleanup("maybe lateinit/nonnull")
4038
private var mIds: ArrayList<Long>? = null
4139
private var mEmptyNotes = false
4240
private var mUpdateCount = 0
@@ -210,9 +208,9 @@ open class NoteImporter(col: com.ichi2.libanki.Collection, file: String) : Impor
210208
addNew(_new)
211209
addUpdates(updates)
212210
// make sure to update sflds, etc
213-
mCol.updateFieldCache(mIds)
211+
mCol.updateFieldCache(mIds!!)
214212
// generate cards
215-
if (mCol.genCards(mIds, mModel)!!.isNotEmpty()) {
213+
if (mCol.genCards(mIds!!, mModel)!!.isNotEmpty()) {
216214
this.log.add(0, getString(R.string.note_importer_empty_cards_found))
217215
}
218216

AnkiDroid/src/main/java/com/ichi2/libanki/sched/BaseSched.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ abstract class BaseSched(val col: Collection) {
388388
"end)"
389389
}
390390

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

0 commit comments

Comments
 (0)