From f8d061236ec36f3673f6487533b86301c079514a Mon Sep 17 00:00:00 2001 From: fengyuecanzhu <1021300691@qq.com> Date: Sat, 21 Aug 2021 09:52:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/activity/BookDetailedActivity.java | 6 +- .../ui/activity/SearchBookActivity.java | 2 +- .../myreader/ui/adapter/BookTagAdapter.java | 6 +- .../fycz/myreader/ui/adapter/SearchAdapter.kt | 132 ++++++------------ .../ui/adapter/holder/FindBookHolder.java | 6 +- .../webapi/crawler/base/BaseReadCrawler.java | 11 +- app/src/main/res/values/strings.xml | 1 - 7 files changed, 59 insertions(+), 105 deletions(-) diff --git a/app/src/main/java/xyz/fycz/myreader/ui/activity/BookDetailedActivity.java b/app/src/main/java/xyz/fycz/myreader/ui/activity/BookDetailedActivity.java index cacc830..96e23d3 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/activity/BookDetailedActivity.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/activity/BookDetailedActivity.java @@ -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)); } diff --git a/app/src/main/java/xyz/fycz/myreader/ui/activity/SearchBookActivity.java b/app/src/main/java/xyz/fycz/myreader/ui/activity/SearchBookActivity.java index ff85754..c78f3a9 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/activity/SearchBookActivity.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/activity/SearchBookActivity.java @@ -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); diff --git a/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookTagAdapter.java b/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookTagAdapter.java index 9f84764..ebfc2cb 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookTagAdapter.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/adapter/BookTagAdapter.java @@ -35,12 +35,12 @@ public class BookTagAdapter extends TagAdapter { 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()); diff --git a/app/src/main/java/xyz/fycz/myreader/ui/adapter/SearchAdapter.kt b/app/src/main/java/xyz/fycz/myreader/ui/adapter/SearchAdapter.kt index d582b58..1dab982 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/adapter/SearchAdapter.kt +++ b/app/src/main/java/xyz/fycz/myreader/ui/adapter/SearchAdapter.kt @@ -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(context) { private val mBooks: ConMVMap = ConMVMap() - private var mList: List = ArrayList() + private lateinit var mList: List private val tagList: MutableList = 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 - ) - } - if (data.desc.isNullOrEmpty()) { - data.desc = "" - } else { - tvBookDesc.text = String.format("简介:%s", data.desc) - } + upLast(binding, data.lastChapter) + tvBookDesc.text = String.format("简介:%s", data.desc ?: "暂无简介") 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 = ArrayList() - books.add(book) - books2SearchBookBean(data, books) - val payload = Bundle() - if (!data.imgUrl.isNullOrEmpty()) - payload.putString( - "imgUrl", - NetworkUtils.getAbsoluteURL(url, data.imgUrl) - ) - 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) - } + } + + 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 + ) + "imgUrl" -> ivBookImg.load( + data.imgUrl, + data.name, + data.author + ) + "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( - R.string.newest_chapter, - data.lastChapter - ) - "desc" -> tvBookDesc.text = String.format("简介:%s", data.desc) - } + if (lastChapter.isNullOrEmpty()) { + tvBookNewestChapter.visibility = View.GONE + } else { + tvBookNewestChapter.visibility = View.VISIBLE + tvBookNewestChapter.text = context.getString( + R.string.newest_chapter, + lastChapter + ) } } } @@ -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) { bookBean.sourceCount = books.size for (book in books) { diff --git a/app/src/main/java/xyz/fycz/myreader/ui/adapter/holder/FindBookHolder.java b/app/src/main/java/xyz/fycz/myreader/ui/adapter/holder/FindBookHolder.java index 6d02497..62dfb5d 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/adapter/holder/FindBookHolder.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/adapter/holder/FindBookHolder.java @@ -128,13 +128,13 @@ public class FindBookHolder extends ViewHolderImpl { 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 { diff --git a/app/src/main/java/xyz/fycz/myreader/webapi/crawler/base/BaseReadCrawler.java b/app/src/main/java/xyz/fycz/myreader/webapi/crawler/base/BaseReadCrawler.java index 8031b54..9d12918 100644 --- a/app/src/main/java/xyz/fycz/myreader/webapi/crawler/base/BaseReadCrawler.java +++ b/app/src/main/java/xyz/fycz/myreader/webapi/crawler/base/BaseReadCrawler.java @@ -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> getBooksFromStrResponse(StrResponse response) { return Observable.create(emitter -> { - emitter.onNext(getBooksFromSearchHtml(response.body())); + ConMVMap 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(); }); } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cedd309..af79c69 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -594,6 +594,5 @@ 默认路径 系统文件夹选择器 - 自带选择器