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
private var debugSource: String? = null
private val tasks: CompositeCoroutine = CompositeCoroutine()
val debugMessageMap = ConcurrentHashMap<String, ArrayList<String>>()
val debugMessageMap = ConcurrentHashMap<String, String>()
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) {

@ -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<String>()
@ -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)
}
}

@ -428,7 +428,6 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
}
override fun observeLiveBus() {
val checkSourceMessageFlow = flow<String> {}
observeEvent<String>(EventBus.CHECK_SOURCE) { msg ->
snackBar?.setText(msg) ?: let {
snackBar = Snackbar
@ -449,24 +448,16 @@ 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()
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))) }
}
}
}

@ -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
}
}

Loading…
Cancel
Save