pull/36/head
kunfei 5 years ago
parent 11d7133f93
commit cde3bcf0cb
  1. 2
      app/src/main/java/io/legado/app/base/BaseViewModel.kt
  2. 10
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt

@ -2,6 +2,7 @@ package io.legado.app.base
import android.app.Application import android.app.Application
import android.content.Context import android.content.Context
import androidx.annotation.CallSuper
import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.AndroidViewModel
import io.legado.app.App import io.legado.app.App
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
@ -31,6 +32,7 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio
return Coroutine.async(scope) { block().await() } return Coroutine.async(scope) { block().await() }
} }
@CallSuper
override fun onCleared() { override fun onCleared() {
super.onCleared() super.onCleared()
cancel() cancel()

@ -12,11 +12,14 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.anko.debug import org.jetbrains.anko.debug
import java.util.concurrent.Executors
class ChangeSourceViewModel(application: Application) : BaseViewModel(application) { class ChangeSourceViewModel(application: Application) : BaseViewModel(application) {
private var searchPool = Executors.newFixedThreadPool(99).asCoroutineDispatcher()
var callBack: CallBack? = null var callBack: CallBack? = null
val searchStateData = MutableLiveData<Boolean>() val searchStateData = MutableLiveData<Boolean>()
var name: String = "" var name: String = ""
@ -51,7 +54,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
} }
fun search() { fun search() {
task = execute { task = execute(context = searchPool) {
searchStateData.postValue(true) searchStateData.postValue(true)
val bookSourceList = App.db.bookSourceDao().allEnabled val bookSourceList = App.db.bookSourceDao().allEnabled
for (item in bookSourceList) { for (item in bookSourceList) {
@ -130,4 +133,9 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
interface CallBack { interface CallBack {
fun adapter(): ChangeSourceAdapter fun adapter(): ChangeSourceAdapter
} }
override fun onCleared() {
searchPool.close()
super.onCleared()
}
} }
Loading…
Cancel
Save