feat: 优化代码

pull/121/head
kunfei 5 years ago
parent e49ff95677
commit f9c1183580
  1. 25
      app/src/main/java/io/legado/app/service/CheckSourceService.kt

@ -21,8 +21,8 @@ class CheckSourceService : BaseService() {
private var searchPool = private var searchPool =
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher()
private var task: Coroutine<*>? = null private var task: Coroutine<*>? = null
private var idsCount = 0 private val allIds = LinkedHashSet<String>()
private val unCheckIds = LinkedHashSet<String>() private val checkedIds = LinkedHashSet<String>()
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
@ -47,12 +47,12 @@ class CheckSourceService : BaseService() {
private fun check(ids: List<String>) { private fun check(ids: List<String>) {
task?.cancel() task?.cancel()
unCheckIds.clear() allIds.clear()
idsCount = ids.size checkedIds.clear()
unCheckIds.addAll(ids) allIds.addAll(ids)
updateNotification(0, getString(R.string.progress_show, 0, idsCount)) updateNotification(0, getString(R.string.progress_show, 0, allIds.size))
task = execute { task = execute(context = searchPool) {
unCheckIds.forEach { sourceUrl -> allIds.forEach { sourceUrl ->
App.db.bookSourceDao().getBookSource(sourceUrl)?.let { source -> App.db.bookSourceDao().getBookSource(sourceUrl)?.let { source ->
val webBook = WebBook(source) val webBook = WebBook(source)
webBook.searchBook("我的", scope = this, context = searchPool) webBook.searchBook("我的", scope = this, context = searchPool)
@ -60,11 +60,10 @@ class CheckSourceService : BaseService() {
source.addGroup("失效") source.addGroup("失效")
App.db.bookSourceDao().update(source) App.db.bookSourceDao().update(source)
}.onFinally { }.onFinally {
unCheckIds.remove(sourceUrl) checkedIds.add(sourceUrl)
val checkedCount = idsCount - unCheckIds.size
updateNotification( updateNotification(
checkedCount, checkedIds.size,
getString(R.string.progress_show, checkedCount, idsCount) getString(R.string.progress_show, checkedIds.size, allIds.size)
) )
} }
} }
@ -95,7 +94,7 @@ class CheckSourceService : BaseService() {
getString(R.string.cancel), getString(R.string.cancel),
IntentHelp.servicePendingIntent<CheckSourceService>(this, IntentAction.stop) IntentHelp.servicePendingIntent<CheckSourceService>(this, IntentAction.stop)
) )
builder.setProgress(idsCount, state, false) builder.setProgress(allIds.size, state, false)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
val notification = builder.build() val notification = builder.build()
startForeground(112202, notification) startForeground(112202, notification)

Loading…
Cancel
Save