From 61796f4cd31a89f18efa88611fb410095d2e2d0f Mon Sep 17 00:00:00 2001 From: tarsin Date: Mon, 3 Jul 2023 18:22:06 +0800 Subject: [PATCH] Cleanup DownloadManager init code --- .../ehviewer/download/DownloadManager.kt | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt index cc69b739c..fea80a795 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt @@ -45,7 +45,7 @@ import kotlin.contracts.contract object DownloadManager : OnSpiderListener { // All download info list - val allInfoList: LinkedList + val allInfoList: LinkedList = LinkedList(EhDB.allDownloadInfo) // All download info map private val mAllInfoMap: MutableMap @@ -69,33 +69,20 @@ object DownloadManager : OnSpiderListener { private var mCurrentSpider: SpiderQueen? = null init { - // Create list for each label - map = HashMap() - for ((_, label1) in labelList) { - map[label1] = LinkedList() - } - - // Create default for non tag - defaultInfoList = LinkedList() - - // Get all info - allInfoList = LinkedList(EhDB.allDownloadInfo) - - mAllInfoMap = allInfoList.associateBy { info -> - // Add to each label list - val label = info.label - - val list = getInfoListForLabel(label) ?: LinkedList().also { - label!! - map[info.label] = it + map = allInfoList.groupBy { it.label }.entries.associate { + it.key?.let { label -> if (!containLabel(label)) { - // Add label to DB and list + // Add non existing label to DB and list labelList.add(EhDB.addDownloadLabel(label)) } } - list.add(info) - info.gid - } as MutableMap + it.key to LinkedList(it.value) + } as MutableMap> + defaultInfoList = map.remove(null) ?: LinkedList() + labelList.forEach { (_, label) -> + map[label] ?: LinkedList().also { map[label] = it } + } + mAllInfoMap = allInfoList.associateBy { it.gid } as MutableMap mWaitList = LinkedList() mSpeedReminder = SpeedReminder() mDownloadInfoListeners = ArrayList() @@ -1196,7 +1183,7 @@ object DownloadManager : OnSpiderListener { } } - private val TAG = "DownloadManager" + private const val TAG = "DownloadManager" private const val TYPE_ON_GET_PAGES = 0 private const val TYPE_ON_GET_509 = 1 private const val TYPE_ON_PAGE_DOWNLOAD = 2