change back to use debugMessageMap

single thread works but last source is not showing the message
pull/1251/head
Jason Yao 3 years ago
parent b5308524db
commit 3539c118e2
  1. 14
      app/src/main/java/io/legado/app/model/Debug.kt
  2. 19
      app/src/main/java/io/legado/app/service/CheckSourceService.kt
  3. 15
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  4. 15
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt

@ -21,7 +21,8 @@ object Debug {
var callback: Callback? = null var callback: Callback? = null
private var debugSource: String? = null private var debugSource: String? = null
private val tasks: CompositeCoroutine = CompositeCoroutine() private val tasks: CompositeCoroutine = CompositeCoroutine()
val debugMessageMap = ConcurrentHashMap<String, ArrayList<String>>() val debugMessageMap = ConcurrentHashMap<String, String>()
var isChecking: Boolean = false
@SuppressLint("ConstantLocale") @SuppressLint("ConstantLocale")
private val DEBUG_TIME_FORMAT = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault()) private val DEBUG_TIME_FORMAT = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault())
@ -37,7 +38,7 @@ object Debug {
state: Int = 1 state: Int = 1
) { ) {
callback?.let { callback?.let {
if (debugSource != sourceUrl || !print) return if ((debugSource != sourceUrl || !print) && !isChecking) return
var printMsg = msg ?: "" var printMsg = msg ?: ""
if (isHtml) { if (isHtml) {
printMsg = HtmlFormatter.format(msg) printMsg = HtmlFormatter.format(msg)
@ -49,7 +50,8 @@ object Debug {
it.printLog(state, printMsg) it.printLog(state, printMsg)
Log.d(EventBus.CHECK_SOURCE_MESSAGE, "debugMessage to filter $printMsg") Log.d(EventBus.CHECK_SOURCE_MESSAGE, "debugMessage to filter $printMsg")
if (sourceUrl != null && printMsg.length < 30) { 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) { fun startChecking(source: BookSource) {
debugSource = source.bookSourceUrl
startTime = System.currentTimeMillis() startTime = System.currentTimeMillis()
debugMessageMap[source.bookSourceUrl] = arrayListOf() isChecking = true
debugMessageMap[source.bookSourceUrl] = "开始校验"
} }
fun startDebug(scope: CoroutineScope, rssSource: RssSource) { fun startDebug(scope: CoroutineScope, rssSource: RssSource) {

@ -10,7 +10,6 @@ import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction import io.legado.app.constant.IntentAction
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.AppConfig
import io.legado.app.help.IntentHelp import io.legado.app.help.IntentHelp
import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.model.Debug import io.legado.app.model.Debug
@ -24,7 +23,7 @@ import java.util.concurrent.Executors
import kotlin.math.min import kotlin.math.min
class CheckSourceService : BaseService() { class CheckSourceService : BaseService() {
private var threadCount = AppConfig.threadCount private var threadCount = 1
private var searchCoroutine = Executors.newFixedThreadPool(min(threadCount,8)).asCoroutineDispatcher() private var searchCoroutine = Executors.newFixedThreadPool(min(threadCount,8)).asCoroutineDispatcher()
private var tasks = CompositeCoroutine() private var tasks = CompositeCoroutine()
private val allIds = ArrayList<String>() private val allIds = ArrayList<String>()
@ -37,7 +36,7 @@ class CheckSourceService : BaseService() {
@Synchronized @Synchronized
override fun printCheckSourceMessage(sourceUrl: String, msg: String) { 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") Log.d(EventBus.CHECK_SOURCE_MESSAGE, "printCheckSourceMessage to post $msg")
} }
} }
@ -79,6 +78,7 @@ class CheckSourceService : BaseService() {
tasks.clear() tasks.clear()
searchCoroutine.close() searchCoroutine.close()
Debug.callback = null Debug.callback = null
Debug.isChecking = false
postEvent(EventBus.CHECK_SOURCE_DONE, 0) postEvent(EventBus.CHECK_SOURCE_DONE, 0)
} }
@ -112,7 +112,7 @@ class CheckSourceService : BaseService() {
if (index < allIds.size) { if (index < allIds.size) {
val sourceUrl = allIds[index] val sourceUrl = allIds[index]
appDb.bookSourceDao.getBookSource(sourceUrl)?.let { source -> appDb.bookSourceDao.getBookSource(sourceUrl)?.let { source ->
Debug.startCheck(source) Debug.startChecking(source)
check(source) check(source)
} ?: onNext(sourceUrl, "") } ?: onNext(sourceUrl, "")
} }
@ -150,6 +150,8 @@ class CheckSourceService : BaseService() {
"error:${it.localizedMessage} "error:${it.localizedMessage}
${source.bookSourceComment}" ${source.bookSourceComment}"
""".trimIndent() """.trimIndent()
val message = Debug.debugMessageMap[source.bookSourceUrl] + " 失败"
postEvent(EventBus.CHECK_SOURCE_MESSAGE, Pair(source.bookSourceUrl, message))
appDb.bookSourceDao.update(source) appDb.bookSourceDao.update(source)
}.onSuccess(searchCoroutine) { }.onSuccess(searchCoroutine) {
source.removeGroup("失效") source.removeGroup("失效")
@ -158,9 +160,16 @@ class CheckSourceService : BaseService() {
?.filterNot { ?.filterNot {
it.startsWith("error:") it.startsWith("error:")
}?.joinToString("\n") }?.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) appDb.bookSourceDao.update(source)
}.onFinally(searchCoroutine) { }.onFinally(searchCoroutine) {
postEvent(EventBus.CHECK_SOURCE_MESSAGE, Pair(source.bookSourceUrl, null))
onNext(source.bookSourceUrl, source.bookSourceName) onNext(source.bookSourceUrl, source.bookSourceName)
} }
} }

@ -428,7 +428,6 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
} }
override fun observeLiveBus() { override fun observeLiveBus() {
val checkSourceMessageFlow = flow<String> {}
observeEvent<String>(EventBus.CHECK_SOURCE) { msg -> observeEvent<String>(EventBus.CHECK_SOURCE) { msg ->
snackBar?.setText(msg) ?: let { snackBar?.setText(msg) ?: let {
snackBar = Snackbar snackBar = Snackbar
@ -449,23 +448,15 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
// } // }
// } // }
} }
observeEvent<Pair<String, String>>(EventBus.CHECK_SOURCE_MESSAGE) { messagePair -> observeEvent<Pair<String, String?>>(EventBus.CHECK_SOURCE_MESSAGE) { messagePair ->
sourceFlowJob?.cancel() sourceFlowJob?.cancel()
val messageFlow = Debug.debugMessageMap[messagePair.first]?.asFlow()
if (messageFlow != null) {
sourceFlowJob = launch { sourceFlowJob = launch {
var index: Int = -1 var index: Int = -1
appDb.bookSourceDao.flowSearch(messagePair.first) appDb.bookSourceDao.flowSearch(messagePair.first)
.map { adapter.getItems().indexOf(it[0]) } .map { adapter.getItems().indexOf(it[0]) }
.collect { .collect {
index = it } index = it
if (index > -1){ adapter.notifyItemChanged(index, bundleOf(Pair(EventBus.CHECK_SOURCE_MESSAGE, messagePair.second))) }
messageFlow.onEach { delay(300L) }.buffer(10)
.collect { adapter.notifyItemChanged(index, bundleOf(Pair(EventBus.CHECK_SOURCE_MESSAGE, it))) }
}
}
} }

@ -109,7 +109,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
} }
swtEnabled.isChecked = item.enabled swtEnabled.isChecked = item.enabled
cbBookSource.isChecked = selected.contains(item) 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 ivDebugText.visibility = if(ivDebugText.text.toString().length > 1) View.VISIBLE else View.GONE
upShowExplore(ivExplore, item) upShowExplore(ivExplore, item)
} else { } else {
@ -117,13 +117,20 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) :
when (it) { when (it) {
"selected" -> cbBookSource.isChecked = selected.contains(item) "selected" -> cbBookSource.isChecked = selected.contains(item)
"startChecking" -> { "startChecking" -> {
ivProgressBar.visibility = if(selected.contains(item)) View.VISIBLE else View.GONE ivProgressBar.visibility = if(selected.contains(item)) View.VISIBLE else ivProgressBar.visibility
} }
"checkSourceDone" -> { "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 -> { 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 ivDebugText.visibility = if(ivDebugText.text.toString().length > 1) View.VISIBLE else View.GONE
} }
} }

Loading…
Cancel
Save