优化搜索

master
fengyuecanzhu 3 years ago
parent 08d0a46204
commit f8d061236e
  1. 6
      app/src/main/java/xyz/fycz/myreader/ui/activity/BookDetailedActivity.java
  2. 2
      app/src/main/java/xyz/fycz/myreader/ui/activity/SearchBookActivity.java
  3. 6
      app/src/main/java/xyz/fycz/myreader/ui/adapter/BookTagAdapter.java
  4. 122
      app/src/main/java/xyz/fycz/myreader/ui/adapter/SearchAdapter.kt
  5. 6
      app/src/main/java/xyz/fycz/myreader/ui/adapter/holder/FindBookHolder.java
  6. 11
      app/src/main/java/xyz/fycz/myreader/webapi/crawler/base/BaseReadCrawler.java
  7. 1
      app/src/main/res/values/strings.xml

@ -168,13 +168,13 @@ public class BookDetailedActivity extends BaseActivity {
tagList.clear();
String type = mBook.getType();
if (!StringHelper.isEmpty(type))
tagList.add("0:" + type);
tagList.add(type);
String wordCount = mBook.getWordCount();
if (!StringHelper.isEmpty(wordCount))
tagList.add("1:" + wordCount);
tagList.add(wordCount);
String status = mBook.getStatus();
if (!StringHelper.isEmpty(status))
tagList.add("2:" + status);
tagList.add(status);
binding.ih.tflBookTag.setAdapter(new BookTagAdapter(this, tagList, 13));
}

@ -584,7 +584,7 @@ public class SearchBookActivity extends BaseActivity {
binding.srlSearchBookList.setEnableRefresh(false);
} else {
showBooks = true;
mSearchBookAdapter = new SearchAdapter(this, searchKey, searchEngine);
mSearchBookAdapter = new SearchAdapter(this, searchKey);
binding.rvSearchBooksList.setAdapter(mSearchBookAdapter);
binding.srlSearchBookList.setEnableRefresh(true);
binding.rvSearchBooksList.setVisibility(View.VISIBLE);

@ -35,12 +35,12 @@ public class BookTagAdapter extends TagAdapter<String> {
tvTagName = (TextView) View.inflate(context, R.layout.item_book_tag, null);
tvTagName.setTextSize(textSize);
//默认为分类
if (tagName.startsWith("1:")) { //字数
if (position % 3 == 1) { //字数
tvTagName.setBackground(ContextCompat.getDrawable(context, R.drawable.tag_green_shape));
} else if (tagName.startsWith("2:")) {//连载状态
} else if (position % 3 == 2) {//连载状态
tvTagName.setBackground(ContextCompat.getDrawable(context, R.drawable.tag_red_shape));
}
tvTagName.setText(tagName.substring(2));
tvTagName.setText(tagName);
} catch (Exception e) {
e.printStackTrace();
ToastUtils.showError("" + e.getLocalizedMessage());

@ -6,31 +6,23 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import xyz.fycz.myreader.R
import xyz.fycz.myreader.application.App
import xyz.fycz.myreader.application.SysManager
import xyz.fycz.myreader.base.BitIntentDataManager
import xyz.fycz.myreader.base.adapter2.DiffRecyclerAdapter
import xyz.fycz.myreader.base.adapter2.ItemViewHolder
import xyz.fycz.myreader.base.adapter2.onClick
import xyz.fycz.myreader.databinding.SearchBookItemBinding
import xyz.fycz.myreader.entity.SearchBookBean
import xyz.fycz.myreader.greendao.entity.Book
import xyz.fycz.myreader.model.SearchEngine
import xyz.fycz.myreader.model.SearchEngine.OnGetBookInfoListener
import xyz.fycz.myreader.model.mulvalmap.ConMVMap
import xyz.fycz.myreader.model.sourceAnalyzer.BookSourceManager
import xyz.fycz.myreader.ui.activity.BookDetailedActivity
import xyz.fycz.myreader.util.help.StringHelper
import xyz.fycz.myreader.util.utils.KeyWordUtils
import xyz.fycz.myreader.util.utils.NetworkUtils
import xyz.fycz.myreader.util.utils.StringUtils
import xyz.fycz.myreader.webapi.crawler.ReadCrawlerUtil
import xyz.fycz.myreader.webapi.crawler.base.BookInfoCrawler
/**
* @author fengyue
@ -39,11 +31,10 @@ import xyz.fycz.myreader.webapi.crawler.base.BookInfoCrawler
class SearchAdapter(
context: Context,
val keyword: String,
private val searchEngine: SearchEngine
val keyword: String
) : DiffRecyclerAdapter<SearchBookBean, SearchBookItemBinding>(context) {
private val mBooks: ConMVMap<SearchBookBean, Book> = ConMVMap()
private var mList: List<SearchBookBean> = ArrayList()
private lateinit var mList: List<SearchBookBean>
private val tagList: MutableList<String> = ArrayList()
private val handler = Handler(Looper.getMainLooper())
private var postTime = 0L
@ -116,82 +107,55 @@ class SearchAdapter(
}
val book = aBooks.getOrNull(0) ?: return
val source = BookSourceManager.getBookSourceByStr(book.source)
val rc = ReadCrawlerUtil.getReadCrawler(source)
data.sourceName = source.sourceName
books2SearchBookBean(data, aBooks)
binding.run {
if (data.imgUrl.isNullOrEmpty()) {
data.imgUrl = ""
} else {
data.imgUrl = NetworkUtils.getAbsoluteURL(rc.nameSpace, data.imgUrl)
}
ivBookImg.load(data.imgUrl, data.name, data.author)
KeyWordUtils.setKeyWord(tvBookName, data.name, keyword)
if (data.author.isNullOrEmpty()) {
data.author = ""
} else {
KeyWordUtils.setKeyWord(tvBookAuthor, data.author, keyword)
}
KeyWordUtils.setKeyWord(tvBookAuthor, data.author ?: "", keyword)
initTagList(this, data)
if (data.lastChapter.isNullOrEmpty()) {
data.lastChapter = ""
} else {
tvBookNewestChapter.text = context.getString(
R.string.newest_chapter,
data.lastChapter
upLast(binding, data.lastChapter)
tvBookDesc.text = String.format("简介:%s", data.desc ?: "暂无简介")
tvBookSource.text = context.getString(
R.string.source_title_num,
data.sourceName,
data.sourceCount
)
}
if (data.desc.isNullOrEmpty()) {
data.desc = ""
} else {
tvBookDesc.text = String.format("简介:%s", data.desc)
}
tvBookSource.text = context.getString(
private fun bindChange(binding: SearchBookItemBinding, data: SearchBookBean, payload: Bundle) {
binding.run {
initTagList(this, data)
payload.keySet().forEach {
when (it) {
"sourceCount" -> tvBookSource.text = context.getString(
R.string.source_title_num,
data.sourceName,
data.sourceCount
)
}
App.getHandler().postDelayed({
val url = rc.nameSpace
if (needGetInfo(data) && rc is BookInfoCrawler) {
Log.i(data.name, "initOtherInfo")
searchEngine.getBookInfo(book, rc) { isSuccess: Boolean ->
if (isSuccess) {
val books: MutableList<Book> = ArrayList()
books.add(book)
books2SearchBookBean(data, books)
val payload = Bundle()
if (!data.imgUrl.isNullOrEmpty())
payload.putString(
"imgUrl",
NetworkUtils.getAbsoluteURL(url, data.imgUrl)
"imgUrl" -> ivBookImg.load(
data.imgUrl,
data.name,
data.author
)
if (!data.type.isNullOrEmpty())
payload.putString("type", data.type)
if (!data.status.isNullOrEmpty())
payload.putString("status", data.status)
if (!data.wordCount.isNullOrEmpty())
payload.putString("wordCount", data.wordCount)
if (!data.lastChapter.isNullOrEmpty())
payload.putString("last", data.lastChapter)
if (!data.desc.isNullOrEmpty())
payload.putString("desc", data.desc)
bindChange(binding, data, payload)
"last" -> upLast(binding, data.lastChapter)
"desc" -> tvBookDesc.text = String.format("简介:%s", data.desc)
}
}
}
}, 1000)
}
private fun initTagList(binding: SearchBookItemBinding, data: SearchBookBean) {
tagList.clear()
val type = data.type
if (!type.isNullOrEmpty()) tagList.add("0:$type")
if (!type.isNullOrEmpty()){
tagList.add(type)
}
val wordCount = data.wordCount
if (!wordCount.isNullOrEmpty()) tagList.add("1:$wordCount")
if (!wordCount.isNullOrEmpty()) tagList.add(wordCount)
val status = data.status
if (!status.isNullOrEmpty()) tagList.add("2:$status")
if (!status.isNullOrEmpty()) tagList.add(status)
binding.run {
if (tagList.size == 0) {
tflBookTag.visibility = View.GONE
@ -202,27 +166,16 @@ class SearchAdapter(
}
}
private fun bindChange(binding: SearchBookItemBinding, data: SearchBookBean, payload: Bundle) {
private fun upLast(binding: SearchBookItemBinding, lastChapter: String?) {
binding.run {
initTagList(this, data)
payload.keySet().forEach {
when (it) {
"sourceCount" -> tvBookSource.text = context.getString(
R.string.source_title_num,
data.sourceName,
data.sourceCount
)
"imgUrl" -> ivBookImg.load(
data.imgUrl,
data.name,
data.author
)
"last" -> tvBookNewestChapter.text = context.getString(
if (lastChapter.isNullOrEmpty()) {
tvBookNewestChapter.visibility = View.GONE
} else {
tvBookNewestChapter.visibility = View.VISIBLE
tvBookNewestChapter.text = context.getString(
R.string.newest_chapter,
data.lastChapter
lastChapter
)
"desc" -> tvBookDesc.text = String.format("简介:%s", data.desc)
}
}
}
}
@ -243,13 +196,6 @@ class SearchAdapter(
}
}
private fun needGetInfo(bookBean: SearchBookBean): Boolean {
if (bookBean.author.isNullOrEmpty()) return true
if (bookBean.type.isNullOrEmpty()) return true
if (bookBean.desc.isNullOrEmpty()) return true
return if (bookBean.lastChapter.isNullOrEmpty()) true else bookBean.imgUrl.isNullOrEmpty()
}
private fun books2SearchBookBean(bookBean: SearchBookBean, books: List<Book>) {
bookBean.sourceCount = books.size
for (book in books) {

@ -128,13 +128,13 @@ public class FindBookHolder extends ViewHolderImpl<Book> {
tagList.clear();
String type = data.getType();
if (!StringHelper.isEmpty(type))
tagList.add("0:" + type);
tagList.add(type);
String wordCount = data.getWordCount();
if (!StringHelper.isEmpty(wordCount))
tagList.add("1:" + wordCount);
tagList.add(wordCount);
String status = data.getStatus();
if (!StringHelper.isEmpty(status))
tagList.add("2:" + status);
tagList.add(status);
if (tagList.size() == 0) {
tflBookTag.setVisibility(View.GONE);
} else {

@ -1,5 +1,7 @@
package xyz.fycz.myreader.webapi.crawler.base;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -12,6 +14,7 @@ import xyz.fycz.myreader.entity.StrResponse;
import xyz.fycz.myreader.greendao.entity.Book;
import xyz.fycz.myreader.greendao.entity.Chapter;
import xyz.fycz.myreader.model.mulvalmap.ConMVMap;
import xyz.fycz.myreader.util.utils.NetworkUtils;
/**
* 因新版书源使用StrResponse为了兼容旧版本书源全部继承自此类
@ -45,7 +48,13 @@ public abstract class BaseReadCrawler implements ReadCrawler {
@Override
public Observable<ConMVMap<SearchBookBean, Book>> getBooksFromStrResponse(StrResponse response) {
return Observable.create(emitter -> {
emitter.onNext(getBooksFromSearchHtml(response.body()));
ConMVMap<SearchBookBean, Book> bookMap = getBooksFromSearchHtml(response.body());
for (Book book : bookMap.values()){
if (!TextUtils.isEmpty(book.getImgUrl())){
book.setImgUrl(NetworkUtils.getAbsoluteURL(getNameSpace(), book.getImgUrl()));
}
}
emitter.onNext(bookMap);
emitter.onComplete();
});
}

@ -594,6 +594,5 @@
<string-array name="select_folder">
<item>默认路径</item>
<item>系统文件夹选择器</item>
<item>自带选择器</item>
</string-array>
</resources>

Loading…
Cancel
Save