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 c3bac0547..300d68536 100644 --- a/app/src/main/java/io/legado/app/model/Debug.kt +++ b/app/src/main/java/io/legado/app/model/Debug.kt @@ -2,6 +2,7 @@ package io.legado.app.model import android.annotation.SuppressLint import io.legado.app.data.entities.* +import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.model.rss.Rss import io.legado.app.model.webBook.WebBook @@ -11,14 +12,15 @@ import io.legado.app.utils.msg import kotlinx.coroutines.CoroutineScope import java.text.SimpleDateFormat import java.util.* -import java.util.concurrent.ConcurrentHashMap +import kotlin.collections.HashMap object Debug { var callback: Callback? = null private var debugSource: String? = null private val tasks: CompositeCoroutine = CompositeCoroutine() - val debugMessageMap = ConcurrentHashMap() - private var isChecking: Boolean = false + val debugMessageMap = HashMap() + private val debugTimeMap = HashMap() + var isChecking: Boolean = false @SuppressLint("ConstantLocale") private val DEBUG_TIME_FORMAT = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) @@ -33,20 +35,30 @@ object Debug { showTime: Boolean = true, state: Int = 1 ) { - callback?.let { - if ((debugSource != sourceUrl || !print) && !isChecking) return - var printMsg = msg ?: "" - if (isHtml) { - printMsg = HtmlFormatter.format(msg) - } - if (showTime) { - val time = DEBUG_TIME_FORMAT.format(Date(System.currentTimeMillis() - startTime)) - printMsg = "$time $printMsg" - } - it.printLog(state, printMsg) - if (sourceUrl != null && printMsg.length < 30) { - debugMessageMap[sourceUrl] = printMsg - callback?.postCheckMessageEvent(sourceUrl) + if (AppConfig.checkSourceMessage && isChecking) { + if (sourceUrl != null && (msg ?: "").length < 30) { + var printMsg = msg ?: "" + if (isHtml) { + printMsg = HtmlFormatter.format(msg) + } + if (showTime && debugTimeMap[sourceUrl] != null) { + val time = DEBUG_TIME_FORMAT.format(Date(System.currentTimeMillis() - debugTimeMap[sourceUrl]!!)) + printMsg = "$time $printMsg" + debugMessageMap[sourceUrl] = printMsg + } + } else { + callback?.let { + if ((debugSource != sourceUrl || !print)) return + var printMsg = msg ?: "" + if (isHtml) { + printMsg = HtmlFormatter.format(msg) + } + if (showTime) { + val time = DEBUG_TIME_FORMAT.format(Date(System.currentTimeMillis() - startTime)) + printMsg = "$time $printMsg" + } + it.printLog(state, printMsg) + } } } } @@ -66,16 +78,23 @@ object Debug { } fun startChecking(source: BookSource) { - startTime = System.currentTimeMillis() isChecking = true - debugMessageMap[source.bookSourceUrl] = "开始校验" + debugTimeMap[source.bookSourceUrl] = System.currentTimeMillis() + debugMessageMap[source.bookSourceUrl] = "${DEBUG_TIME_FORMAT.format(Date(0))} 开始校验" } fun finishChecking() { - callback = null isChecking = false } + fun updateFinalMessage(sourceUrl: String, state: String) { + if (debugTimeMap[sourceUrl] != null && debugMessageMap[sourceUrl] != null) { + val time = DEBUG_TIME_FORMAT.format(Date(System.currentTimeMillis() - debugTimeMap[sourceUrl]!!)) + val originalMessage = debugMessageMap[sourceUrl]!!.substringAfter("] ") + debugMessageMap[sourceUrl] = "$time $originalMessage $state" + } + } + fun startDebug(scope: CoroutineScope, rssSource: RssSource) { cancelDebug() debugSource = rssSource.sourceUrl @@ -261,7 +280,5 @@ object Debug { interface Callback { fun printLog(state: Int, msg: String) - fun postCheckMessageEvent(sourceUrl: String) } - } \ No newline at end of file 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 c788fae58..66fcf24fc 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -30,7 +30,7 @@ class CheckSourceService : BaseService() { private val checkedIds = ArrayList() private var processIndex = 0 private var notificationMsg = "" - private var debugCallback : Debug.Callback? = null + private val showCheckSourceMessage = AppConfig.checkSourceMessage private val notificationBuilder by lazy { NotificationCompat.Builder(this, AppConst.channelIdReadAloud) .setSmallIcon(R.drawable.ic_network_check) @@ -50,18 +50,6 @@ class CheckSourceService : BaseService() { override fun onCreate() { super.onCreate() notificationMsg = getString(R.string.start) - if (AppConfig.checkSourceMessage) { - debugCallback = object : Debug.Callback { - override fun printLog(state: Int, msg: String) {} - - @Synchronized - override fun postCheckMessageEvent(sourceUrl: String) { - postEvent(EventBus.CHECK_SOURCE_MESSAGE, sourceUrl) - } - } - Debug.callback = debugCallback - threadCount = 1 - } upNotification() } @@ -77,6 +65,7 @@ class CheckSourceService : BaseService() { override fun onDestroy() { super.onDestroy() + Debug.finishChecking() tasks.clear() searchCoroutine.close() postEvent(EventBus.CHECK_SOURCE_DONE, 0) @@ -120,7 +109,7 @@ class CheckSourceService : BaseService() { fun check(source: BookSource) { execute(context = searchCoroutine) { - if (AppConfig.checkSourceMessage) { + if (showCheckSourceMessage) { Debug.startChecking(source) } val webBook = WebBook(source) @@ -152,9 +141,8 @@ class CheckSourceService : BaseService() { "error:${it.localizedMessage} ${source.bookSourceComment}" """.trimIndent() - debugCallback?.let { - Debug.debugMessageMap[source.bookSourceUrl] = Debug.debugMessageMap[source.bookSourceUrl] + " 失败" - postEvent(EventBus.CHECK_SOURCE_MESSAGE, source.bookSourceUrl) + if (showCheckSourceMessage) { + Debug.updateFinalMessage(source.bookSourceUrl, "失败") } appDb.bookSourceDao.update(source) }.onSuccess(searchCoroutine) { @@ -164,11 +152,9 @@ class CheckSourceService : BaseService() { ?.filterNot { it.startsWith("error:") }?.joinToString("\n") - debugCallback?.let { debugCallback - Debug.debugMessageMap[source.bookSourceUrl] = Debug.debugMessageMap[source.bookSourceUrl] + " 成功" - postEvent(EventBus.CHECK_SOURCE_MESSAGE, source.bookSourceUrl) + if (showCheckSourceMessage) { + Debug.updateFinalMessage(source.bookSourceUrl, "成功") } - appDb.bookSourceDao.update(source) }.onFinally(searchCoroutine) { onNext(source.bookSourceUrl, source.bookSourceName) diff --git a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt index 7d79dfca2..991591029 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugModel.kt @@ -51,8 +51,6 @@ class BookSourceDebugModel(application: Application) : BaseViewModel(application } } - override fun postCheckMessageEvent(sourceUrl: String) {} - override fun onCleared() { super.onCleared() Debug.cancelDebug(true) 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 a107c178b..4db5082bd 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 @@ -21,6 +21,7 @@ import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource import io.legado.app.databinding.ActivityBookSourceBinding import io.legado.app.databinding.DialogEditTextBinding +import io.legado.app.help.AppConfig import io.legado.app.help.LocalConfig import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.ATH @@ -40,10 +41,10 @@ import io.legado.app.ui.widget.recycler.ItemTouchCallback import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding -import kotlinx.coroutines.Job +import kotlinx.coroutines.* import kotlinx.coroutines.flow.* -import kotlinx.coroutines.launch import java.io.File +import kotlin.math.min class BookSourceActivity : VMBaseActivity(), PopupMenu.OnMenuItemClickListener, @@ -340,6 +341,9 @@ class BookSourceActivity : VMBaseActivity(EventBus.CHECK_SOURCE_DONE) { - Debug.finishChecking() snackBar?.dismiss() snackBar = null groups.map { group -> @@ -446,14 +453,27 @@ class BookSourceActivity : VMBaseActivity(EventBus.CHECK_SOURCE_MESSAGE) { bookSourceUrl -> - sourceFlowJob?.cancel() - sourceFlowJob = launch { - appDb.bookSourceDao.flowSearch(bookSourceUrl) - .map { adapter.getItems().indexOf(it[0]) } - .collect { - adapter.notifyItemChanged(it, bundleOf(Pair(EventBus.CHECK_SOURCE_MESSAGE, null))) } + } + + private fun checkMessageRefreshJob(): Job { + val firstIndex = adapter.getItems().indexOf(adapter.selection.first()) + val lastIndex = adapter.getItems().indexOf(adapter.selection.last()) + var refreshCount = 0 + Debug.isChecking = true + return async(start = CoroutineStart.LAZY) { + flow { + while (true) { + refreshCount += 1 + emit(refreshCount) + delay(300L) + } + }.collect { + adapter.notifyItemRangeChanged(firstIndex, lastIndex + 1, bundleOf(Pair("checkSourceMessage", null))) + if (!Debug.isChecking || (refreshCount > (600 * (lastIndex + 1 - firstIndex) / min(AppConfig.threadCount,8)))) { + Debug.finishChecking() + this.cancel() } + } } } 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 7e0937b3c..6b5b8e845 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 @@ -13,7 +13,6 @@ import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter -import io.legado.app.constant.EventBus import io.legado.app.data.entities.BookSource import io.legado.app.databinding.ItemBookSourceBinding import io.legado.app.lib.theme.backgroundColor @@ -115,11 +114,20 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : } else { payload.keySet().map { when (it) { - "selected" -> cbBookSource.isChecked = selected.contains(item) - EventBus.CHECK_SOURCE_MESSAGE -> { - ivDebugText.text = Debug.debugMessageMap[item.bookSourceUrl] ?: "" - ivDebugText.visibility = if(ivDebugText.text.toString().length > 1) View.VISIBLE else View.GONE - ivProgressBar.visibility = if(ivDebugText.text.toString().contains(Regex("成功|失败"))) View.GONE else View.VISIBLE + "selected" -> cbBookSource.isChecked = selected.contains(item) + "checkSourceMessage" -> { + ivDebugText.text = Debug.debugMessageMap[item.bookSourceUrl] ?: "" + val isEmpty = ivDebugText.text.toString().isEmpty() + var isFinalMessage = ivDebugText.text.toString().contains(Regex("成功|失败")) + if (!isEmpty && !Debug.isChecking && !isFinalMessage){ + Debug.updateFinalMessage(item.bookSourceUrl, "失败") + ivDebugText.text = Debug.debugMessageMap[item.bookSourceUrl] ?: "" + isFinalMessage = true + } + ivDebugText.visibility = + if (!isEmpty) View.VISIBLE else View.GONE + ivProgressBar.visibility = + if (isFinalMessage || isEmpty) View.GONE else View.VISIBLE } } } diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt index 5d0ada568..23892dc6f 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt @@ -44,8 +44,6 @@ class RssSourceDebugModel(application: Application) : BaseViewModel(application) } } - override fun postCheckMessageEvent(sourceUrl: String) {} - override fun onCleared() { super.onCleared() Debug.cancelDebug(true) diff --git a/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt b/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt index 534a68b47..c2c2f893f 100644 --- a/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt +++ b/app/src/main/java/io/legado/app/web/SourceDebugWebSocket.kt @@ -95,8 +95,4 @@ class SourceDebugWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) : } } - override fun postCheckMessageEvent(sourceUrl: String) { - TODO("Not yet implemented") - } - } diff --git a/app/src/main/res/values-es-rES/strings.xml b/app/src/main/res/values-es-rES/strings.xml index 216dd64f1..f7d66e2cc 100644 --- a/app/src/main/res/values-es-rES/strings.xml +++ b/app/src/main/res/values-es-rES/strings.xml @@ -854,5 +854,5 @@ 上一段|下一段/上一章|下一章 及时翻页,翻页时会停顿一下 La fuente del libro de cheques muestra un mensaje de depuración - Muestra los pasos y el tiempo de la solicitud de red durante la verificación de la fuente del libro; actualmente solo admite la verificación de un solo hilo + Muestra los pasos y el tiempo de la solicitud de red durante la verificación de la fuente del libro diff --git a/app/src/main/res/values-ja-rJP/strings.xml b/app/src/main/res/values-ja-rJP/strings.xml index 081b09ee5..b97574e03 100644 --- a/app/src/main/res/values-ja-rJP/strings.xml +++ b/app/src/main/res/values-ja-rJP/strings.xml @@ -855,5 +855,5 @@ 上一段|下一段/上一章|下一章 及时翻页,翻页时会停顿一下 Check book source shows debug message - Show network status and timestamp during source checking, currently only supports single-thread + Show network status and timestamp during source checking diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 03cdc19b9..b043ada60 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -854,5 +854,5 @@ 上一段|下一段/上一章|下一章 及时翻页,翻页时会停顿一下 Verificar a fonte do livro mostra uma mensagem de depuração - Exibir etapas de solicitação de rede e tempo durante a verificação da fonte do livro; atualmente, oferece suporte apenas à verificação de thread único + Exibir etapas de solicitação de rede e tempo durante a verificação da fonte do livro diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index aea6f7aae..ae8c19bce 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -856,6 +856,6 @@ 上一段|下一段/上一章|下一章 及时翻页,翻页时会停顿一下 校驗書源顯示詳細信息 - 書源校驗時顯示網絡請求步驟和時間,當前只支持單線程校驗 + 書源校驗時顯示網絡請求步驟和時間 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index d5984a40f..13f7cd8e6 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -857,6 +857,6 @@ 上一段|下一段/上一章|下一章 及时翻页,翻页时会停顿一下 校驗書源顯示詳細信息 - 書源校驗時顯示網絡請求步驟和時間,當前只支持單線程校驗 + 書源校驗時顯示網絡請求步驟和時間 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index bae9c5131..cfb947d90 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -858,6 +858,6 @@ 上一段|下一段/上一章|下一章 及时翻页,翻页时会停顿一下 校验显示详细信息 - 书源校验时显示网络请求步骤和时间,当前只支持单线程校验 + 书源校验时显示网络请求步骤和时间 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b721a2de9..92ca027b1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -859,6 +859,6 @@ 上一段|下一段/上一章|下一章 及时翻页,翻页时会停顿一下 Check book source shows debug message - Show network status and timestamp during source checking, currently only supports single-thread + Show network status and timestamp during source checking