|
|
@ -7,12 +7,14 @@ import android.os.Bundle |
|
|
|
import android.util.Log |
|
|
|
import android.util.Log |
|
|
|
import android.view.View |
|
|
|
import android.view.View |
|
|
|
import androidx.lifecycle.LiveData |
|
|
|
import androidx.lifecycle.LiveData |
|
|
|
|
|
|
|
import com.hankcs.hanlp.HanLP |
|
|
|
import io.legado.app.App |
|
|
|
import io.legado.app.App |
|
|
|
import io.legado.app.R |
|
|
|
import io.legado.app.R |
|
|
|
import io.legado.app.base.VMBaseFragment |
|
|
|
import io.legado.app.base.VMBaseFragment |
|
|
|
import io.legado.app.constant.EventBus |
|
|
|
import io.legado.app.constant.EventBus |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
import io.legado.app.data.entities.Book |
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
import io.legado.app.data.entities.BookChapter |
|
|
|
|
|
|
|
import io.legado.app.help.AppConfig |
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
import io.legado.app.help.BookHelp |
|
|
|
import io.legado.app.lib.theme.bottomBackground |
|
|
|
import io.legado.app.lib.theme.bottomBackground |
|
|
|
import io.legado.app.lib.theme.getPrimaryTextColor |
|
|
|
import io.legado.app.lib.theme.getPrimaryTextColor |
|
|
@ -40,7 +42,7 @@ class SearchListFragment : VMBaseFragment<SearchListViewModel>(R.layout.fragment |
|
|
|
lateinit var adapter: SearchListAdapter |
|
|
|
lateinit var adapter: SearchListAdapter |
|
|
|
private lateinit var mLayoutManager: UpLinearLayoutManager |
|
|
|
private lateinit var mLayoutManager: UpLinearLayoutManager |
|
|
|
private var searchResultCounts = 0 |
|
|
|
private var searchResultCounts = 0 |
|
|
|
private var pageSize = 0 |
|
|
|
private var durChapterIndex = 0 |
|
|
|
private var searchResultList: MutableList<SearchResult> = mutableListOf() |
|
|
|
private var searchResultList: MutableList<SearchResult> = mutableListOf() |
|
|
|
|
|
|
|
|
|
|
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { |
|
|
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { |
|
|
@ -76,9 +78,11 @@ class SearchListFragment : VMBaseFragment<SearchListViewModel>(R.layout.fragment |
|
|
|
@SuppressLint("SetTextI18n") |
|
|
|
@SuppressLint("SetTextI18n") |
|
|
|
private fun initBook() { |
|
|
|
private fun initBook() { |
|
|
|
launch { |
|
|
|
launch { |
|
|
|
|
|
|
|
|
|
|
|
tv_current_search_info.text = "搜索结果:$searchResultCounts" |
|
|
|
tv_current_search_info.text = "搜索结果:$searchResultCounts" |
|
|
|
viewModel.book?.let { |
|
|
|
viewModel.book?.let { |
|
|
|
initCacheFileNames(it) |
|
|
|
initCacheFileNames(it) |
|
|
|
|
|
|
|
durChapterIndex = it.durChapterIndex |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -117,15 +121,15 @@ class SearchListFragment : VMBaseFragment<SearchListViewModel>(R.layout.fragment |
|
|
|
val job = async(IO){ |
|
|
|
val job = async(IO){ |
|
|
|
if (isLocalBook || adapter.cacheFileNames.contains(BookHelp.formatChapterName(chapter))) { |
|
|
|
if (isLocalBook || adapter.cacheFileNames.contains(BookHelp.formatChapterName(chapter))) { |
|
|
|
searchResults = searchChapter(newText, chapter) |
|
|
|
searchResults = searchChapter(newText, chapter) |
|
|
|
Log.d("h11128", "find ${searchResults.size} results in chapter ${chapter.title}") |
|
|
|
//Log.d("h11128", "find ${searchResults.size} results in chapter ${chapter.title}") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
job.await() |
|
|
|
job.await() |
|
|
|
if (searchResults.isNotEmpty()){ |
|
|
|
if (searchResults.isNotEmpty()){ |
|
|
|
Log.d("h11128", "load ${searchResults.size} results in chapter ${chapter.title}") |
|
|
|
//Log.d("h11128", "load ${searchResults.size} results in chapter ${chapter.title}") |
|
|
|
searchResultList.addAll(searchResults) |
|
|
|
searchResultList.addAll(searchResults) |
|
|
|
tv_current_search_info.text = "搜索结果:$searchResultCounts" |
|
|
|
tv_current_search_info.text = "搜索结果:$searchResultCounts" |
|
|
|
Log.d("h11128", "searchResultList size ${searchResultList.size}") |
|
|
|
//Log.d("h11128", "searchResultList size ${searchResultList.size}") |
|
|
|
adapter.addItems(searchResults) |
|
|
|
adapter.addItems(searchResults) |
|
|
|
searchResults = listOf<SearchResult>() |
|
|
|
searchResults = listOf<SearchResult>() |
|
|
|
} |
|
|
|
} |
|
|
@ -134,55 +138,51 @@ class SearchListFragment : VMBaseFragment<SearchListViewModel>(R.layout.fragment |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun searchChapter(query: String, chapter: BookChapter?): List<SearchResult> { |
|
|
|
private suspend fun searchChapter(query: String, chapter: BookChapter?): List<SearchResult> { |
|
|
|
val searchResults: MutableList<SearchResult> = mutableListOf() |
|
|
|
val searchResults: MutableList<SearchResult> = mutableListOf() |
|
|
|
var positions : List<Int>? = listOf() |
|
|
|
var positions : List<Int> = listOf() |
|
|
|
|
|
|
|
var replaceContents: List<String>? = null |
|
|
|
|
|
|
|
var totalContents = "" |
|
|
|
if (chapter != null){ |
|
|
|
if (chapter != null){ |
|
|
|
viewModel.book?.let { bookSource -> |
|
|
|
viewModel.book?.let { bookSource -> |
|
|
|
val bookContent = BookHelp.getContent(bookSource, chapter) |
|
|
|
val bookContent = BookHelp.getContent(bookSource, chapter) |
|
|
|
if (bookContent != null){ |
|
|
|
if (bookContent != null){ |
|
|
|
//todo: 搜索替换后的正文句子列表 |
|
|
|
//搜索替换后的正文 |
|
|
|
/* |
|
|
|
val job = async(IO) { |
|
|
|
chapter.title = when (AppConfig.chineseConverterType) { |
|
|
|
chapter.title = when (AppConfig.chineseConverterType) { |
|
|
|
1 -> HanLP.convertToSimplifiedChinese(chapter.title) |
|
|
|
1 -> HanLP.convertToSimplifiedChinese(chapter.title) |
|
|
|
2 -> HanLP.convertToTraditionalChinese(chapter.title) |
|
|
|
2 -> HanLP.convertToTraditionalChinese(chapter.title) |
|
|
|
else -> chapter.title |
|
|
|
else -> chapter.title |
|
|
|
} |
|
|
|
} |
|
|
|
var replaceContents: List<String>? = null |
|
|
|
|
|
|
|
bookContent?.let { |
|
|
|
|
|
|
|
replaceContents = BookHelp.disposeContent( |
|
|
|
replaceContents = BookHelp.disposeContent( |
|
|
|
chapter.title, |
|
|
|
chapter.title, |
|
|
|
bookSource.name, |
|
|
|
bookSource.name, |
|
|
|
bookSource.bookUrl, |
|
|
|
bookSource.bookUrl, |
|
|
|
it, |
|
|
|
bookContent, |
|
|
|
bookSource.useReplaceRule |
|
|
|
bookSource.useReplaceRule |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
job.await() |
|
|
|
replaceContents?.map { |
|
|
|
while (replaceContents == null){ |
|
|
|
async(IO) { |
|
|
|
delay(100L) |
|
|
|
if (it.contains(query)) { |
|
|
|
} |
|
|
|
Log.d("targetd contents", it) |
|
|
|
totalContents = replaceContents!!.joinToString("") |
|
|
|
searchResult.add(it) |
|
|
|
positions = searchPosition(totalContents, query) |
|
|
|
} |
|
|
|
var count = 1 |
|
|
|
} |
|
|
|
positions.map{ |
|
|
|
}?.awaitAll() |
|
|
|
val construct = constructText(totalContents, it, query) |
|
|
|
*/ |
|
|
|
|
|
|
|
positions = searchPosition(bookContent, query) |
|
|
|
|
|
|
|
positions?.map{ |
|
|
|
|
|
|
|
val construct = constructText(bookContent, it, query) |
|
|
|
|
|
|
|
val result = SearchResult(index = searchResultCounts, |
|
|
|
val result = SearchResult(index = searchResultCounts, |
|
|
|
|
|
|
|
indexWithinChapter = count, |
|
|
|
text = construct[1] as String, |
|
|
|
text = construct[1] as String, |
|
|
|
chapterTitle = chapter.title, |
|
|
|
chapterTitle = chapter.title, |
|
|
|
query = query, |
|
|
|
query = query, |
|
|
|
pageIndex = 0, //todo: 计算搜索结果所在的pageIndex直接跳转 |
|
|
|
|
|
|
|
chapterIndex = chapter.index, |
|
|
|
chapterIndex = chapter.index, |
|
|
|
newPosition = construct[0] as Int, |
|
|
|
newPosition = construct[0] as Int, |
|
|
|
contentPosition = it |
|
|
|
contentPosition = it |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
count += 1 |
|
|
|
searchResultCounts += 1 |
|
|
|
searchResultCounts += 1 |
|
|
|
searchResults.add(result) |
|
|
|
searchResults.add(result) |
|
|
|
// Log.d("h11128", result.presentText) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -228,6 +228,7 @@ class SearchListFragment : VMBaseFragment<SearchListViewModel>(R.layout.fragment |
|
|
|
searchData.putExtra("index", searchResult.chapterIndex) |
|
|
|
searchData.putExtra("index", searchResult.chapterIndex) |
|
|
|
searchData.putExtra("contentPosition", searchResult.contentPosition) |
|
|
|
searchData.putExtra("contentPosition", searchResult.contentPosition) |
|
|
|
searchData.putExtra("query", searchResult.query) |
|
|
|
searchData.putExtra("query", searchResult.query) |
|
|
|
|
|
|
|
searchData.putExtra("indexWithinChapter", searchResult.indexWithinChapter) |
|
|
|
Log.d("h11128","current chapter index ${searchResult.chapterIndex}") |
|
|
|
Log.d("h11128","current chapter index ${searchResult.chapterIndex}") |
|
|
|
activity?.setResult(RESULT_OK, searchData) |
|
|
|
activity?.setResult(RESULT_OK, searchData) |
|
|
|
activity?.finish() |
|
|
|
activity?.finish() |
|
|
@ -235,4 +236,8 @@ class SearchListFragment : VMBaseFragment<SearchListViewModel>(R.layout.fragment |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun durChapterIndex(): Int { |
|
|
|
|
|
|
|
return durChapterIndex |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |