From 74d6e901247630636a2eec2da57491639f17f804 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 13 Oct 2022 17:56:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/data/entities/ReplaceRule.kt | 2 +- .../ui/replace/edit/ReplaceEditViewModel.kt | 8 ++++---- .../io/legado/app/utils/RegexExtensions.kt | 19 ++++++++----------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt b/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt index 4c7f92047..9f69d31d8 100644 --- a/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt @@ -48,7 +48,7 @@ data class ReplaceRule( var timeoutMillisecond: Long = 3000L, //排序 @ColumnInfo(name = "sortOrder", defaultValue = "0") - var order: Int = 0 + var order: Int = Int.MIN_VALUE ) : Parcelable { override fun equals(other: Any?): Boolean { diff --git a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt index fd37bb36d..8e5f963fd 100644 --- a/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replace/edit/ReplaceEditViewModel.kt @@ -13,13 +13,13 @@ class ReplaceEditViewModel(application: Application) : BaseViewModel(application fun initData(intent: Intent, finally: (replaceRule: ReplaceRule) -> Unit) { execute { val id = intent.getLongExtra("id", -1) - if (id > 0) { - replaceRule = appDb.replaceRuleDao.findById(id) + replaceRule = if (id > 0) { + appDb.replaceRuleDao.findById(id) } else { val pattern = intent.getStringExtra("pattern") ?: "" val isRegex = intent.getBooleanExtra("isRegex", false) val scope = intent.getStringExtra("scope") - replaceRule = ReplaceRule( + ReplaceRule( name = pattern, pattern = pattern, isRegex = isRegex, @@ -35,7 +35,7 @@ class ReplaceEditViewModel(application: Application) : BaseViewModel(application fun save(replaceRule: ReplaceRule, success: () -> Unit) { execute { - if (replaceRule.order == 0) { + if (replaceRule.order == Int.MIN_VALUE) { replaceRule.order = appDb.replaceRuleDao.maxOrder + 1 } appDb.replaceRuleDao.insert(replaceRule) diff --git a/app/src/main/java/io/legado/app/utils/RegexExtensions.kt b/app/src/main/java/io/legado/app/utils/RegexExtensions.kt index 1fba7d741..a70a31536 100644 --- a/app/src/main/java/io/legado/app/utils/RegexExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/RegexExtensions.kt @@ -5,9 +5,7 @@ import com.script.SimpleBindings import io.legado.app.constant.AppConst import io.legado.app.exception.RegexTimeoutException import io.legado.app.help.CrashHandler -import io.legado.app.help.coroutine.Coroutine import kotlinx.coroutines.suspendCancellableCoroutine -import splitties.init.appCtx import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException @@ -20,7 +18,7 @@ private val handler by lazy { buildMainHandler() } suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String { val charSequence = this return suspendCancellableCoroutine { block -> - val coroutine = Coroutine.async { + val thread = Thread { try { if (replacement.startsWith("@js:")) { val js = replacement.substring(4) @@ -43,18 +41,17 @@ suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Lon block.resumeWithException(e) } } + thread.start() handler.postDelayed(timeout) { - if (coroutine.isActive) { - val timeoutMsg = "替换超时,3秒后还未结束将重启应用\n替换规则$regex\n替换内容:${this}" + if (thread.isAlive) { + runCatching { + @Suppress("DEPRECATION") + thread.stop() + } + val timeoutMsg = "替换超时,将禁用替换规则" val exception = RegexTimeoutException(timeoutMsg) block.cancel(exception) - appCtx.longToastOnUi(timeoutMsg) CrashHandler.saveCrashInfo2File(exception) - handler.postDelayed(3000) { - if (coroutine.isActive) { - appCtx.restart() - } - } } } }