Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Dec 1, 2022
1 parent 1a52813 commit 3a69271
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 33 deletions.
16 changes: 8 additions & 8 deletions app/src/main/java/io/legado/app/help/book/ContentProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ class ContentProcessor private constructor(
var mContent = content
if (content != "null") {
//去除重复标题
try {
val name = Pattern.quote(book.name)
val title = Pattern.quote(chapter.title)
val titleRegex = "^(\\s|\\p{P}|${name})*${title}(\\s)*".toRegex()
mContent = mContent.replace(titleRegex, "")
} catch (e: Exception) {
AppLog.put("去除重复标题出错\n${e.localizedMessage}", e)
}
// try {
// val name = Pattern.quote(book.name)
// val title = Pattern.quote(chapter.title)
// val titleRegex = "^(\\s|\\p{P}|${name})*${title}(\\s)*".toRegex()
// mContent = mContent.replace(titleRegex, "")
// } catch (e: Exception) {
// AppLog.put("去除重复标题出错\n${e.localizedMessage}", e)
// }
if (reSegment && book.getReSegment()) {
//重新分段
mContent = ContentHelp.reSegment(mContent, chapter.title)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ suspend fun BookSource.exploreKinds(): List<ExploreKind> {
withContext(Dispatchers.IO) {
kotlin.runCatching {
var ruleStr = exploreUrl
if (exploreUrl.startsWith("<js>", false)
|| exploreUrl.startsWith("@js:", false)
if (exploreUrl.startsWith("<js>", true)
|| exploreUrl.startsWith("@js:", true)
) {
ruleStr = aCache.getAsString(exploreKindsKey)
if (ruleStr.isNullOrBlank()) {
Expand All @@ -50,8 +50,8 @@ suspend fun BookSource.exploreKinds(): List<ExploreKind> {
}
}
if (ruleStr.isJsonArray()) {
GSON.fromJsonArray<ExploreKind>(ruleStr).getOrThrow()?.let {
kinds.addAll(it)
GSON.fromJsonArray<ExploreKind?>(ruleStr).getOrThrow()?.let {
kinds.addAll(it.filterNotNull())
}
} else {
ruleStr.split("(&&|\n)+".toRegex()).forEach { kindStr ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_
}
R.id.menu_start_stop -> viewModel.startOrStopSearch()
R.id.menu_source_manage -> startActivity<BookSourceActivity>()
R.id.menu_refresh_list -> viewModel.startRefreshList()
else -> if (item?.groupId == R.id.source_group) {
if (!item.isChecked) {
item.isChecked = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
private var tasks = CompositeCoroutine()
private var screenKey: String = ""
private var bookSourceList = arrayListOf<BookSource>()
private var searchBookList = arrayListOf<SearchBook>()
private val searchBooks = Collections.synchronizedList(arrayListOf<SearchBook>())
private val tocMap = ConcurrentHashMap<String, List<BookChapter>>()
private var searchCallback: SourceCallback? = null
Expand Down Expand Up @@ -136,6 +137,9 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
}
}

/**
* 搜索书籍
*/
fun startSearch() {
execute {
stopSearch()
Expand Down Expand Up @@ -235,6 +239,58 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
}
}

/**
* 刷新列表
*/
fun startRefreshList() {
execute {
stopSearch()
searchBookList.clear()
searchBookList.addAll(searchBooks)
searchBooks.clear()
searchStateData.postValue(true)
initSearchPool()
for (i in 0 until threadCount) {
refreshList()
}
}
}

private fun refreshList() {
synchronized(this) {
if (searchIndex >= searchBookList.lastIndex) {
return
}
searchIndex++
}
val searchBook = searchBookList[searchIndex]
val task = Coroutine.async(scope = viewModelScope, context = searchPool!!) {
val source = appDb.bookSourceDao.getBookSource(searchBook.origin) ?: return@async
loadBookInfo(source, searchBook.toBook())
}.timeout(60000L)
.onError {
nextRefreshList()
}
.onSuccess {
nextRefreshList()
}
tasks.add(task)
}

private fun nextRefreshList() {
synchronized(this) {
if (searchIndex < searchBookList.lastIndex) {
refreshList()
} else {
searchIndex++
}
if (searchIndex >= searchBookList.lastIndex + min(searchBookList.size, threadCount)) {
searchStateData.postValue(false)
tasks.clear()
}
}
}

private fun getDbSearchBooks(): List<SearchBook> {
return if (screenKey.isEmpty()) {
if (AppConfig.changeSourceCheckAuthor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
book != null -> initBook(book)
else -> ReadBook.upMsg(context.getString(R.string.no_book))
}
}.onError {
val msg = "初始化数据失败\n${it.localizedMessage}"
ReadBook.upMsg(msg)
AppLog.put(msg, it)
}.onFinally {
ReadBook.saveRead()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,10 @@ class BookSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, BookS
}
}
binding.textToc.onClick {
val query = searchView.query
if (query.isNullOrBlank() || query.length <= 2) {
searchView.setQuery("++", false)
} else {
if (!query.startsWith("++")) {
searchView.setQuery("++$query", true)
} else {
searchView.setQuery(query, true)
}
}
prefixAutoComplete("++")
}
binding.textContent.onClick {
val query = searchView.query
if (query.isNullOrBlank() || query.length <= 2) {
searchView.setQuery("--", false)
} else {
if (!query.startsWith("--")) {
searchView.setQuery("--$query", true)
} else {
searchView.setQuery(query, true)
}
}
prefixAutoComplete("--")
}
launch {
val exploreKinds = viewModel.bookSource?.exploreKinds()?.filter {
Expand All @@ -156,6 +138,19 @@ class BookSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, BookS
}
}

private fun prefixAutoComplete(prefix: String) {
val query = searchView.query
if (query.isNullOrBlank() || query.length <= 2) {
searchView.setQuery(prefix, false)
} else {
if (!query.startsWith(prefix)) {
searchView.setQuery("$prefix$query", true)
} else {
searchView.setQuery(query, true)
}
}
}

/**
* 打开关闭历史界面
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class VerticalSeekBar @JvmOverloads constructor(context: Context, attrs: Attribu
}

init {
applyTint(context.accentColor)
if (!isInEditMode) {
applyTint(context.accentColor)
}
ViewCompat.setLayoutDirection(this, ViewCompat.LAYOUT_DIRECTION_LTR)

if (attrs != null) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/menu/change_source.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
android:title="@string/book_source_manage"
app:showAsAction="never" />

<item
android:id="@+id/menu_refresh_list"
android:title="@string/refresh_list"
app:showAsAction="never" />

<item
android:id="@+id/menu_check_author"
android:title="@string/checkAuthor"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es-rES/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1058,4 +1058,5 @@
<string name="groups_or_source">多分组/书源</string>
<string name="replace_state_change">替换(启用/禁用)</string>
<string name="show_last_update_time">显示上次更新时间</string>
<string name="refresh_list">刷新列表</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ja-rJP/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1061,4 +1061,5 @@
<string name="groups_or_source">多分组/书源</string>
<string name="replace_state_change">替换(启用/禁用)</string>
<string name="show_last_update_time">显示上次更新时间</string>
<string name="refresh_list">刷新列表</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1061,4 +1061,5 @@
<string name="groups_or_source">多分组/书源</string>
<string name="replace_state_change">替换(启用/禁用)</string>
<string name="show_last_update_time">显示上次更新时间</string>
<string name="refresh_list">刷新列表</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1058,4 +1058,5 @@
<string name="groups_or_source">多分组/书源</string>
<string name="replace_state_change">替换(启用/禁用)</string>
<string name="show_last_update_time">显示上次更新时间</string>
<string name="refresh_list">刷新列表</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1060,4 +1060,5 @@
<string name="groups_or_source">多分組/書源</string>
<string name="replace_state_change">取代(啟用/禁用)</string>
<string name="show_last_update_time">顯示上次更新時間</string>
<string name="refresh_list">刷新列表</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1060,4 +1060,5 @@
<string name="groups_or_source">多分组/书源</string>
<string name="replace_state_change">替换(启用/禁用)</string>
<string name="show_last_update_time">显示上次更新时间</string>
<string name="refresh_list">刷新列表</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1061,4 +1061,5 @@
<string name="groups_or_source">多分组/书源</string>
<string name="replace_state_change">替换(启用/禁用)</string>
<string name="show_last_update_time">显示上次更新时间</string>
<string name="refresh_list">刷新列表</string>
</resources>

0 comments on commit 3a69271

Please sign in to comment.