From 4593714298bb8d977cb8165693a37afa518bf60b Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Wed, 16 Aug 2023 22:16:36 +0800 Subject: [PATCH] Cleanup DownloadManager --- .../ehviewer/download/DownloadManager.kt | 148 +++--------------- 1 file changed, 24 insertions(+), 124 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 ed7896c07..5eda17d11 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.kt @@ -71,7 +71,7 @@ object DownloadManager : OnSpiderListener { // Store download info wait to start private val mWaitList = LinkedList() private val mSpeedReminder = SpeedReminder() - private val mDownloadInfoListeners = mutableListOf() + private var mDownloadInfoListener: DownloadInfoListener? = null private val mNotifyTaskPool = ConcurrentPool(5) private var mDownloadListener: DownloadListener? = null private var mCurrentTask: DownloadInfo? = null @@ -125,14 +125,6 @@ object DownloadManager : OnSpiderListener { return info?.state ?: DownloadInfo.STATE_INVALID } - fun addDownloadInfoListener(downloadInfoListener: DownloadInfoListener) { - mDownloadInfoListeners.add(downloadInfoListener) - } - - fun removeDownloadInfoListener(downloadInfoListener: DownloadInfoListener) { - mDownloadInfoListeners.remove(downloadInfoListener) - } - fun setDownloadListener(listener: DownloadListener?) { mDownloadListener = listener } @@ -166,12 +158,7 @@ object DownloadManager : OnSpiderListener { mDownloadListener!!.onStart(info) } // Notify state update - val list: List? = getInfoListForLabel(info.label) - if (list != null) { - for (l in mDownloadInfoListeners) { - l.onUpdate(info, list) - } - } + mDownloadInfoListener?.onUpdate(info) } } @@ -192,12 +179,7 @@ object DownloadManager : OnSpiderListener { // Update in DB EhDB.putDownloadInfo(info) // Notify state update - val list: List? = getInfoListForLabel(info.label) - if (list != null) { - for (l in mDownloadInfoListeners) { - l.onUpdate(info, list) - } - } + mDownloadInfoListener?.onUpdate(info) // Make sure download is running ensureDownload() } @@ -227,9 +209,7 @@ object DownloadManager : OnSpiderListener { EhDB.putDownloadInfo(info) // Notify - for (l in mDownloadInfoListeners) { - l.onAdd(info, list, list.size - 1) - } + mDownloadInfoListener?.onUpdate(info) // Make sure download is running ensureDownload() @@ -259,9 +239,7 @@ object DownloadManager : OnSpiderListener { } if (update) { // Notify Listener - for (l in mDownloadInfoListeners) { - l.onUpdateAll() - } + mDownloadInfoListener?.onUpdateAll() // Ensure download ensureDownload() } @@ -271,29 +249,17 @@ object DownloadManager : OnSpiderListener { private val _stateFlow = callbackFlow { val listener = object : DownloadInfoListener { - override fun onAdd(info: DownloadInfo, list: List, position: Int) { - trySend(info.gid) - } - - override fun onUpdate(info: DownloadInfo, list: List) { + override fun onUpdate(info: DownloadInfo) { trySend(info.gid) } override fun onUpdateAll() { trySend(0) } - - override fun onReload() { - trySend(0) - } - - override fun onRemove(info: DownloadInfo, list: List, position: Int) { - trySend(info.gid) - } } - addDownloadInfoListener(listener) + mDownloadInfoListener = listener awaitClose { - removeDownloadInfoListener(listener) + mDownloadInfoListener = null } }.shareIn(callbackFlowScope, SharingStarted.Eagerly) @@ -317,9 +283,7 @@ object DownloadManager : OnSpiderListener { } if (update) { // Notify Listener - for (l in mDownloadInfoListeners) { - l.onUpdateAll() - } + mDownloadInfoListener?.onUpdateAll() // Ensure download ensureDownload() } @@ -355,9 +319,7 @@ object DownloadManager : OnSpiderListener { // Notify if (notify) { - for (l in mDownloadInfoListeners) { - l.onReload() - } + mDownloadInfoListener?.onUpdateAll() } } @@ -388,9 +350,7 @@ object DownloadManager : OnSpiderListener { // Notify withUIContext { - for (l in mDownloadInfoListeners) { - l.onAdd(info, list, list.size - 1) - } + mDownloadInfoListener?.onUpdate(info) } } @@ -398,12 +358,7 @@ object DownloadManager : OnSpiderListener { val info = stopDownloadInternal(gid) if (info != null) { // Update listener - val list: List? = getInfoListForLabel(info.label) - if (list != null) { - for (l in mDownloadInfoListeners) { - l.onUpdate(info, list) - } - } + mDownloadInfoListener?.onUpdate(info) // Ensure download ensureDownload() } @@ -413,12 +368,7 @@ object DownloadManager : OnSpiderListener { val info = stopCurrentDownloadInternal() if (info != null) { // Update listener - val list: List? = getInfoListForLabel(info.label) - if (list != null) { - for (l in mDownloadInfoListeners) { - l.onUpdate(info, list) - } - } + mDownloadInfoListener?.onUpdate(info) // Ensure download ensureDownload() } @@ -428,9 +378,7 @@ object DownloadManager : OnSpiderListener { stopRangeDownloadInternal(gidList) // Update listener - for (l in mDownloadInfoListeners) { - l.onUpdateAll() - } + mDownloadInfoListener?.onUpdateAll() // Ensure download ensureDownload() @@ -449,9 +397,7 @@ object DownloadManager : OnSpiderListener { stopCurrentDownloadInternal() // Notify mDownloadInfoListener - for (l in mDownloadInfoListeners) { - l.onUpdateAll() - } + mDownloadInfoListener?.onUpdateAll() } suspend fun deleteDownload(gid: Long, deleteFiles: Boolean = false) { @@ -472,9 +418,7 @@ object DownloadManager : OnSpiderListener { if (index >= 0) { list.remove(info) // Update listener - for (l in mDownloadInfoListeners) { - l.onRemove(info, list, index) - } + mDownloadInfoListener?.onUpdate(info) } } @@ -497,9 +441,7 @@ object DownloadManager : OnSpiderListener { allInfoList.removeAll(list.toSet()) // Update listener - for (l in mDownloadInfoListeners) { - l.onReload() - } + mDownloadInfoListener?.onUpdateAll() // Ensure download ensureDownload() @@ -659,9 +601,7 @@ object DownloadManager : OnSpiderListener { // Save to DB EhDB.putDownloadInfo(info) } - for (l in mDownloadInfoListeners) { - l.onReload() - } + mDownloadInfoListener?.onUpdateAll() } suspend fun addLabel(label: String?) { @@ -811,30 +751,15 @@ object DownloadManager : OnSpiderListener { } interface DownloadInfoListener { - /** - * Add the special info to the special position - */ - fun onAdd(info: DownloadInfo, list: List, position: Int) - /** * The special info is changed */ - fun onUpdate(info: DownloadInfo, list: List) + fun onUpdate(info: DownloadInfo) /** * Maybe all data is changed, but size is the same */ fun onUpdateAll() - - /** - * Maybe all data is changed, maybe list is changed - */ - fun onReload() - - /** - * Remove the special info from the special position - */ - fun onRemove(info: DownloadInfo, list: List, position: Int) } interface DownloadListener { @@ -941,12 +866,7 @@ object DownloadManager : OnSpiderListener { Log.e(TAG, "Current task is null, but it should not be") } else { info.total = mPages - val list: List? = getInfoListForLabel(info.label) - if (list != null) { - for (l in mDownloadInfoListeners) { - l.onUpdate(info, list) - } - } + mDownloadInfoListener?.onUpdate(info) } } @@ -975,12 +895,7 @@ object DownloadManager : OnSpiderListener { if (mDownloadListener != null) { mDownloadListener!!.onGetPage(info) } - val list: List? = getInfoListForLabel(info.label) - if (list != null) { - for (l in mDownloadInfoListeners) { - l.onUpdate(info, list) - } - } + mDownloadInfoListener?.onUpdate(info) } } @@ -993,12 +908,7 @@ object DownloadManager : OnSpiderListener { info.finished = mFinished info.downloaded = mDownloaded info.total = mTotal - val list: List? = getInfoListForLabel(info.label) - if (list != null) { - for (l in mDownloadInfoListeners) { - l.onUpdate(info, list) - } - } + mDownloadInfoListener?.onUpdate(info) } } @@ -1037,12 +947,7 @@ object DownloadManager : OnSpiderListener { if (mDownloadListener != null) { mDownloadListener!!.onFinish(info) } - val list: List? = getInfoListForLabel(info.label) - if (list != null) { - for (l in mDownloadInfoListeners) { - l.onUpdate(info, list) - } - } + mDownloadInfoListener?.onUpdate(info) // Start next download ensureDownload() } @@ -1128,12 +1033,7 @@ object DownloadManager : OnSpiderListener { if (mDownloadListener != null) { mDownloadListener!!.onDownload(info) } - val list: List? = getInfoListForLabel(info.label) - if (list != null) { - for (l in mDownloadInfoListeners) { - l.onUpdate(info, list) - } - } + mDownloadInfoListener?.onUpdate(info) } mBytesRead = 0 if (!mStop) {