pull/32/head
kunfei 5 years ago
parent b5a33fd98b
commit c56ba47cfb
  1. 39
      app/src/main/java/io/legado/app/model/WebBook.kt
  2. 6
      app/src/main/java/io/legado/app/model/webbook/BookList.kt
  3. 10
      app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt
  4. 4
      app/src/main/java/io/legado/app/ui/explore/ExploreShowAdapter.kt
  5. 27
      app/src/main/java/io/legado/app/ui/explore/ExploreShowViewModel.kt

@ -17,12 +17,11 @@ class WebBook(val bookSource: BookSource) {
val sourceUrl: String val sourceUrl: String
get() = bookSource.bookSourceUrl get() = bookSource.bookSourceUrl
fun searchBook( /**
key: String, * 搜索
page: Int? = 1, */
isSearch: Boolean = true, fun searchBook(key: String, page: Int? = 1, scope: CoroutineScope = Coroutine.DEFAULT)
scope: CoroutineScope = Coroutine.DEFAULT : Coroutine<List<SearchBook>> {
): Coroutine<List<SearchBook>> {
return Coroutine.async(scope) { return Coroutine.async(scope) {
bookSource.getSearchRule().searchUrl?.let { searchUrl -> bookSource.getSearchRule().searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
@ -33,11 +32,31 @@ class WebBook(val bookSource: BookSource) {
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) )
val response = analyzeUrl.getResponseAsync().await() val response = analyzeUrl.getResponseAsync().await()
BookList.analyzeBookList(response, bookSource, analyzeUrl, isSearch) BookList.analyzeBookList(response, bookSource, analyzeUrl, true)
} ?: arrayListOf() } ?: arrayListOf()
} }
} }
/**
* 发现
*/
fun exploreBook(url: String, page: Int? = 1, scope: CoroutineScope = Coroutine.DEFAULT)
: Coroutine<List<SearchBook>> {
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<Book> { fun getBookInfo(book: Book, scope: CoroutineScope = Coroutine.DEFAULT): Coroutine<Book> {
return Coroutine.async(scope) { return Coroutine.async(scope) {
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
@ -52,6 +71,9 @@ class WebBook(val bookSource: BookSource) {
} }
} }
/**
* 目录
*/
fun getChapterList( fun getChapterList(
book: Book, book: Book,
scope: CoroutineScope = Coroutine.DEFAULT scope: CoroutineScope = Coroutine.DEFAULT
@ -68,6 +90,9 @@ class WebBook(val bookSource: BookSource) {
} }
} }
/**
* 章节内容
*/
fun getContent( fun getContent(
book: Book, book: Book,
bookChapter: BookChapter, bookChapter: BookChapter,

@ -43,7 +43,11 @@ object BookList {
} }
val collections: List<Any> val collections: List<Any>
var reverse = false 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 ?: "" var ruleList = bookListRule.bookList ?: ""
if (ruleList.startsWith("-")) { if (ruleList.startsWith("-")) {
reverse = true reverse = true

@ -1,11 +1,13 @@
package io.legado.app.ui.explore package io.legado.app.ui.explore
import android.os.Bundle import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.SearchBook
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_explore_show.* import kotlinx.android.synthetic.main.activity_explore_show.*
@ -21,6 +23,7 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activi
title_bar.title = it title_bar.title = it
} }
initRecyclerView() initRecyclerView()
viewModel.booksData.observe(this, Observer { upData(it) })
} }
private fun initRecyclerView() { private fun initRecyclerView() {
@ -30,12 +33,11 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activi
recycler_view.adapter = adapter recycler_view.adapter = adapter
} }
private fun initData() { private fun upData(books: List<SearchBook>) {
val sourceUrl = intent.getStringExtra("sourceUrl") adapter.addItems(books)
val exploreUrl = intent.getStringExtra("exploreUrl")
} }
override fun showBookInfo(name: String, author: String) { override fun showBookInfo(bookUrl: String) {
} }
} }

@ -65,11 +65,11 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) :
.setAsDrawable(iv_cover) .setAsDrawable(iv_cover)
} }
onClick { onClick {
callBack.showBookInfo(item.name, item.author) callBack.showBookInfo(item.bookUrl)
} }
} }
interface CallBack { interface CallBack {
fun showBookInfo(name: String, author: String) fun showBookInfo(bookUrl: String)
} }
} }

@ -1,9 +1,36 @@
package io.legado.app.ui.explore package io.legado.app.ui.explore
import android.app.Application 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.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) { class ExploreShowViewModel(application: Application) : BaseViewModel(application) {
val booksData = MutableLiveData<List<SearchBook>>()
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
}
}
}
}
}
} }
Loading…
Cancel
Save