优化搜索

master
fengyuecanzhu 4 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. 132
      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(); tagList.clear();
String type = mBook.getType(); String type = mBook.getType();
if (!StringHelper.isEmpty(type)) if (!StringHelper.isEmpty(type))
tagList.add("0:" + type); tagList.add(type);
String wordCount = mBook.getWordCount(); String wordCount = mBook.getWordCount();
if (!StringHelper.isEmpty(wordCount)) if (!StringHelper.isEmpty(wordCount))
tagList.add("1:" + wordCount); tagList.add(wordCount);
String status = mBook.getStatus(); String status = mBook.getStatus();
if (!StringHelper.isEmpty(status)) if (!StringHelper.isEmpty(status))
tagList.add("2:" + status); tagList.add(status);
binding.ih.tflBookTag.setAdapter(new BookTagAdapter(this, tagList, 13)); binding.ih.tflBookTag.setAdapter(new BookTagAdapter(this, tagList, 13));
} }

@ -584,7 +584,7 @@ public class SearchBookActivity extends BaseActivity {
binding.srlSearchBookList.setEnableRefresh(false); binding.srlSearchBookList.setEnableRefresh(false);
} else { } else {
showBooks = true; showBooks = true;
mSearchBookAdapter = new SearchAdapter(this, searchKey, searchEngine); mSearchBookAdapter = new SearchAdapter(this, searchKey);
binding.rvSearchBooksList.setAdapter(mSearchBookAdapter); binding.rvSearchBooksList.setAdapter(mSearchBookAdapter);
binding.srlSearchBookList.setEnableRefresh(true); binding.srlSearchBookList.setEnableRefresh(true);
binding.rvSearchBooksList.setVisibility(View.VISIBLE); 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 = (TextView) View.inflate(context, R.layout.item_book_tag, null);
tvTagName.setTextSize(textSize); tvTagName.setTextSize(textSize);
//默认为分类 //默认为分类
if (tagName.startsWith("1:")) { //字数 if (position % 3 == 1) { //字数
tvTagName.setBackground(ContextCompat.getDrawable(context, R.drawable.tag_green_shape)); 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.setBackground(ContextCompat.getDrawable(context, R.drawable.tag_red_shape));
} }
tvTagName.setText(tagName.substring(2)); tvTagName.setText(tagName);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
ToastUtils.showError("" + e.getLocalizedMessage()); ToastUtils.showError("" + e.getLocalizedMessage());

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

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

@ -1,5 +1,7 @@
package xyz.fycz.myreader.webapi.crawler.base; package xyz.fycz.myreader.webapi.crawler.base;
import android.text.TextUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; 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.Book;
import xyz.fycz.myreader.greendao.entity.Chapter; import xyz.fycz.myreader.greendao.entity.Chapter;
import xyz.fycz.myreader.model.mulvalmap.ConMVMap; import xyz.fycz.myreader.model.mulvalmap.ConMVMap;
import xyz.fycz.myreader.util.utils.NetworkUtils;
/** /**
* 因新版书源使用StrResponse为了兼容旧版本书源全部继承自此类 * 因新版书源使用StrResponse为了兼容旧版本书源全部继承自此类
@ -45,7 +48,13 @@ public abstract class BaseReadCrawler implements ReadCrawler {
@Override @Override
public Observable<ConMVMap<SearchBookBean, Book>> getBooksFromStrResponse(StrResponse response) { public Observable<ConMVMap<SearchBookBean, Book>> getBooksFromStrResponse(StrResponse response) {
return Observable.create(emitter -> { 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(); emitter.onComplete();
}); });
} }

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

Loading…
Cancel
Save