This commit is contained in:
Horis
2023-06-21 14:51:05 +08:00
parent c0367b4bd9
commit 42df748775
6 changed files with 26 additions and 9 deletions
@@ -18,8 +18,9 @@ abstract class BaseService : LifecycleService(), CoroutineScope by MainScope() {
scope: CoroutineScope = this, scope: CoroutineScope = this,
context: CoroutineContext = Dispatchers.IO, context: CoroutineContext = Dispatchers.IO,
start: CoroutineStart = CoroutineStart.DEFAULT, start: CoroutineStart = CoroutineStart.DEFAULT,
executeContext: CoroutineContext = Dispatchers.Main,
block: suspend CoroutineScope.() -> T block: suspend CoroutineScope.() -> T
) = Coroutine.async(scope, context, start) { block() } ) = Coroutine.async(scope, context, start, executeContext, block)
@CallSuper @CallSuper
override fun onCreate() { override fun onCreate() {
@@ -57,7 +58,7 @@ abstract class BaseService : LifecycleService(), CoroutineScope by MainScope() {
/** /**
* 检测通知权限 * 检测通知权限
*/ */
private fun checkNotificationPermission() { private fun checkNotificationPermission() {
PermissionsCompat.Builder() PermissionsCompat.Builder()
.addPermissions(Permissions.POST_NOTIFICATIONS) .addPermissions(Permissions.POST_NOTIFICATIONS)
.rationale(R.string.notification_permission_rationale) .rationale(R.string.notification_permission_rationale)
@@ -7,6 +7,7 @@ import androidx.lifecycle.viewModelScope
import io.legado.app.App import io.legado.app.App
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Deferred import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@@ -19,9 +20,11 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio
fun <T> execute( fun <T> execute(
scope: CoroutineScope = viewModelScope, scope: CoroutineScope = viewModelScope,
context: CoroutineContext = Dispatchers.IO, context: CoroutineContext = Dispatchers.IO,
start: CoroutineStart = CoroutineStart.DEFAULT,
executeContext: CoroutineContext = Dispatchers.Main,
block: suspend CoroutineScope.() -> T block: suspend CoroutineScope.() -> T
): Coroutine<T> { ): Coroutine<T> {
return Coroutine.async(scope, context) { block() } return Coroutine.async(scope, context, start, executeContext, block)
} }
fun <R> submit( fun <R> submit(
@@ -25,6 +25,7 @@ class Coroutine<T>(
val scope: CoroutineScope, val scope: CoroutineScope,
context: CoroutineContext = Dispatchers.IO, context: CoroutineContext = Dispatchers.IO,
val startOption: CoroutineStart = CoroutineStart.DEFAULT, val startOption: CoroutineStart = CoroutineStart.DEFAULT,
val executeContext: CoroutineContext = Dispatchers.Main,
block: suspend CoroutineScope.() -> T block: suspend CoroutineScope.() -> T
) { ) {
@@ -36,9 +37,10 @@ class Coroutine<T>(
scope: CoroutineScope = DEFAULT, scope: CoroutineScope = DEFAULT,
context: CoroutineContext = Dispatchers.IO, context: CoroutineContext = Dispatchers.IO,
start: CoroutineStart = CoroutineStart.DEFAULT, start: CoroutineStart = CoroutineStart.DEFAULT,
executeContext: CoroutineContext = Dispatchers.Main,
block: suspend CoroutineScope.() -> T block: suspend CoroutineScope.() -> T
): Coroutine<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, context: CoroutineContext,
block: suspend CoroutineScope.() -> T block: suspend CoroutineScope.() -> T
): Job { ): Job {
return (scope.plus(Dispatchers.Main)).launch(start = startOption) { return (scope.plus(executeContext)).launch(start = startOption) {
try { try {
start?.let { dispatchVoidCallback(this, it) } start?.let { dispatchVoidCallback(this, it) }
ensureActive() ensureActive()
@@ -122,7 +122,11 @@ class CheckSourceService : BaseService() {
*校验书源 *校验书源
*/ */
private fun check(source: BookSource) { private fun check(source: BookSource) {
execute(context = searchCoroutine, start = CoroutineStart.LAZY) { execute(
context = searchCoroutine,
start = CoroutineStart.LAZY,
executeContext = IO
) {
Debug.startChecking(source) Debug.startChecking(source)
var searchWord = CheckSource.keyword var searchWord = CheckSource.keyword
source.ruleSearch?.checkKeyWord?.let { source.ruleSearch?.checkKeyWord?.let {
@@ -76,7 +76,6 @@ class HttpReadAloudService : BaseReadAloudService(),
override fun play() { override fun play() {
pageChanged = false pageChanged = false
exoPlayer.stop() exoPlayer.stop()
exoPlayer.clearMediaItems()
if (!requestFocus()) return if (!requestFocus()) return
if (contentList.isEmpty()) { if (contentList.isEmpty()) {
AppLog.putDebug("朗读列表为空") AppLog.putDebug("朗读列表为空")
@@ -101,6 +100,7 @@ class HttpReadAloudService : BaseReadAloudService(),
} }
private fun downloadAndPlayAudios() { private fun downloadAndPlayAudios() {
exoPlayer.clearMediaItems()
downloadTask?.cancel() downloadTask?.cancel()
downloadTask = execute { downloadTask = execute {
downloadTaskActiveLock.withLock { downloadTaskActiveLock.withLock {
@@ -133,8 +133,12 @@ class HttpReadAloudService : BaseReadAloudService(),
val file = getSpeakFileAsMd5(fileName) val file = getSpeakFileAsMd5(fileName)
val mediaItem = MediaItem.fromUri(Uri.fromFile(file)) val mediaItem = MediaItem.fromUri(Uri.fromFile(file))
launch(Main) { launch(Main) {
if (exoPlayer.playbackState == Player.STATE_ENDED) {
exoPlayer.stop()
exoPlayer.clearMediaItems()
}
exoPlayer.addMediaItem(mediaItem) exoPlayer.addMediaItem(mediaItem)
if (!exoPlayer.isPlaying && nowSpeak == index) { if (!exoPlayer.isPlaying) {
exoPlayer.playWhenReady = !pause exoPlayer.playWhenReady = !pause
exoPlayer.prepare() exoPlayer.prepare()
} }
@@ -20,6 +20,7 @@ import io.legado.app.model.webBook.WebBook
import io.legado.app.service.CacheBookService import io.legado.app.service.CacheBookService
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.min import kotlin.math.min
@@ -85,9 +86,11 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
upTocJob?.cancel() upTocJob?.cancel()
upTocJob = null upTocJob = null
} }
onUpTocBooks.size < threadCount -> { onUpTocBooks.size < threadCount -> {
updateToc() updateToc()
} }
else -> { else -> {
delay(500) delay(500)
} }
@@ -119,7 +122,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
} }
waitUpTocBooks.remove(bookUrl) waitUpTocBooks.remove(bookUrl)
upTocAdd(bookUrl) upTocAdd(bookUrl)
execute(context = upTocPool) { execute(context = upTocPool, executeContext = IO) {
kotlin.runCatching { kotlin.runCatching {
val oldBook = book.copy() val oldBook = book.copy()
WebBook.runPreUpdateJs(source, book) WebBook.runPreUpdateJs(source, book)