From 3539c118e24cd8256a42d831f4108bbc46220960 Mon Sep 17 00:00:00 2001 From: Jason Yao Date: Mon, 16 Aug 2021 02:36:18 -0400 Subject: [PATCH] change back to use debugMessageMap single thread works but last source is not showing the message --- .../main/java/io/legado/app/model/Debug.kt | 14 ++++++++------ .../legado/app/service/CheckSourceService.kt | 19 ++++++++++++++----- .../book/source/manage/BookSourceActivity.kt | 15 +++------------ .../book/source/manage/BookSourceAdapter.kt | 15 +++++++++++---- 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/Debug.kt b/app/src/main/java/io/legado/app/model/Debug.kt index a67fc9ca9..b696a108f 100644 --- a/app/src/main/java/io/legado/app/model/Debug.kt +++ b/app/src/main/java/io/legado/app/model/Debug.kt @@ -21,7 +21,8 @@ object Debug { var callback: Callback? = null private var debugSource: String? = null private val tasks: CompositeCoroutine = CompositeCoroutine() - val debugMessageMap = ConcurrentHashMap>() + val debugMessageMap = ConcurrentHashMap() + var isChecking: Boolean = false @SuppressLint("ConstantLocale") private val DEBUG_TIME_FORMAT = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) @@ -37,7 +38,7 @@ object Debug { state: Int = 1 ) { callback?.let { - if (debugSource != sourceUrl || !print) return + if ((debugSource != sourceUrl || !print) && !isChecking) return var printMsg = msg ?: "" if (isHtml) { printMsg = HtmlFormatter.format(msg) @@ -49,7 +50,8 @@ object Debug { it.printLog(state, printMsg) Log.d(EventBus.CHECK_SOURCE_MESSAGE, "debugMessage to filter $printMsg") if (sourceUrl != null && printMsg.length < 30) { - debugMessageMap[sourceUrl]?.add(printMsg) + debugMessageMap[sourceUrl] = printMsg + callback?.printCheckSourceMessage(sourceUrl, printMsg) } } } @@ -68,10 +70,10 @@ object Debug { } } - fun startCheck(source: BookSource) { - debugSource = source.bookSourceUrl + fun startChecking(source: BookSource) { startTime = System.currentTimeMillis() - debugMessageMap[source.bookSourceUrl] = arrayListOf() + isChecking = true + debugMessageMap[source.bookSourceUrl] = "开始校验" } fun startDebug(scope: CoroutineScope, rssSource: RssSource) { diff --git a/app/src/main/java/io/legado/app/service/CheckSourceService.kt b/app/src/main/java/io/legado/app/service/CheckSourceService.kt index fd03e4822..900825709 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -10,7 +10,6 @@ import io.legado.app.constant.EventBus import io.legado.app.constant.IntentAction import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource -import io.legado.app.help.AppConfig import io.legado.app.help.IntentHelp import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.model.Debug @@ -24,7 +23,7 @@ import java.util.concurrent.Executors import kotlin.math.min class CheckSourceService : BaseService() { - private var threadCount = AppConfig.threadCount + private var threadCount = 1 private var searchCoroutine = Executors.newFixedThreadPool(min(threadCount,8)).asCoroutineDispatcher() private var tasks = CompositeCoroutine() private val allIds = ArrayList() @@ -37,7 +36,7 @@ class CheckSourceService : BaseService() { @Synchronized override fun printCheckSourceMessage(sourceUrl: String, msg: String) { - postEvent(EventBus.CHECK_SOURCE_MESSAGE, Pair(sourceUrl, msg)) + postEvent(EventBus.CHECK_SOURCE_MESSAGE, Pair(sourceUrl, null)) Log.d(EventBus.CHECK_SOURCE_MESSAGE, "printCheckSourceMessage to post $msg") } } @@ -79,6 +78,7 @@ class CheckSourceService : BaseService() { tasks.clear() searchCoroutine.close() Debug.callback = null + Debug.isChecking = false postEvent(EventBus.CHECK_SOURCE_DONE, 0) } @@ -112,7 +112,7 @@ class CheckSourceService : BaseService() { if (index < allIds.size) { val sourceUrl = allIds[index] appDb.bookSourceDao.getBookSource(sourceUrl)?.let { source -> - Debug.startCheck(source) + Debug.startChecking(source) check(source) } ?: onNext(sourceUrl, "") } @@ -150,6 +150,8 @@ class CheckSourceService : BaseService() { "error:${it.localizedMessage} ${source.bookSourceComment}" """.trimIndent() + val message = Debug.debugMessageMap[source.bookSourceUrl] + " 失败" + postEvent(EventBus.CHECK_SOURCE_MESSAGE, Pair(source.bookSourceUrl, message)) appDb.bookSourceDao.update(source) }.onSuccess(searchCoroutine) { source.removeGroup("失效") @@ -158,9 +160,16 @@ class CheckSourceService : BaseService() { ?.filterNot { it.startsWith("error:") }?.joinToString("\n") + Debug.debugMessageMap[source.bookSourceUrl]?.let { lastMessage -> + lastMessage.indexOf("]").let { + if (it > 0) { + val timeMessage = lastMessage.substring(0, it) + " 校验成功" + postEvent(EventBus.CHECK_SOURCE_MESSAGE, Pair(source.bookSourceUrl, timeMessage)) + } + } + } appDb.bookSourceDao.update(source) }.onFinally(searchCoroutine) { - postEvent(EventBus.CHECK_SOURCE_MESSAGE, Pair(source.bookSourceUrl, null)) onNext(source.bookSourceUrl, source.bookSourceName) } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 0d78984c5..7e3d3bf64 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -428,7 +428,6 @@ class BookSourceActivity : VMBaseActivity {} observeEvent(EventBus.CHECK_SOURCE) { msg -> snackBar?.setText(msg) ?: let { snackBar = Snackbar @@ -449,24 +448,16 @@ class BookSourceActivity : VMBaseActivity>(EventBus.CHECK_SOURCE_MESSAGE) { messagePair -> + observeEvent>(EventBus.CHECK_SOURCE_MESSAGE) { messagePair -> sourceFlowJob?.cancel() - - val messageFlow = Debug.debugMessageMap[messagePair.first]?.asFlow() - if (messageFlow != null) { sourceFlowJob = launch { var index: Int = -1 appDb.bookSourceDao.flowSearch(messagePair.first) .map { adapter.getItems().indexOf(it[0]) } .collect { - index = it } - if (index > -1){ - messageFlow.onEach { delay(300L) }.buffer(10) - .collect { adapter.notifyItemChanged(index, bundleOf(Pair(EventBus.CHECK_SOURCE_MESSAGE, it))) } - } - + index = it + adapter.notifyItemChanged(index, bundleOf(Pair(EventBus.CHECK_SOURCE_MESSAGE, messagePair.second))) } } - } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt index 9b575df85..4ebddca5e 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt @@ -109,7 +109,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : } swtEnabled.isChecked = item.enabled cbBookSource.isChecked = selected.contains(item) - ivDebugText.text = Debug.debugMessageMap[item.bookSourceUrl]?.lastOrNull() ?: "" + ivDebugText.text = Debug.debugMessageMap[item.bookSourceUrl] ?: "" ivDebugText.visibility = if(ivDebugText.text.toString().length > 1) View.VISIBLE else View.GONE upShowExplore(ivExplore, item) } else { @@ -117,13 +117,20 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : when (it) { "selected" -> cbBookSource.isChecked = selected.contains(item) "startChecking" -> { - ivProgressBar.visibility = if(selected.contains(item)) View.VISIBLE else View.GONE + ivProgressBar.visibility = if(selected.contains(item)) View.VISIBLE else ivProgressBar.visibility } "checkSourceDone" -> { - ivProgressBar.visibility = if(selected.contains(item)) View.VISIBLE else ivProgressBar.visibility + ivProgressBar.visibility = if(selected.contains(item)) View.GONE else ivProgressBar.visibility } EventBus.CHECK_SOURCE_MESSAGE -> { - ivDebugText.text = (payload[it] as? String) ?: "" + val message = payload[it] as? String? + if (message != null) { + ivDebugText.text = message + ivProgressBar.visibility = View.GONE + } + else{ + ivDebugText.text = Debug.debugMessageMap[item.bookSourceUrl] ?: "" + } ivDebugText.visibility = if(ivDebugText.text.toString().length > 1) View.VISIBLE else View.GONE } }