diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index 5eaa243c5..0e460edba 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -17,12 +17,11 @@ class WebBook(val bookSource: BookSource) { val sourceUrl: String get() = bookSource.bookSourceUrl - fun searchBook( - key: String, - page: Int? = 1, - isSearch: Boolean = true, - scope: CoroutineScope = Coroutine.DEFAULT - ): Coroutine> { + /** + * 搜索 + */ + fun searchBook(key: String, page: Int? = 1, scope: CoroutineScope = Coroutine.DEFAULT) + : Coroutine> { return Coroutine.async(scope) { bookSource.getSearchRule().searchUrl?.let { searchUrl -> val analyzeUrl = AnalyzeUrl( @@ -33,11 +32,31 @@ class WebBook(val bookSource: BookSource) { headerMapF = bookSource.getHeaderMap() ) val response = analyzeUrl.getResponseAsync().await() - BookList.analyzeBookList(response, bookSource, analyzeUrl, isSearch) + BookList.analyzeBookList(response, bookSource, analyzeUrl, true) } ?: arrayListOf() } } + /** + * 发现 + */ + fun exploreBook(url: String, page: Int? = 1, scope: CoroutineScope = Coroutine.DEFAULT) + : Coroutine> { + return Coroutine.async(scope) { + val analyzeUrl = AnalyzeUrl( + ruleUrl = url, + page = page, + baseUrl = sourceUrl, + headerMapF = bookSource.getHeaderMap() + ) + val response = analyzeUrl.getResponseAsync().await() + BookList.analyzeBookList(response, bookSource, analyzeUrl, false) + } + } + + /** + * 书籍信息 + */ fun getBookInfo(book: Book, scope: CoroutineScope = Coroutine.DEFAULT): Coroutine { return Coroutine.async(scope) { val analyzeUrl = AnalyzeUrl( @@ -52,6 +71,9 @@ class WebBook(val bookSource: BookSource) { } } + /** + * 目录 + */ fun getChapterList( book: Book, scope: CoroutineScope = Coroutine.DEFAULT @@ -68,6 +90,9 @@ class WebBook(val bookSource: BookSource) { } } + /** + * 章节内容 + */ fun getContent( book: Book, bookChapter: BookChapter, diff --git a/app/src/main/java/io/legado/app/model/webbook/BookList.kt b/app/src/main/java/io/legado/app/model/webbook/BookList.kt index 517ee4ecb..1e2bfb6c9 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookList.kt @@ -43,7 +43,11 @@ object BookList { } val collections: List var reverse = false - val bookListRule = if (isSearch) bookSource.getSearchRule() else bookSource.getExploreRule() + val bookListRule = when { + isSearch -> bookSource.getSearchRule() + bookSource.getExploreRule().bookList.isNullOrBlank() -> bookSource.getSearchRule() + else -> bookSource.getExploreRule() + } var ruleList = bookListRule.bookList ?: "" if (ruleList.startsWith("-")) { reverse = true diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt index 25d480633..695d9ac68 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt @@ -1,11 +1,13 @@ package io.legado.app.ui.explore import android.os.Bundle +import androidx.lifecycle.Observer import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.data.entities.SearchBook import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_explore_show.* @@ -21,6 +23,7 @@ class ExploreShowActivity : VMBaseActivity(R.layout.activi title_bar.title = it } initRecyclerView() + viewModel.booksData.observe(this, Observer { upData(it) }) } private fun initRecyclerView() { @@ -30,12 +33,11 @@ class ExploreShowActivity : VMBaseActivity(R.layout.activi recycler_view.adapter = adapter } - private fun initData() { - val sourceUrl = intent.getStringExtra("sourceUrl") - val exploreUrl = intent.getStringExtra("exploreUrl") + private fun upData(books: List) { + adapter.addItems(books) } - override fun showBookInfo(name: String, author: String) { + override fun showBookInfo(bookUrl: String) { } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt index a9f8bf4d0..6510735f5 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt @@ -65,11 +65,11 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) : .setAsDrawable(iv_cover) } onClick { - callBack.showBookInfo(item.name, item.author) + callBack.showBookInfo(item.bookUrl) } } interface CallBack { - fun showBookInfo(name: String, author: String) + fun showBookInfo(bookUrl: String) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowViewModel.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowViewModel.kt index de3efd590..b3d692ca3 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowViewModel.kt @@ -1,9 +1,36 @@ package io.legado.app.ui.explore import android.app.Application +import android.content.Intent +import androidx.lifecycle.MutableLiveData +import io.legado.app.App import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.BookSource +import io.legado.app.data.entities.SearchBook +import io.legado.app.model.WebBook class ExploreShowViewModel(application: Application) : BaseViewModel(application) { + val booksData = MutableLiveData>() + var bookSource: BookSource? = null + var page = 1 + + fun initData(intent: Intent) { + execute { + val sourceUrl = intent.getStringExtra("sourceUrl") + val exploreUrl = intent.getStringExtra("exploreUrl") + if (bookSource == null) { + bookSource = App.db.bookSourceDao().getBookSource(sourceUrl) + } + bookSource?.let { + WebBook(it).exploreBook(exploreUrl, page, this) + .onSuccess { searchBooks -> + searchBooks?.let { + booksData.value = searchBooks + } + } + } + } + } } \ No newline at end of file