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 cb08cf6f9..36a9ff22d 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) { + if (isChecking && 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,13 +78,12 @@ 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 } @@ -261,7 +272,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..945f0eda3 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() } @@ -120,7 +108,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 +140,8 @@ class CheckSourceService : BaseService() { "error:${it.localizedMessage} ${source.bookSourceComment}" """.trimIndent() - debugCallback?.let { + if (showCheckSourceMessage) { Debug.debugMessageMap[source.bookSourceUrl] = Debug.debugMessageMap[source.bookSourceUrl] + " 失败" - postEvent(EventBus.CHECK_SOURCE_MESSAGE, source.bookSourceUrl) } appDb.bookSourceDao.update(source) }.onSuccess(searchCoroutine) { @@ -164,11 +151,9 @@ class CheckSourceService : BaseService() { ?.filterNot { it.startsWith("error:") }?.joinToString("\n") - debugCallback?.let { debugCallback + if (showCheckSourceMessage) { Debug.debugMessageMap[source.bookSourceUrl] = Debug.debugMessageMap[source.bookSourceUrl] + " 成功" - postEvent(EventBus.CHECK_SOURCE_MESSAGE, 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..108aaa387 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,9 +41,8 @@ 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 class BookSourceActivity : VMBaseActivity(), @@ -340,6 +340,9 @@ 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()) + Debug.isChecking = true + return async(start = CoroutineStart.LAZY) { + flow { + while (true) { + emit(Debug.isChecking) + delay(300L) } + }.collect { + adapter.notifyItemRangeChanged(firstIndex, lastIndex + 1, bundleOf(Pair("checkSourceMessage", null))) + if (!it) { + 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..4a20c7818 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,15 @@ 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() + ivDebugText.visibility = + if (!isEmpty) View.VISIBLE else View.GONE + ivProgressBar.visibility = + if (ivDebugText.text.toString().contains(Regex("成功|失败")) || 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") - } - }