|
@@ -14,7 +14,6 @@ import android.view.inputmethod.InputMethodManager
|
|
import android.widget.*
|
|
import android.widget.*
|
|
import com.kdanmobile.kmpdfkit.pdfcommon.TextWord
|
|
import com.kdanmobile.kmpdfkit.pdfcommon.TextWord
|
|
import com.kdanmobile.reader.R
|
|
import com.kdanmobile.reader.R
|
|
-import com.kdanmobile.reader.ReaderViewModel
|
|
|
|
import com.kdanmobile.reader.screen.adapter.SearchAdapter
|
|
import com.kdanmobile.reader.screen.adapter.SearchAdapter
|
|
import com.kdanmobile.reader.screen.model.SearchResultInfo
|
|
import com.kdanmobile.reader.screen.model.SearchResultInfo
|
|
import com.kdanmobile.reader.screen.handler.PdfInfoHandler
|
|
import com.kdanmobile.reader.screen.handler.PdfInfoHandler
|
|
@@ -31,8 +30,8 @@ import kotlinx.android.synthetic.main.view_search.view.*
|
|
import java.lang.ref.WeakReference
|
|
import java.lang.ref.WeakReference
|
|
|
|
|
|
class SearchView: RelativeLayout, View.OnClickListener {
|
|
class SearchView: RelativeLayout, View.OnClickListener {
|
|
- private lateinit var pdfInfoHandler: PdfInfoHandler
|
|
|
|
- private lateinit var searchHandler: SearchHandler
|
|
|
|
|
|
+ private var pdfInfoHandler: PdfInfoHandler? = null
|
|
|
|
+ private var searchHandler: SearchHandler? = null
|
|
private lateinit var linearLayout: LinearLayout
|
|
private lateinit var linearLayout: LinearLayout
|
|
private lateinit var tv_page: TextView
|
|
private lateinit var tv_page: TextView
|
|
private lateinit var tv_text:TextView
|
|
private lateinit var tv_text:TextView
|
|
@@ -54,36 +53,38 @@ class SearchView: RelativeLayout, View.OnClickListener {
|
|
|
|
|
|
private var disposable: Disposable? = null
|
|
private var disposable: Disposable? = null
|
|
|
|
|
|
- constructor(context: Context, viewModel: ReaderViewModel) : super(context) {
|
|
|
|
- initView(viewModel, viewModel)
|
|
|
|
|
|
+ constructor(context: Context) : super(context) {
|
|
|
|
+ initView()
|
|
}
|
|
}
|
|
|
|
|
|
- constructor(context: Context, attributeSet: AttributeSet, viewModel: ReaderViewModel) : super(context, attributeSet) {
|
|
|
|
- initView(viewModel, viewModel)
|
|
|
|
|
|
+ constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
|
|
|
|
+ initView()
|
|
}
|
|
}
|
|
|
|
|
|
- constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int, viewModel: ReaderViewModel) : super(context, attributeSet, defStyleAttr) {
|
|
|
|
- initView(viewModel, viewModel)
|
|
|
|
|
|
+ constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) {
|
|
|
|
+ initView()
|
|
}
|
|
}
|
|
|
|
|
|
- private fun initView(pdfInfoHandler: PdfInfoHandler, searchHandler: SearchHandler) {
|
|
|
|
|
|
+ private fun initView() {
|
|
LayoutInflater.from(context).inflate(R.layout.view_search, this)
|
|
LayoutInflater.from(context).inflate(R.layout.view_search, this)
|
|
|
|
|
|
- this.pdfInfoHandler = pdfInfoHandler
|
|
|
|
- this.searchHandler = searchHandler
|
|
|
|
-
|
|
|
|
initHandler()
|
|
initHandler()
|
|
|
|
|
|
- initView()
|
|
|
|
|
|
+ initLayout()
|
|
|
|
|
|
setListener()
|
|
setListener()
|
|
|
|
|
|
postDelayed( { showSoftKeyboard() }, 200)
|
|
postDelayed( { showSoftKeyboard() }, 200)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fun setHandlers(pdfInfoHandler: PdfInfoHandler, searchHandler: SearchHandler) {
|
|
|
|
+ this.pdfInfoHandler = pdfInfoHandler
|
|
|
|
+ this.searchHandler = searchHandler
|
|
|
|
+ }
|
|
|
|
+
|
|
private fun initHandler() {
|
|
private fun initHandler() {
|
|
|
|
|
|
- class MyHandler(view: SearchView) : Handler() {
|
|
|
|
|
|
+ class WeakReferenceHandler(view: SearchView) : Handler() {
|
|
private val mView: WeakReference<SearchView> = WeakReference(view)
|
|
private val mView: WeakReference<SearchView> = WeakReference(view)
|
|
|
|
|
|
override fun handleMessage(msg: Message) {
|
|
override fun handleMessage(msg: Message) {
|
|
@@ -105,16 +106,16 @@ class SearchView: RelativeLayout, View.OnClickListener {
|
|
val position = msg.arg1
|
|
val position = msg.arg1
|
|
val info = view.list[position]
|
|
val info = view.list[position]
|
|
SearchTaskResult.set(SearchTaskResult(info.search, info.page, arrayOf(info.rf)))
|
|
SearchTaskResult.set(SearchTaskResult(info.search, info.page, arrayOf(info.rf)))
|
|
- view.pdfInfoHandler.setCurrentPage(view.list[position].page)
|
|
|
|
|
|
+ view.pdfInfoHandler?.setCurrentPage(view.list[position].page)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- msgHandler = MyHandler(this)
|
|
|
|
|
|
+ msgHandler = WeakReferenceHandler(this)
|
|
}
|
|
}
|
|
|
|
|
|
- private fun initView() {
|
|
|
|
|
|
+ private fun initLayout() {
|
|
|
|
|
|
linearLayout = ll_fragmentPdfReaderSearch
|
|
linearLayout = ll_fragmentPdfReaderSearch
|
|
tv_page = tv_fragmentPdfReaderSearch_page
|
|
tv_page = tv_fragmentPdfReaderSearch_page
|
|
@@ -179,10 +180,7 @@ class SearchView: RelativeLayout, View.OnClickListener {
|
|
msgHandler?.sendMessageDelayed(msgHandler?.obtainMessage(30, position, 0), 50)
|
|
msgHandler?.sendMessageDelayed(msgHandler?.obtainMessage(30, position, 0), 50)
|
|
}
|
|
}
|
|
|
|
|
|
- et_search.setOnEditorActionListener { v, actionId, event ->
|
|
|
|
- // if (actionId == EditorInfo.IME_ACTION_DONE) {
|
|
|
|
- // search();
|
|
|
|
- // }
|
|
|
|
|
|
+ et_search.setOnEditorActionListener { _, actionId, _ ->
|
|
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
|
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
|
if (!isPage && et_search.text.toString().length < 3) {
|
|
if (!isPage && et_search.text.toString().length < 3) {
|
|
val la = IntArray(2)
|
|
val la = IntArray(2)
|
|
@@ -212,12 +210,12 @@ class SearchView: RelativeLayout, View.OnClickListener {
|
|
et_search.setText(text)
|
|
et_search.setText(text)
|
|
et_search.setSelection(text.length)
|
|
et_search.setSelection(text.length)
|
|
}
|
|
}
|
|
- val searchSequence = et_search.text.toString()
|
|
|
|
- if (searchSequence.isNotEmpty()) {
|
|
|
|
- val page = Integer.parseInt(searchSequence)
|
|
|
|
- val pageCount = pdfInfoHandler.getPdfPageCount() ?: 0
|
|
|
|
|
|
+ val searchText = et_search.text.toString()
|
|
|
|
+ if (searchText.isNotEmpty()) {
|
|
|
|
+ val page = Integer.parseInt(searchText)
|
|
|
|
+ val pageCount = pdfInfoHandler?.getPdfPageCount() ?: 0
|
|
if (page > pageCount) {
|
|
if (page > pageCount) {
|
|
- val text = searchSequence.subSequence(0, searchSequence.length - 1).toString()
|
|
|
|
|
|
+ val text = searchText.subSequence(0, searchText.length - 1).toString()
|
|
et_search.setText(text)
|
|
et_search.setText(text)
|
|
et_search.setSelection(text.length)
|
|
et_search.setSelection(text.length)
|
|
}
|
|
}
|
|
@@ -275,14 +273,14 @@ class SearchView: RelativeLayout, View.OnClickListener {
|
|
}
|
|
}
|
|
|
|
|
|
private fun search() {
|
|
private fun search() {
|
|
- val search = et_search.text.toString()
|
|
|
|
- if (search.isNotEmpty()) {
|
|
|
|
|
|
+ val searchText = et_search.text.toString()
|
|
|
|
+ if (searchText.isNotEmpty()) {
|
|
if (isPage) {
|
|
if (isPage) {
|
|
- val page = Integer.parseInt(search)
|
|
|
|
|
|
+ val page = Integer.parseInt(searchText)
|
|
if (page > 0) {
|
|
if (page > 0) {
|
|
- pdfInfoHandler.setCurrentPage(page - 1)
|
|
|
|
|
|
+ pdfInfoHandler?.setCurrentPage(page - 1)
|
|
}
|
|
}
|
|
- } else if (search.length >= 3) {
|
|
|
|
|
|
+ } else if (searchText.length >= 3) {
|
|
if (list.size > 0) {
|
|
if (list.size > 0) {
|
|
adapter.setSelected(-1)
|
|
adapter.setSelected(-1)
|
|
adapter.clear()
|
|
adapter.clear()
|
|
@@ -292,16 +290,18 @@ class SearchView: RelativeLayout, View.OnClickListener {
|
|
|
|
|
|
isDone = false
|
|
isDone = false
|
|
disposable = Completable.create {
|
|
disposable = Completable.create {
|
|
- val pageCount = pdfInfoHandler.getPdfPageCount()!!
|
|
|
|
- for (i in 0 until pageCount) {
|
|
|
|
- if (!isDone) {
|
|
|
|
- val results = searchHandler.searchPage(i, search)
|
|
|
|
- if (results?.isNotEmpty() != null) {
|
|
|
|
- val texts = pdfInfoHandler.textLines(i)
|
|
|
|
- val l = dealData(texts, results, search, i)
|
|
|
|
- msgHandler?.sendMessage(msgHandler?.obtainMessage(10, l))
|
|
|
|
- if (list.size + l.size >= MAX_SEARCH_RESULT) {
|
|
|
|
- isDone = true
|
|
|
|
|
|
+ if (null != pdfInfoHandler && null != searchHandler) {
|
|
|
|
+ val pageCount = pdfInfoHandler!!.getPdfPageCount()
|
|
|
|
+ for (i in 0 until pageCount) {
|
|
|
|
+ if (!isDone) {
|
|
|
|
+ val results = searchHandler!!.searchPage(i, searchText)
|
|
|
|
+ if (results.isNotEmpty()) {
|
|
|
|
+ val texts = pdfInfoHandler!!.textLines(i)
|
|
|
|
+ val list = dealData(texts, results, searchText, i)
|
|
|
|
+ msgHandler?.sendMessage(msgHandler?.obtainMessage(10, list))
|
|
|
|
+ if (this.list.size + list.size >= MAX_SEARCH_RESULT) {
|
|
|
|
+ isDone = true
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -322,6 +322,14 @@ class SearchView: RelativeLayout, View.OnClickListener {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ override fun onDetachedFromWindow() {
|
|
|
|
+ super.onDetachedFromWindow()
|
|
|
|
+ if (null != disposable && !disposable!!.isDisposed) {
|
|
|
|
+ disposable?.dispose()
|
|
|
|
+ disposable = null
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
private fun dealData(texts: Array<Array<TextWord>>?, results: Array<RectF>?, search: String, page: Int): List<SearchResultInfo> {
|
|
private fun dealData(texts: Array<Array<TextWord>>?, results: Array<RectF>?, search: String, page: Int): List<SearchResultInfo> {
|
|
val list = ArrayList<SearchResultInfo>()
|
|
val list = ArrayList<SearchResultInfo>()
|
|
if (texts == null || results == null) {
|
|
if (texts == null || results == null) {
|