This commit is contained in:
Horis 2023-06-21 14:51:05 +08:00
parent 9158f63911
commit 36096e3632
6 changed files with 26 additions and 9 deletions

View File

@ -18,8 +18,9 @@ abstract class BaseService : LifecycleService(), CoroutineScope by MainScope() {
scope: CoroutineScope = this,
context: CoroutineContext = Dispatchers.IO,
start: CoroutineStart = CoroutineStart.DEFAULT,
executeContext: CoroutineContext = Dispatchers.Main,
block: suspend CoroutineScope.() -> T
) = Coroutine.async(scope, context, start) { block() }
) = Coroutine.async(scope, context, start, executeContext, block)
@CallSuper
override fun onCreate() {
@ -57,7 +58,7 @@ abstract class BaseService : LifecycleService(), CoroutineScope by MainScope() {
/**
* 检测通知权限
*/
private fun checkNotificationPermission() {
private fun checkNotificationPermission() {
PermissionsCompat.Builder()
.addPermissions(Permissions.POST_NOTIFICATIONS)
.rationale(R.string.notification_permission_rationale)

View File

@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope
import io.legado.app.App
import io.legado.app.help.coroutine.Coroutine
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext
@ -19,9 +20,11 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio
fun <T> execute(
scope: CoroutineScope = viewModelScope,
context: CoroutineContext = Dispatchers.IO,
start: CoroutineStart = CoroutineStart.DEFAULT,
executeContext: CoroutineContext = Dispatchers.Main,
block: suspend CoroutineScope.() -> T
): Coroutine<T> {
return Coroutine.async(scope, context) { block() }
return Coroutine.async(scope, context, start, executeContext, block)
}
fun <R> submit(

View File

@ -25,6 +25,7 @@ class Coroutine<T>(
val scope: CoroutineScope,
context: CoroutineContext = Dispatchers.IO,
val startOption: CoroutineStart = CoroutineStart.DEFAULT,
val executeContext: CoroutineContext = Dispatchers.Main,
block: suspend CoroutineScope.() -> T
) {
@ -36,9 +37,10 @@ class Coroutine<T>(
scope: CoroutineScope = DEFAULT,
context: CoroutineContext = Dispatchers.IO,
start: CoroutineStart = CoroutineStart.DEFAULT,
executeContext: CoroutineContext = Dispatchers.Main,
block: suspend CoroutineScope.() -> T
): Coroutine<T> {
return Coroutine(scope, context, start, block)
return Coroutine(scope, context, start, executeContext, block)
}
}
@ -158,7 +160,7 @@ class Coroutine<T>(
context: CoroutineContext,
block: suspend CoroutineScope.() -> T
): Job {
return (scope.plus(Dispatchers.Main)).launch(start = startOption) {
return (scope.plus(executeContext)).launch(start = startOption) {
try {
start?.let { dispatchVoidCallback(this, it) }
ensureActive()

View File

@ -122,7 +122,11 @@ class CheckSourceService : BaseService() {
*校验书源
*/
private fun check(source: BookSource) {
execute(context = searchCoroutine, start = CoroutineStart.LAZY) {
execute(
context = searchCoroutine,
start = CoroutineStart.LAZY,
executeContext = IO
) {
Debug.startChecking(source)
var searchWord = CheckSource.keyword
source.ruleSearch?.checkKeyWord?.let {

View File

@ -76,7 +76,6 @@ class HttpReadAloudService : BaseReadAloudService(),
override fun play() {
pageChanged = false
exoPlayer.stop()
exoPlayer.clearMediaItems()
if (!requestFocus()) return
if (contentList.isEmpty()) {
AppLog.putDebug("朗读列表为空")
@ -101,6 +100,7 @@ class HttpReadAloudService : BaseReadAloudService(),
}
private fun downloadAndPlayAudios() {
exoPlayer.clearMediaItems()
downloadTask?.cancel()
downloadTask = execute {
downloadTaskActiveLock.withLock {
@ -133,8 +133,12 @@ class HttpReadAloudService : BaseReadAloudService(),
val file = getSpeakFileAsMd5(fileName)
val mediaItem = MediaItem.fromUri(Uri.fromFile(file))
launch(Main) {
if (exoPlayer.playbackState == Player.STATE_ENDED) {
exoPlayer.stop()
exoPlayer.clearMediaItems()
}
exoPlayer.addMediaItem(mediaItem)
if (!exoPlayer.isPlaying && nowSpeak == index) {
if (!exoPlayer.isPlaying) {
exoPlayer.playWhenReady = !pause
exoPlayer.prepare()
}

View File

@ -20,6 +20,7 @@ import io.legado.app.model.webBook.WebBook
import io.legado.app.service.CacheBookService
import io.legado.app.utils.postEvent
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.Executors
import kotlin.math.min
@ -85,9 +86,11 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
upTocJob?.cancel()
upTocJob = null
}
onUpTocBooks.size < threadCount -> {
updateToc()
}
else -> {
delay(500)
}
@ -119,7 +122,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
}
waitUpTocBooks.remove(bookUrl)
upTocAdd(bookUrl)
execute(context = upTocPool) {
execute(context = upTocPool, executeContext = IO) {
kotlin.runCatching {
val oldBook = book.copy()
WebBook.runPreUpdateJs(source, book)