From dfbb47b56e4d69e7b54b39c1d2f09dbe66536538 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 28 Dec 2019 11:14:30 +0800 Subject: [PATCH] up --- app/src/main/java/io/legado/app/constant/Bus.kt | 1 + .../java/io/legado/app/service/DownloadService.kt | 13 +++++++++++++ .../io/legado/app/ui/download/DownloadActivity.kt | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/app/src/main/java/io/legado/app/constant/Bus.kt b/app/src/main/java/io/legado/app/constant/Bus.kt index b14c9f538..7795773e6 100644 --- a/app/src/main/java/io/legado/app/constant/Bus.kt +++ b/app/src/main/java/io/legado/app/constant/Bus.kt @@ -19,4 +19,5 @@ object Bus { const val AUDIO_SPEED = "audioSpeed" const val SHOW_RSS = "showRss" const val WEB_SERVICE_STOP = "webServiceStop" + const val UP_DOWNLOAD = "upDownload" } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/DownloadService.kt b/app/src/main/java/io/legado/app/service/DownloadService.kt index 8f5a6c606..a1042b4d6 100644 --- a/app/src/main/java/io/legado/app/service/DownloadService.kt +++ b/app/src/main/java/io/legado/app/service/DownloadService.kt @@ -1,16 +1,19 @@ package io.legado.app.service import android.content.Intent +import android.os.Handler import androidx.core.app.NotificationCompat import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseService import io.legado.app.constant.Action import io.legado.app.constant.AppConst +import io.legado.app.constant.Bus import io.legado.app.help.BookHelp import io.legado.app.help.IntentHelp import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook +import io.legado.app.utils.postEvent import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.asCoroutineDispatcher import java.util.concurrent.Executors @@ -18,10 +21,13 @@ import java.util.concurrent.Executors class DownloadService : BaseService() { private var searchPool = Executors.newFixedThreadPool(16).asCoroutineDispatcher() private var tasks: ArrayList> = arrayListOf() + private val handler = Handler() + private var runnable: Runnable = Runnable { upDownload() } override fun onCreate() { super.onCreate() updateNotification("正在启动下载") + handler.postDelayed(runnable, 1000) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -41,6 +47,7 @@ class DownloadService : BaseService() { override fun onDestroy() { tasks.clear() searchPool.close() + handler.removeCallbacks(runnable) super.onDestroy() } @@ -80,6 +87,12 @@ class DownloadService : BaseService() { stopSelf() } + private fun upDownload() { + postEvent(Bus.UP_DOWNLOAD, true) + handler.removeCallbacks(runnable) + handler.postDelayed(runnable, 1000) + } + /** * 更新通知 */ diff --git a/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt b/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt index 0ca90a472..9789bc273 100644 --- a/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt +++ b/app/src/main/java/io/legado/app/ui/download/DownloadActivity.kt @@ -9,8 +9,10 @@ import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseActivity +import io.legado.app.constant.Bus import io.legado.app.data.entities.Book import io.legado.app.service.help.Download +import io.legado.app.utils.observeEvent import kotlinx.android.synthetic.main.activity_download.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch @@ -60,4 +62,10 @@ class DownloadActivity : BaseActivity(R.layout.activity_download) { adapter.setItems(it) }) } + + override fun observeLiveBus() { + observeEvent(Bus.UP_DOWNLOAD) { + adapter.notifyItemRangeChanged(0, adapter.itemCount, true) + } + } } \ No newline at end of file