Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Nov 28, 2023
1 parent 87c89b5 commit a84d6e2
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@
## 对外提供api
-keep class io.legado.app.api.ReturnData{*;}

# Apache Commons Compress
-keep class org.apache.commons.compress.archivers.** {*;}
# 繁简转换
-keep class com.github.liuyueyi.quick.transfer.** {*;}


#-------------------Cronet------------------------------------
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/io/legado/app/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.content.Context
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Build
import com.github.liuyueyi.quick.transfer.ChineseUtils
import com.github.liuyueyi.quick.transfer.constants.TransType
import com.jeremyliao.liveeventbus.LiveEventBus
import io.legado.app.base.AppContextWrapper
Expand All @@ -31,6 +30,7 @@ import io.legado.app.help.http.okHttpClient
import io.legado.app.help.source.SourceHelp
import io.legado.app.help.storage.Backup
import io.legado.app.model.BookCover
import io.legado.app.utils.ChineseUtils
import io.legado.app.utils.defaultSharedPreferences
import io.legado.app.utils.getPrefBoolean
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -73,7 +73,10 @@ class App : Application() {
Backup.clearCache()
//初始化简繁转换引擎
when (AppConfig.chineseConverterType) {
1 -> ChineseUtils.preLoad(true, TransType.TRADITIONAL_TO_SIMPLE)
1 -> launch {
ChineseUtils.fixT2sDict()
}

2 -> ChineseUtils.preLoad(true, TransType.SIMPLE_TO_TRADITIONAL)
}
//调整排序序号
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Ignore
import androidx.room.Index
import com.github.liuyueyi.quick.transfer.ChineseUtils
import io.legado.app.R
import io.legado.app.constant.AppLog
import io.legado.app.constant.AppPattern
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/io/legado/app/help/JsExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.webkit.WebSettings
import androidx.annotation.Keep
import cn.hutool.core.codec.Base64
import cn.hutool.core.util.HexUtil
import com.github.liuyueyi.quick.transfer.ChineseUtils
import io.legado.app.utils.ChineseUtils
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppConst.dateFormat
import io.legado.app.constant.AppLog
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.legado.app.help.book

import com.github.liuyueyi.quick.transfer.ChineseUtils
import io.legado.app.constant.AppLog
import io.legado.app.constant.AppPattern.spaceRegex
import io.legado.app.data.appDb
Expand All @@ -10,6 +9,7 @@ import io.legado.app.data.entities.ReplaceRule
import io.legado.app.exception.RegexTimeoutException
import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.ReadBookConfig
import io.legado.app.utils.ChineseUtils
import io.legado.app.utils.escapeRegex
import io.legado.app.utils.replace
import io.legado.app.utils.stackTraceStr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.core.view.get
import com.github.liuyueyi.quick.transfer.ChineseUtils
import com.github.liuyueyi.quick.transfer.constants.TransType
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package io.legado.app.ui.book.searchContent


import android.app.Application
import com.github.liuyueyi.quick.transfer.ChineseUtils
import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.help.book.BookHelp
import io.legado.app.help.book.ContentProcessor
import io.legado.app.help.config.AppConfig
import io.legado.app.utils.ChineseUtils
import kotlinx.coroutines.ensureActive
import kotlin.coroutines.coroutineContext

class SearchContentViewModel(application: Application) : BaseViewModel(application) {
var bookUrl: String = ""
var book: Book? = null
var contentProcessor: ContentProcessor? = null
private var contentProcessor: ContentProcessor? = null
var lastQuery: String = ""
var searchResultCounts = 0
val cacheChapterNames = hashSetOf<String>()
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/io/legado/app/utils/ChineseUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.legado.app.utils

import com.github.liuyueyi.quick.transfer.ChineseUtils
import com.github.liuyueyi.quick.transfer.constants.TransType
import com.github.liuyueyi.quick.transfer.dictionary.DictionaryContainer

object ChineseUtils {

private var fixed = false

fun s2t(content: String): String {
return ChineseUtils.s2t(content)
}

fun t2s(content: String): String {
if (!fixed) {
fixed = true
fixT2sDict()
}
return ChineseUtils.t2s(content)
}

fun preLoad(async: Boolean, vararg transType: TransType) {
ChineseUtils.preLoad(async, *transType)
}

fun unLoad(vararg transType: TransType) {
ChineseUtils.unLoad(*transType)
}

fun fixT2sDict() {
val dict = DictionaryContainer.getInstance().getDictionary(TransType.TRADITIONAL_TO_SIMPLE)
dict.chars.run {
remove('')
remove('')
}
kotlin.runCatching {
dict.dict.run {
remove("支援")
remove("路易斯")
}
}
}

}
39 changes: 39 additions & 0 deletions app/src/main/java/io/legado/app/utils/TrieExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.legado.app.utils

import com.github.liuyueyi.quick.transfer.Trie
import com.github.liuyueyi.quick.transfer.TrieNode
import java.util.HashMap

fun <T> Trie<T>.getRoot(): TrieNode<T> {
val rootField = javaClass.getDeclaredField("root")
.apply { isAccessible = true }
@Suppress("UNCHECKED_CAST")
return rootField.get(this) as TrieNode<T>
}

fun <T> TrieNode<T>.getChildren(): HashMap<Char, TrieNode<T>> {
val childrenField = javaClass.getDeclaredField("children")
.apply { isAccessible = true }
@Suppress("UNCHECKED_CAST")
return childrenField.get(this) as HashMap<Char, TrieNode<T>>
}

fun <T> Trie<T>.remove(value: String) {
var node = getRoot()
val nodes = arrayListOf<TrieNode<T>>()
val chars = value.toCharArray()
for (c in chars) {
nodes.add(node)
node = node.getChildren()[c] ?: break
if (!node.isLeaf) {
continue
}
for ((ch, n) in chars.reversed().zip(nodes.reversed())) {
val children = n.getChildren()
children.remove(ch)
if (children.isNotEmpty()) {
break
}
}
}
}

0 comments on commit a84d6e2

Please sign in to comment.