Cleanup some nullability

This commit is contained in:
tarsin committed 2023-02-22 20:26:06 +08:00
1 parent ed588bf26c
commit 0d594da6c9
2 files changed
+34 -38

No files matched your search

+14 -30
View File
@@ -49,16 +49,10 @@ object EhDB {
// Fix state
@get:Synchronized
val allDownloadInfo: List<DownloadInfo>
get() {
val dao = db.downloadsDao()
val list = dao.list()
// Fix state
for (info in list) {
if (info.state == DownloadInfo.STATE_WAIT || info.state == DownloadInfo.STATE_DOWNLOAD) {
info.state = DownloadInfo.STATE_NONE
}
get() = db.downloadsDao().list().onEach {
if (it.state == DownloadInfo.STATE_WAIT || it.state == DownloadInfo.STATE_DOWNLOAD) {
it.state = DownloadInfo.STATE_NONE
}
return list
}
@Synchronized
@@ -67,16 +61,14 @@ object EhDB {
dao.update(downloadInfos)
}
// Insert or update
@Synchronized
fun putDownloadInfo(downloadInfo: DownloadInfo) {
val dao = db.downloadsDao()
if (null != dao.load(downloadInfo.gid)) {
// Update
dao.update(downloadInfo)
} else {
// Insert
dao.insert(downloadInfo)
db.downloadsDao().run {
if (load(downloadInfo.gid) != null) {
update(downloadInfo)
} else {
insert(downloadInfo)
}
}
}
@@ -92,20 +84,15 @@ object EhDB {
return raw?.dirname
}
/**
* Insert or update
*/
@Synchronized
fun putDownloadDirname(gid: Long, dirname: String?) {
val dao = db.downloadDirnameDao()
var raw = dao.load(gid)
if (raw != null) { // Update
if (raw != null) {
raw.dirname = dirname
dao.update(raw)
} else { // Insert
raw = DownloadDirname()
raw.gid = gid
raw.dirname = dirname
} else {
raw = DownloadDirname(gid, dirname)
dao.insert(raw)
}
}
@@ -124,13 +111,10 @@ object EhDB {
@get:Synchronized
val allDownloadLabelList: List<DownloadLabel>
get() {
val dao = db.downloadLabelDao()
return dao.list()
}
get() = db.downloadLabelDao().list()
@Synchronized
fun addDownloadLabel(label: String?): DownloadLabel {
fun addDownloadLabel(label: String): DownloadLabel {
val dao = db.downloadLabelDao()
val raw = DownloadLabel()
raw.label = label
@@ -37,6 +37,8 @@ import com.hippo.yorozuya.collect.LongList
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import java.util.LinkedList
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
object DownloadManager : OnSpiderListener {
// All download info list
@@ -92,14 +94,20 @@ object DownloadManager : OnSpiderListener {
allInfoMap.put(info.gid, info)
// Add to each label list
var list = getInfoListForLabel(info.label)
if (list == null) {
// Can't find the label in label list
list = LinkedList()
map[info.label] = list
if (!containLabel(info.label)) {
val label = info.label
val list = getInfoListForLabel(label) ?: LinkedList<DownloadInfo>().also {
/* Contracts says if [getInfoListForLabel] return null, the param [label] is not null
* [label] is local immutable
* Why [label] is not smart casted to [String!] here ???
*
* TODO: Report it to kotlin compiler team
*/
label!!
map[info.label] = it
if (!containLabel(label)) {
// Add label to DB and list
labels.add(EhDB.addDownloadLabel(info.label))
labels.add(EhDB.addDownloadLabel(label))
}
}
list.add(info)
@@ -109,7 +117,11 @@ object DownloadManager : OnSpiderListener {
mDownloadInfoListeners = ArrayList()
}
@OptIn(ExperimentalContracts::class)
private fun getInfoListForLabel(label: String?): LinkedList<DownloadInfo>? {
contract {
returns(null) implies (label != null)
}
return if (label == null) {
mDefaultInfoList
} else {
@@ -342,7 +354,7 @@ object DownloadManager : OnSpiderListener {
mMap[info.label] = list
if (!containLabel(info.label)) {
// Add label to DB and list
mLabelList.add(EhDB.addDownloadLabel(info.label))
mLabelList.add(EhDB.addDownloadLabel(info.label!!))
}
}
list.add(info)