This commit is contained in:
Horis 2023-06-19 23:16:26 +08:00
parent 234dc7a8fa
commit e4c7de185c
3 changed files with 32 additions and 23 deletions

View File

@ -3,6 +3,7 @@ package io.legado.app.model
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.content.ContextCompat
import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction
import io.legado.app.data.appDb
@ -50,7 +51,7 @@ object ReadAloud {
intent.putExtra("play", play)
intent.putExtra("pageIndex", pageIndex)
intent.putExtra("startPos", startPos)
context.startService(intent)
ContextCompat.startForegroundService(context, intent)
}
fun playByEventBus(
@ -70,7 +71,7 @@ object ReadAloud {
if (BaseReadAloudService.isRun) {
val intent = Intent(context, aloudClass)
intent.action = IntentAction.pause
context.startService(intent)
ContextCompat.startForegroundService(context, intent)
}
}
@ -78,7 +79,7 @@ object ReadAloud {
if (BaseReadAloudService.isRun) {
val intent = Intent(context, aloudClass)
intent.action = IntentAction.resume
context.startService(intent)
ContextCompat.startForegroundService(context, intent)
}
}
@ -86,7 +87,7 @@ object ReadAloud {
if (BaseReadAloudService.isRun) {
val intent = Intent(context, aloudClass)
intent.action = IntentAction.stop
context.startService(intent)
ContextCompat.startForegroundService(context, intent)
}
}
@ -94,7 +95,7 @@ object ReadAloud {
if (BaseReadAloudService.isRun) {
val intent = Intent(context, aloudClass)
intent.action = IntentAction.prevParagraph
context.startService(intent)
ContextCompat.startForegroundService(context, intent)
}
}
@ -102,7 +103,7 @@ object ReadAloud {
if (BaseReadAloudService.isRun) {
val intent = Intent(context, aloudClass)
intent.action = IntentAction.nextParagraph
context.startService(intent)
ContextCompat.startForegroundService(context, intent)
}
}
@ -110,7 +111,7 @@ object ReadAloud {
if (BaseReadAloudService.isRun) {
val intent = Intent(context, aloudClass)
intent.action = IntentAction.upTtsSpeechRate
context.startService(intent)
ContextCompat.startForegroundService(context, intent)
}
}
@ -119,7 +120,7 @@ object ReadAloud {
val intent = Intent(context, aloudClass)
intent.action = IntentAction.setTimer
intent.putExtra("minute", minute)
context.startService(intent)
ContextCompat.startForegroundService(context, intent)
}
}

View File

@ -7,6 +7,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppLog
import io.legado.app.constant.AppPattern
import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey
@ -90,23 +91,30 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
searchCallback = null
}
}.map {
searchBooks.sortedWith { o1, o2 ->
val o1bs = SourceConfig.getBookScore(o1.origin, o1.name, o1.author)
val o2bs = SourceConfig.getBookScore(o2.origin, o2.name, o2.author)
when {
o1bs - o2bs > 0 -> -1
o1bs - o2bs < 0 -> 1
else -> {
val o1ss = SourceConfig.getSourceScore(o1.origin)
val o2ss = SourceConfig.getSourceScore(o2.origin)
when {
o1ss - o2ss > 0 -> -1
o1ss - o2ss < 0 -> 1
else -> o1.originOrder - o2.originOrder
kotlin.runCatching {
searchBooks.sortedWith { o1, o2 ->
val o1bs = SourceConfig.getBookScore(o1.origin, o1.name, o1.author)
val o2bs = SourceConfig.getBookScore(o2.origin, o2.name, o2.author)
when {
o1bs - o2bs > 0 -> -1
o1bs - o2bs < 0 -> 1
else -> {
val o1ss = SourceConfig.getSourceScore(o1.origin)
val o2ss = SourceConfig.getSourceScore(o2.origin)
when {
o1ss - o2ss > 0 -> -1
o1ss - o2ss < 0 -> 1
else -> {
val n = o1.originOrder - o2.originOrder
if (n == 0) -1 else n
}
}
}
}
}
}
}.onFailure {
AppLog.put("换源排序出错\n${it.localizedMessage}", it)
}.getOrDefault(searchBooks)
}.flowOn(IO)
@Volatile

View File

@ -43,7 +43,7 @@ inline fun <reified A : Activity> Context.startActivity(configIntent: Intent.()
}
inline fun <reified T : Service> Context.startService(configIntent: Intent.() -> Unit = {}) {
startService(Intent(this, T::class.java).apply(configIntent))
ContextCompat.startForegroundService(this, Intent(this, T::class.java).apply(configIntent))
}
inline fun <reified T : Service> Context.stopService() {