search content

be able to show the counts of query for each chapter
Still don't know why Coroutine lauch will execute repeatedly
pull/374/head
Jason Yao 4 years ago
parent 08e1650ca1
commit f48c44bb92
  1. 4
      app/src/main/AndroidManifest.xml
  2. 3
      app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt
  3. 3
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 93
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchListFragment.kt
  5. 9
      app/src/main/res/layout/view_read_menu.xml

@ -222,6 +222,10 @@
android:name=".ui.book.chapterlist.ChapterListActivity"
android:launchMode="singleTop"
android:screenOrientation="behind" />
<activity
android:name=".ui.book.searchContent.SearchListActivity"
android:launchMode="singleTop"
android:screenOrientation="behind" />
<!-- RSS条目 -->
<activity
android:name=".ui.rss.article.RssSortActivity"

@ -25,6 +25,9 @@ interface BookChapterDao {
@Query("select * from chapters where bookUrl = :bookUrl and `index` = :index")
fun getChapter(bookUrl: String, index: Int): BookChapter?
@Query("select * from chapters where bookUrl = :bookUrl and `title` = :title")
fun getChapter(bookUrl: String, title: String): BookChapter?
@Query("select count(url) from chapters where bookUrl = :bookUrl")
fun getChapterCount(bookUrl: String): Int

@ -47,6 +47,7 @@ import io.legado.app.ui.book.read.page.ContentTextView
import io.legado.app.ui.book.read.page.PageView
import io.legado.app.ui.book.read.page.TextPageFactory
import io.legado.app.ui.book.read.page.delegate.PageDelegate
import io.legado.app.ui.book.searchContent.SearchListActivity
import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.login.SourceLogin
import io.legado.app.ui.replacerule.ReplaceRuleActivity
@ -672,7 +673,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
*/
override fun openSearchList() {
ReadBook.book?.let {
startActivityForResult<ChapterListActivity>(
startActivityForResult<SearchListActivity>(
requestCodeChapterList,
Pair("bookUrl", it.bookUrl)
)

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.LiveData
import io.legado.app.App
@ -13,19 +14,16 @@ import io.legado.app.constant.EventBus
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.lib.theme.bottomBackground
import io.legado.app.lib.theme.getPrimaryTextColor
import io.legado.app.ui.widget.recycler.UpLinearLayoutManager
import io.legado.app.ui.widget.recycler.VerticalDivider
import io.legado.app.utils.ColorUtils
import io.legado.app.utils.getViewModelOfActivity
import io.legado.app.utils.observeEvent
import kotlinx.android.synthetic.main.fragment_search_list.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.anko.sdk27.listeners.onClick
import java.util.regex.Pattern
class SearchListFragment : VMBaseFragment<SearchListViewModel>(R.layout.fragment_search_list),
SearchListAdapter.Callback,
@ -123,20 +121,91 @@ class SearchListFragment : VMBaseFragment<SearchListViewModel>(R.layout.fragment
override fun startContentSearch(newText: String?) {
if (newText.isNullOrBlank()) {
//initDoc()
initDoc()
} else {
if (isLocalBook){
var count: Int = 0
val beginTime = System.currentTimeMillis()
App.db.bookChapterDao().getChapterList(viewModel.bookUrl).map{
launch(IO) {
if (isLocalBook ||
adapter.cacheFileNames.contains(BookHelp.formatChapterName(it))
) {
val value = searchChapter(newText, it)
count += value
}
}
}
//adapter.setItems(list)
val finishedTime = System.currentTimeMillis() - beginTime
Log.d("Jason", "Search finished, the total time cost is $finishedTime")
Log.d("Jason", "Search finished, the total count is $count")
//tocLiveData?.removeObservers(this)
//tocLiveData = App.db.bookChapterDao().liveDataSearch(viewModel.bookUrl, newText)
//tocLiveData?.observe(viewLifecycleOwner, {
//adapter.setItems(it)
//})
}
}
tocLiveData?.removeObservers(this)
tocLiveData = App.db.bookChapterDao().liveDataSearch(viewModel.bookUrl, newText)
tocLiveData?.observe(viewLifecycleOwner, {
adapter.setItems(it)
})
private suspend fun searchChapter(query: String, chapter: BookChapter?): Int {
val startTime = System.currentTimeMillis()
val searchResult: MutableList<String> = mutableListOf()
var count = 0
if (chapter != null){
Log.d("Jason", "Search ${chapter.title}")
viewModel.book?.let { bookSource ->
val bookContent = BookHelp.getContent(bookSource, chapter)
/* replace content, let's focus on original content first
chapter.title = when (AppConfig.chineseConverterType) {
1 -> HanLP.convertToSimplifiedChinese(chapter.title)
2 -> HanLP.convertToTraditionalChinese(chapter.title)
else -> chapter.title
}
var replaceContents: List<String>? = null
bookContent?.let {
replaceContents = BookHelp.disposeContent(
chapter.title,
bookSource.name,
bookSource.bookUrl,
it,
bookSource.useReplaceRule
)
}
replaceContents?.map {
async(IO) {
if (it.contains(query)) {
Log.d("targetd contents", it)
searchResult.add(it)
}
}
}?.awaitAll()
*/
count = bookContent?.let { countMatches(it, query) }!!
Log.d("Jason", "Search ${chapter.title} finished, the appeared count is $count")
}
val endTime = System.currentTimeMillis() - startTime
Log.d("Jason", "Search ${chapter.title} finished, the time cost is $endTime")
}
return count
}
fun countMatches(string: String, pattern: String): Int {
val matcher = Pattern.compile(pattern).matcher(string)
var count = 0
while (matcher.find()) {
count++
}
return count
}
override val isLocalBook: Boolean
get() = viewModel.book?.isLocalBook() == true

@ -260,7 +260,12 @@
android:textSize="12sp" />
</LinearLayout>
<!--目录按钮-->
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
<!--搜索按钮-->
<LinearLayout
android:id="@+id/ll_search"
android:layout_width="60dp"
@ -278,7 +283,7 @@
android:layout_height="0dp"
android:layout_weight="1"
android:contentDescription="@string/search_content"
android:src="@drawable/ic_toc"
android:src="@drawable/ic_search"
app:tint="@color/primaryText"
tools:ignore="NestedWeights" />

Loading…
Cancel
Save